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