jdk/src/java.desktop/share/classes/javax/swing/text/PlainView.java
author alexsch
Tue, 05 Jul 2016 09:36:41 +0300
changeset 39554 12687019f2b9
parent 39553 965a62655c4c
child 41807 f9eb6cb54fed
permissions -rw-r--r--
8142966: Wrong cursor position in text components on HiDPI display Reviewed-by: prr, serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
30462
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 28059
diff changeset
     2
 * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing.text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.Properties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.*;
39554
12687019f2b9 8142966: Wrong cursor position in text components on HiDPI display
alexsch
parents: 39553
diff changeset
    30
import java.util.Objects;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Implements View interface for a simple multi-line text view
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * that has text in one font and color.  The view represents each
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * child element as a line of text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @author  Timothy Prinzing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @see     View
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public class PlainView extends View implements TabExpander {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     * Constructs a new PlainView wrapped on an element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * @param elem the element
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    public PlainView(Element elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        super(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * Returns the tab size set for the document, defaulting to 8.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * @return the tab size
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    protected int getTabSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        Integer i = (Integer) getDocument().getProperty(PlainDocument.tabSizeAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        int size = (i != null) ? i.intValue() : 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
39553
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
    64
     * Returns the tab size set for the document, defaulting to 8.
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
    65
     *
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
    66
     * @implSpec This implementation calls {@link #getTabSize() getTabSize()}.
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
    67
     *
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
    68
     * @return the tab size
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
    69
     */
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
    70
    protected float getFractionalTabSize() {
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
    71
        return getTabSize();
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
    72
    }
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
    73
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
    74
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Renders a line of text, suppressing whitespace at the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * and expanding any tabs.  This is implemented to make calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * to the methods <code>drawUnselectedText</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * <code>drawSelectedText</code> so that the way selected and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * unselected text are rendered can be customized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
    81
     * @param lineIndex the line to draw &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @param g the <code>Graphics</code> context
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
    83
     * @param x the starting X position &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
    84
     * @param y the starting Y position &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @see #drawUnselectedText
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @see #drawSelectedText
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    protected void drawLine(int lineIndex, Graphics g, int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        Element line = getElement().getElement(lineIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        Element elem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            if (line.isLeaf()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                drawElement(lineIndex, line, g, x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                // this line contains the composed text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                int count = line.getElementCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                for(int i = 0; i < count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                    elem = line.getElement(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                    x = drawElement(lineIndex, elem, g, x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            throw new StateInvariantError("Can't render line: " + lineIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
39553
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   108
    /**
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   109
     * 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
   110
     * 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
   111
     * to the methods {@code drawUnselectedText} and
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   112
     * {@code drawSelectedText} so that the way selected and
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   113
     * unselected text are rendered can be customized.
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   114
     *
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   115
     * @implSpec This implementation calls
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   116
     * {@link #drawLine(int, Graphics, int, int)
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   117
     * drawLine(lineIndex, (Graphics)g, (int) x, (int) y)}.
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   118
     *
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   119
     * @param lineIndex the line to draw {@code >= 0}
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   120
     * @param g the {@code Graphics} context
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   121
     * @param x the starting X position {@code >= 0}
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   122
     * @param y the starting Y position {@code >= 0}
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   123
     * @see #drawUnselectedText
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   124
     * @see #drawSelectedText
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   125
     */
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   126
    protected void drawLine(int lineIndex, Graphics2D g, float x, float y) {
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   127
        drawLine(lineIndex, (Graphics)g, (int) x, (int) y);
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   128
    }
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   129
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private int drawElement(int lineIndex, Element elem, Graphics g, int x, int y) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        int p0 = elem.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        int p1 = elem.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        p1 = Math.min(getDocument().getLength(), p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if (lineIndex == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            x += firstLineOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        AttributeSet attr = elem.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if (Utilities.isComposedTextAttributeDefined(attr)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            g.setColor(unselected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            x = Utilities.drawComposedText(this, attr, g, x, y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                                        p0-elem.getStartOffset(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                                        p1-elem.getStartOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            if (sel0 == sel1 || selected == unselected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                // no selection, or it is invisible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                x = drawUnselectedText(g, x, y, p0, p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            } else if ((p0 >= sel0 && p0 <= sel1) && (p1 >= sel0 && p1 <= sel1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                x = drawSelectedText(g, x, y, p0, p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            } else if (sel0 >= p0 && sel0 <= p1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                if (sel1 >= p0 && sel1 <= p1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    x = drawUnselectedText(g, x, y, p0, sel0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    x = drawSelectedText(g, x, y, sel0, sel1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    x = drawUnselectedText(g, x, y, sel1, p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    x = drawUnselectedText(g, x, y, p0, sel0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    x = drawSelectedText(g, x, y, sel0, p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            } else if (sel1 >= p0 && sel1 <= p1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                x = drawSelectedText(g, x, y, p0, sel1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                x = drawUnselectedText(g, x, y, sel1, p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                x = drawUnselectedText(g, x, y, p0, p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * Renders the given range in the model as normal unselected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * text.  Uses the foreground or disabled color to render the text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @param g the graphics context
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   175
     * @param x the starting X coordinate &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   176
     * @param y the starting Y coordinate &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   177
     * @param p0 the beginning position in the model &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   178
     * @param p1 the ending position in the model &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   179
     * @return the X location of the end of the range &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @exception BadLocationException if the range is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    protected int drawUnselectedText(Graphics g, int x, int y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                                     int p0, int p1) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        g.setColor(unselected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        Segment s = SegmentCache.getSharedSegment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        doc.getText(p0, p1 - p0, s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        int ret = Utilities.drawTabbedText(this, s, x, y, g, this, p0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        SegmentCache.releaseSharedSegment(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    /**
39553
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   194
     * 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
   195
     * text.  Uses the foreground or disabled color to render the text.
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   196
     *
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   197
     * @implSpec This implementation calls
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   198
     * {@link #drawUnselectedText(Graphics, int, int, int, int)
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   199
     * drawUnselectedText((Graphics)g, (int) x, (int) y, p0, p1)}.
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   200
     *
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   201
     * @param g the graphics context
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   202
     * @param x the starting X coordinate {@code >= 0}
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   203
     * @param y the starting Y coordinate {@code >= 0}
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   204
     * @param p0 the beginning position in the model {@code >= 0}
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   205
     * @param p1 the ending position in the model {@code >= 0}
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   206
     * @return the X location of the end of the range {@code >= 0}
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   207
     * @exception BadLocationException if the range is invalid
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   208
     */
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   209
    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
   210
                                       int p0, int p1) throws BadLocationException {
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   211
        return drawUnselectedText((Graphics)g, (int) x, (int) y, p0, p1);
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   212
    }
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   213
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   214
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * Renders the given range in the model as selected text.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * is implemented to render the text in the color specified in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * the hosting component.  It assumes the highlighter will render
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * the selected background.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @param g the graphics context
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   221
     * @param x the starting X coordinate &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   222
     * @param y the starting Y coordinate &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   223
     * @param p0 the beginning position in the model &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   224
     * @param p1 the ending position in the model &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @return the location of the end of the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @exception BadLocationException if the range is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    protected int drawSelectedText(Graphics g, int x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                                   int y, int p0, int p1) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        g.setColor(selected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        Segment s = SegmentCache.getSharedSegment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        doc.getText(p0, p1 - p0, s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        int ret = Utilities.drawTabbedText(this, s, x, y, g, this, p0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        SegmentCache.releaseSharedSegment(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    /**
39553
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   240
     * 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
   241
     * 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
   242
     * 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
   243
     * the selected background.
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   244
     *
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   245
     * @implSpec This implementation calls
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   246
     * {@link #drawSelectedText(Graphics, int, int, int, int)
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   247
     * drawSelectedText((Graphics)g, (int) x, (int) y, p0, p1)}.
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 {@code >= 0}
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   251
     * @param y the starting Y coordinate {@code >= 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 {@code >= 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 {@code >= 0}
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   254
     * @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
   255
     * @exception BadLocationException if the range is invalid
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   256
     */
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   257
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   258
    protected float drawSelectedText(Graphics2D g, float x,
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   259
                                     float y, int p0, int p1) throws BadLocationException {
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   260
        return drawSelectedText((Graphics)g, (int) x, (int) y, p0, p1);
965a62655c4c 8132119: Provide public API for text related methods in SwingUtilities2
alexsch
parents: 30462
diff changeset
   261
    }
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
     * Gives access to a buffer that can be used to fetch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * text from the associated document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @return the buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    protected final Segment getLineBuffer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        if (lineBuffer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            lineBuffer = new Segment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        return lineBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * Checks to see if the font metrics and longest line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * are up-to-date.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    protected void updateMetrics() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        Component host = getContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        Font f = host.getFont();
39554
12687019f2b9 8142966: Wrong cursor position in text components on HiDPI display
alexsch
parents: 39553
diff changeset
   285
        FontMetrics fm = (font == null) ? null : host.getFontMetrics(font);
12687019f2b9 8142966: Wrong cursor position in text components on HiDPI display
alexsch
parents: 39553
diff changeset
   286
        if (font != f || !Objects.equals(metrics, fm)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            // The font changed, we need to recalculate the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            // longest line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            calculateLongestLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            tabSize = getTabSize() * metrics.charWidth('m');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    // ---- View methods ----------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * Determines the preferred span for this view along an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * @param axis may be either View.X_AXIS or View.Y_AXIS
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   301
     * @return   the span the view would like to be rendered into &gt;= 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     *           Typically the view is told to render into the span
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     *           that is returned, although there is no guarantee.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     *           The parent may choose to resize or break the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * @exception IllegalArgumentException for an invalid axis
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    public float getPreferredSpan(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        updateMetrics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        switch (axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        case View.X_AXIS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            return getLineWidth(longLine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        case View.Y_AXIS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            return getElement().getElementCount() * metrics.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            throw new IllegalArgumentException("Invalid axis: " + axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * Renders using the given rendering surface and area on that surface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * The view may need to do layout and create child views to enable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * itself to render into the given allocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * @param g the rendering surface to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * @see View#paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    public void paint(Graphics g, Shape a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        Shape originalA = a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        a = adjustPaintRegion(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        Rectangle alloc = (Rectangle) a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        tabBase = alloc.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        JTextComponent host = (JTextComponent) getContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        Highlighter h = host.getHighlighter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        g.setFont(host.getFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        sel0 = host.getSelectionStart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        sel1 = host.getSelectionEnd();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        unselected = (host.isEnabled()) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            host.getForeground() : host.getDisabledTextColor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        Caret c = host.getCaret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        selected = c.isSelectionVisible() && h != null ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                       host.getSelectedTextColor() : unselected;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        updateMetrics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        // If the lines are clipped then we don't expend the effort to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        // try and paint them.  Since all of the lines are the same height
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        // with this object, determination of what lines need to be repainted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        // is quick.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        Rectangle clip = g.getClipBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        int fontHeight = metrics.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        int heightBelow = (alloc.y + alloc.height) - (clip.y + clip.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        int heightAbove = clip.y - alloc.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        int linesBelow, linesAbove, linesTotal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        if (fontHeight > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            linesBelow = Math.max(0, heightBelow / fontHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            linesAbove = Math.max(0, heightAbove / fontHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            linesTotal = alloc.height / fontHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            if (alloc.height % fontHeight != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                linesTotal++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            linesBelow = linesAbove = linesTotal = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        // update the visible lines
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        Rectangle lineArea = lineToRect(a, linesAbove);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        int y = lineArea.y + metrics.getAscent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        int x = lineArea.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        Element map = getElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        int lineCount = map.getElementCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        int endLine = Math.min(lineCount, linesTotal - linesBelow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        lineCount--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        LayeredHighlighter dh = (h instanceof LayeredHighlighter) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                           (LayeredHighlighter)h : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        for (int line = linesAbove; line < endLine; line++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            if (dh != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                Element lineElement = map.getElement(line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                if (line == lineCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                    dh.paintLayeredHighlights(g, lineElement.getStartOffset(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                                              lineElement.getEndOffset(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                                              originalA, host, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                    dh.paintLayeredHighlights(g, lineElement.getStartOffset(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                                              lineElement.getEndOffset() - 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                                              originalA, host, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            drawLine(line, g, x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            y += fontHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            if (line == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                // This should never really happen, in so far as if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                // firstLineOffset is non 0, there should only be one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                // line of text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                x -= firstLineOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * Should return a shape ideal for painting based on the passed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * Shape <code>a</code>. This is useful if painting in a different
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * region. The default implementation returns <code>a</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    Shape adjustPaintRegion(Shape a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * Provides a mapping from the document model coordinate space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * to the coordinate space of the view mapped to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     *
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   415
     * @param pos the position to convert &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * @return the bounding box of the given position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * @exception BadLocationException  if the given position does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     *   represent a valid location in the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * @see View#modelToView
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        // line coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        Element map = getElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        int lineIndex = map.getElementIndex(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        if (lineIndex < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            return lineToRect(a, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        Rectangle lineArea = lineToRect(a, lineIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        // determine span from the start of the line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        tabBase = lineArea.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        Element line = map.getElement(lineIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        int p0 = line.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        Segment s = SegmentCache.getSharedSegment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        doc.getText(p0, pos - p0, s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        int xOffs = Utilities.getTabbedTextWidth(s, metrics, tabBase, this,p0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        SegmentCache.releaseSharedSegment(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        // fill in the results and return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        lineArea.x += xOffs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        lineArea.width = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        lineArea.height = metrics.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        return lineArea;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * Provides a mapping from the view coordinate space to the logical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * coordinate space of the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     *
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   452
     * @param fx the X coordinate &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   453
     * @param fy the Y coordinate &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * @return the location within the model that best represents the
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   456
     *  given point in the view &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * @see View#viewToModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    public int viewToModel(float fx, float fy, Shape a, Position.Bias[] bias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        // PENDING(prinz) properly calculate bias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        bias[0] = Position.Bias.Forward;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        Rectangle alloc = a.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        int x = (int) fx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        int y = (int) fy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        if (y < alloc.y) {
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 25859
diff changeset
   468
            // above the area covered by this icon, so the position
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            // is assumed to be the start of the coverage for this view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            return getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        } 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
   472
            // below the area covered by this icon, so the position
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            // is assumed to be the end of the coverage for this view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            return getEndOffset() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            // positioned within the coverage of this view vertically,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            // so we figure out which line the point corresponds to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            // if the line is greater than the number of lines contained, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            // simply use the last line as it represents the last possible place
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            // we can position to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            Element map = doc.getDefaultRootElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            int fontHeight = metrics.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            int lineIndex = (fontHeight > 0 ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                                Math.abs((y - alloc.y) / fontHeight) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                                map.getElementCount() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            if (lineIndex >= map.getElementCount()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                return getEndOffset() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            Element line = map.getElement(lineIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            int dx = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            if (lineIndex == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                alloc.x += firstLineOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                alloc.width -= firstLineOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            if (x < alloc.x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                // point is to the left of the line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                return line.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            } else if (x > alloc.x + alloc.width) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                // point is to the right of the line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                return line.getEndOffset() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                // Determine the offset into the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                    int p0 = line.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                    int p1 = line.getEndOffset() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                    Segment s = SegmentCache.getSharedSegment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                    doc.getText(p0, p1 - p0, s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                    tabBase = alloc.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                    int offs = p0 + Utilities.getTabbedTextOffset(s, metrics,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                                                                  tabBase, x, this, p0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                    SegmentCache.releaseSharedSegment(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                    return offs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                    // should not happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * Gives notification that something was inserted into the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * in a location that this view is responsible for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * @param changes the change information from the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * @param a the current allocation of the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * @param f the factory to use to rebuild if the view has children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * @see View#insertUpdate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    public void insertUpdate(DocumentEvent changes, Shape a, ViewFactory f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        updateDamage(changes, a, f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * Gives notification that something was removed from the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * in a location that this view is responsible for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * @param changes the change information from the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * @param a the current allocation of the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * @param f the factory to use to rebuild if the view has children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * @see View#removeUpdate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    public void removeUpdate(DocumentEvent changes, Shape a, ViewFactory f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        updateDamage(changes, a, f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * Gives notification from the document that attributes were changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * in a location that this view is responsible for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * @param changes the change information from the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * @param a the current allocation of the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * @param f the factory to use to rebuild if the view has children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * @see View#changedUpdate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    public void changedUpdate(DocumentEvent changes, Shape a, ViewFactory f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        updateDamage(changes, a, f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * Sets the size of the view.  This should cause
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * layout of the view along the given axis, if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * has any layout duties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     *
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   565
     * @param width the width &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   566
     * @param height the height &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    public void setSize(float width, float height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        super.setSize(width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        updateMetrics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    // --- TabExpander methods ------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * Returns the next tab stop position after a given reference position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * This implementation does not support things like centering so it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * ignores the tabOffset argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     *
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   580
     * @param x the current position &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * @param tabOffset the position within the text stream
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   582
     *   that the tab occurred at &gt;= 0.
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   583
     * @return the tab stop, measured in points &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    public float nextTabStop(float x, int tabOffset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        if (tabSize == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        int ntabs = (((int) x) - tabBase) / tabSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        return tabBase + ((ntabs + 1) * tabSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    // --- local methods ------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * Repaint the region of change covered by the given document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * event.  Damages the line that begins the range to cover
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * the case when the insert/remove is only on one line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * If lines are added or removed, damages the whole
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * view.  The longest line is checked to see if it has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     *
30462
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 28059
diff changeset
   603
     * @param changes the change information from the associated document
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 28059
diff changeset
   604
     * @param a the current allocation of the view
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 28059
diff changeset
   605
     * @param f the factory to use to rebuild if the view has children
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    protected void updateDamage(DocumentEvent changes, Shape a, ViewFactory f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        Component host = getContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        updateMetrics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        Element elem = getElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        DocumentEvent.ElementChange ec = changes.getChange(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        Element[] added = (ec != null) ? ec.getChildrenAdded() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        Element[] removed = (ec != null) ? ec.getChildrenRemoved() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        if (((added != null) && (added.length > 0)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            ((removed != null) && (removed.length > 0))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            // lines were added or removed...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            if (added != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                int currWide = getLineWidth(longLine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                for (int i = 0; i < added.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                    int w = getLineWidth(added[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                    if (w > currWide) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                        currWide = w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                        longLine = added[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            if (removed != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                for (int i = 0; i < removed.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                    if (removed[i] == longLine) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                        calculateLongestLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            preferenceChanged(null, true, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            host.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            Element map = getElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            int line = map.getElementIndex(changes.getOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            damageLineRange(line, line, a, host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            if (changes.getType() == DocumentEvent.EventType.INSERT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                // check to see if the line is longer than current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                // longest line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                int w = getLineWidth(longLine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                Element e = map.getElement(line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                if (e == longLine) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                    preferenceChanged(null, true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                } else if (getLineWidth(e) > w) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                    longLine = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                    preferenceChanged(null, true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            } else if (changes.getType() == DocumentEvent.EventType.REMOVE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                if (map.getElement(line) == longLine) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                    // removed from longest line... recalc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                    calculateLongestLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                    preferenceChanged(null, true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * Repaint the given line range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * @param host the component hosting the view (used to call repaint)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * @param a  the region allocated for the view to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * @param line0 the starting line number to repaint.  This must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     *   be a valid line number in the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * @param line1 the ending line number to repaint.  This must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     *   be a valid line number in the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    protected void damageLineRange(int line0, int line1, Shape a, Component host) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        if (a != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            Rectangle area0 = lineToRect(a, line0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            Rectangle area1 = lineToRect(a, line1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            if ((area0 != null) && (area1 != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                Rectangle damage = area0.union(area1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                host.repaint(damage.x, damage.y, damage.width, damage.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                host.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * Determine the rectangle that represents the given line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * @param a  the region allocated for the view to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * @param line the line number to find the region of.  This must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     *   be a valid line number in the model.
30462
507bcb03c954 8076624: Fix missing doclint warnings in javax.swing.text
darcy
parents: 28059
diff changeset
   694
     * @return the rectangle that represents the given line
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    protected Rectangle lineToRect(Shape a, int line) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        Rectangle r = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        updateMetrics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        if (metrics != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
            Rectangle alloc = a.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            if (line == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                alloc.x += firstLineOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                alloc.width -= firstLineOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            r = new Rectangle(alloc.x, alloc.y + (line * metrics.getHeight()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                              alloc.width, metrics.getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * Iterate over the lines represented by the child elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * of the element this view represents, looking for the line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * that is the longest.  The <em>longLine</em> variable is updated to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * represent the longest line contained.  The <em>font</em> variable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * is updated to indicate the font used to calculate the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * longest line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    private void calculateLongestLine() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        Component c = getContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        font = c.getFont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        metrics = c.getFontMetrics(font);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        Element lines = getElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        int n = lines.getElementCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        int maxWidth = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            Element line = lines.getElement(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            int w = getLineWidth(line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            if (w > maxWidth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                maxWidth = w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                longLine = line;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     * Calculate the width of the line represented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * the given element.  It is assumed that the font
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * and font metrics are up-to-date.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
    private int getLineWidth(Element line) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        if (line == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        int p0 = line.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        int p1 = line.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        int w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        Segment s = SegmentCache.getSharedSegment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            line.getDocument().getText(p0, p1 - p0, s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            w = Utilities.getTabbedTextWidth(s, metrics, tabBase, this, p0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        } catch (BadLocationException ble) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            w = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        SegmentCache.releaseSharedSegment(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        return w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    // --- member variables -----------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * Font metrics for the current font.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
    protected FontMetrics metrics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * The current longest line.  This is used to calculate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * the preferred width of the view.  Since the calculation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     * is potentially expensive we try to avoid it by stashing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * which line is currently the longest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    Element longLine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     * Font used to calculate the longest line... if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * changes we need to recalculate the longest line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
    Font font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
    Segment lineBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
    int tabSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
    int tabBase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
    int sel0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
    int sel1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
    Color unselected;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
    Color selected;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * Offset of where to draw the first character on the first line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * This is a hack and temporary until we can better address the problem
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * of text measuring. This field is actually never set directly in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * PlainView, but by FieldView.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
    int firstLineOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
}