jdk/src/java.desktop/share/classes/javax/swing/text/ParagraphView.java
author alexsch
Thu, 20 Oct 2016 12:18:15 +0300
changeset 41807 f9eb6cb54fed
parent 40719 4ae72a69bd3b
child 42216 621af0ebf6c4
permissions -rw-r--r--
8156217: Selected text is shifted on HiDPI display Reviewed-by: prr, serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
30462
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 28059
diff changeset
     2
 * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3713
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3713
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3713
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3713
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3713
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing.text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.font.TextAttribute;
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 40719
diff changeset
    30
import java.awt.geom.Rectangle2D;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.swing.SizeRequirements;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * View of a simple line-wrapping paragraph that supports
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * multiple fonts, colors, components, icons, etc.  It is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * basically a vertical box with a margin around it.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * contents of the box are a bunch of rows which are special
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * horizontal boxes.  This view creates a collection of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * views that represent the child elements of the paragraph
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * element.  Each of these views are placed into a row
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * directly if they will fit, otherwise the <code>breakView</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * method is called to try and carve the view into pieces
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * that fit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @author  Timothy Prinzing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @author  Scott Violet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @author  Igor Kushnirskiy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @see     View
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
public class ParagraphView extends FlowView implements TabExpander {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * Constructs a <code>ParagraphView</code> for the given element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * @param elem the element that this view is responsible for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public ParagraphView(Element elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        super(elem, View.Y_AXIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        setPropertiesFromAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        Document doc = elem.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        Object i18nFlag = doc.getProperty(AbstractDocument.I18NProperty);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        if ((i18nFlag != null) && i18nFlag.equals(Boolean.TRUE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            try {
40719
4ae72a69bd3b 8129854: Remove reflection from AWT/Swing classes
alexsch
parents: 32865
diff changeset
    65
                // the classname should probably come from a property file.
4ae72a69bd3b 8129854: Remove reflection from AWT/Swing classes
alexsch
parents: 32865
diff changeset
    66
                strategy = new TextLayoutStrategy();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            } catch (Throwable e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                throw new StateInvariantError("ParagraphView: Can't create i18n strategy: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                                              + e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Sets the type of justification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @param j one of the following values:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * <li><code>StyleConstants.ALIGN_LEFT</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * <li><code>StyleConstants.ALIGN_CENTER</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * <li><code>StyleConstants.ALIGN_RIGHT</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    protected void setJustification(int j) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        justification = j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * Sets the line spacing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @param ls the value is a factor of the line hight
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    protected void setLineSpacing(float ls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        lineSpacing = ls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * Sets the indent on the first line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @param fi the value in points
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    protected void setFirstLineIndent(float fi) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        firstLineIndent = (int) fi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * Set the cached properties from the attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    protected void setPropertiesFromAttributes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        AttributeSet attr = getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (attr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            setParagraphInsets(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            Integer a = (Integer)attr.getAttribute(StyleConstants.Alignment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            int alignment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            if (a == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                Document doc = getElement().getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                Object o = doc.getProperty(TextAttribute.RUN_DIRECTION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                if ((o != null) && o.equals(TextAttribute.RUN_DIRECTION_RTL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                    alignment = StyleConstants.ALIGN_RIGHT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    alignment = StyleConstants.ALIGN_LEFT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                alignment = a.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            setJustification(alignment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            setLineSpacing(StyleConstants.getLineSpacing(attr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            setFirstLineIndent(StyleConstants.getFirstLineIndent(attr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * Returns the number of views that this view is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * responsible for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * The child views of the paragraph are rows which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * have been used to arrange pieces of the <code>View</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * that represent the child elements.  This is the number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * of views that have been tiled in two dimensions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * and should be equivalent to the number of child elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * to the element this view is responsible for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @return the number of views that this <code>ParagraphView</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *          is responsible for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    protected int getLayoutViewCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        return layoutPool.getViewCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * Returns the view at a given <code>index</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * The child views of the paragraph are rows which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * have been used to arrange pieces of the <code>Views</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * that represent the child elements.  This methods returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * the view responsible for the child element index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * (prior to breaking).  These are the Views that were
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * produced from a factory (to represent the child
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * elements) and used for layout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @param index the <code>index</code> of the desired view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @return the view at <code>index</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    protected View getLayoutView(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        return layoutPool.getView(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * Returns the next visual position for the cursor, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * either the east or west direction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Overridden from <code>CompositeView</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @param pos position into the model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @param b either <code>Position.Bias.Forward</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *          <code>Position.Bias.Backward</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @param direction either <code>SwingConstants.NORTH</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *          or <code>SwingConstants.SOUTH</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @param biasRet an array containing the bias that were checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *  in this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @return the location in the model that represents the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *  next location visual position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    protected int getNextNorthSouthVisualPositionFrom(int pos, Position.Bias b,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                                                      Shape a, int direction,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                                                      Position.Bias[] biasRet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                                                throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        int vIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        if(pos == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            vIndex = (direction == NORTH) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                     getViewCount() - 1 : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            if(b == Position.Bias.Backward && pos > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                vIndex = getViewIndexAtPosition(pos - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                vIndex = getViewIndexAtPosition(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            if(direction == NORTH) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                if(vIndex == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                vIndex--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            else if(++vIndex >= getViewCount()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        // vIndex gives index of row to look in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        JTextComponent text = (JTextComponent)getContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        Caret c = text.getCaret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        Point magicPoint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        magicPoint = (c != null) ? c.getMagicCaretPosition() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        int x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        if(magicPoint == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            Shape posBounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                posBounds = text.getUI().modelToView(text, pos, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            } catch (BadLocationException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                posBounds = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            if(posBounds == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                x = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                x = posBounds.getBounds().x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            x = magicPoint.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        return getClosestPositionTo(pos, b, a, direction, biasRet, vIndex, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * Returns the closest model position to <code>x</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * <code>rowIndex</code> gives the index of the view that corresponds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * that should be looked in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @param pos  position into the model
30462
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 28059
diff changeset
   238
     * @param b the bias
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @param direction one of the following values:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * <li><code>SwingConstants.NORTH</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * <li><code>SwingConstants.SOUTH</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @param biasRet an array containing the bias that were checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *  in this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @param rowIndex the index of the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @param x the x coordinate of interest
30462
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 28059
diff changeset
   249
     * @throws BadLocationException if a bad location is encountered
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @return the closest model position to <code>x</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    // NOTE: This will not properly work if ParagraphView contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    // other ParagraphViews. It won't raise, but this does not message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    // the children views with getNextVisualPositionFrom.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    protected int getClosestPositionTo(int pos, Position.Bias b, Shape a,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                                       int direction, Position.Bias[] biasRet,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                                       int rowIndex, int x)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
              throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        JTextComponent text = (JTextComponent)getContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        View row = getView(rowIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        int lastPos = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        // This could be made better to check backward positions too.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        biasRet[0] = Position.Bias.Forward;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        for(int vc = 0, numViews = row.getViewCount(); vc < numViews; vc++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            View v = row.getView(vc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            int start = v.getStartOffset();
20103
f640c22218a3 6968363: ClassCastException while entering HINDI characters with CustomDocument
malenkov
parents: 9212
diff changeset
   268
            boolean ltr = AbstractDocument.isLeftToRight(doc, start, start + 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            if(ltr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                lastPos = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                for(int end = v.getEndOffset(); lastPos < end; lastPos++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                    float xx = text.modelToView(lastPos).getBounds().x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                    if(xx >= x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                        while (++lastPos < end &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                               text.modelToView(lastPos).getBounds().x == xx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                        return --lastPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                lastPos--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                for(lastPos = v.getEndOffset() - 1; lastPos >= start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                    lastPos--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                    float xx = text.modelToView(lastPos).getBounds().x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                    if(xx >= x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                        while (--lastPos >= start &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                               text.modelToView(lastPos).getBounds().x == xx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                        return ++lastPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                lastPos++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        if(lastPos == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            return getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        return lastPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * Determines in which direction the next view lays.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * Consider the <code>View</code> at index n.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * Typically the <code>View</code>s are layed out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * from left to right, so that the <code>View</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * to the EAST will be at index n + 1, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * <code>View</code> to the WEST will be at index n - 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * In certain situations, such as with bidirectional text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * it is possible that the <code>View</code> to EAST is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * at index n + 1, but rather at index n - 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * or that the <code>View</code> to the WEST is not at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * index n - 1, but index n + 1.  In this case this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * would return true, indicating the <code>View</code>s are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * layed out in descending order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * This will return true if the text is layed out right
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * to left at position, otherwise false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @param position position into the model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * @param bias either <code>Position.Bias.Forward</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     *          <code>Position.Bias.Backward</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * @return true if the text is layed out right to left at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     *         position, otherwise false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    protected boolean flipEastAndWestAtEnds(int position,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                                            Position.Bias bias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        Document doc = getDocument();
20103
f640c22218a3 6968363: ClassCastException while entering HINDI characters with CustomDocument
malenkov
parents: 9212
diff changeset
   329
        position = getStartOffset();
f640c22218a3 6968363: ClassCastException while entering HINDI characters with CustomDocument
malenkov
parents: 9212
diff changeset
   330
        return !AbstractDocument.isLeftToRight(doc, position, position + 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    // --- FlowView methods ---------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * Fetches the constraining span to flow against for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * the given child index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * @param index the index of the view being queried
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * @return the constraining span for the given view at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *  <code>index</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    public int getFlowSpan(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        View child = getView(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        int adjust = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        if (child instanceof Row) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            Row row = (Row) child;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            adjust = row.getLeftInset() + row.getRightInset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        return (layoutSpan == Integer.MAX_VALUE) ? layoutSpan
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                                                 : (layoutSpan - adjust);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * Fetches the location along the flow axis that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * flow span will start at.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * @param index the index of the view being queried
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * @return the location for the given view at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     *  <code>index</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    public int getFlowStart(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        View child = getView(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        int adjust = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        if (child instanceof Row) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            Row row = (Row) child;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            adjust = row.getLeftInset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        return tabBase + adjust;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * Create a <code>View</code> that should be used to hold a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * a row's worth of children in a flow.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * @return the new <code>View</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    protected View createRow() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        return new Row(getElement());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    // --- TabExpander methods ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * Returns the next tab stop position given a reference position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * This view implements the tab coordinate system, and calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * <code>getTabbedSpan</code> on the logical children in the process
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * of layout to determine the desired span of the children.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * logical children can delegate their tab expansion upward to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * the paragraph which knows how to expand tabs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * <code>LabelView</code> is an example of a view that delegates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * its tab expansion needs upward to the paragraph.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * This is implemented to try and locate a <code>TabSet</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * in the paragraph element's attribute set.  If one can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * found, its settings will be used, otherwise a default expansion
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 25859
diff changeset
   397
     * will be provided.  The base location for tab expansion
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * is the left inset from the paragraphs most recent allocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * (which is what the layout of the children is based upon).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * @param x the X reference position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * @param tabOffset the position within the text stream
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 20103
diff changeset
   403
     *   that the tab occurred at &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 20103
diff changeset
   404
     * @return the trailing end of the tab expansion &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * @see TabSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * @see TabStop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * @see LabelView
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    public float nextTabStop(float x, int tabOffset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        // If the text isn't left justified, offset by 10 pixels!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        if(justification != StyleConstants.ALIGN_LEFT)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            return x + 10.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        x -= tabBase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        TabSet tabs = getTabSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        if(tabs == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            // a tab every 72 pixels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            return (float)(tabBase + (((int)x / 72 + 1) * 72));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        TabStop tab = tabs.getTabAfter(x + .01f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        if(tab == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            // no tab, do a default of 5 pixels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            // Should this cause a wrapping of the line?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            return tabBase + x + 5.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        int alignment = tab.getAlignment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        int offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        switch(alignment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        case TabStop.ALIGN_LEFT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            // Simple case, left tab.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            return tabBase + tab.getPosition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        case TabStop.ALIGN_BAR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            // PENDING: what does this mean?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            return tabBase + tab.getPosition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        case TabStop.ALIGN_RIGHT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        case TabStop.ALIGN_CENTER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            offset = findOffsetToCharactersInString(tabChars,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                                                    tabOffset + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        case TabStop.ALIGN_DECIMAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            offset = findOffsetToCharactersInString(tabDecimalChars,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                                                    tabOffset + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        if (offset == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            offset = getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        float charsSize = getPartialSize(tabOffset + 1, offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        switch(alignment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        case TabStop.ALIGN_RIGHT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        case TabStop.ALIGN_DECIMAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            // right and decimal are treated the same way, the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            // position will be the location of the tab less the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            // partialSize.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            return tabBase + Math.max(x, tab.getPosition() - charsSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        case TabStop.ALIGN_CENTER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            // Similar to right, but half the partialSize.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            return tabBase + Math.max(x, tab.getPosition() - charsSize / 2.0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        // will never get here!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * Gets the <code>Tabset</code> to be used in calculating tabs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * @return the <code>TabSet</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    protected TabSet getTabSet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        return StyleConstants.getTabSet(getElement().getAttributes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * Returns the size used by the views between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * <code>startOffset</code> and <code>endOffset</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * This uses <code>getPartialView</code> to calculate the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * size if the child view implements the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * <code>TabableView</code> interface. If a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * size is needed and a <code>View</code> does not implement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * the <code>TabableView</code> interface,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * the <code>preferredSpan</code> will be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     *
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 20103
diff changeset
   483
     * @param startOffset the starting document offset &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 20103
diff changeset
   484
     * @param endOffset the ending document offset &gt;= startOffset
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 20103
diff changeset
   485
     * @return the size &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    protected float getPartialSize(int startOffset, int endOffset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        float size = 0.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        int viewIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        int numViews = getViewCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        View view;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        int viewEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        int tempEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        // Have to search layoutPool!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        // PENDING: when ParagraphView supports breaking location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        // into layoutPool will have to change!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        viewIndex = getElement().getElementIndex(startOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        numViews = layoutPool.getViewCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        while(startOffset < endOffset && viewIndex < numViews) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            view = layoutPool.getView(viewIndex++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            viewEnd = view.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            tempEnd = Math.min(endOffset, viewEnd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            if(view instanceof TabableView)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                size += ((TabableView)view).getPartialSpan(startOffset, tempEnd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            else if(startOffset == view.getStartOffset() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                    tempEnd == view.getEndOffset())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                size += view.getPreferredSpan(View.X_AXIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                // PENDING: should we handle this better?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                return 0.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            startOffset = viewEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * Finds the next character in the document with a character in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * <code>string</code>, starting at offset <code>start</code>. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * there are no characters found, -1 will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * @param string the string of characters
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 20103
diff changeset
   523
     * @param start where to start in the model &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * @return the document offset, or -1 if no characters found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    protected int findOffsetToCharactersInString(char[] string,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                                                 int start) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        int stringLength = string.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        int end = getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        Segment seg = new Segment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            getDocument().getText(start, end - start, seg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        } catch (BadLocationException ble) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        for(int counter = seg.offset, maxCounter = seg.offset + seg.count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            counter < maxCounter; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            char currentChar = seg.array[counter];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            for(int subCounter = 0; subCounter < stringLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                subCounter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                if(currentChar == string[subCounter])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                    return counter - seg.offset + start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        // No match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * Returns where the tabs are calculated from.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * @return where tabs are calculated from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    protected float getTabBase() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        return (float)tabBase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    // ---- View methods ----------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * Renders using the given rendering surface and area on that
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20158
diff changeset
   561
     * surface.  This is implemented to delegate to the superclass
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * after stashing the base coordinate for tab calculations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * @param g the rendering surface to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * @see View#paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    public void paint(Graphics g, Shape a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        Rectangle alloc = (a instanceof Rectangle) ? (Rectangle)a : a.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        tabBase = alloc.x + getLeftInset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        super.paint(g, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        // line with the negative firstLineIndent value needs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        // special handling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        if (firstLineIndent < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            Shape sh = getChildAllocation(0, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            if ((sh != null) &&  sh.intersects(alloc)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                int x = alloc.x + getLeftInset() + firstLineIndent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                int y = alloc.y + getTopInset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                Rectangle clip = g.getClipBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                tempRect.x = x + getOffset(X_AXIS, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                tempRect.y = y + getOffset(Y_AXIS, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                tempRect.width = getSpan(X_AXIS, 0) - firstLineIndent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                tempRect.height = getSpan(Y_AXIS, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                if (tempRect.intersects(clip)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                    tempRect.x = tempRect.x - firstLineIndent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                    paintChild(g, tempRect, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * Determines the desired alignment for this view along an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * axis.  This is implemented to give the alignment to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * center of the first row along the y axis, and the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * along the x axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * @param axis may be either <code>View.X_AXIS</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     *   <code>View.Y_AXIS</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * @return the desired alignment.  This should be a value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     *   between 0.0 and 1.0 inclusive, where 0 indicates alignment at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     *   origin and 1.0 indicates alignment to the full span
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     *   away from the origin.  An alignment of 0.5 would be the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     *   center of the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    public float getAlignment(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        switch (axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        case Y_AXIS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            float a = 0.5f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            if (getViewCount() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                int paragraphSpan = (int) getPreferredSpan(View.Y_AXIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                View v = getView(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                int rowSpan = (int) v.getPreferredSpan(View.Y_AXIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                a = (paragraphSpan != 0) ? ((float)(rowSpan / 2)) / paragraphSpan : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        case X_AXIS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            return 0.5f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            throw new IllegalArgumentException("Invalid axis: " + axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * Breaks this view on the given axis at the given length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * <code>ParagraphView</code> instances are breakable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * along the <code>Y_AXIS</code> only, and only if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * <code>len</code> is after the first line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * @param axis may be either <code>View.X_AXIS</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     *  or <code>View.Y_AXIS</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * @param len specifies where a potential break is desired
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 20103
diff changeset
   636
     *  along the given axis &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * @param a the current allocation of the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * @return the fragment of the view that represents the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     *  given span, if the view can be broken; if the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     *  doesn't support breaking behavior, the view itself is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     *  returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * @see View#breakView
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    public View breakView(int axis, float len, Shape a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        if(axis == View.Y_AXIS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            if(a != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                Rectangle alloc = a.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                setSize(alloc.width, alloc.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            // Determine what row to break on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            // PENDING(prinz) add break support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * Gets the break weight for a given location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * <code>ParagraphView</code> instances are breakable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * along the <code>Y_AXIS</code> only, and only if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * <code>len</code> is after the first row.  If the length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * is less than one row, a value of <code>BadBreakWeight</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * @param axis may be either <code>View.X_AXIS</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     *  or <code>View.Y_AXIS</code>
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 20103
diff changeset
   669
     * @param len specifies where a potential break is desired &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * @return a value indicating the attractiveness of breaking here;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     *  either <code>GoodBreakWeight</code> or <code>BadBreakWeight</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * @see View#getBreakWeight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    public int getBreakWeight(int axis, float len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        if(axis == View.Y_AXIS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            // PENDING(prinz) make this return a reasonable value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            // when paragraph breaking support is re-implemented.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            // If less than one row, bad weight value should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            // returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            //return GoodBreakWeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            return BadBreakWeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        return BadBreakWeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * Calculate the needs for the paragraph along the minor axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * <p>This uses size requirements of the superclass, modified to take into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * account the non-breakable areas at the adjacent views edges.  The minimal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * size requirements for such views should be no less than the sum of all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * adjacent fragments.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * <p>If the {@code axis} parameter is neither {@code View.X_AXIS} nor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * {@code View.Y_AXIS}, {@link IllegalArgumentException} is thrown.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * {@code r} parameter is {@code null,} a new {@code SizeRequirements}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * object is created, otherwise the supplied {@code SizeRequirements}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     * object is returned.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * @param axis  the minor axis
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     * @param r     the input {@code SizeRequirements} object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * @return      the new or adjusted {@code SizeRequirements} object
3709
e7cf22d025bb 6876628: @throw instead of @throws in two ParagraphView classes
darcy
parents: 3342
diff changeset
   703
     * @throws IllegalArgumentException  if the {@code axis} parameter is invalid
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    protected SizeRequirements calculateMinorAxisRequirements(int axis,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                                                        SizeRequirements r) {
9212
2123bfe0b40b 6973777: JCK manual case JEditorPaneTests.html#JEditorPane fails in jdk7 b100
rupashka
parents: 5506
diff changeset
   708
        r = super.calculateMinorAxisRequirements(axis, r);
2123bfe0b40b 6973777: JCK manual case JEditorPaneTests.html#JEditorPane fails in jdk7 b100
rupashka
parents: 5506
diff changeset
   709
2123bfe0b40b 6973777: JCK manual case JEditorPaneTests.html#JEditorPane fails in jdk7 b100
rupashka
parents: 5506
diff changeset
   710
        float min = 0;
2123bfe0b40b 6973777: JCK manual case JEditorPaneTests.html#JEditorPane fails in jdk7 b100
rupashka
parents: 5506
diff changeset
   711
        float glue = 0;
2123bfe0b40b 6973777: JCK manual case JEditorPaneTests.html#JEditorPane fails in jdk7 b100
rupashka
parents: 5506
diff changeset
   712
        int n = getLayoutViewCount();
2123bfe0b40b 6973777: JCK manual case JEditorPaneTests.html#JEditorPane fails in jdk7 b100
rupashka
parents: 5506
diff changeset
   713
        for (int i = 0; i < n; i++) {
2123bfe0b40b 6973777: JCK manual case JEditorPaneTests.html#JEditorPane fails in jdk7 b100
rupashka
parents: 5506
diff changeset
   714
            View v = getLayoutView(i);
2123bfe0b40b 6973777: JCK manual case JEditorPaneTests.html#JEditorPane fails in jdk7 b100
rupashka
parents: 5506
diff changeset
   715
            float span = v.getMinimumSpan(axis);
2123bfe0b40b 6973777: JCK manual case JEditorPaneTests.html#JEditorPane fails in jdk7 b100
rupashka
parents: 5506
diff changeset
   716
            if (v.getBreakWeight(axis, 0, v.getMaximumSpan(axis)) > View.BadBreakWeight) {
2123bfe0b40b 6973777: JCK manual case JEditorPaneTests.html#JEditorPane fails in jdk7 b100
rupashka
parents: 5506
diff changeset
   717
                // find the longest non-breakable fragments at the view edges
2123bfe0b40b 6973777: JCK manual case JEditorPaneTests.html#JEditorPane fails in jdk7 b100
rupashka
parents: 5506
diff changeset
   718
                int p0 = v.getStartOffset();
2123bfe0b40b 6973777: JCK manual case JEditorPaneTests.html#JEditorPane fails in jdk7 b100
rupashka
parents: 5506
diff changeset
   719
                int p1 = v.getEndOffset();
2123bfe0b40b 6973777: JCK manual case JEditorPaneTests.html#JEditorPane fails in jdk7 b100
rupashka
parents: 5506
diff changeset
   720
                float start = findEdgeSpan(v, axis, p0, p0, p1);
2123bfe0b40b 6973777: JCK manual case JEditorPaneTests.html#JEditorPane fails in jdk7 b100
rupashka
parents: 5506
diff changeset
   721
                float end = findEdgeSpan(v, axis, p1, p0, p1);
2123bfe0b40b 6973777: JCK manual case JEditorPaneTests.html#JEditorPane fails in jdk7 b100
rupashka
parents: 5506
diff changeset
   722
                glue += start;
2123bfe0b40b 6973777: JCK manual case JEditorPaneTests.html#JEditorPane fails in jdk7 b100
rupashka
parents: 5506
diff changeset
   723
                min = Math.max(min, Math.max(span, glue));
2123bfe0b40b 6973777: JCK manual case JEditorPaneTests.html#JEditorPane fails in jdk7 b100
rupashka
parents: 5506
diff changeset
   724
                glue = end;
2123bfe0b40b 6973777: JCK manual case JEditorPaneTests.html#JEditorPane fails in jdk7 b100
rupashka
parents: 5506
diff changeset
   725
            } else {
2123bfe0b40b 6973777: JCK manual case JEditorPaneTests.html#JEditorPane fails in jdk7 b100
rupashka
parents: 5506
diff changeset
   726
                // non-breakable view
2123bfe0b40b 6973777: JCK manual case JEditorPaneTests.html#JEditorPane fails in jdk7 b100
rupashka
parents: 5506
diff changeset
   727
                glue += span;
2123bfe0b40b 6973777: JCK manual case JEditorPaneTests.html#JEditorPane fails in jdk7 b100
rupashka
parents: 5506
diff changeset
   728
                min = Math.max(min, glue);
2123bfe0b40b 6973777: JCK manual case JEditorPaneTests.html#JEditorPane fails in jdk7 b100
rupashka
parents: 5506
diff changeset
   729
            }
2123bfe0b40b 6973777: JCK manual case JEditorPaneTests.html#JEditorPane fails in jdk7 b100
rupashka
parents: 5506
diff changeset
   730
        }
2123bfe0b40b 6973777: JCK manual case JEditorPaneTests.html#JEditorPane fails in jdk7 b100
rupashka
parents: 5506
diff changeset
   731
        r.minimum = Math.max(r.minimum, (int) min);
2123bfe0b40b 6973777: JCK manual case JEditorPaneTests.html#JEditorPane fails in jdk7 b100
rupashka
parents: 5506
diff changeset
   732
        r.preferred = Math.max(r.minimum, r.preferred);
2123bfe0b40b 6973777: JCK manual case JEditorPaneTests.html#JEditorPane fails in jdk7 b100
rupashka
parents: 5506
diff changeset
   733
        r.maximum = Math.max(r.preferred, r.maximum);
2123bfe0b40b 6973777: JCK manual case JEditorPaneTests.html#JEditorPane fails in jdk7 b100
rupashka
parents: 5506
diff changeset
   734
2123bfe0b40b 6973777: JCK manual case JEditorPaneTests.html#JEditorPane fails in jdk7 b100
rupashka
parents: 5506
diff changeset
   735
        return r;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * Binary search for the longest non-breakable fragment at the view edge.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
    private float findEdgeSpan(View v, int axis, int fp, int p0, int p1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        int len = p1 - p0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        if (len <= 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            // further fragmentation is not possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            return v.getMinimumSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            int mid = p0 + len / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            boolean startEdge = mid > fp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
            // initial view is breakable hence must support fragmentation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            View f = startEdge ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                v.createFragment(fp, mid) : v.createFragment(mid, fp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            boolean breakable = f.getBreakWeight(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                    axis, 0, f.getMaximumSpan(axis)) > View.BadBreakWeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            if (breakable == startEdge) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                p1 = mid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                p0 = mid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            return findEdgeSpan(f, axis, fp, p0, p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * Gives notification from the document that attributes were changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * in a location that this view is responsible for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * @param changes the change information from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     *  associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * @param a the current allocation of the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * @param f the factory to use to rebuild if the view has children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     * @see View#changedUpdate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
    public void changedUpdate(DocumentEvent changes, Shape a, ViewFactory f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        // update any property settings stored, and layout should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        // recomputed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        setPropertiesFromAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        layoutChanged(X_AXIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        layoutChanged(Y_AXIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        super.changedUpdate(changes, a, f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
    // --- variables -----------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
    private int justification;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
    private float lineSpacing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
    /** Indentation for the first line, from the left inset. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
    protected int firstLineIndent = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * Used by the TabExpander functionality to determine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * where to base the tab calculations.  This is basically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * the location of the left side of the paragraph.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
    private int tabBase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     * Used to create an i18n-based layout strategy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     */
25193
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 23010
diff changeset
   800
    static Class<?> i18nStrategy;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
    /** Used for searching for a tab. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
    static char[] tabChars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
    /** Used for searching for a tab or decimal character. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
    static char[] tabDecimalChars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        tabChars = new char[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        tabChars[0] = '\t';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        tabDecimalChars = new char[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        tabDecimalChars[0] = '\t';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        tabDecimalChars[1] = '.';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * Internally created view that has the purpose of holding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     * the views that represent the children of the paragraph
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * that have been arranged in rows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    class Row extends BoxView {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        Row(Element elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
            super(elem, View.X_AXIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
         * This is reimplemented to do nothing since the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
         * paragraph fills in the row with its needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
         * children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        protected void loadChildren(ViewFactory f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
         * Fetches the attributes to use when rendering.  This view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
         * isn't directly responsible for an element so it returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
         * the outer classes attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        public AttributeSet getAttributes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
            View p = getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            return (p != null) ? p.getAttributes() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        public float getAlignment(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            if (axis == View.X_AXIS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                switch (justification) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                case StyleConstants.ALIGN_LEFT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                case StyleConstants.ALIGN_RIGHT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                    return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                case StyleConstants.ALIGN_CENTER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                    return 0.5f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                case StyleConstants.ALIGN_JUSTIFIED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                    float rv = 0.5f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                    //if we can justifiy the content always align to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                    //the left.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                    if (isJustifiableDocument()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                        rv = 0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                    return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            return super.getAlignment(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
         * Provides a mapping from the document model coordinate space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
         * to the coordinate space of the view mapped to it.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
         * implemented to let the superclass find the position along
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
         * the major axis and the allocation of the row is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
         * along the minor axis, so that even though the children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
         * are different heights they all get the same caret height.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
         * @param pos the position to convert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
         * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
         * @return the bounding box of the given position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
         * @exception BadLocationException  if the given position does not represent a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
         *   valid location in the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
         * @see View#modelToView
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
            Rectangle r = a.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
            View v = getViewAtPosition(pos, r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
            if ((v != null) && (!v.getElement().isLeaf())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                // Don't adjust the height if the view represents a branch.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
                return super.modelToView(pos, a, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
            r = a.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
            int height = r.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            int y = r.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
            Shape loc = super.modelToView(pos, a, b);
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 40719
diff changeset
   892
            Rectangle2D bounds = loc.getBounds2D();
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 40719
diff changeset
   893
            bounds.setRect(bounds.getX(), y, bounds.getWidth(), height);
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 40719
diff changeset
   894
            return bounds;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
         * Range represented by a row in the paragraph is only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
         * a subset of the total range of the paragraph element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
         * @see View#getRange
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        public int getStartOffset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
            int offs = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
            int n = getViewCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
            for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                View v = getView(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                offs = Math.min(offs, v.getStartOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
            return offs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        public int getEndOffset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
            int offs = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
            int n = getViewCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
                View v = getView(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
                offs = Math.max(offs, v.getEndOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
            return offs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
         * Perform layout for the minor axis of the box (i.e. the
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20158
diff changeset
   924
         * axis orthogonal to the axis that it represents).  The results
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
         * of the layout should be placed in the given arrays which represent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
         * the allocations to the children along the minor axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
         * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
         * This is implemented to do a baseline layout of the children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
         * by calling BoxView.baselineLayout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
         * @param targetSpan the total span given to the view, which
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20158
diff changeset
   932
         *  would be used to layout the children.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
         * @param axis the axis being layed out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
         * @param offsets the offsets from the origin of the view for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
         *  each of the child views.  This is a return value and is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
         *  filled in by the implementation of this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
         * @param spans the span of each child view.  This is a return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
         *  value and is filled in by the implementation of this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
         * @return the offset and span for each child view in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
         *  offsets and spans parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        protected void layoutMinorAxis(int targetSpan, int axis, int[] offsets, int[] spans) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
            baselineLayout(targetSpan, axis, offsets, spans);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
        protected SizeRequirements calculateMinorAxisRequirements(int axis,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
                                                                  SizeRequirements r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            return baselineRequirements(axis, r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        private boolean isLastRow() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
            View parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            return ((parent = getParent()) == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                    || this == parent.getView(parent.getViewCount() - 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        private boolean isBrokenRow() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
            boolean rv = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
            int viewsCount = getViewCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
            if (viewsCount > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
                View lastView = getView(viewsCount - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
                if (lastView.getBreakWeight(X_AXIS, 0, 0) >=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
                      ForcedBreakWeight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                    rv = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
            return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        private boolean isJustifiableDocument() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
            return (! Boolean.TRUE.equals(getDocument().getProperty(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                          AbstractDocument.I18NProperty)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
         * Whether we need to justify this {@code Row}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
         * At this time (jdk1.6) we support justification on for non
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
         * 18n text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
         * @return {@code true} if this {@code Row} should be justified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        private boolean isJustifyEnabled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
            boolean ret = (justification == StyleConstants.ALIGN_JUSTIFIED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
            //no justification for i18n documents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
            ret = ret && isJustifiableDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
            //no justification for the last row
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
            ret = ret && ! isLastRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
            //no justification for the broken rows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
            ret = ret && ! isBrokenRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
            return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        //Calls super method after setting spaceAddon to 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
        //Justification should not affect MajorAxisRequirements
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        protected SizeRequirements calculateMajorAxisRequirements(int axis,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
                SizeRequirements r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
            int oldJustficationData[] = justificationData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
            justificationData = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
            SizeRequirements ret = super.calculateMajorAxisRequirements(axis, r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
            if (isJustifyEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
                justificationData = oldJustficationData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
            return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        protected void layoutMajorAxis(int targetSpan, int axis,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                                       int[] offsets, int[] spans) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
            int oldJustficationData[] = justificationData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
            justificationData = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
            super.layoutMajorAxis(targetSpan, axis, offsets, spans);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
            if (! isJustifyEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
            int currentSpan = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
            for (int span : spans) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
                currentSpan += span;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
            if (currentSpan == targetSpan) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
                //no need to justify
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            // we justify text by enlarging spaces by the {@code spaceAddon}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
            // justification is started to the right of the rightmost TAB.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
            // leading and trailing spaces are not extendable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
            // GlyphPainter1 uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
            // justificationData
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
            // for all painting and measurement.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            int extendableSpaces = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
            int startJustifiableContent = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
            int endJustifiableContent = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
            int lastLeadingSpaces = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
            int rowStartOffset = getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
            int rowEndOffset = getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
            int spaceMap[] = new int[rowEndOffset - rowStartOffset];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
            Arrays.fill(spaceMap, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
            for (int i = getViewCount() - 1; i >= 0 ; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
                View view = getView(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
                if (view instanceof GlyphView) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
                    GlyphView.JustificationInfo justificationInfo =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
                        ((GlyphView) view).getJustificationInfo(rowStartOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
                    final int viewStartOffset = view.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
                    final int offset = viewStartOffset - rowStartOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
                    for (int j = 0; j < justificationInfo.spaceMap.length(); j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                        if (justificationInfo.spaceMap.get(j)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
                            spaceMap[j + offset] = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
                    if (startJustifiableContent > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
                        if (justificationInfo.end >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
                            extendableSpaces += justificationInfo.trailingSpaces;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
                            lastLeadingSpaces += justificationInfo.trailingSpaces;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
                    if (justificationInfo.start >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
                        startJustifiableContent =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
                            justificationInfo.start + viewStartOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
                        extendableSpaces += lastLeadingSpaces;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
                    if (justificationInfo.end >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
                          && endJustifiableContent < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
                        endJustifiableContent =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
                            justificationInfo.end + viewStartOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
                    extendableSpaces += justificationInfo.contentSpaces;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
                    lastLeadingSpaces = justificationInfo.leadingSpaces;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
                    if (justificationInfo.hasTab) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
            if (extendableSpaces <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
                //there is nothing we can do to justify
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
            int adjustment = (targetSpan - currentSpan);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
            int spaceAddon = (extendableSpaces > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
                ?  adjustment / extendableSpaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
                : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
            int spaceAddonLeftoverEnd = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
            for (int i = startJustifiableContent - rowStartOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
                     leftover = adjustment - spaceAddon * extendableSpaces;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
                     leftover > 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
                     leftover -= spaceMap[i],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
                     i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
                spaceAddonLeftoverEnd = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
            if (spaceAddon > 0 || spaceAddonLeftoverEnd >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
                justificationData = (oldJustficationData != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
                    ? oldJustficationData
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
                    : new int[END_JUSTIFIABLE + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
                justificationData[SPACE_ADDON] = spaceAddon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
                justificationData[SPACE_ADDON_LEFTOVER_END] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
                    spaceAddonLeftoverEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
                justificationData[START_JUSTIFIABLE] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
                    startJustifiableContent - rowStartOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
                justificationData[END_JUSTIFIABLE] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
                    endJustifiableContent - rowStartOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
                super.layoutMajorAxis(targetSpan, axis, offsets, spans);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
        //for justified row we assume the maximum horizontal span
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        //is MAX_VALUE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
        @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
        public float getMaximumSpan(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
            float ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
            if (View.X_AXIS == axis
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
                  && isJustifyEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
                ret = Float.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
              ret = super.getMaximumSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
            return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
         * Fetches the child view index representing the given position in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
         * the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
         *
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 20103
diff changeset
  1134
         * @param pos the position &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
         * @return  index of the view representing the given position, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
         *   -1 if no view represents that position
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
        protected int getViewIndexAtPosition(int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
            // This is expensive, but are views are not necessarily layed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
            // out in model order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
            if(pos < getStartOffset() || pos >= getEndOffset())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
            for(int counter = getViewCount() - 1; counter >= 0; counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
                View v = getView(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
                if(pos >= v.getStartOffset() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
                   pos < v.getEndOffset()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
                    return counter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
         * Gets the left inset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
         * @return the inset
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
        protected short getLeftInset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
            View parentView;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
            int adjustment = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
            if ((parentView = getParent()) != null) { //use firstLineIdent for the first row
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
                if (this == parentView.getView(0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
                    adjustment = firstLineIndent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
            return (short)(super.getLeftInset() + adjustment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
        protected short getBottomInset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
            return (short)(super.getBottomInset() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
                           ((minorRequest != null) ? minorRequest.preferred : 0) *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
                           lineSpacing);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 30462
diff changeset
  1175
        static final int SPACE_ADDON = 0;
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 30462
diff changeset
  1176
        static final int SPACE_ADDON_LEFTOVER_END = 1;
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 30462
diff changeset
  1177
        static final int START_JUSTIFIABLE = 2;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        //this should be the last index in justificationData
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 30462
diff changeset
  1179
        static final int END_JUSTIFIABLE = 3;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
        int justificationData[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
}