jdk/src/share/classes/javax/swing/text/DefaultHighlighter.java
author malenkov
Wed, 30 Apr 2014 19:28:05 +0400
changeset 24544 c0133e7c7162
parent 23010 6dadb192ad81
permissions -rw-r--r--
8041917: unexcepted behavior of LineBorder while using Boolean variable true Reviewed-by: alexsch, serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21278
diff changeset
     2
 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
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: 1639
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: 1639
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
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.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.swing.plaf.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * Implements the Highlighter interfaces.  Implements a simple highlight
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * painter that renders in a solid color.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @author  Timothy Prinzing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @see     Highlighter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class DefaultHighlighter extends LayeredHighlighter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
     * Creates a new DefaultHighlighther object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    public DefaultHighlighter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        drawsLayeredHighlights = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    // ---- Highlighter methods ----------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * Renders the highlights.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * @param g the graphics context
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public void paint(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        // PENDING(prinz) - should cull ranges not visible
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        int len = highlights.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        for (int i = 0; i < len; i++) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
    59
            HighlightInfo info = highlights.elementAt(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            if (!(info instanceof LayeredHighlightInfo)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                // Avoid allocing unless we need it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                Rectangle a = component.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                Insets insets = component.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                a.x = insets.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                a.y = insets.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                a.width -= insets.left + insets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                a.height -= insets.top + insets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                for (; i < len; i++) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
    69
                    info = highlights.elementAt(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                    if (!(info instanceof LayeredHighlightInfo)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                        Highlighter.HighlightPainter p = info.getPainter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                        p.paint(g, info.getStartOffset(), info.getEndOffset(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                                a, component);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * Called when the UI is being installed into the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * interface of a JTextComponent.  Installs the editor, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * removes any existing highlights.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @param c the editor component
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @see Highlighter#install
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public void install(JTextComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        component = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        removeAllHighlights();
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
     * Called when the UI is being removed from the interface of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * a JTextComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @param c the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @see Highlighter#deinstall
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public void deinstall(JTextComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        component = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Adds a highlight to the view.  Returns a tag that can be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * to refer to the highlight.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 7668
diff changeset
   108
     * @param p0   the start offset of the range to highlight &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 7668
diff changeset
   109
     * @param p1   the end offset of the range to highlight &gt;= p0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @param p    the painter to use to actually render the highlight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @return     an object that can be used as a tag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *   to refer to the highlight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @exception BadLocationException if the specified location is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public Object addHighlight(int p0, int p1, Highlighter.HighlightPainter p) throws BadLocationException {
7161
75ca9d01ff2b 6771184: Some methods in text package don't throw BadLocationException when expected
rupashka
parents: 5506
diff changeset
   116
        if (p0 < 0) {
75ca9d01ff2b 6771184: Some methods in text package don't throw BadLocationException when expected
rupashka
parents: 5506
diff changeset
   117
            throw new BadLocationException("Invalid start offset", p0);
75ca9d01ff2b 6771184: Some methods in text package don't throw BadLocationException when expected
rupashka
parents: 5506
diff changeset
   118
        }
75ca9d01ff2b 6771184: Some methods in text package don't throw BadLocationException when expected
rupashka
parents: 5506
diff changeset
   119
75ca9d01ff2b 6771184: Some methods in text package don't throw BadLocationException when expected
rupashka
parents: 5506
diff changeset
   120
        if (p1 < p0) {
75ca9d01ff2b 6771184: Some methods in text package don't throw BadLocationException when expected
rupashka
parents: 5506
diff changeset
   121
            throw new BadLocationException("Invalid end offset", p1);
75ca9d01ff2b 6771184: Some methods in text package don't throw BadLocationException when expected
rupashka
parents: 5506
diff changeset
   122
        }
75ca9d01ff2b 6771184: Some methods in text package don't throw BadLocationException when expected
rupashka
parents: 5506
diff changeset
   123
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        Document doc = component.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        HighlightInfo i = (getDrawsLayeredHighlights() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                           (p instanceof LayeredHighlighter.LayerPainter)) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                          new LayeredHighlightInfo() : new HighlightInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        i.painter = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        i.p0 = doc.createPosition(p0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        i.p1 = doc.createPosition(p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        highlights.addElement(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        safeDamageRange(p0, p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * Removes a highlight from the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @param tag the reference to the highlight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    public void removeHighlight(Object tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if (tag instanceof LayeredHighlightInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            LayeredHighlightInfo lhi = (LayeredHighlightInfo)tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            if (lhi.width > 0 && lhi.height > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                component.repaint(lhi.x, lhi.y, lhi.width, lhi.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            HighlightInfo info = (HighlightInfo) tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            safeDamageRange(info.p0, info.p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        highlights.removeElement(tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * Removes all highlights.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public void removeAllHighlights() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        TextUI mapper = component.getUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (getDrawsLayeredHighlights()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            int len = highlights.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            if (len != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                int minX = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                int minY = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                int maxX = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                int maxY = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                int p0 = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                int p1 = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                for (int i = 0; i < len; i++) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   170
                    HighlightInfo hi = highlights.elementAt(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    if (hi instanceof LayeredHighlightInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                        LayeredHighlightInfo info = (LayeredHighlightInfo)hi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                        minX = Math.min(minX, info.x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                        minY = Math.min(minY, info.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                        maxX = Math.max(maxX, info.x + info.width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                        maxY = Math.max(maxY, info.y + info.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                        if (p0 == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                            p0 = hi.p0.getOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                            p1 = hi.p1.getOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                            p0 = Math.min(p0, hi.p0.getOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                            p1 = Math.max(p1, hi.p1.getOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                if (minX != maxX && minY != maxY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    component.repaint(minX, minY, maxX - minX, maxY - minY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                if (p0 != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                        safeDamageRange(p0, p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                    } catch (BadLocationException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                highlights.removeAllElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        else if (mapper != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            int len = highlights.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            if (len != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                int p0 = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                int p1 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                for (int i = 0; i < len; i++) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   206
                    HighlightInfo info = highlights.elementAt(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                    p0 = Math.min(p0, info.p0.getOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                    p1 = Math.max(p1, info.p1.getOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                    safeDamageRange(p0, p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                } catch (BadLocationException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                highlights.removeAllElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * Changes a highlight.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @param tag the highlight tag
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 7668
diff changeset
   223
     * @param p0 the beginning of the range &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 7668
diff changeset
   224
     * @param p1 the end of the range &gt;= p0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @exception BadLocationException if the specified location is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    public void changeHighlight(Object tag, int p0, int p1) throws BadLocationException {
7161
75ca9d01ff2b 6771184: Some methods in text package don't throw BadLocationException when expected
rupashka
parents: 5506
diff changeset
   228
        if (p0 < 0) {
75ca9d01ff2b 6771184: Some methods in text package don't throw BadLocationException when expected
rupashka
parents: 5506
diff changeset
   229
            throw new BadLocationException("Invalid beginning of the range", p0);
75ca9d01ff2b 6771184: Some methods in text package don't throw BadLocationException when expected
rupashka
parents: 5506
diff changeset
   230
        }
75ca9d01ff2b 6771184: Some methods in text package don't throw BadLocationException when expected
rupashka
parents: 5506
diff changeset
   231
75ca9d01ff2b 6771184: Some methods in text package don't throw BadLocationException when expected
rupashka
parents: 5506
diff changeset
   232
        if (p1 < p0) {
75ca9d01ff2b 6771184: Some methods in text package don't throw BadLocationException when expected
rupashka
parents: 5506
diff changeset
   233
            throw new BadLocationException("Invalid end of the range", p1);
75ca9d01ff2b 6771184: Some methods in text package don't throw BadLocationException when expected
rupashka
parents: 5506
diff changeset
   234
        }
75ca9d01ff2b 6771184: Some methods in text package don't throw BadLocationException when expected
rupashka
parents: 5506
diff changeset
   235
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        Document doc = component.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        if (tag instanceof LayeredHighlightInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            LayeredHighlightInfo lhi = (LayeredHighlightInfo)tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            if (lhi.width > 0 && lhi.height > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                component.repaint(lhi.x, lhi.y, lhi.width, lhi.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            // Mark the highlights region as invalid, it will reset itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            // next time asked to paint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            lhi.width = lhi.height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            lhi.p0 = doc.createPosition(p0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            lhi.p1 = doc.createPosition(p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            safeDamageRange(Math.min(p0, p1), Math.max(p0, p1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            HighlightInfo info = (HighlightInfo) tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            int oldP0 = info.p0.getOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            int oldP1 = info.p1.getOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            if (p0 == oldP0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                safeDamageRange(Math.min(oldP1, p1),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                                   Math.max(oldP1, p1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            } else if (p1 == oldP1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                safeDamageRange(Math.min(p0, oldP0),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                                   Math.max(p0, oldP0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                safeDamageRange(oldP0, oldP1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                safeDamageRange(p0, p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            info.p0 = doc.createPosition(p0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            info.p1 = doc.createPosition(p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * Makes a copy of the highlights.  Does not actually clone each highlight,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * but only makes references to them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @return the copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @see Highlighter#getHighlights
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    public Highlighter.Highlight[] getHighlights() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        int size = highlights.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        if (size == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            return noHighlights;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        Highlighter.Highlight[] h = new Highlighter.Highlight[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        highlights.copyInto(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        return h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * When leaf Views (such as LabelView) are rendering they should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * call into this method. If a highlight is in the given region it will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * be drawn immediately.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @param g Graphics used to draw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @param p0 starting offset of view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * @param p1 ending offset of view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @param viewBounds Bounds of View
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @param editor JTextComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @param view View instance being rendered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    public void paintLayeredHighlights(Graphics g, int p0, int p1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                                       Shape viewBounds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                                       JTextComponent editor, View view) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        for (int counter = highlights.size() - 1; counter >= 0; counter--) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   301
            HighlightInfo tag = highlights.elementAt(counter);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            if (tag instanceof LayeredHighlightInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                LayeredHighlightInfo lhi = (LayeredHighlightInfo)tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                int start = lhi.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                int end = lhi.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                if ((p0 < start && p1 > start) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                    (p0 >= start && p0 < end)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                    lhi.paintLayeredHighlights(g, p0, p1, viewBounds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                                               editor, view);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * Queues damageRange() call into event dispatch thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * to be sure that views are in consistent state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    private void safeDamageRange(final Position p0, final Position p1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        safeDamager.damageRange(p0, p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * Queues damageRange() call into event dispatch thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * to be sure that views are in consistent state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    private void safeDamageRange(int a0, int a1) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        Document doc = component.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        safeDamageRange(doc.createPosition(a0), doc.createPosition(a1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * If true, highlights are drawn as the Views draw the text. That is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * the Views will call into <code>paintLayeredHighlight</code> which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * will result in a rectangle being drawn before the text is drawn
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * (if the offsets are in a highlighted region that is). For this to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * work the painter supplied must be an instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * LayeredHighlightPainter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    public void setDrawsLayeredHighlights(boolean newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        drawsLayeredHighlights = newValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    public boolean getDrawsLayeredHighlights() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        return drawsLayeredHighlights;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    // ---- member variables --------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    private final static Highlighter.Highlight[] noHighlights =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            new Highlighter.Highlight[0];
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   352
    private Vector<HighlightInfo> highlights = new Vector<HighlightInfo>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    private JTextComponent component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    private boolean drawsLayeredHighlights;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    private SafeDamager safeDamager = new SafeDamager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * Default implementation of LayeredHighlighter.LayerPainter that can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * be used for painting highlights.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * As of 1.4 this field is final.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    public static final LayeredHighlighter.LayerPainter DefaultPainter = new DefaultHighlightPainter(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * Simple highlight painter that fills a highlighted area with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * a solid color.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    public static class DefaultHighlightPainter extends LayeredHighlighter.LayerPainter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
         * Constructs a new highlight painter. If <code>c</code> is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
         * the JTextComponent will be queried for its selection color.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
         * @param c the color for the highlight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        public DefaultHighlightPainter(Color c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            color = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
         * Returns the color of the highlight.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
         * @return the color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        public Color getColor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            return color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        // --- HighlightPainter methods ---------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
         * Paints a highlight.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
         * @param g the graphics context
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 7668
diff changeset
   398
         * @param offs0 the starting model offset &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 7668
diff changeset
   399
         * @param offs1 the ending model offset &gt;= offs1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
         * @param bounds the bounding box for the highlight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
         * @param c the editor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        public void paint(Graphics g, int offs0, int offs1, Shape bounds, JTextComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            Rectangle alloc = bounds.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                // --- determine locations ---
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                TextUI mapper = c.getUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                Rectangle p0 = mapper.modelToView(c, offs0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                Rectangle p1 = mapper.modelToView(c, offs1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                // --- render ---
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                Color color = getColor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                if (color == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                    g.setColor(c.getSelectionColor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                    g.setColor(color);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                if (p0.y == p1.y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                    // same line, render a rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    Rectangle r = p0.union(p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                    g.fillRect(r.x, r.y, r.width, r.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                    // different lines
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                    int p0ToMarginWidth = alloc.x + alloc.width - p0.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                    g.fillRect(p0.x, p0.y, p0ToMarginWidth, p0.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                    if ((p0.y + p0.height) != p1.y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                        g.fillRect(alloc.x, p0.y + p0.height, alloc.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                                   p1.y - (p0.y + p0.height));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                    g.fillRect(alloc.x, p1.y, (p1.x - alloc.x), p1.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                // can't render
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
        // --- LayerPainter methods ----------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
         * Paints a portion of a highlight.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
         * @param g the graphics context
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 7668
diff changeset
   444
         * @param offs0 the starting model offset &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 7668
diff changeset
   445
         * @param offs1 the ending model offset &gt;= offs1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
         * @param bounds the bounding box of the view, which is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
         *        necessarily the region to paint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
         * @param c the editor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
         * @param view View painting for
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20158
diff changeset
   450
         * @return region drawing occurred in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        public Shape paintLayer(Graphics g, int offs0, int offs1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                                Shape bounds, JTextComponent c, View view) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            Color color = getColor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            if (color == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                g.setColor(c.getSelectionColor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                g.setColor(color);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            Rectangle r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            if (offs0 == view.getStartOffset() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                offs1 == view.getEndOffset()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                // Contained in view, can just use bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                if (bounds instanceof Rectangle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                    r = (Rectangle) bounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                    r = bounds.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                // Should only render part of View.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                    // --- determine locations ---
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                    Shape shape = view.modelToView(offs0, Position.Bias.Forward,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                                                   offs1,Position.Bias.Backward,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                                                   bounds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                    r = (shape instanceof Rectangle) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                                  (Rectangle)shape : shape.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                    // can't render
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                    r = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            if (r != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                // If we are asked to highlight, we should draw something even
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                // if the model-to-view projection is of zero width (6340106).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                r.width = Math.max(r.width, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                g.fillRect(r.x, r.y, r.width, r.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        private Color color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    class HighlightInfo implements Highlighter.Highlight {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        public int getStartOffset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            return p0.getOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        public int getEndOffset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            return p1.getOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        public Highlighter.HighlightPainter getPainter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            return painter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        Position p0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        Position p1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        Highlighter.HighlightPainter painter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * LayeredHighlightPainter is used when a drawsLayeredHighlights is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * true. It maintains a rectangle of the region to paint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    class LayeredHighlightInfo extends HighlightInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        void union(Shape bounds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            if (bounds == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            Rectangle alloc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            if (bounds instanceof Rectangle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                alloc = (Rectangle)bounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                alloc = bounds.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            if (width == 0 || height == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                x = alloc.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                y = alloc.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                width = alloc.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                height = alloc.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                width = Math.max(x + width, alloc.x + alloc.width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                height = Math.max(y + height, alloc.y + alloc.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                x = Math.min(x, alloc.x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                width -= x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                y = Math.min(y, alloc.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                height -= y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
         * Restricts the region based on the receivers offsets and messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
         * the painter to paint the region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        void paintLayeredHighlights(Graphics g, int p0, int p1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                                    Shape viewBounds, JTextComponent editor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                                    View view) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            int start = getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            int end = getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            // Restrict the region to what we represent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            p0 = Math.max(start, p0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            p1 = Math.min(end, p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            // Paint the appropriate region using the painter and union
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            // the effected region with our bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            union(((LayeredHighlighter.LayerPainter)painter).paintLayer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                  (g, p0, p1, viewBounds, editor, view));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        int x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        int y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        int width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        int height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * This class invokes <code>mapper.damageRange</code> in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * EventDispatchThread. The only one instance per Highlighter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * is cretaed. When a number of ranges should be damaged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * it collects them into queue and damages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * them in consecutive order in <code>run</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    class SafeDamager implements Runnable {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   592
        private Vector<Position> p0 = new Vector<Position>(10);
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   593
        private Vector<Position> p1 = new Vector<Position>(10);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        private Document lastDoc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
         * Executes range(s) damage and cleans range queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        public synchronized void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            if (component != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                TextUI mapper = component.getUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                if (mapper != null && lastDoc == component.getDocument()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                    // the Document should be the same to properly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                    // display highlights
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                    int len = p0.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                    for (int i = 0; i < len; i++){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                        mapper.damageRange(component,
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   608
                                p0.get(i).getOffset(),
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   609
                                p1.get(i).getOffset());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            p0.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            p1.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            // release reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            lastDoc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
         * Adds the range to be damaged into the range queue. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
         * range queue is empty (the first call or run() was already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
         * invoked) then adds this class instance into EventDispatch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
         * queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
         * The method also tracks if the current document changed or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
         * component is null. In this case it removes all ranges added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
         * before from range queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        public synchronized void damageRange(Position pos0, Position pos1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            if (component == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                p0.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                lastDoc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            boolean addToQueue = p0.isEmpty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            Document curDoc = component.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            if (curDoc != lastDoc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                if (!p0.isEmpty()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                    p0.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                    p1.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                lastDoc = curDoc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            p0.add(pos0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            p1.add(pos1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            if (addToQueue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                SwingUtilities.invokeLater(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
}