jdk/src/java.desktop/share/classes/javax/swing/text/WrappedPlainView.java
author serb
Mon, 06 Mar 2017 22:52:45 +0300
changeset 44155 1bf93336ea45
parent 43075 668b6d00cc79
permissions -rw-r--r--
8158209: Editing in TableView breaks the layout, when the document is I18n Reviewed-by: serb, alexsch Contributed-by: Abossolo Foh Guy <guy.abossolo.foh@scientificware.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
30462
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 28059
diff changeset
     2
 * Copyright (c) 1998, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3504
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: 3504
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: 3504
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3504
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3504
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.*;
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
    28
import java.awt.font.FontRenderContext;
42717
367db14731fb 8169719: WrappedPlainView.modelToView() should return Rectangle2D
alexsch
parents: 42216
diff changeset
    29
import java.awt.geom.Rectangle2D;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.ref.SoftReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.swing.event.*;
43075
668b6d00cc79 8169922: SwingMark/TextArea: 2-7% regression on Linux, Mac, Windows in 9-b143
alexsch
parents: 42717
diff changeset
    32
import static javax.swing.text.PlainView.FPMethodArgs.*;
668b6d00cc79 8169922: SwingMark/TextArea: 2-7% regression on Linux, Mac, Windows in 9-b143
alexsch
parents: 42717
diff changeset
    33
