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