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