import static javax.swing.text.PlainView.getFPMethodOverridden;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * View of plain text (text with only one font and color)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * that does line-wrapping.  This view expects that its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * associated element has child elements that represent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * the lines it should be wrapping.  It is implemented
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * as a vertical box that contains logical line views.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * The logical line views are nested classes that render
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * the logical line as multiple physical line if the logical
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * line is too wide to fit within the allocation.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * line views draw upon the outer class for its state
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * to reduce their memory requirements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * The line views do all of their rendering through the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <code>drawLine</code> method which in turn does all of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * its rendering through the <code>drawSelectedText</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * and <code>drawUnselectedText</code> methods.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * enables subclasses to easily specialize the rendering
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * without concern for the layout aspects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * @author  Timothy Prinzing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @see     View
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
public class WrappedPlainView extends BoxView implements TabExpander {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * Creates a new WrappedPlainView.  Lines will be wrapped
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * on character boundaries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * @param elem the element underlying the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public WrappedPlainView(Element elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        this(elem, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Creates a new WrappedPlainView.  Lines can be wrapped on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * either character or word boundaries depending upon the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * setting of the wordWrap parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * @param elem the element underlying the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * @param wordWrap should lines be wrapped on word boundaries?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public WrappedPlainView(Element elem, boolean wordWrap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        super(elem, Y_AXIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        this.wordWrap = wordWrap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Returns the tab size set for the document, defaulting to 8.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @return the tab size
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    protected int getTabSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        Integer i = (Integer) getDocument().getProperty(PlainDocument.tabSizeAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        int size = (i != null) ? i.intValue() : 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Renders a line of text, suppressing whitespace at the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * and expanding any tabs.  This is implemented to make calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * to the methods <code>drawUnselectedText</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * <code>drawSelectedText</code> so that the way selected and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * unselected text are rendered can be customized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 9035
diff changeset
   100
     * @param p0 the starting document location to use &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 9035
diff changeset
   101
     * @param p1 the ending document location to use &gt;= p1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @param g the graphics context
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 9035
diff changeset
   103
     * @param x the starting X position &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 9035
diff changeset
   104
     * @param y the starting Y position &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @see #drawUnselectedText
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @see #drawSelectedText
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   107
     *
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   108
     * @deprecated replaced by
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   109
     *     {@link #drawLine(int, int, Graphics2D, float, float)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   111
    @Deprecated(since = "9")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    protected void drawLine(int p0, int p1, Graphics g, int x, int y) {
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   113
        drawLineImpl(p0, p1, g, x, y, false);
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   114
    }
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   115
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   116
    private void drawLineImpl(int p0, int p1, Graphics g, float x, float y,
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   117
                              boolean useFPAPI) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        Element lineMap = getElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        Element line = lineMap.getElement(lineMap.getElementIndex(p0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        Element elem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            if (line.isLeaf()) {
5762
6b50a6f25d81 6857057: api/javax_swing/text/GlyphView/index.html#Methods test fails
rupashka
parents: 5597
diff changeset
   124
                 drawText(line, p0, p1, g, x, y);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                // this line contains the composed text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                int idx = line.getElementIndex(p0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                int lastIdx = line.getElementIndex(p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                for(; idx <= lastIdx; idx++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    elem = line.getElement(idx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                    int start = Math.max(elem.getStartOffset(), p0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                    int end = Math.min(elem.getEndOffset(), p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    x = drawText(elem, start, end, g, x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            throw new StateInvariantError("Can't render: " + p0 + "," + p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
39553
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   141
    /**
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   142
     * Renders a line of text, suppressing whitespace at the end
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   143
     * and expanding any tabs.  This is implemented to make calls
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   144
     * to the methods <code>drawUnselectedText</code> and
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   145
     * <code>drawSelectedText</code> so that the way selected and
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   146
     * unselected text are rendered can be customized.
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   147
     *
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   148
     * @param p0 the starting document location to use &gt;= 0
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   149
     * @param p1 the ending document location to use &gt;= p1
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   150
     * @param g the graphics context
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   151
     * @param x the starting X position &gt;= 0
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   152
     * @param y the starting Y position &gt;= 0
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   153
     * @see #drawUnselectedText
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   154
     * @see #drawSelectedText
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   155
     *
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   156
     * @since 9
39553
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   157
     */
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   158
    protected void drawLine(int p0, int p1, Graphics2D g, float x, float y) {
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   159
        drawLineImpl(p0, p1, g, x, y, true);
39553
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   160
    }
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   161
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   162
    private float drawText(Element elem, int p0, int p1, Graphics g,
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   163
                           float x, float y)
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   164
            throws BadLocationException
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   165
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        p1 = Math.min(getDocument().getLength(), p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        AttributeSet attr = elem.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if (Utilities.isComposedTextAttributeDefined(attr)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            g.setColor(unselected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            x = Utilities.drawComposedText(this, attr, g, x, y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                                        p0-elem.getStartOffset(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                                        p1-elem.getStartOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            if (sel0 == sel1 || selected == unselected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                // no selection, or it is invisible
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   177
                x = callDrawUnselectedText(g, x, y, p0, p1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            } else if ((p0 >= sel0 && p0 <= sel1) && (p1 >= sel0 && p1 <= sel1)) {
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   179
                x = callDrawSelectedText(g, x, y, p0, p1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            } else if (sel0 >= p0 && sel0 <= p1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                if (sel1 >= p0 && sel1 <= p1) {
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   182
                    x = callDrawUnselectedText(g, x, y, p0, sel0);
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   183
                    x = callDrawSelectedText(g, x, y, sel0, sel1);
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   184
                    x = callDrawUnselectedText(g, x, y, sel1, p1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                } else {
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   186
                    x = callDrawUnselectedText(g, x, y, p0, sel0);
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   187
                    x = callDrawSelectedText(g, x, y, sel0, p1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            } else if (sel1 >= p0 && sel1 <= p1) {
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   190
                x = callDrawSelectedText(g, x, y, p0, sel1);
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   191
                x = callDrawUnselectedText(g, x, y, sel1, p1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            } else {
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   193
                x = callDrawUnselectedText(g, x, y, p0, p1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * Renders the given range in the model as normal unselected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @param g the graphics context
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 9035
diff changeset
   205
     * @param x the starting X coordinate &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 9035
diff changeset
   206
     * @param y the starting Y coordinate &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 9035
diff changeset
   207
     * @param p0 the beginning position in the model &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 9035
diff changeset
   208
     * @param p1 the ending position in the model &gt;= p0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 9035
diff changeset
   209
     * @return the X location of the end of the range &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @exception BadLocationException if the range is invalid
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   211
     *
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   212
     * @deprecated replaced by
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   213
     *     {@link #drawUnselectedText(Graphics2D, float, float, int, int)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     */
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   215
    @Deprecated(since = "9")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    protected int drawUnselectedText(Graphics g, int x, int y,
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   217
                                     int p0, int p1) throws BadLocationException
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   218
    {
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   219
        return (int) drawUnselectedTextImpl(g, x, y, p0, p1, false);
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   220
    }
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   221
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   222
    private float callDrawUnselectedText(Graphics g, float x, float y,
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   223
                                         int p0, int p1)
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   224
                                         throws BadLocationException
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   225
    {
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   226
        return drawUnselectedTextOverridden && g instanceof Graphics2D
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   227
                ? drawUnselectedText((Graphics2D) g, x, y, p0, p1)
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   228
                : drawUnselectedText(g, (int) x, (int) y, p0, p1);
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   229
    }
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   230
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   231
    private float drawUnselectedTextImpl(Graphics g, float x, float y,
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   232
                                         int p0, int p1, boolean useFPAPI)
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   233
            throws BadLocationException
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   234
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        g.setColor(unselected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        Segment segment = SegmentCache.getSharedSegment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        doc.getText(p0, p1 - p0, segment);
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   239
        float ret = Utilities.drawTabbedText(this, segment, x, y, g, this, p0,
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   240
                                             null, useFPAPI);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        SegmentCache.releaseSharedSegment(segment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    /**
39553
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   246
     * Renders the given range in the model as normal unselected
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   247
     * text.
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   248
     *
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   249
     * @param g the graphics context
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   250
     * @param x the starting X coordinate &gt;= 0
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   251
     * @param y the starting Y coordinate &gt;= 0
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   252
     * @param p0 the beginning position in the model &gt;= 0
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   253
     * @param p1 the ending position in the model &gt;= p0
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   254
     * @return the X location of the end of the range &gt;= 0
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   255
     * @exception BadLocationException if the range is invalid
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   256
     *
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   257
     * @since 9
39553
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   258
     */
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   259
    protected float drawUnselectedText(Graphics2D g, float x, float y,
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   260
                                     int p0, int p1) throws BadLocationException {
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   261
        return drawUnselectedTextImpl(g, x, y, p0, p1, true);
39553
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   262
    }
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   263
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * Renders the given range in the model as selected text.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * is implemented to render the text in the color specified in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * the hosting component.  It assumes the highlighter will render
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * the selected background.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @param g the graphics context
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 9035
diff changeset
   270
     * @param x the starting X coordinate &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 9035
diff changeset
   271
     * @param y the starting Y coordinate &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 9035
diff changeset
   272
     * @param p0 the beginning position in the model &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 9035
diff changeset
   273
     * @param p1 the ending position in the model &gt;= p0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @return the location of the end of the range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @exception BadLocationException if the range is invalid
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   276
     *
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   277
     * @deprecated replaced by
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   278
     *     {@link #drawSelectedText(Graphics2D, float, float, int, int)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     */
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   280
    @Deprecated(since = "9")
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   281
    protected int drawSelectedText(Graphics g, int x, int y, int p0, int p1)
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   282
            throws BadLocationException
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   283
    {
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   284
        return (int) drawSelectedTextImpl(g, x, y, p0, p1, false);
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   285
    }
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   286
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   287
    private float callDrawSelectedText(Graphics g, float x, float y,
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   288
                                       int p0, int p1)
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   289
                                       throws BadLocationException
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   290
    {
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   291
        return drawSelectedTextOverridden && g instanceof Graphics2D
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   292
                ? drawSelectedText((Graphics2D) g, x, y, p0, p1)
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   293
                : drawSelectedText(g, (int) x, (int) y, p0, p1);
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   294
    }
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   295
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   296
    private float drawSelectedTextImpl(Graphics g, float x, float y,
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   297
                                       int p0, int p1,
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   298
                                       boolean useFPAPI)
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   299
            throws BadLocationException
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   300
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        g.setColor(selected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        Segment segment = SegmentCache.getSharedSegment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        doc.getText(p0, p1 - p0, segment);
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   305
        float ret = Utilities.drawTabbedText(this, segment, x, y, g, this, p0,
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   306
                                             null, useFPAPI);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        SegmentCache.releaseSharedSegment(segment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    /**
39553
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   312
     * Renders the given range in the model as selected text.  This
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   313
     * is implemented to render the text in the color specified in
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   314
     * the hosting component.  It assumes the highlighter will render
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   315
     * the selected background.
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   316
     *
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   317
     * @param g the graphics context
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   318
     * @param x the starting X coordinate &gt;= 0
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   319
     * @param y the starting Y coordinate &gt;= 0
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   320
     * @param p0 the beginning position in the model &gt;= 0
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   321
     * @param p1 the ending position in the model &gt;= p0
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   322
     * @return the location of the end of the range.
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   323
     * @exception BadLocationException if the range is invalid
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   324
     *
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   325
     * @since 9
39553
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   326
     */
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   327
    protected float drawSelectedText(Graphics2D g, float x, float y,
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   328
                                     int p0, int p1) throws BadLocationException {
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   329
        return drawSelectedTextImpl(g, x, y, p0, p1, true);
39553
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   330
    }
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   331
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * Gives access to a buffer that can be used to fetch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * text from the associated document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @return the buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    protected final Segment getLineBuffer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        if (lineBuffer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            lineBuffer = new Segment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        return lineBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * This is called by the nested wrapped line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * views to determine the break location.  This can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * be reimplemented to alter the breaking behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * It will either break at word or character boundaries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * depending upon the break argument given at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * construction.
30462
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 28059
diff changeset
   351
     * @param p0 the starting document location
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 28059
diff changeset
   352
     * @param p1 the ending document location to use
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 28059
diff changeset
   353
     * @return the break position
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     */
42216
621af0ebf6c4 8169518: Suppress Deprecation warnings for deprecated Swing APIs
prr
parents: 41807
diff changeset
   355
    @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    protected int calculateBreakPosition(int p0, int p1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        int p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        Segment segment = SegmentCache.getSharedSegment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        loadText(segment, p0, p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        int currentWidth = getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        if (wordWrap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            p = p0 + Utilities.getBreakLocation(segment, metrics,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                                                tabBase, tabBase + currentWidth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                                                this, p0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        } else {
8533
0537e84f6e69 6760148: Certain fonts are not correctly soft wrapped when using JTextComponent.print()
rupashka
parents: 7668
diff changeset
   366
            p = p0 + Utilities.getTabbedTextOffset(segment, metrics,
0537e84f6e69 6760148: Certain fonts are not correctly soft wrapped when using JTextComponent.print()
rupashka
parents: 7668
diff changeset
   367
                                                   tabBase, tabBase + currentWidth,
0537e84f6e69 6760148: Certain fonts are not correctly soft wrapped when using JTextComponent.print()
rupashka
parents: 7668
diff changeset
   368
                                                   this, p0, false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        SegmentCache.releaseSharedSegment(segment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * Loads all of the children to initialize the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * This is called by the <code>setParent</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * Subclasses can reimplement this to initialize their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * child views in a different manner.  The default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * implementation creates a child view for each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * child element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * @param f the view factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    protected void loadChildren(ViewFactory f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        Element e = getElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        int n = e.getElementCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            View[] added = new View[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                added[i] = new WrappedLine(e.getElement(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            replace(0, 0, added);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * Update the child views in response to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * document event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    void updateChildren(DocumentEvent e, Shape a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        Element elem = getElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        DocumentEvent.ElementChange ec = e.getChange(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        if (ec != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            // the structure of this element changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            Element[] removedElems = ec.getChildrenRemoved();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            Element[] addedElems = ec.getChildrenAdded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            View[] added = new View[addedElems.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            for (int i = 0; i < addedElems.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                added[i] = new WrappedLine(addedElems[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            replace(ec.getIndex(), removedElems.length, added);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            // should damge a little more intelligently.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            if (a != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                preferenceChanged(null, true, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                getContainer().repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        // update font metrics which may be used by the child views
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        updateMetrics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * Load the text buffer with the given range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * of text.  This is used by the fragments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * broken off of this view as well as this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * view itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    final void loadText(Segment segment, int p0, int p1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            doc.getText(p0, p1 - p0, segment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        } catch (BadLocationException bl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            throw new StateInvariantError("Can't get line text");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    final void updateMetrics() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        Component host = getContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        Font f = host.getFont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        metrics = host.getFontMetrics(f);
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   443
        if (useFloatingPointAPI) {
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   444
            FontRenderContext frc = metrics.getFontRenderContext();
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   445
            float tabWidth = (float) f.getStringBounds("m", frc).getWidth();
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   446
            tabSize = getTabSize() * tabWidth;
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   447
        } else {
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   448
            tabSize = getTabSize() * metrics.charWidth('m');
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   449
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    // --- TabExpander methods ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * Returns the next tab stop position after a given reference position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * This implementation does not support things like centering so it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * ignores the tabOffset argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     *
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 9035
diff changeset
   459
     * @param x the current position &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * @param tabOffset the position within the text stream
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 9035
diff changeset
   461
     *   that the tab occurred at &gt;= 0.
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 9035
diff changeset
   462
     * @return the tab stop, measured in points &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    public float nextTabStop(float x, int tabOffset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        if (tabSize == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            return x;
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   467
        float ntabs = (x - tabBase) / tabSize;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        return tabBase + ((ntabs + 1) * tabSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    // --- View methods -------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * Renders using the given rendering surface and area
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * on that surface.  This is implemented to stash the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * selection positions, selection colors, and font
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * metrics for the nested lines to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * @param g the rendering surface to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * @see View#paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    public void paint(Graphics g, Shape a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        Rectangle alloc = (Rectangle) a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        tabBase = alloc.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        JTextComponent host = (JTextComponent) getContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        sel0 = host.getSelectionStart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        sel1 = host.getSelectionEnd();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        unselected = (host.isEnabled()) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            host.getForeground() : host.getDisabledTextColor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        Caret c = host.getCaret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        selected = c.isSelectionVisible() && host.getHighlighter() != null ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                        host.getSelectedTextColor() : unselected;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        g.setFont(host.getFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        // superclass paints the children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        super.paint(g, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * Sets the size of the view.  This should cause
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * layout of the view along the given axis, if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * has any layout duties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     *
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 9035
diff changeset
   507
     * @param width the width &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 9035
diff changeset
   508
     * @param height the height &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    public void setSize(float width, float height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        updateMetrics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        if ((int) width != getWidth()) {
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20158
diff changeset
   513
            // invalidate the view itself since the desired widths
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20158
diff changeset
   514
            // of the children will be based upon this views width.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            preferenceChanged(null, true, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            widthChanging = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        super.setSize(width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        widthChanging = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * Determines the preferred span for this view along an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * axis.  This is implemented to provide the superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * behavior after first making sure that the current font
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * metrics are cached (for the nested lines which use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * the metrics to determine the height of the potentially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * wrapped lines).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * @param axis may be either View.X_AXIS or View.Y_AXIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * @return  the span the view would like to be rendered into.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     *           Typically the view is told to render into the span
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     *           that is returned, although there is no guarantee.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     *           The parent may choose to resize or break the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * @see View#getPreferredSpan
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    public float getPreferredSpan(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        updateMetrics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        return super.getPreferredSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * Determines the minimum span for this view along an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * axis.  This is implemented to provide the superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * behavior after first making sure that the current font
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * metrics are cached (for the nested lines which use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * the metrics to determine the height of the potentially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * wrapped lines).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * @param axis may be either View.X_AXIS or View.Y_AXIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * @return  the span the view would like to be rendered into.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     *           Typically the view is told to render into the span
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     *           that is returned, although there is no guarantee.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     *           The parent may choose to resize or break the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * @see View#getMinimumSpan
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    public float getMinimumSpan(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        updateMetrics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        return super.getMinimumSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * Determines the maximum span for this view along an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * axis.  This is implemented to provide the superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * behavior after first making sure that the current font
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * metrics are cached (for the nested lines which use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * the metrics to determine the height of the potentially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * wrapped lines).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * @param axis may be either View.X_AXIS or View.Y_AXIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * @return  the span the view would like to be rendered into.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     *           Typically the view is told to render into the span
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     *           that is returned, although there is no guarantee.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     *           The parent may choose to resize or break the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * @see View#getMaximumSpan
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    public float getMaximumSpan(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        updateMetrics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        return super.getMaximumSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * Gives notification that something was inserted into the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * document in a location that this view is responsible for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * This is implemented to simply update the children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * @param e the change information from the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * @param a the current allocation of the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * @param f the factory to use to rebuild if the view has children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * @see View#insertUpdate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    public void insertUpdate(DocumentEvent e, Shape a, ViewFactory f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        updateChildren(e, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        Rectangle alloc = ((a != null) && isAllocationValid()) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            getInsideAllocation(a) : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        int pos = e.getOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        View v = getViewAtPosition(pos, alloc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        if (v != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            v.insertUpdate(e, alloc, f);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * Gives notification that something was removed from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * document in a location that this view is responsible for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * This is implemented to simply update the children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * @param e the change information from the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * @param a the current allocation of the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * @param f the factory to use to rebuild if the view has children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * @see View#removeUpdate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    public void removeUpdate(DocumentEvent e, Shape a, ViewFactory f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        updateChildren(e, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        Rectangle alloc = ((a != null) && isAllocationValid()) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            getInsideAllocation(a) : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        int pos = e.getOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        View v = getViewAtPosition(pos, alloc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        if (v != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            v.removeUpdate(e, alloc, f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * Gives notification from the document that attributes were changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * in a location that this view is responsible for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * @param e the change information from the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * @param a the current allocation of the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * @param f the factory to use to rebuild if the view has children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * @see View#changedUpdate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    public void changedUpdate(DocumentEvent e, Shape a, ViewFactory f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        updateChildren(e, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    // --- variables -------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    FontMetrics metrics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
    Segment lineBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    boolean widthChanging;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    int tabBase;
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   645
    float tabSize;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    boolean wordWrap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    int sel0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
    int sel1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    Color unselected;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    Color selected;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * Simple view of a line that wraps if it doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * fit withing the horizontal space allocated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * This class tries to be lightweight by carrying little
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * state of it's own and sharing the state of the outer class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * with it's sibblings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    class WrappedLine extends View {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        WrappedLine(Element elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            super(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            lineCount = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
         * Determines the preferred span for this view along an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
         * axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
         * @param axis may be either X_AXIS or Y_AXIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
         * @return   the span the view would like to be rendered into.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
         *           Typically the view is told to render into the span
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
         *           that is returned, although there is no guarantee.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
         *           The parent may choose to resize or break the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
         * @see View#getPreferredSpan
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        public float getPreferredSpan(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            switch (axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            case View.X_AXIS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                float width = getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                if (width == Integer.MAX_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                    // We have been initially set to MAX_VALUE, but we don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                    // want this as our preferred.
5592
fc99326b0953 6925473: REGRESSION: JOptionPane in dialog is full-screen height
rupashka
parents: 3504
diff changeset
   686
                    return 100f;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                return width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            case View.Y_AXIS:
5592
fc99326b0953 6925473: REGRESSION: JOptionPane in dialog is full-screen height
rupashka
parents: 3504
diff changeset
   690
                if (lineCount < 0 || widthChanging) {
fc99326b0953 6925473: REGRESSION: JOptionPane in dialog is full-screen height
rupashka
parents: 3504
diff changeset
   691
                    breakLines(getStartOffset());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                }
5592
fc99326b0953 6925473: REGRESSION: JOptionPane in dialog is full-screen height
rupashka
parents: 3504
diff changeset
   693
                return lineCount * metrics.getHeight();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                throw new IllegalArgumentException("Invalid axis: " + axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
         * Renders using the given rendering surface and area on that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
         * surface.  The view may need to do layout and create child
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
         * views to enable itself to render into the given allocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
         * @param g the rendering surface to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
         * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
         * @see View#paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        public void paint(Graphics g, Shape a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            Rectangle alloc = (Rectangle) a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            int y = alloc.y + metrics.getAscent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            int x = alloc.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            JTextComponent host = (JTextComponent)getContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            Highlighter h = host.getHighlighter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            LayeredHighlighter dh = (h instanceof LayeredHighlighter) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                                     (LayeredHighlighter)h : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            int start = getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            int end = getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            int p0 = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            int[] lineEnds = getLineEnds();
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   722
            boolean useDrawLineFP = drawLineOverridden && g instanceof Graphics2D;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
            for (int i = 0; i < lineCount; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                int p1 = (lineEnds == null) ? end :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                                             start + lineEnds[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                if (dh != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                    int hOffset = (p1 == end)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                                  ? (p1 - 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                                  : p1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                    dh.paintLayeredHighlights(g, p0, hOffset, a, host, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                }
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   732
                if (useDrawLineFP) {
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   733
                    drawLine(p0, p1, (Graphics2D) g, (float) x, (float) y);
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   734
                } else {
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   735
                    drawLine(p0, p1, g, x, y);
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   736
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                p0 = p1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                y += metrics.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
         * Provides a mapping from the document model coordinate space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
         * to the coordinate space of the view mapped to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
         * @param pos the position to convert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
         * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
         * @return the bounding box of the given position is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
         * @exception BadLocationException  if the given position does not represent a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
         *   valid location in the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
         * @see View#modelToView
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        public Shape modelToView(int pos, Shape a, Position.Bias b)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            Rectangle alloc = a.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            alloc.height = metrics.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            alloc.width = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            int p0 = getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            if (pos < p0 || pos > getEndOffset()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                throw new BadLocationException("Position out of range", pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            int testP = (b == Position.Bias.Forward) ? pos :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                        Math.max(p0, pos - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            int line = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            int[] lineEnds = getLineEnds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            if (lineEnds != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                line = findLine(testP - p0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                if (line > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                    p0 += lineEnds[line - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                alloc.y += alloc.height * line;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            if (pos > p0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                Segment segment = SegmentCache.getSharedSegment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                loadText(segment, p0, pos);
42717
367db14731fb 8169719: WrappedPlainView.modelToView() should return Rectangle2D
alexsch
parents: 42216
diff changeset
   779
                float x = alloc.x;
367db14731fb 8169719: WrappedPlainView.modelToView() should return Rectangle2D
alexsch
parents: 42216
diff changeset
   780
                x += Utilities.getTabbedTextWidth(segment, metrics, x,
367db14731fb 8169719: WrappedPlainView.modelToView() should return Rectangle2D
alexsch
parents: 42216
diff changeset
   781
                                                  WrappedPlainView.this, p0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                SegmentCache.releaseSharedSegment(segment);
42717
367db14731fb 8169719: WrappedPlainView.modelToView() should return Rectangle2D
alexsch
parents: 42216
diff changeset
   783
                return new Rectangle2D.Float(x, alloc.y, alloc.width, alloc.height);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            return alloc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
         * Provides a mapping from the view coordinate space to the logical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
         * coordinate space of the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
         * @param fx the X coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
         * @param fy the Y coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
         * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
         * @return the location within the model that best represents the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
         *  given point in the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
         * @see View#viewToModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
         */
42216
621af0ebf6c4 8169518: Suppress Deprecation warnings for deprecated Swing APIs
prr
parents: 41807
diff changeset
   799
        @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        public int viewToModel(float fx, float fy, Shape a, Position.Bias[] bias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            // PENDING(prinz) implement bias properly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            bias[0] = Position.Bias.Forward;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            Rectangle alloc = (Rectangle) a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            int x = (int) fx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            int y = (int) fy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
            if (y < alloc.y) {
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 25859
diff changeset
   808
                // above the area covered by this icon, so the position
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                // is assumed to be the start of the coverage for this view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                return getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            } else if (y > alloc.y + alloc.height) {
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 25859
diff changeset
   812
                // below the area covered by this icon, so the position
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                // is assumed to be the end of the coverage for this view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                return getEndOffset() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                // positioned within the coverage of this view vertically,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                // so we figure out which line the point corresponds to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                // if the line is greater than the number of lines contained, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                // simply use the last line as it represents the last possible place
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                // we can position to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                alloc.height = metrics.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                int line = (alloc.height > 0 ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                            (y - alloc.y) / alloc.height : lineCount - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                if (line >= lineCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                    return getEndOffset() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                    int p0 = getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                    int p1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                    if (lineCount == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                        p1 = getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                        int[] lineEnds = getLineEnds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                        p1 = p0 + lineEnds[line];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                        if (line > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                            p0 += lineEnds[line - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                    if (x < alloc.x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                        // point is to the left of the line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                        return p0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                    } else if (x > alloc.x + alloc.width) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                        // point is to the right of the line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                        return p1 - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                        // Determine the offset into the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                        Segment segment = SegmentCache.getSharedSegment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                        loadText(segment, p0, p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                        int n = Utilities.getTabbedTextOffset(segment, metrics,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                                                   alloc.x, x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                                                   WrappedPlainView.this, p0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                        SegmentCache.releaseSharedSegment(segment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                        return Math.min(p0 + n, p1 - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        public void insertUpdate(DocumentEvent e, Shape a, ViewFactory f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
            update(e, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        public void removeUpdate(DocumentEvent e, Shape a, ViewFactory f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            update(e, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        private void update(DocumentEvent ev, Shape a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
            int oldCount = lineCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
            breakLines(ev.getOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
            if (oldCount != lineCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                WrappedPlainView.this.preferenceChanged(this, false, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
                // have to repaint any views after the receiver.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
                getContainer().repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
            } else if (a != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                Component c = getContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                Rectangle alloc = (Rectangle) a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                c.repaint(alloc.x, alloc.y, alloc.width, alloc.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
         * Returns line cache. If the cache was GC'ed, recreates it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
         * If there's no cache, returns null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        final int[] getLineEnds() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
            if (lineCache == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
                int[] lineEnds = lineCache.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
                if (lineEnds == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                    // Cache was GC'ed, so rebuild it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                    return breakLines(getStartOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                    return lineEnds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
         * Creates line cache if text breaks into more than one physical line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
         * @param startPos position to start breaking from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
         * @return the cache created, ot null if text breaks into one line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        final int[] breakLines(int startPos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
            int[] lineEnds = (lineCache == null) ? null : lineCache.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
            int[] oldLineEnds = lineEnds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
            int start = getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
            int lineIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
            if (lineEnds != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                lineIndex = findLine(startPos - start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                if (lineIndex > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                    lineIndex--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            int p0 = (lineIndex == 0) ? start : start + lineEnds[lineIndex - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            int p1 = getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            while (p0 < p1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
                int p = calculateBreakPosition(p0, p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
                p0 = (p == p0) ? ++p : p;      // 4410243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
                if (lineIndex == 0 && p0 >= p1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
                    // do not use cache if there's only one line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
                    lineCache = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
                    lineEnds = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
                    lineIndex = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                } else if (lineEnds == null || lineIndex >= lineEnds.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                    // we have 2+ lines, and the cache is not big enough
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
                    // we try to estimate total number of lines
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
                    double growFactor = ((double)(p1 - start) / (p0 - start));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                    int newSize = (int)Math.ceil((lineIndex + 1) * growFactor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
                    newSize = Math.max(newSize, lineIndex + 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                    int[] tmp = new int[newSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                    if (lineEnds != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
                        System.arraycopy(lineEnds, 0, tmp, 0, lineIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                    lineEnds = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
                lineEnds[lineIndex++] = p0 - start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
            lineCount = lineIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
            if (lineCount > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                // check if the cache is too big
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
                int maxCapacity = lineCount + lineCount / 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
                if (lineEnds.length > maxCapacity) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
                    int[] tmp = new int[maxCapacity];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                    System.arraycopy(lineEnds, 0, tmp, 0, lineCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                    lineEnds = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            if (lineEnds != null && lineEnds != oldLineEnds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                lineCache = new SoftReference<int[]>(lineEnds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
            return lineEnds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
         * Binary search in the cache for line containing specified offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
         * (which is relative to the beginning of the view). This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
         * assumes that cache exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        private int findLine(int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
            int[] lineEnds = lineCache.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
            if (offset < lineEnds[0]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
            } else if (offset > lineEnds[lineCount - 1]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
                return lineCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                return findLine(lineEnds, offset, 0, lineCount - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        private int findLine(int[] array, int offset, int min, int max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
            if (max - min <= 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                return max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
                int mid = (max + min) / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
                return (offset < array[mid]) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                        findLine(array, offset, min, mid) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
                        findLine(array, offset, mid, max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        int lineCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        SoftReference<int[]> lineCache = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
    }
41807
f9eb6cb54fed 8156217: Selected text is shifted on HiDPI display
alexsch
parents: 39553
diff changeset
   990
43075
668b6d00cc79 8169922: SwingMark/TextArea: 2-7% regression on Linux, Mac, Windows in 9-b143
alexsch
parents: 42717
diff changeset
   991
    private final boolean drawLineOverridden =
668b6d00cc79 8169922: SwingMark/TextArea: 2-7% regression on Linux, Mac, Windows in 9-b143
alexsch
parents: 42717
diff changeset
   992
            getFPMethodOverridden(getClass(), "drawLine", IIGNN);
668b6d00cc79 8169922: SwingMark/TextArea: 2-7% regression on Linux, Mac, Windows in 9-b143
alexsch
parents: 42717
diff changeset
   993
    private final boolean drawSelectedTextOverridden =
668b6d00cc79 8169922: SwingMark/TextArea: 2-7% regression on Linux, Mac, Windows in 9-b143
alexsch
parents: 42717
diff changeset
   994
            getFPMethodOverridden(getClass(), "drawSelectedText", GNNII);
668b6d00cc79 8169922: SwingMark/TextArea: 2-7% regression on Linux, Mac, Windows in 9-b143
alexsch
parents: 42717
diff changeset
   995
    private final boolean drawUnselectedTextOverridden =
668b6d00cc79 8169922: SwingMark/TextArea: 2-7% regression on Linux, Mac, Windows in 9-b143
alexsch
parents: 42717
diff changeset
   996
            getFPMethodOverridden(getClass(), "drawUnselectedText", GNNII);
668b6d00cc79 8169922: SwingMark/TextArea: 2-7% regression on Linux, Mac, Windows in 9-b143
alexsch
parents: 42717
diff changeset
   997
    private final boolean useFloatingPointAPI =
668b6d00cc79 8169922: SwingMark/TextArea: 2-7% regression on Linux, Mac, Windows in 9-b143
alexsch
parents: 42717
diff changeset
   998
            drawUnselectedTextOverridden || drawSelectedTextOverridden;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
}