jdk/src/java.desktop/share/classes/javax/swing/text/GlyphView.java
author prr
Sat, 19 Sep 2015 15:45:59 -0700
changeset 32865 f9cb6e427f9e
parent 32859 0b6271318b51
child 40719 4ae72a69bd3b
permissions -rw-r--r--
8136783: Run blessed-modifier-order script on java.desktop Reviewed-by: martin, serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
30462
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 27063
diff changeset
     2
 * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3503
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: 3503
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: 3503
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3503
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3503
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.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.text.BreakIterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.BitSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
2493
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
    33
import javax.swing.UIManager;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.swing.SwingUtilities2;
5763
9a942d6fb0aa 6636983: Japanese text does not display correctly in a JEditorPane
rupashka
parents: 5506
diff changeset
    35
import static sun.swing.SwingUtilities2.IMPLIED_CR;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * A GlyphView is a styled chunk of text that represents a view
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * mapped over an element in the text model. This view is generally
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * responsible for displaying text glyphs using character level
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * attributes in some way.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * An implementation of the GlyphPainter class is used to do the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * actual rendering and model/view translations.  This separates
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * rendering from layout and management of the association with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * The view supports breaking for the purpose of formatting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * The fragments produced by breaking share the view that has
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * primary responsibility for the element (i.e. they are nested
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * classes and carry only a small amount of state of their own)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * so they can share its resources.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * Since this view
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * represents text that may have tabs embedded in it, it implements the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <code>TabableView</code> interface.  Tabs will only be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * expanded if this view is embedded in a container that does
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * tab expansion.  ParagraphView is an example of a container
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * that does tab expansion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * @author  Timothy Prinzing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
public class GlyphView extends View implements TabableView, Cloneable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * Constructs a new view wrapped on an element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * @param elem the element
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public GlyphView(Element elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        super(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        offset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        length = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        Element parent = elem.getParentElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        AttributeSet attr = elem.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        //         if there was an implied CR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        impliedCR = (attr != null && attr.getAttribute(IMPLIED_CR) != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        //         if this is non-empty paragraph
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                   parent != null && parent.getElementCount() > 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        skipWidth = elem.getName().equals("br");
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
     * Creates a shallow copy.  This is used by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * createFragment and breakView methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @return the copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    protected final Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        Object o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            o = super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        } catch (CloneNotSupportedException cnse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            o = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        return o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Fetch the currently installed glyph painter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * If a painter has not yet been installed, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * a default was not yet needed, null is returned.
30462
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 27063
diff changeset
   105
     * @return the currently installed glyph painter
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public GlyphPainter getGlyphPainter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        return painter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * Sets the painter to use for rendering glyphs.
30462
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 27063
diff changeset
   113
     * @param p the painter to use for rendering glyphs
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public void setGlyphPainter(GlyphPainter p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        painter = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Fetch a reference to the text that occupies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * the given range.  This is normally used by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * the GlyphPainter to determine what characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * it should render glyphs for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 19176
diff changeset
   125
     * @param p0  the starting document offset &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 19176
diff changeset
   126
     * @param p1  the ending document offset &gt;= p0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @return    the <code>Segment</code> containing the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     public Segment getText(int p0, int p1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
         // When done with the returned Segment it should be released by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
         // invoking:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
         //    SegmentCache.releaseSharedSegment(segment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
         Segment text = SegmentCache.getSharedSegment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
         try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
             Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
             doc.getText(p0, p1 - p0, text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
         } catch (BadLocationException bl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
             throw new StateInvariantError("GlyphView: Stale view: " + bl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
         return text;
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
     * Fetch the background color to use to render the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * glyphs.  If there is no background color, null should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * be returned.  This is implemented to call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * <code>StyledDocument.getBackground</code> if the associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * document is a styled document, otherwise it returns null.
30462
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 27063
diff changeset
   149
     * @return the background color to use to render the glyphs
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public Color getBackground() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if (doc instanceof StyledDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            AttributeSet attr = getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            if (attr.isDefined(StyleConstants.Background)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                return ((StyledDocument)doc).getBackground(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * Fetch the foreground color to use to render the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * glyphs.  If there is no foreground color, null should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * be returned.  This is implemented to call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * <code>StyledDocument.getBackground</code> if the associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * document is a StyledDocument.  If the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * is not a StyledDocument, the associated components foreground
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * color is used.  If there is no associated component, null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * is returned.
30462
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 27063
diff changeset
   171
     * @return the foreground color to use to render the glyphs
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public Color getForeground() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if (doc instanceof StyledDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            AttributeSet attr = getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            return ((StyledDocument)doc).getForeground(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        Component c = getContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            return c.getForeground();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * Fetch the font that the glyphs should be based
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * upon.  This is implemented to call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * <code>StyledDocument.getFont</code> if the associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * document is a StyledDocument.  If the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * is not a StyledDocument, the associated components font
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * is used.  If there is no associated component, null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * is returned.
30462
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 27063
diff changeset
   194
     * @return the font that the glyphs should be based upon
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    public Font getFont() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        if (doc instanceof StyledDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            AttributeSet attr = getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            return ((StyledDocument)doc).getFont(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        Component c = getContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            return c.getFont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * Determine if the glyphs should be underlined.  If true,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * an underline should be drawn through the baseline.
30462
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 27063
diff changeset
   212
     * @return if the glyphs should be underlined
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    public boolean isUnderline() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        AttributeSet attr = getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        return StyleConstants.isUnderline(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * Determine if the glyphs should have a strikethrough
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * line.  If true, a line should be drawn through the center
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * of the glyphs.
30462
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 27063
diff changeset
   223
     * @return if the glyphs should have a strikethrough line
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    public boolean isStrikeThrough() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        AttributeSet attr = getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        return StyleConstants.isStrikeThrough(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * Determine if the glyphs should be rendered as superscript.
30462
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 27063
diff changeset
   232
     * @return if the glyphs should be rendered as superscript
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    public boolean isSubscript() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        AttributeSet attr = getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        return StyleConstants.isSubscript(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * Determine if the glyphs should be rendered as subscript.
30462
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 27063
diff changeset
   241
     * @return if the glyphs should be rendered as subscript
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    public boolean isSuperscript() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        AttributeSet attr = getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        return StyleConstants.isSuperscript(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * Fetch the TabExpander to use if tabs are present in this view.
30462
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 27063
diff changeset
   250
     * @return the TabExpander to use if tabs are present in this view
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    public TabExpander getTabExpander() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        return expander;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * Check to see that a glyph painter exists.  If a painter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * doesn't exist, a default glyph painter will be installed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    protected void checkPainter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        if (painter == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            if (defaultPainter == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                // the classname should probably come from a property file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                String classname = "javax.swing.text.GlyphPainter1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                try {
25193
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 23715
diff changeset
   266
                    Class<?> c;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                    ClassLoader loader = getClass().getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    if (loader != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                        c = loader.loadClass(classname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                        c = Class.forName(classname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                    Object o = c.newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                    if (o instanceof GlyphPainter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                        defaultPainter = (GlyphPainter) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                } catch (Throwable e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                    throw new StateInvariantError("GlyphView: Can't load glyph painter: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                                                  + classname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            setGlyphPainter(defaultPainter.getPainter(this, getStartOffset(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                                                      getEndOffset()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    // --- TabableView methods --------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * Determines the desired span when using the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * tab expansion implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @param x the position the view would be located
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 19176
diff changeset
   294
     *  at for the purpose of tab expansion &gt;= 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @param e how to expand the tabs when encountered.
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 19176
diff changeset
   296
     * @return the desired span &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @see TabableView#getTabbedSpan
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    public float getTabbedSpan(float x, TabExpander e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        checkPainter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        TabExpander old = expander;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        expander = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        if (expander != old) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            // setting expander can change horizontal span of the view,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            // so we have to call preferenceChanged()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            preferenceChanged(null, true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        this.x = (int) x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        int p0 = getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        int p1 = getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        float width = painter.getSpan(this, p0, p1, expander, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        return width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * Determines the span along the same axis as tab
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * expansion for a portion of the view.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * intended for use by the TabExpander for cases
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * where the tab expansion involves aligning the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * portion of text that doesn't have whitespace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * relative to the tab stop.  There is therefore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * an assumption that the range given does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * contain tabs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * This method can be called while servicing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * getTabbedSpan or getPreferredSize.  It has to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * arrange for its own text buffer to make the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * measurements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 19176
diff changeset
   333
     * @param p0 the starting document offset &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 19176
diff changeset
   334
     * @param p1 the ending document offset &gt;= p0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 19176
diff changeset
   335
     * @return the span &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    public float getPartialSpan(int p0, int p1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        checkPainter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        float width = painter.getSpan(this, p0, p1, expander, x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        return width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    // --- View methods ---------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * Fetches the portion of the model that this view is responsible for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * @return the starting offset into the model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * @see View#getStartOffset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    public int getStartOffset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        Element e = getElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        return (length > 0) ? e.getStartOffset() + offset : e.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * Fetches the portion of the model that this view is responsible for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @return the ending offset into the model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * @see View#getEndOffset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    public int getEndOffset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        Element e = getElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        return (length > 0) ? e.getStartOffset() + offset + length : e.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * Lazily initializes the selections field
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    private void initSelections(int p0, int p1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        int viewPosCount = p1 - p0 + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        if (selections == null || viewPosCount > selections.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            selections = new byte[viewPosCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        for (int i = 0; i < viewPosCount; selections[i++] = 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * Renders a portion of a text style run.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * @param g the rendering surface to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    public void paint(Graphics g, Shape a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        checkPainter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        boolean paintedText = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        Component c = getContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        int p0 = getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        int p1 = getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        Rectangle alloc = (a instanceof Rectangle) ? (Rectangle)a : a.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        Color bg = getBackground();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        Color fg = getForeground();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
2493
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
   396
        if (c != null && ! c.isEnabled()) {
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
   397
            fg = (c instanceof JTextComponent ?
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
   398
                ((JTextComponent)c).getDisabledTextColor() :
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
   399
                UIManager.getColor("textInactiveText"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        if (bg != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            g.setColor(bg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            g.fillRect(alloc.x, alloc.y, alloc.width, alloc.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        if (c instanceof JTextComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            JTextComponent tc = (JTextComponent) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            Highlighter h = tc.getHighlighter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            if (h instanceof LayeredHighlighter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                ((LayeredHighlighter)h).paintLayeredHighlights
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                    (g, p0, p1, a, tc, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        if (Utilities.isComposedTextElement(getElement())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            Utilities.paintComposedText(g, a.getBounds(), this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            paintedText = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        } else if(c instanceof JTextComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            JTextComponent tc = (JTextComponent) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            Color selFG = tc.getSelectedTextColor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            if (// there's a highlighter (bug 4532590), and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                (tc.getHighlighter() != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                // selected text color is different from regular foreground
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                (selFG != null) && !selFG.equals(fg)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                Highlighter.Highlight[] h = tc.getHighlighter().getHighlights();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                if(h.length != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                    boolean initialized = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                    int viewSelectionCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                    for (int i = 0; i < h.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                        Highlighter.Highlight highlight = h[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                        int hStart = highlight.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                        int hEnd = highlight.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                        if (hStart > p1 || hEnd < p0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                            // the selection is out of this view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                        if (!SwingUtilities2.useSelectedTextColor(highlight, tc)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                        if (hStart <= p0 && hEnd >= p1){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                            // the whole view is selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                            paintTextUsingColor(g, a, selFG, p0, p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                            paintedText = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                        // the array is lazily created only when the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                        // is partially selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                            initSelections(p0, p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                            initialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                        hStart = Math.max(p0, hStart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                        hEnd = Math.min(p1, hEnd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                        paintTextUsingColor(g, a, selFG, hStart, hEnd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                        // the array represents view positions [0, p1-p0+1]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                        // later will iterate this array and sum its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                        // elements. Positions with sum == 0 are not selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                        selections[hStart-p0]++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                        selections[hEnd-p0]--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                        viewSelectionCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                    if (!paintedText && viewSelectionCount > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                        // the view is partially selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                        int curPos = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                        int startPos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                        int viewLen = p1 - p0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                        while (curPos++ < viewLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                            // searching for the next selection start
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                            while(curPos < viewLen &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                                    selections[curPos] == 0) curPos++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                            if (startPos != curPos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                                // paint unselected text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                                paintTextUsingColor(g, a, fg,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                                        p0 + startPos, p0 + curPos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                            int checkSum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                            // searching for next start position of unselected text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                            while (curPos < viewLen &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                                    (checkSum += selections[curPos]) != 0) curPos++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                            startPos = curPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                        paintedText = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        if(!paintedText)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            paintTextUsingColor(g, a, fg, p0, p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * Paints the specified region of text in the specified color.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    final void paintTextUsingColor(Graphics g, Shape a, Color c, int p0, int p1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        // render the glyphs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        g.setColor(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        painter.paint(this, g, a, p0, p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        // render underline or strikethrough if set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        boolean underline = isUnderline();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        boolean strike = isStrikeThrough();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        if (underline || strike) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            // calculate x coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            Rectangle alloc = (a instanceof Rectangle) ? (Rectangle)a : a.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            View parent = getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            if ((parent != null) && (parent.getEndOffset() == p1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                // strip whitespace on end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                Segment s = getText(p0, p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                while (Character.isWhitespace(s.last())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                    p1 -= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                    s.count -= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                SegmentCache.releaseSharedSegment(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            int x0 = alloc.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            int p = getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            if (p != p0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                x0 += (int) painter.getSpan(this, p, p0, getTabExpander(), x0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            int x1 = x0 + (int) painter.getSpan(this, p0, p1, getTabExpander(), x0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            // calculate y coordinate
19176
208ff865828c 8016833: Underlines and strikethrough not rendering correctly
mcherkas
parents: 9212
diff changeset
   526
            int y = alloc.y + (int)(painter.getHeight(this) - painter.getDescent(this));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            if (underline) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                int yTmp = y + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                g.drawLine(x0, yTmp, x1, yTmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            if (strike) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                // move y coordinate above baseline
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                int yTmp = y - (int) (painter.getAscent(this) * 0.3f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                g.drawLine(x0, yTmp, x1, yTmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * Determines the minimum span for this view along an axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * <p>This implementation returns the longest non-breakable area within
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * the view as a minimum span for {@code View.X_AXIS}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * @param axis  may be either {@code View.X_AXIS} or {@code View.Y_AXIS}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * @return      the minimum span the view can be rendered into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * @throws IllegalArgumentException if the {@code axis} parameter is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * @see         javax.swing.text.View#getMinimumSpan
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    public float getMinimumSpan(int axis) {
9212
2123bfe0b40b 6973777: JCK manual case JEditorPaneTests.html#JEditorPane fails in jdk7 b100
rupashka
parents: 7668
diff changeset
   553
        switch (axis) {
2123bfe0b40b 6973777: JCK manual case JEditorPaneTests.html#JEditorPane fails in jdk7 b100
rupashka
parents: 7668
diff changeset
   554
            case View.X_AXIS:
2123bfe0b40b 6973777: JCK manual case JEditorPaneTests.html#JEditorPane fails in jdk7 b100
rupashka
parents: 7668
diff changeset
   555
                if (minimumSpan < 0) {
2123bfe0b40b 6973777: JCK manual case JEditorPaneTests.html#JEditorPane fails in jdk7 b100
rupashka
parents: 7668
diff changeset
   556
                    minimumSpan = 0;
2123bfe0b40b 6973777: JCK manual case JEditorPaneTests.html#JEditorPane fails in jdk7 b100
rupashka
parents: 7668
diff changeset
   557
                    int p0 = getStartOffset();
2123bfe0b40b 6973777: JCK manual case JEditorPaneTests.html#JEditorPane fails in jdk7 b100
rupashka
parents: 7668
diff changeset
   558
                    int p1 = getEndOffset();
2123bfe0b40b 6973777: JCK manual case JEditorPaneTests.html#JEditorPane fails in jdk7 b100
rupashka
parents: 7668
diff changeset
   559
                    while (p1 > p0) {
2123bfe0b40b 6973777: JCK manual case JEditorPaneTests.html#JEditorPane fails in jdk7 b100
rupashka
parents: 7668
diff changeset
   560
                        int breakSpot = getBreakSpot(p0, p1);
2123bfe0b40b 6973777: JCK manual case JEditorPaneTests.html#JEditorPane fails in jdk7 b100
rupashka
parents: 7668
diff changeset
   561
                        if (breakSpot == BreakIterator.DONE) {
2123bfe0b40b 6973777: JCK manual case JEditorPaneTests.html#JEditorPane fails in jdk7 b100
rupashka
parents: 7668
diff changeset
   562
                            // the rest of the view is non-breakable
2123bfe0b40b 6973777: JCK manual case JEditorPaneTests.html#JEditorPane fails in jdk7 b100
rupashka
parents: 7668
diff changeset
   563
                            breakSpot = p0;
2123bfe0b40b 6973777: JCK manual case JEditorPaneTests.html#JEditorPane fails in jdk7 b100
rupashka
parents: 7668
diff changeset
   564
                        }
2123bfe0b40b 6973777: JCK manual case JEditorPaneTests.html#JEditorPane fails in jdk7 b100
rupashka
parents: 7668
diff changeset
   565
                        minimumSpan = Math.max(minimumSpan,
2123bfe0b40b 6973777: JCK manual case JEditorPaneTests.html#JEditorPane fails in jdk7 b100
rupashka
parents: 7668
diff changeset
   566
                                getPartialSpan(breakSpot, p1));
2123bfe0b40b 6973777: JCK manual case JEditorPaneTests.html#JEditorPane fails in jdk7 b100
rupashka
parents: 7668
diff changeset
   567
                        // Note: getBreakSpot returns the *last* breakspot
2123bfe0b40b 6973777: JCK manual case JEditorPaneTests.html#JEditorPane fails in jdk7 b100
rupashka
parents: 7668
diff changeset
   568
                        p1 = breakSpot - 1;
2123bfe0b40b 6973777: JCK manual case JEditorPaneTests.html#JEditorPane fails in jdk7 b100
rupashka
parents: 7668
diff changeset
   569
                    }
2123bfe0b40b 6973777: JCK manual case JEditorPaneTests.html#JEditorPane fails in jdk7 b100
rupashka
parents: 7668
diff changeset
   570
                }
2123bfe0b40b 6973777: JCK manual case JEditorPaneTests.html#JEditorPane fails in jdk7 b100
rupashka
parents: 7668
diff changeset
   571
                return minimumSpan;
2123bfe0b40b 6973777: JCK manual case JEditorPaneTests.html#JEditorPane fails in jdk7 b100
rupashka
parents: 7668
diff changeset
   572
            case View.Y_AXIS:
2123bfe0b40b 6973777: JCK manual case JEditorPaneTests.html#JEditorPane fails in jdk7 b100
rupashka
parents: 7668
diff changeset
   573
                return super.getMinimumSpan(axis);
2123bfe0b40b 6973777: JCK manual case JEditorPaneTests.html#JEditorPane fails in jdk7 b100
rupashka
parents: 7668
diff changeset
   574
            default:
2123bfe0b40b 6973777: JCK manual case JEditorPaneTests.html#JEditorPane fails in jdk7 b100
rupashka
parents: 7668
diff changeset
   575
                throw new IllegalArgumentException("Invalid axis: " + axis);
2123bfe0b40b 6973777: JCK manual case JEditorPaneTests.html#JEditorPane fails in jdk7 b100
rupashka
parents: 7668
diff changeset
   576
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * Determines the preferred span for this view along an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * @param axis may be either View.X_AXIS or View.Y_AXIS
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 19176
diff changeset
   584
     * @return   the span the view would like to be rendered into &gt;= 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     *           Typically the view is told to render into the span
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     *           that is returned, although there is no guarantee.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     *           The parent may choose to resize or break the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    public float getPreferredSpan(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        if (impliedCR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        checkPainter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        int p0 = getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        int p1 = getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        switch (axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        case View.X_AXIS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            if (skipWidth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            return painter.getSpan(this, p0, p1, expander, this.x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        case View.Y_AXIS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            float h = painter.getHeight(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            if (isSuperscript()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                h += h/3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            return h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            throw new IllegalArgumentException("Invalid axis: " + axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * Determines the desired alignment for this view along an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * axis.  For the label, the alignment is along the font
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * baseline for the y axis, and the superclasses alignment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * along the x axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * @param axis may be either View.X_AXIS or View.Y_AXIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * @return the desired alignment.  This should be a value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     *   between 0.0 and 1.0 inclusive, where 0 indicates alignment at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     *   origin and 1.0 indicates alignment to the full span
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     *   away from the origin.  An alignment of 0.5 would be the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     *   center of the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    public float getAlignment(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        checkPainter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        if (axis == View.Y_AXIS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            boolean sup = isSuperscript();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            boolean sub = isSubscript();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            float h = painter.getHeight(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            float d = painter.getDescent(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            float a = painter.getAscent(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            float align;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            if (sup) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                align = 1.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            } else if (sub) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                align = (h > 0) ? (h - (d + (a / 2))) / h : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                align = (h > 0) ? (h - d) / h : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            return align;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        return super.getAlignment(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * Provides a mapping from the document model coordinate space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * to the coordinate space of the view mapped to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     *
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 19176
diff changeset
   651
     * @param pos the position to convert &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * @param a   the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * @param b   either <code>Position.Bias.Forward</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     *                or <code>Position.Bias.Backward</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * @return the bounding box of the given position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * @exception BadLocationException  if the given position does not represent a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     *   valid location in the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * @see View#modelToView
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        checkPainter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        return painter.modelToView(this, pos, b, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * Provides a mapping from the view coordinate space to the logical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * coordinate space of the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     *
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 19176
diff changeset
   669
     * @param x the X coordinate &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 19176
diff changeset
   670
     * @param y the Y coordinate &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * @param biasReturn either <code>Position.Bias.Forward</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     *  or <code>Position.Bias.Backward</code> is returned as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     *  zero-th element of this array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * @return the location within the model that best represents the
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 19176
diff changeset
   676
     *  given point of view &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * @see View#viewToModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    public int viewToModel(float x, float y, Shape a, Position.Bias[] biasReturn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        checkPainter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        return painter.viewToModel(this, x, y, a, biasReturn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * Determines how attractive a break opportunity in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * this view is.  This can be used for determining which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * view is the most attractive to call <code>breakView</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * on in the process of formatting.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * higher the weight, the more attractive the break.  A
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * value equal to or lower than <code>View.BadBreakWeight</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * should not be considered for a break.  A value greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * than or equal to <code>View.ForcedBreakWeight</code> should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * be broken.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * This is implemented to forward to the superclass for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * the Y_AXIS.  Along the X_AXIS the following values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * may be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     * <dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * <dt><b>View.ExcellentBreakWeight</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * <dd>if there is whitespace proceeding the desired break
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     *   location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * <dt><b>View.BadBreakWeight</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * <dd>if the desired break location results in a break
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     *   location of the starting offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     * <dt><b>View.GoodBreakWeight</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * <dd>if the other conditions don't occur.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * </dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * This will normally result in the behavior of breaking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * on a whitespace location if one can be found, otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * breaking between characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * @param axis may be either View.X_AXIS or View.Y_AXIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * @param pos the potential location of the start of the
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 19176
diff changeset
   714
     *   broken view &gt;= 0.  This may be useful for calculating tab
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     *   positions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * @param len specifies the relative length from <em>pos</em>
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 19176
diff changeset
   717
     *   where a potential break is desired &gt;= 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * @return the weight, which should be a value between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     *   View.ForcedBreakWeight and View.BadBreakWeight.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * @see LabelView
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * @see ParagraphView
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * @see View#BadBreakWeight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     * @see View#GoodBreakWeight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * @see View#ExcellentBreakWeight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     * @see View#ForcedBreakWeight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    public int getBreakWeight(int axis, float pos, float len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        if (axis == View.X_AXIS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            checkPainter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            int p0 = getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            int p1 = painter.getBoundedPosition(this, p0, pos, len);
3340
78a96d1fe557 6612541: api/javax_swing/text/LabelView/index.html#getXXX[LabelView0004] fails since JDK 7 b20
gsm
parents: 2493
diff changeset
   732
            return p1 == p0 ? View.BadBreakWeight :
78a96d1fe557 6612541: api/javax_swing/text/LabelView/index.html#getXXX[LabelView0004] fails since JDK 7 b20
gsm
parents: 2493
diff changeset
   733
                   getBreakSpot(p0, p1) != BreakIterator.DONE ?
78a96d1fe557 6612541: api/javax_swing/text/LabelView/index.html#getXXX[LabelView0004] fails since JDK 7 b20
gsm
parents: 2493
diff changeset
   734
                            View.ExcellentBreakWeight : View.GoodBreakWeight;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        return super.getBreakWeight(axis, pos, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * Breaks this view on the given axis at the given length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * This is implemented to attempt to break on a whitespace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * location, and returns a fragment with the whitespace at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * the end.  If a whitespace location can't be found, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     * nearest character is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     * @param axis may be either View.X_AXIS or View.Y_AXIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     * @param p0 the location in the model where the
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 19176
diff changeset
   748
     *  fragment should start it's representation &gt;= 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     * @param pos the position along the axis that the
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 19176
diff changeset
   750
     *  broken view would occupy &gt;= 0.  This may be useful for
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     *  things like tab calculations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * @param len specifies the distance along the axis
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 19176
diff changeset
   753
     *  where a potential break is desired &gt;= 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * @return the fragment of the view that represents the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     *  given span, if the view can be broken.  If the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     *  doesn't support breaking behavior, the view itself is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     *  returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * @see View#breakView
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
    public View breakView(int axis, int p0, float pos, float len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        if (axis == View.X_AXIS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            checkPainter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            int p1 = painter.getBoundedPosition(this, p0, pos, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            int breakSpot = getBreakSpot(p0, p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            if (breakSpot != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                p1 = breakSpot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            // else, no break in the region, return a fragment of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            // bounded region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            if (p0 == getStartOffset() && p1 == getEndOffset()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            GlyphView v = (GlyphView) createFragment(p0, p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            v.x = (int) pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            return v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * Returns a location to break at in the passed in region, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * BreakIterator.DONE if there isn't a good location to break at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * in the specified region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
    private int getBreakSpot(int p0, int p1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        if (breakSpots == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            // Re-calculate breakpoints for the whole view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            int start = getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            int end = getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            int[] bs = new int[end + 1 - start];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            int ix = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            // Breaker should work on the parent element because there may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            // a valid breakpoint at the end edge of the view (space, etc.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            Element parent = getElement().getParentElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            int pstart = (parent == null ? start : parent.getStartOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            int pend = (parent == null ? end : parent.getEndOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            Segment s = getText(pstart, pend);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            s.first();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            BreakIterator breaker = getBreaker();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            breaker.setText(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            // Backward search should start from end+1 unless there's NO end+1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            int startFrom = end + (pend > end ? 1 : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                startFrom = breaker.preceding(s.offset + (startFrom - pstart))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                          + (pstart - s.offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                if (startFrom > start) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                    // The break spot is within the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                    bs[ix++] = startFrom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
            SegmentCache.releaseSharedSegment(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            breakSpots = new int[ix];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            System.arraycopy(bs, 0, breakSpots, 0, ix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        int breakSpot = BreakIterator.DONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        for (int i = 0; i < breakSpots.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            int bsp = breakSpots[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            if (bsp <= p1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                if (bsp > p0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                    breakSpot = bsp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        return breakSpot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * Return break iterator appropriate for the current document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     * For non-i18n documents a fast whitespace-based break iterator is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    private BreakIterator getBreaker() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        if ((doc != null) && Boolean.TRUE.equals(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                    doc.getProperty(AbstractDocument.MultiByteProperty))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            Container c = getContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
            Locale locale = (c == null ? Locale.getDefault() : c.getLocale());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            return BreakIterator.getLineInstance(locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            return new WhitespaceBasedBreakIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * Creates a view that represents a portion of the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * This is potentially useful during formatting operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     * for taking measurements of fragments of the view.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * the view doesn't support fragmenting (the default), it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * should return itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * This view does support fragmenting.  It is implemented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     * to return a nested class that shares state in this view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     * representing only a portion of the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     *
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 19176
diff changeset
   864
     * @param p0 the starting offset &gt;= 0.  This should be a value
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     *   greater or equal to the element starting offset and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     *   less than the element ending offset.
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 19176
diff changeset
   867
     * @param p1 the ending offset &gt; p0.  This should be a value
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     *   less than or equal to the elements end offset and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     *   greater than the elements starting offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * @return the view fragment, or itself if the view doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     *   support breaking into fragments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * @see LabelView
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
    public View createFragment(int p0, int p1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        checkPainter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        Element elem = getElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        GlyphView v = (GlyphView) clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        v.offset = p0 - elem.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        v.length = p1 - p0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        v.painter = painter.getPainter(v, p0, p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        v.justificationInfo = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        return v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * Provides a way to determine the next visually represented model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * location that one might place a caret.  Some views may not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * visible, they might not be in the same order found in the model, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     * they just might not allow access to some of the locations in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * model.
21256
dfb1f9090caa 7016396: (spec) JCK test mentioned in 6735293 is still failing
malenkov
parents: 20158
diff changeset
   891
     * This method enables specifying a position to convert
dfb1f9090caa 7016396: (spec) JCK test mentioned in 6735293 is still failing
malenkov
parents: 20158
diff changeset
   892
     * within the range of &gt;=0.  If the value is -1, a position
dfb1f9090caa 7016396: (spec) JCK test mentioned in 6735293 is still failing
malenkov
parents: 20158
diff changeset
   893
     * will be calculated automatically.  If the value &lt; -1,
dfb1f9090caa 7016396: (spec) JCK test mentioned in 6735293 is still failing
malenkov
parents: 20158
diff changeset
   894
     * the {@code BadLocationException} will be thrown.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     *
21256
dfb1f9090caa 7016396: (spec) JCK test mentioned in 6735293 is still failing
malenkov
parents: 20158
diff changeset
   896
     * @param pos the position to convert
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     * @param direction the direction from the current position that can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     *  be thought of as the arrow keys typically found on a keyboard.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     *  This may be SwingConstants.WEST, SwingConstants.EAST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     *  SwingConstants.NORTH, or SwingConstants.SOUTH.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     * @return the location within the model that best represents the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     *  location visual position.
21256
dfb1f9090caa 7016396: (spec) JCK test mentioned in 6735293 is still failing
malenkov
parents: 20158
diff changeset
   904
     * @exception BadLocationException the given position is not a valid
dfb1f9090caa 7016396: (spec) JCK test mentioned in 6735293 is still failing
malenkov
parents: 20158
diff changeset
   905
     *                                 position within the document
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     * @exception IllegalArgumentException for an invalid direction
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
    public int getNextVisualPositionFrom(int pos, Position.Bias b, Shape a,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                                         int direction,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                                         Position.Bias[] biasRet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
27063
964ad56e8d1c 8058305: BadLocationException is not thrown by javax.swing.text.View.getNextVisualPositionFrom() for invalid positions
alexsch
parents: 26742
diff changeset
   913
        if (pos < -1 || pos > getDocument().getLength()) {
21256
dfb1f9090caa 7016396: (spec) JCK test mentioned in 6735293 is still failing
malenkov
parents: 20158
diff changeset
   914
            throw new BadLocationException("invalid position", pos);
dfb1f9090caa 7016396: (spec) JCK test mentioned in 6735293 is still failing
malenkov
parents: 20158
diff changeset
   915
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        return painter.getNextVisualPositionFrom(this, pos, b, a, direction, biasRet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     * Gives notification that something was inserted into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     * the document in a location that this view is responsible for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     * This is implemented to call preferenceChanged along the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * axis the glyphs are rendered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     * @param e the change information from the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     * @param a the current allocation of the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     * @param f the factory to use to rebuild if the view has children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     * @see View#insertUpdate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
    public void insertUpdate(DocumentEvent e, Shape a, ViewFactory f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        justificationInfo = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        breakSpots = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
        minimumSpan = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        syncCR();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        preferenceChanged(null, true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     * Gives notification that something was removed from the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     * in a location that this view is responsible for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     * This is implemented to call preferenceChanged along the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     * axis the glyphs are rendered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * @param e the change information from the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * @param a the current allocation of the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     * @param f the factory to use to rebuild if the view has children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     * @see View#removeUpdate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
    public void removeUpdate(DocumentEvent e, Shape a, ViewFactory f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        justificationInfo = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        breakSpots = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        minimumSpan = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        syncCR();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        preferenceChanged(null, true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     * Gives notification from the document that attributes were changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
     * in a location that this view is responsible for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     * This is implemented to call preferenceChanged along both the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     * horizontal and vertical axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     * @param e the change information from the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     * @param a the current allocation of the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     * @param f the factory to use to rebuild if the view has children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     * @see View#changedUpdate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
    public void changedUpdate(DocumentEvent e, Shape a, ViewFactory f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        minimumSpan = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        syncCR();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        preferenceChanged(null, true, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
    // checks if the paragraph is empty and updates impliedCR flag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
    // accordingly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
    private void syncCR() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        if (impliedCR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
            Element parent = getElement().getParentElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
            impliedCR = (parent != null && parent.getElementCount() > 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
26742
ff54dcccf417 8048110: Using tables in JTextPane leads to infinite loop in FlowLayout.layoutRow
dmarkov
parents: 25859
diff changeset
   983
    /** {@inheritDoc} */
ff54dcccf417 8048110: Using tables in JTextPane leads to infinite loop in FlowLayout.layoutRow
dmarkov
parents: 25859
diff changeset
   984
    @Override
ff54dcccf417 8048110: Using tables in JTextPane leads to infinite loop in FlowLayout.layoutRow
dmarkov
parents: 25859
diff changeset
   985
    void updateAfterChange() {
ff54dcccf417 8048110: Using tables in JTextPane leads to infinite loop in FlowLayout.layoutRow
dmarkov
parents: 25859
diff changeset
   986
        // Drop the break spots. They will be re-calculated during
ff54dcccf417 8048110: Using tables in JTextPane leads to infinite loop in FlowLayout.layoutRow
dmarkov
parents: 25859
diff changeset
   987
        // layout. It is necessary for proper line break calculation.
ff54dcccf417 8048110: Using tables in JTextPane leads to infinite loop in FlowLayout.layoutRow
dmarkov
parents: 25859
diff changeset
   988
        breakSpots = null;
ff54dcccf417 8048110: Using tables in JTextPane leads to infinite loop in FlowLayout.layoutRow
dmarkov
parents: 25859
diff changeset
   989
    }
ff54dcccf417 8048110: Using tables in JTextPane leads to infinite loop in FlowLayout.layoutRow
dmarkov
parents: 25859
diff changeset
   990
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     * Class to hold data needed to justify this GlyphView in a PargraphView.Row
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
    static class JustificationInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        //justifiable content start
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        final int start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        //justifiable content end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
        final int end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        final int leadingSpaces;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
        final int contentSpaces;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        final int trailingSpaces;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        final boolean hasTab;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        final BitSet spaceMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        JustificationInfo(int start, int end,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
                          int leadingSpaces,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
                          int contentSpaces,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
                          int trailingSpaces,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
                          boolean hasTab,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
                          BitSet spaceMap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
            this.start = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
            this.end = end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
            this.leadingSpaces = leadingSpaces;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
            this.contentSpaces = contentSpaces;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            this.trailingSpaces = trailingSpaces;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
            this.hasTab = hasTab;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
            this.spaceMap = spaceMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
    JustificationInfo getJustificationInfo(int rowStartOffset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        if (justificationInfo != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
            return justificationInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
        //states for the parsing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        final int TRAILING = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        final int CONTENT  = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        final int SPACES   = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        int startOffset = getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        int endOffset = getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        Segment segment = getText(startOffset, endOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
        int txtOffset = segment.offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        int txtEnd = segment.offset + segment.count - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        int startContentPosition = txtEnd + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        int endContentPosition = txtOffset - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        int lastTabPosition = txtOffset - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
        int trailingSpaces = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
        int contentSpaces = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
        int leadingSpaces = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
        boolean hasTab = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        BitSet spaceMap = new BitSet(endOffset - startOffset + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
        //we parse conent to the right of the rightmost TAB only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        //we are looking for the trailing and leading spaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        //position after the leading spaces (startContentPosition)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        //position before the trailing spaces (endContentPosition)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        for (int i = txtEnd, state = TRAILING; i >= txtOffset; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
            if (' ' == segment.array[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
                spaceMap.set(i - txtOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
                if (state == TRAILING) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
                    trailingSpaces++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
                } else if (state == CONTENT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
                    state = SPACES;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
                    leadingSpaces = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
                } else if (state == SPACES) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                    leadingSpaces++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
            } else if ('\t' == segment.array[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
                hasTab = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
                if (state == TRAILING) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
                    if ('\n' != segment.array[i]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
                          && '\r' != segment.array[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
                        state = CONTENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
                        endContentPosition = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
                } else if (state == CONTENT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
                    //do nothing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
                } else if (state == SPACES) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
                    contentSpaces += leadingSpaces;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
                    leadingSpaces = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
                startContentPosition = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
        SegmentCache.releaseSharedSegment(segment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        int startJustifiableContent = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        if (startContentPosition < txtEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
            startJustifiableContent =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
                startContentPosition - txtOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
        int endJustifiableContent = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        if (endContentPosition > txtOffset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
            endJustifiableContent =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
                endContentPosition - txtOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        justificationInfo =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
            new JustificationInfo(startJustifiableContent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
                                  endJustifiableContent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
                                  leadingSpaces,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
                                  contentSpaces,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
                                  trailingSpaces,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
                                  hasTab,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
                                  spaceMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        return justificationInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
    // --- variables ------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
    * Used by paint() to store highlighted view positions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
    private byte[] selections = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
    int offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
    int length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
    // if it is an implied newline character
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
    boolean impliedCR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
    boolean skipWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
     * how to expand tabs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
    TabExpander expander;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
    /** Cached minimum x-span value  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
    private float minimumSpan = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
    /** Cached breakpoints within the view  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
    private int[] breakSpots = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     * location for determining tab expansion against.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
    int x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
     * Glyph rendering functionality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
    GlyphPainter painter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
     * The prototype painter used by default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
    static GlyphPainter defaultPainter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
    private JustificationInfo justificationInfo = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     * A class to perform rendering of the glyphs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     * This can be implemented to be stateless, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     * to hold some information as a cache to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
     * facilitate faster rendering and model/view
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
     * translation.  At a minimum, the GlyphPainter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
     * allows a View implementation to perform its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     * duties independant of a particular version
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
     * of JVM and selection of capabilities (i.e.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
     * shaping for i18n, etc).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
     */
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 32859
diff changeset
  1156
    public abstract static class GlyphPainter {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
         * Determine the span the glyphs given a start location
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
         * (for tab expansion).
30462
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 27063
diff changeset
  1161
         * @param v  the {@code GlyphView}
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 27063
diff changeset
  1162
         * @param p0 the beginning position
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 27063
diff changeset
  1163
         * @param p1 the ending position
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 27063
diff changeset
  1164
         * @param e  how to expand the tabs when encountered
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 27063
diff changeset
  1165
         * @param x the X coordinate
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 27063
diff changeset
  1166
         * @return the span the glyphs given a start location
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
        public abstract float getSpan(GlyphView v, int p0, int p1, TabExpander e, float x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
30462
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 27063
diff changeset
  1170
        /**
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 27063
diff changeset
  1171
         * Returns of the height.
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 27063
diff changeset
  1172
         * @param v  the {@code GlyphView}
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 27063
diff changeset
  1173
         * @return of the height
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 27063
diff changeset
  1174
         */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        public abstract float getHeight(GlyphView v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
30462
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 27063
diff changeset
  1177
        /**
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 27063
diff changeset
  1178
         * Returns of the ascent.
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 27063
diff changeset
  1179
         * @param v  the {@code GlyphView}
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 27063
diff changeset
  1180
         * @return of the ascent
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 27063
diff changeset
  1181
         */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        public abstract float getAscent(GlyphView v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
30462
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 27063
diff changeset
  1184
        /**
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 27063
diff changeset
  1185
         * Returns of the descent.
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 27063
diff changeset
  1186
         * @param v  the {@code GlyphView}
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 27063
diff changeset
  1187
         * @return of the descent
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 27063
diff changeset
  1188
         */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        public abstract float getDescent(GlyphView v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
         * Paint the glyphs representing the given range.
30462
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 27063
diff changeset
  1193
         * @param v the {@code GlyphView}
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 27063
diff changeset
  1194
         * @param g the graphics context
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 27063
diff changeset
  1195
         * @param a the current allocation of the view
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 27063
diff changeset
  1196
         * @param p0 the beginning position
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 27063
diff changeset
  1197
         * @param p1 the ending position
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
        public abstract void paint(GlyphView v, Graphics g, Shape a, int p0, int p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
         * Provides a mapping from the document model coordinate space
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
         * to the coordinate space of the view mapped to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
         * This is shared by the broken views.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
         * @param v     the <code>GlyphView</code> containing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
         *              destination coordinate space
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
         * @param pos   the position to convert
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
         * @param bias  either <code>Position.Bias.Forward</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
         *                  or <code>Position.Bias.Backward</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
         * @param a     Bounds of the View
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
         * @return      the bounding box of the given position
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
         * @exception BadLocationException  if the given position does not represent a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
         *   valid location in the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
         * @see View#modelToView
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
        public abstract Shape modelToView(GlyphView v,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
                                          int pos, Position.Bias bias,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
                                          Shape a) throws BadLocationException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
         * Provides a mapping from the view coordinate space to the logical
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
         * coordinate space of the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
         * @param v          the <code>GlyphView</code> to provide a mapping for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
         * @param x          the X coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
         * @param y          the Y coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
         * @param a          the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
         * @param biasReturn either <code>Position.Bias.Forward</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
         *                   or <code>Position.Bias.Backward</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
         *                   is returned as the zero-th element of this array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
         * @return the location within the model that best represents the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
         *         given point of view
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
         * @see View#viewToModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
        public abstract int viewToModel(GlyphView v,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
                                        float x, float y, Shape a,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
                                        Position.Bias[] biasReturn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
         * Determines the model location that represents the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
         * maximum advance that fits within the given span.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
         * This could be used to break the given view.  The result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
         * should be a location just shy of the given advance.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
         * differs from viewToModel which returns the closest
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
         * position which might be proud of the maximum advance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
         * @param v the view to find the model location to break at.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
         * @param p0 the location in the model where the
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 19176
diff changeset
  1250
         *  fragment should start it's representation &gt;= 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
         * @param x  the graphic location along the axis that the
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 19176
diff changeset
  1252
         *  broken view would occupy &gt;= 0.  This may be useful for
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
         *  things like tab calculations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
         * @param len specifies the distance into the view
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 19176
diff changeset
  1255
         *  where a potential break is desired &gt;= 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
         * @return the maximum model location possible for a break.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
         * @see View#breakView
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
        public abstract int getBoundedPosition(GlyphView v, int p0, float x, float len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
         * Create a painter to use for the given GlyphView.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
         * the painter carries state it can create another painter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
         * to represent a new GlyphView that is being created.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
         * the painter doesn't hold any significant state, it can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
         * return itself.  The default behavior is to return itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
         * @param v  the <code>GlyphView</code> to provide a painter for
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 19176
diff changeset
  1268
         * @param p0 the starting document offset &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 19176
diff changeset
  1269
         * @param p1 the ending document offset &gt;= p0
30462
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 27063
diff changeset
  1270
         * @return a painter to use for the given GlyphView
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
        public GlyphPainter getPainter(GlyphView v, int p0, int p1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
         * Provides a way to determine the next visually represented model
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
         * location that one might place a caret.  Some views may not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
         * visible, they might not be in the same order found in the model, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
         * they just might not allow access to some of the locations in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
         * model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
         * @param v the view to use
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 19176
diff changeset
  1284
         * @param pos the position to convert &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
         * @param b   either <code>Position.Bias.Forward</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
         *                or <code>Position.Bias.Backward</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
         * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
         * @param direction the direction from the current position that can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
         *  be thought of as the arrow keys typically found on a keyboard.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
         *  This may be SwingConstants.WEST, SwingConstants.EAST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
         *  SwingConstants.NORTH, or SwingConstants.SOUTH.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
         * @param biasRet  either <code>Position.Bias.Forward</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
         *                 or <code>Position.Bias.Backward</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
         *                 is returned as the zero-th element of this array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
         * @return the location within the model that best represents the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
         *  location visual position.
22260
c9185e010e03 8031082: Fix non-missing doclint problems in client libraries
darcy
parents: 21256
diff changeset
  1297
         * @exception BadLocationException for a bad location within a document model
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
         * @exception IllegalArgumentException for an invalid direction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
        public int getNextVisualPositionFrom(GlyphView v, int pos, Position.Bias b, Shape a,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
                                             int direction,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
                                             Position.Bias[] biasRet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
            throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
            int startOffset = v.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
            int endOffset = v.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
            Segment text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
            switch (direction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
            case View.NORTH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
            case View.SOUTH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
                if (pos != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
                    // Presumably pos is between startOffset and endOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
                    // since GlyphView is only one line, we won't contain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
                    // the position to the nort/south, therefore return -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
                Container container = v.getContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
                if (container instanceof JTextComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
                    Caret c = ((JTextComponent)container).getCaret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
                    Point magicPoint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
                    magicPoint = (c != null) ? c.getMagicCaretPosition() :null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
                    if (magicPoint == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
                        biasRet[0] = Position.Bias.Forward;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
                        return startOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
                    int value = v.viewToModel(magicPoint.x, 0f, a, biasRet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
                    return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
            case View.EAST:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
                if(startOffset == v.getDocument().getLength()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
                    if(pos == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
                        biasRet[0] = Position.Bias.Forward;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
                        return startOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
                    // End case for bidi text where newline is at beginning
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
                    // of line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
                if(pos == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
                    biasRet[0] = Position.Bias.Forward;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
                    return startOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
                if(pos == endOffset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
                if(++pos == endOffset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
                    // Assumed not used in bidi text, GlyphPainter2 will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
                    // override as necessary, therefore return -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
                    biasRet[0] = Position.Bias.Forward;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
                return pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
            case View.WEST:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
                if(startOffset == v.getDocument().getLength()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
                    if(pos == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
                        biasRet[0] = Position.Bias.Forward;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
                        return startOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
                    // End case for bidi text where newline is at beginning
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
                    // of line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
                if(pos == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
                    // Assumed not used in bidi text, GlyphPainter2 will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
                    // override as necessary, therefore return -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
                    biasRet[0] = Position.Bias.Forward;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
                    return endOffset - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
                if(pos == startOffset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
                biasRet[0] = Position.Bias.Forward;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
                return (pos - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
                throw new IllegalArgumentException("Bad direction: " + direction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
            return pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
}