jdk/src/share/classes/javax/swing/text/Utilities.java
author malenkov
Fri, 04 Apr 2014 20:18:53 +0400
changeset 24158 6afb40c4e9f8
parent 23010 6dadb192ad81
permissions -rw-r--r--
8039137: KSS: JTextComponent.isProcessInputMethodEventOverridden 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: 20158
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: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing.text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.Component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.Rectangle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.Graphics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.FontMetrics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.Shape;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.Toolkit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.Graphics2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.awt.font.FontRenderContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.awt.font.TextLayout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.awt.font.TextAttribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.swing.JComponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.swing.SwingConstants;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.swing.text.ParagraphView.Row;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import sun.swing.SwingUtilities2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * A collection of methods to deal with various text
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * related activities.
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
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
public class Utilities {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * If <code>view</code>'s container is a <code>JComponent</code> it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * is returned, after casting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    static JComponent getJComponent(View view) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        if (view != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            Component component = view.getContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            if (component instanceof JComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                return (JComponent)component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * Draws the given text, expanding any tabs that are contained
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * using the given tab expansion technique.  This particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * implementation renders in a 1.1 style coordinate system
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * where ints are used and 72dpi is assumed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * @param s  the source of the text
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 9768
diff changeset
    74
     * @param x  the X origin &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 9768
diff changeset
    75
     * @param y  the Y origin &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @param g  the graphics context
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @param e  how to expand the tabs.  If this value is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *   tabs will be expanded as a space character.
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 9768
diff changeset
    79
     * @param startOffset starting offset of the text in the document &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * @return  the X location at the end of the rendered text
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public static final int drawTabbedText(Segment s, int x, int y, Graphics g,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                                           TabExpander e, int startOffset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        return drawTabbedText(null, s, x, y, g, e, startOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * Draws the given text, expanding any tabs that are contained
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * using the given tab expansion technique.  This particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * implementation renders in a 1.1 style coordinate system
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * where ints are used and 72dpi is assumed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @param view View requesting rendering, may be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @param s  the source of the text
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 9768
diff changeset
    95
     * @param x  the X origin &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 9768
diff changeset
    96
     * @param y  the Y origin &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @param g  the graphics context
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @param e  how to expand the tabs.  If this value is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *   tabs will be expanded as a space character.
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 9768
diff changeset
   100
     * @param startOffset starting offset of the text in the document &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @return  the X location at the end of the rendered text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    static final int drawTabbedText(View view,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                                Segment s, int x, int y, Graphics g,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                                TabExpander e, int startOffset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        return drawTabbedText(view, s, x, y, g, e, startOffset, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    // In addition to the previous method it can extend spaces for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    // justification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    // all params are the same as in the preious method except the last
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    // one:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    // @param justificationData justificationData for the row.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    // if null not justification is needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    static final int drawTabbedText(View view,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                                Segment s, int x, int y, Graphics g,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                                TabExpander e, int startOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                                int [] justificationData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        JComponent component = getJComponent(view);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        FontMetrics metrics = SwingUtilities2.getFontMetrics(component, g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        int nextX = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        char[] txt = s.array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        int txtOffset = s.offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        int flushLen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        int flushIndex = s.offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        int spaceAddon = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        int spaceAddonLeftoverEnd = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        int startJustifiableContent = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        int endJustifiableContent = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (justificationData != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            int offset = - startOffset + txtOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            View parent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            if (view != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                  && (parent = view.getParent()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                offset += parent.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            spaceAddon =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                justificationData[Row.SPACE_ADDON];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            spaceAddonLeftoverEnd =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                justificationData[Row.SPACE_ADDON_LEFTOVER_END] + offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            startJustifiableContent =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                justificationData[Row.START_JUSTIFIABLE] + offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            endJustifiableContent =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                justificationData[Row.END_JUSTIFIABLE] + offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        int n = s.offset + s.count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        for (int i = txtOffset; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            if (txt[i] == '\t'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                || ((spaceAddon != 0 || i <= spaceAddonLeftoverEnd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    && (txt[i] == ' ')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    && startJustifiableContent <= i
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    && i <= endJustifiableContent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    )) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                if (flushLen > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    nextX = SwingUtilities2.drawChars(component, g, txt,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                                                flushIndex, flushLen, x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    flushLen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                flushIndex = i + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                if (txt[i] == '\t') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                    if (e != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                        nextX = (int) e.nextTabStop((float) nextX, startOffset + i - txtOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                        nextX += metrics.charWidth(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                } else if (txt[i] == ' ') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    nextX += metrics.charWidth(' ') + spaceAddon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                    if (i <= spaceAddonLeftoverEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                        nextX++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                x = nextX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            } else if ((txt[i] == '\n') || (txt[i] == '\r')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                if (flushLen > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    nextX = SwingUtilities2.drawChars(component, g, txt,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                                                flushIndex, flushLen, x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    flushLen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                flushIndex = i + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                x = nextX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                flushLen += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        if (flushLen > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            nextX = SwingUtilities2.drawChars(component, g,txt, flushIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                                              flushLen, x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        return nextX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * Determines the width of the given segment of text taking tabs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * into consideration.  This is implemented in a 1.1 style coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * system where ints are used and 72dpi is assumed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @param s  the source of the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @param metrics the font metrics to use for the calculation
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 9768
diff changeset
   200
     * @param x  the X origin &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @param e  how to expand the tabs.  If this value is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *   tabs will be expanded as a space character.
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 9768
diff changeset
   203
     * @param startOffset starting offset of the text in the document &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @return  the width of the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    public static final int getTabbedTextWidth(Segment s, FontMetrics metrics, int x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                                               TabExpander e, int startOffset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        return getTabbedTextWidth(null, s, metrics, x, e, startOffset, null);
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
    // In addition to the previous method it can extend spaces for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    // justification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    // all params are the same as in the preious method except the last
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    // one:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    // @param justificationData justificationData for the row.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    // if null not justification is needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    static final int getTabbedTextWidth(View view, Segment s, FontMetrics metrics, int x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                                        TabExpander e, int startOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                                        int[] justificationData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        int nextX = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        char[] txt = s.array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        int txtOffset = s.offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        int n = s.offset + s.count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        int charCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        int spaceAddon = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        int spaceAddonLeftoverEnd = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        int startJustifiableContent = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        int endJustifiableContent = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        if (justificationData != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            int offset = - startOffset + txtOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            View parent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            if (view != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                  && (parent = view.getParent()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                offset += parent.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            spaceAddon =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                justificationData[Row.SPACE_ADDON];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            spaceAddonLeftoverEnd =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                justificationData[Row.SPACE_ADDON_LEFTOVER_END] + offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            startJustifiableContent =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                justificationData[Row.START_JUSTIFIABLE] + offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            endJustifiableContent =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                justificationData[Row.END_JUSTIFIABLE] + offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        for (int i = txtOffset; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            if (txt[i] == '\t'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                || ((spaceAddon != 0 || i <= spaceAddonLeftoverEnd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                    && (txt[i] == ' ')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    && startJustifiableContent <= i
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    && i <= endJustifiableContent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                    )) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                nextX += metrics.charsWidth(txt, i-charCount, charCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                charCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                if (txt[i] == '\t') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    if (e != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                        nextX = (int) e.nextTabStop((float) nextX,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                                                    startOffset + i - txtOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                        nextX += metrics.charWidth(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                } else if (txt[i] == ' ') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                    nextX += metrics.charWidth(' ') + spaceAddon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                    if (i <= spaceAddonLeftoverEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                        nextX++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            } else if(txt[i] == '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            // Ignore newlines, they take up space and we shouldn't be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            // counting them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                nextX += metrics.charsWidth(txt, i - charCount, charCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                charCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                charCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        nextX += metrics.charsWidth(txt, n - charCount, charCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        return nextX - x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * Determines the relative offset into the given text that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * best represents the given span in the view coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * system.  This is implemented in a 1.1 style coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * system where ints are used and 72dpi is assumed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * @param s  the source of the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @param metrics the font metrics to use for the calculation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @param x0 the starting view location representing the start
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 9768
diff changeset
   292
     *   of the given text &gt;= 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @param x  the target view location to translate to an
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 9768
diff changeset
   294
     *   offset into the text &gt;= 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @param e  how to expand the tabs.  If this value is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *   tabs will be expanded as a space character.
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 9768
diff changeset
   297
     * @param startOffset starting offset of the text in the document &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 9768
diff changeset
   298
     * @return  the offset into the text &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    public static final int getTabbedTextOffset(Segment s, FontMetrics metrics,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                                             int x0, int x, TabExpander e,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                                             int startOffset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        return getTabbedTextOffset(s, metrics, x0, x, e, startOffset, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    static final int getTabbedTextOffset(View view, Segment s, FontMetrics metrics,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                                         int x0, int x, TabExpander e,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                                         int startOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                                         int[] justificationData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        return getTabbedTextOffset(view, s, metrics, x0, x, e, startOffset, true,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                                   justificationData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    public static final int getTabbedTextOffset(Segment s,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                                                FontMetrics metrics,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                                                int x0, int x, TabExpander e,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                                                int startOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                                                boolean round) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        return getTabbedTextOffset(null, s, metrics, x0, x, e, startOffset, round, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    // In addition to the previous method it can extend spaces for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    // justification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    // all params are the same as in the preious method except the last
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    // one:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    // @param justificationData justificationData for the row.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    // if null not justification is needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    static final int getTabbedTextOffset(View view,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                                         Segment s,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                                         FontMetrics metrics,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                                         int x0, int x, TabExpander e,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                                         int startOffset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                                         boolean round,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                                         int[] justificationData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        if (x0 >= x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            // x before x0, return.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        }
9768
07404abef28e 7045593: Possible Regression : JTextfield cursor placement behavior algorithm has changed.
rupashka
parents: 9232
diff changeset
   340
        int nextX = x0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        // s may be a shared segment, so it is copied prior to calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        // the tab expander
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        char[] txt = s.array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        int txtOffset = s.offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        int txtCount = s.count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        int spaceAddon = 0 ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        int spaceAddonLeftoverEnd = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        int startJustifiableContent = 0 ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        int endJustifiableContent = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        if (justificationData != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            int offset = - startOffset + txtOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            View parent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            if (view != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                  && (parent = view.getParent()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                offset += parent.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            spaceAddon =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                justificationData[Row.SPACE_ADDON];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            spaceAddonLeftoverEnd =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                justificationData[Row.SPACE_ADDON_LEFTOVER_END] + offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            startJustifiableContent =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                justificationData[Row.START_JUSTIFIABLE] + offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            endJustifiableContent =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                justificationData[Row.END_JUSTIFIABLE] + offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        int n = s.offset + s.count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        for (int i = s.offset; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            if (txt[i] == '\t'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                || ((spaceAddon != 0 || i <= spaceAddonLeftoverEnd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                    && (txt[i] == ' ')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                    && startJustifiableContent <= i
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                    && i <= endJustifiableContent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                    )){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                if (txt[i] == '\t') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                    if (e != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                        nextX = (int) e.nextTabStop((float) nextX,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                                                    startOffset + i - txtOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                        nextX += metrics.charWidth(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                } else if (txt[i] == ' ') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                    nextX += metrics.charWidth(' ') + spaceAddon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                    if (i <= spaceAddonLeftoverEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                        nextX++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                nextX += metrics.charWidth(txt[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            }
9768
07404abef28e 7045593: Possible Regression : JTextfield cursor placement behavior algorithm has changed.
rupashka
parents: 9232
diff changeset
   390
            if (x < nextX) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                // found the hit position... return the appropriate side
9768
07404abef28e 7045593: Possible Regression : JTextfield cursor placement behavior algorithm has changed.
rupashka
parents: 9232
diff changeset
   392
                int offset;
07404abef28e 7045593: Possible Regression : JTextfield cursor placement behavior algorithm has changed.
rupashka
parents: 9232
diff changeset
   393
8533
0537e84f6e69 6760148: Certain fonts are not correctly soft wrapped when using JTextComponent.print()
rupashka
parents: 7668
diff changeset
   394
                // the length of the string measured as a whole may differ from
0537e84f6e69 6760148: Certain fonts are not correctly soft wrapped when using JTextComponent.print()
rupashka
parents: 7668
diff changeset
   395
                // the sum of individual character lengths, for example if
0537e84f6e69 6760148: Certain fonts are not correctly soft wrapped when using JTextComponent.print()
rupashka
parents: 7668
diff changeset
   396
                // fractional metrics are enabled; and we must guard from this.
9768
07404abef28e 7045593: Possible Regression : JTextfield cursor placement behavior algorithm has changed.
rupashka
parents: 9232
diff changeset
   397
                if (round) {
07404abef28e 7045593: Possible Regression : JTextfield cursor placement behavior algorithm has changed.
rupashka
parents: 9232
diff changeset
   398
                    offset = i + 1 - txtOffset;
07404abef28e 7045593: Possible Regression : JTextfield cursor placement behavior algorithm has changed.
rupashka
parents: 9232
diff changeset
   399
07404abef28e 7045593: Possible Regression : JTextfield cursor placement behavior algorithm has changed.
rupashka
parents: 9232
diff changeset
   400
                    int width = metrics.charsWidth(txt, txtOffset, offset);
07404abef28e 7045593: Possible Regression : JTextfield cursor placement behavior algorithm has changed.
rupashka
parents: 9232
diff changeset
   401
                    int span = x - x0;
07404abef28e 7045593: Possible Regression : JTextfield cursor placement behavior algorithm has changed.
rupashka
parents: 9232
diff changeset
   402
07404abef28e 7045593: Possible Regression : JTextfield cursor placement behavior algorithm has changed.
rupashka
parents: 9232
diff changeset
   403
                    if (span < width) {
07404abef28e 7045593: Possible Regression : JTextfield cursor placement behavior algorithm has changed.
rupashka
parents: 9232
diff changeset
   404
                        while (offset > 0) {
07404abef28e 7045593: Possible Regression : JTextfield cursor placement behavior algorithm has changed.
rupashka
parents: 9232
diff changeset
   405
                            int nextWidth = offset > 1 ? metrics.charsWidth(txt, txtOffset, offset - 1) : 0;
07404abef28e 7045593: Possible Regression : JTextfield cursor placement behavior algorithm has changed.
rupashka
parents: 9232
diff changeset
   406
07404abef28e 7045593: Possible Regression : JTextfield cursor placement behavior algorithm has changed.
rupashka
parents: 9232
diff changeset
   407
                            if (span >= nextWidth) {
07404abef28e 7045593: Possible Regression : JTextfield cursor placement behavior algorithm has changed.
rupashka
parents: 9232
diff changeset
   408
                                if (span - nextWidth < width - span) {
07404abef28e 7045593: Possible Regression : JTextfield cursor placement behavior algorithm has changed.
rupashka
parents: 9232
diff changeset
   409
                                    offset--;
07404abef28e 7045593: Possible Regression : JTextfield cursor placement behavior algorithm has changed.
rupashka
parents: 9232
diff changeset
   410
                                }
07404abef28e 7045593: Possible Regression : JTextfield cursor placement behavior algorithm has changed.
rupashka
parents: 9232
diff changeset
   411
07404abef28e 7045593: Possible Regression : JTextfield cursor placement behavior algorithm has changed.
rupashka
parents: 9232
diff changeset
   412
                                break;
07404abef28e 7045593: Possible Regression : JTextfield cursor placement behavior algorithm has changed.
rupashka
parents: 9232
diff changeset
   413
                            }
07404abef28e 7045593: Possible Regression : JTextfield cursor placement behavior algorithm has changed.
rupashka
parents: 9232
diff changeset
   414
07404abef28e 7045593: Possible Regression : JTextfield cursor placement behavior algorithm has changed.
rupashka
parents: 9232
diff changeset
   415
                            width = nextWidth;
07404abef28e 7045593: Possible Regression : JTextfield cursor placement behavior algorithm has changed.
rupashka
parents: 9232
diff changeset
   416
                            offset--;
07404abef28e 7045593: Possible Regression : JTextfield cursor placement behavior algorithm has changed.
rupashka
parents: 9232
diff changeset
   417
                        }
07404abef28e 7045593: Possible Regression : JTextfield cursor placement behavior algorithm has changed.
rupashka
parents: 9232
diff changeset
   418
                    }
07404abef28e 7045593: Possible Regression : JTextfield cursor placement behavior algorithm has changed.
rupashka
parents: 9232
diff changeset
   419
                } else {
07404abef28e 7045593: Possible Regression : JTextfield cursor placement behavior algorithm has changed.
rupashka
parents: 9232
diff changeset
   420
                    offset = i - txtOffset;
07404abef28e 7045593: Possible Regression : JTextfield cursor placement behavior algorithm has changed.
rupashka
parents: 9232
diff changeset
   421
07404abef28e 7045593: Possible Regression : JTextfield cursor placement behavior algorithm has changed.
rupashka
parents: 9232
diff changeset
   422
                    while (offset > 0 && metrics.charsWidth(txt, txtOffset, offset) > (x - x0)) {
07404abef28e 7045593: Possible Regression : JTextfield cursor placement behavior algorithm has changed.
rupashka
parents: 9232
diff changeset
   423
                        offset--;
07404abef28e 7045593: Possible Regression : JTextfield cursor placement behavior algorithm has changed.
rupashka
parents: 9232
diff changeset
   424
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                }
9768
07404abef28e 7045593: Possible Regression : JTextfield cursor placement behavior algorithm has changed.
rupashka
parents: 9232
diff changeset
   426
9218
883c22142aa9 7030623: closed/javax/accessibility/4864610/bug4864610.java test fails just against jdk7 b134
rupashka
parents: 8533
diff changeset
   427
                return offset;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        // didn't find, return end offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        return txtCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * Determine where to break the given text to fit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * within the given span. This tries to find a word boundary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * @param s  the source of the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * @param metrics the font metrics to use for the calculation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * @param x0 the starting view location representing the start
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     *   of the given text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * @param x  the target view location to translate to an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     *   offset into the text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * @param e  how to expand the tabs.  If this value is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     *   tabs will be expanded as a space character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * @param startOffset starting offset in the document of the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * @return  the offset into the given text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    public static final int getBreakLocation(Segment s, FontMetrics metrics,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                                             int x0, int x, TabExpander e,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                                             int startOffset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        char[] txt = s.array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        int txtOffset = s.offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        int txtCount = s.count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        int index = Utilities.getTabbedTextOffset(s, metrics, x0, x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                                                  e, startOffset, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        if (index >= txtCount - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            return txtCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        for (int i = txtOffset + index; i >= txtOffset; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            char ch = txt[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            if (ch < 256) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                // break on whitespace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                if (Character.isWhitespace(ch)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                    index = i - txtOffset + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                // a multibyte char found; use BreakIterator to find line break
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                BreakIterator bit = BreakIterator.getLineInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                bit.setText(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                int breakPos = bit.preceding(i + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                if (breakPos > txtOffset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                    index = breakPos - txtOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * Determines the starting row model position of the row that contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * the specified model position.  The component given must have a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * size to compute the result.  If the component doesn't have a size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * a value of -1 will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * @param c the editor
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 9768
diff changeset
   491
     * @param offs the offset in the document &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 9768
diff changeset
   492
     * @return the position &gt;= 0 if the request can be computed, otherwise
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     *  a value of -1 will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * @exception BadLocationException if the offset is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    public static final int getRowStart(JTextComponent c, int offs) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        Rectangle r = c.modelToView(offs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        if (r == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        int lastOffs = offs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        int y = r.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        while ((r != null) && (y == r.y)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            // Skip invisible elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            if(r.height !=0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                offs = lastOffs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            lastOffs -= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            r = (lastOffs >= 0) ? c.modelToView(lastOffs) : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        return offs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * Determines the ending row model position of the row that contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * the specified model position.  The component given must have a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * size to compute the result.  If the component doesn't have a size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * a value of -1 will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * @param c the editor
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 9768
diff changeset
   521
     * @param offs the offset in the document &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 9768
diff changeset
   522
     * @return the position &gt;= 0 if the request can be computed, otherwise
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     *  a value of -1 will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * @exception BadLocationException if the offset is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    public static final int getRowEnd(JTextComponent c, int offs) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        Rectangle r = c.modelToView(offs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        if (r == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        int n = c.getDocument().getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        int lastOffs = offs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        int y = r.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        while ((r != null) && (y == r.y)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            // Skip invisible elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            if (r.height !=0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                offs = lastOffs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            lastOffs += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            r = (lastOffs <= n) ? c.modelToView(lastOffs) : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        return offs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * Determines the position in the model that is closest to the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * view location in the row above.  The component given must have a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * size to compute the result.  If the component doesn't have a size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * a value of -1 will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * @param c the editor
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 9768
diff changeset
   552
     * @param offs the offset in the document &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 9768
diff changeset
   553
     * @param x the X coordinate &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 9768
diff changeset
   554
     * @return the position &gt;= 0 if the request can be computed, otherwise
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     *  a value of -1 will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * @exception BadLocationException if the offset is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    public static final int getPositionAbove(JTextComponent c, int offs, int x) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        int lastOffs = getRowStart(c, offs) - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        if (lastOffs < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        int bestSpan = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        int y = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        Rectangle r = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        if (lastOffs >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            r = c.modelToView(lastOffs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            y = r.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        while ((r != null) && (y == r.y)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            int span = Math.abs(r.x - x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            if (span < bestSpan) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                offs = lastOffs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                bestSpan = span;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            lastOffs -= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            r = (lastOffs >= 0) ? c.modelToView(lastOffs) : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        return offs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * Determines the position in the model that is closest to the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * view location in the row below.  The component given must have a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * size to compute the result.  If the component doesn't have a size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * a value of -1 will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * @param c the editor
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 9768
diff changeset
   589
     * @param offs the offset in the document &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 9768
diff changeset
   590
     * @param x the X coordinate &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 9768
diff changeset
   591
     * @return the position &gt;= 0 if the request can be computed, otherwise
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     *  a value of -1 will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * @exception BadLocationException if the offset is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    public static final int getPositionBelow(JTextComponent c, int offs, int x) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        int lastOffs = getRowEnd(c, offs) + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        if (lastOffs <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        int bestSpan = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        int n = c.getDocument().getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        int y = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        Rectangle r = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        if (lastOffs <= n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            r = c.modelToView(lastOffs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            y = r.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        while ((r != null) && (y == r.y)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            int span = Math.abs(x - r.x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            if (span < bestSpan) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                offs = lastOffs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                bestSpan = span;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            lastOffs += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            r = (lastOffs <= n) ? c.modelToView(lastOffs) : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        return offs;
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
     * Determines the start of a word for the given model location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * Uses BreakIterator.getWordInstance() to actually get the words.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * @param c the editor
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 9768
diff changeset
   625
     * @param offs the offset in the document &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 9768
diff changeset
   626
     * @return the location in the model of the word start &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * @exception BadLocationException if the offset is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    public static final int getWordStart(JTextComponent c, int offs) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        Document doc = c.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        Element line = getParagraphElement(c, offs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        if (line == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            throw new BadLocationException("No word at " + offs, offs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        int lineStart = line.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        int lineEnd = Math.min(line.getEndOffset(), doc.getLength());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        Segment seg = SegmentCache.getSharedSegment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        doc.getText(lineStart, lineEnd - lineStart, seg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        if(seg.count > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            BreakIterator words = BreakIterator.getWordInstance(c.getLocale());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            words.setText(seg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            int wordPosition = seg.offset + offs - lineStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            if(wordPosition >= words.last()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                wordPosition = words.last() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            words.following(wordPosition);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            offs = lineStart + words.previous() - seg.offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        SegmentCache.releaseSharedSegment(seg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        return offs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * Determines the end of a word for the given location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * Uses BreakIterator.getWordInstance() to actually get the words.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * @param c the editor
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 9768
diff changeset
   659
     * @param offs the offset in the document &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 9768
diff changeset
   660
     * @return the location in the model of the word end &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * @exception BadLocationException if the offset is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    public static final int getWordEnd(JTextComponent c, int offs) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        Document doc = c.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        Element line = getParagraphElement(c, offs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        if (line == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            throw new BadLocationException("No word at " + offs, offs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        int lineStart = line.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        int lineEnd = Math.min(line.getEndOffset(), doc.getLength());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        Segment seg = SegmentCache.getSharedSegment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        doc.getText(lineStart, lineEnd - lineStart, seg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        if(seg.count > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            BreakIterator words = BreakIterator.getWordInstance(c.getLocale());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            words.setText(seg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            int wordPosition = offs - lineStart + seg.offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            if(wordPosition >= words.last()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                wordPosition = words.last() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            offs = lineStart + words.following(wordPosition) - seg.offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        SegmentCache.releaseSharedSegment(seg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        return offs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * Determines the start of the next word for the given location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * Uses BreakIterator.getWordInstance() to actually get the words.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * @param c the editor
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 9768
diff changeset
   692
     * @param offs the offset in the document &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 9768
diff changeset
   693
     * @return the location in the model of the word start &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * @exception BadLocationException if the offset is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    public static final int getNextWord(JTextComponent c, int offs) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        int nextWord;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        Element line = getParagraphElement(c, offs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        for (nextWord = getNextWordInParagraph(c, line, offs, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
             nextWord == BreakIterator.DONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
             nextWord = getNextWordInParagraph(c, line, offs, true)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            // didn't find in this line, try the next line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            offs = line.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            line = getParagraphElement(c, offs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        return nextWord;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * Finds the next word in the given elements text.  The first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * parameter allows searching multiple paragraphs where even
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * the first offset is desired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * Returns the offset of the next word, or BreakIterator.DONE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * if there are no more words in the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    static int getNextWordInParagraph(JTextComponent c, Element line, int offs, boolean first) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        if (line == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            throw new BadLocationException("No more words", offs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        Document doc = line.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        int lineStart = line.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        int lineEnd = Math.min(line.getEndOffset(), doc.getLength());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        if ((offs >= lineEnd) || (offs < lineStart)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            throw new BadLocationException("No more words", offs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        Segment seg = SegmentCache.getSharedSegment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        doc.getText(lineStart, lineEnd - lineStart, seg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        BreakIterator words = BreakIterator.getWordInstance(c.getLocale());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        words.setText(seg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        if ((first && (words.first() == (seg.offset + offs - lineStart))) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            (! Character.isWhitespace(seg.array[words.first()]))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            return offs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        int wordPosition = words.following(seg.offset + offs - lineStart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        if ((wordPosition == BreakIterator.DONE) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            (wordPosition >= seg.offset + seg.count)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                // there are no more words on this line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                return BreakIterator.DONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        // if we haven't shot past the end... check to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        // see if the current boundary represents whitespace.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        // if so, we need to try again
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        char ch = seg.array[wordPosition];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        if (! Character.isWhitespace(ch)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            return lineStart + wordPosition - seg.offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        // it was whitespace, try again.  The assumption
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        // is that it must be a word start if the last
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        // one had whitespace following it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        wordPosition = words.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        if (wordPosition != BreakIterator.DONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            offs = lineStart + wordPosition - seg.offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            if (offs != lineEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                return offs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        SegmentCache.releaseSharedSegment(seg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        return BreakIterator.DONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * Determine the start of the prev word for the given location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * Uses BreakIterator.getWordInstance() to actually get the words.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * @param c the editor
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 9768
diff changeset
   770
     * @param offs the offset in the document &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 9768
diff changeset
   771
     * @return the location in the model of the word start &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * @exception BadLocationException if the offset is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    public static final int getPreviousWord(JTextComponent c, int offs) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        int prevWord;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        Element line = getParagraphElement(c, offs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        for (prevWord = getPrevWordInParagraph(c, line, offs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
             prevWord == BreakIterator.DONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
             prevWord = getPrevWordInParagraph(c, line, offs)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            // didn't find in this line, try the prev line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            offs = line.getStartOffset() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            line = getParagraphElement(c, offs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        return prevWord;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * Finds the previous word in the given elements text.  The first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * parameter allows searching multiple paragraphs where even
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * the first offset is desired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * Returns the offset of the next word, or BreakIterator.DONE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * if there are no more words in the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
    static int getPrevWordInParagraph(JTextComponent c, Element line, int offs) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        if (line == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            throw new BadLocationException("No more words", offs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        Document doc = line.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        int lineStart = line.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        int lineEnd = line.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        if ((offs > lineEnd) || (offs < lineStart)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            throw new BadLocationException("No more words", offs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        Segment seg = SegmentCache.getSharedSegment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        doc.getText(lineStart, lineEnd - lineStart, seg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        BreakIterator words = BreakIterator.getWordInstance(c.getLocale());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        words.setText(seg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        if (words.following(seg.offset + offs - lineStart) == BreakIterator.DONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            words.last();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        int wordPosition = words.previous();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        if (wordPosition == (seg.offset + offs - lineStart)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
            wordPosition = words.previous();
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 (wordPosition == BreakIterator.DONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
            // there are no more words on this line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            return BreakIterator.DONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        // if we haven't shot past the end... check to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        // see if the current boundary represents whitespace.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        // if so, we need to try again
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        char ch = seg.array[wordPosition];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        if (! Character.isWhitespace(ch)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            return lineStart + wordPosition - seg.offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        // it was whitespace, try again.  The assumption
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        // is that it must be a word start if the last
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        // one had whitespace following it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        wordPosition = words.previous();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        if (wordPosition != BreakIterator.DONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            return lineStart + wordPosition - seg.offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        SegmentCache.releaseSharedSegment(seg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        return BreakIterator.DONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * Determines the element to use for a paragraph/line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * @param c the editor
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 9768
diff changeset
   844
     * @param offs the starting offset in the document &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     * @return the element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
    public static final Element getParagraphElement(JTextComponent c, int offs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        Document doc = c.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        if (doc instanceof StyledDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
            return ((StyledDocument)doc).getParagraphElement(offs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        Element map = doc.getDefaultRootElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        int index = map.getElementIndex(offs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        Element paragraph = map.getElement(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        if ((offs >= paragraph.getStartOffset()) && (offs < paragraph.getEndOffset())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
            return paragraph;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
    static boolean isComposedTextElement(Document doc, int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        Element elem = doc.getDefaultRootElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        while (!elem.isLeaf()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            elem = elem.getElement(elem.getElementIndex(offset));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        return isComposedTextElement(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
    static boolean isComposedTextElement(Element elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        AttributeSet as = elem.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        return isComposedTextAttributeDefined(as);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
    static boolean isComposedTextAttributeDefined(AttributeSet as) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        return ((as != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                (as.isDefined(StyleConstants.ComposedTextAttribute)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * Draws the given composed text passed from an input method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     * @param view View hosting text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     * @param attr the attributes containing the composed text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     * @param g  the graphics context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * @param x  the X origin
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * @param y  the Y origin
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * @param p0 starting offset in the composed text to be rendered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * @param p1 ending offset in the composed text to be rendered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     * @return  the new insertion position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
    static int drawComposedText(View view, AttributeSet attr, Graphics g,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                                int x, int y, int p0, int p1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                                     throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        Graphics2D g2d = (Graphics2D)g;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        AttributedString as = (AttributedString)attr.getAttribute(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
            StyleConstants.ComposedTextAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        as.addAttribute(TextAttribute.FONT, g.getFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        if (p0 >= p1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
            return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        AttributedCharacterIterator aci = as.getIterator(null, p0, p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        return x + (int)SwingUtilities2.drawString(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                             getJComponent(view), g2d,aci,x,y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     * Paints the composed text in a GlyphView
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
    static void paintComposedText(Graphics g, Rectangle alloc, GlyphView v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        if (g instanceof Graphics2D) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
            Graphics2D g2d = (Graphics2D) g;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
            int p0 = v.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
            int p1 = v.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            AttributeSet attrSet = v.getElement().getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            AttributedString as =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
                (AttributedString)attrSet.getAttribute(StyleConstants.ComposedTextAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            int start = v.getElement().getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
            int y = alloc.y + alloc.height - (int)v.getGlyphPainter().getDescent(v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
            int x = alloc.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
            //Add text attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            as.addAttribute(TextAttribute.FONT, v.getFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            as.addAttribute(TextAttribute.FOREGROUND, v.getForeground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
            if (StyleConstants.isBold(v.getAttributes())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
                as.addAttribute(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
            if (StyleConstants.isItalic(v.getAttributes())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                as.addAttribute(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
            if (v.isUnderline()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                as.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            if (v.isStrikeThrough()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                as.addAttribute(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
            if (v.isSuperscript()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                as.addAttribute(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUPER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
            if (v.isSubscript()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                as.addAttribute(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
            // draw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
            AttributedCharacterIterator aci = as.getIterator(null, p0 - start, p1 - start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
            SwingUtilities2.drawString(getJComponent(v),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
                                       g2d,aci,x,y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     * Convenience function for determining ComponentOrientation.  Helps us
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     * avoid having Munge directives throughout the code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
    static boolean isLeftToRight( java.awt.Component c ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        return c.getComponentOrientation().isLeftToRight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     * Provides a way to determine the next visually represented model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     * location that one might place a caret.  Some views may not be visible,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     * they might not be in the same order found in the model, or they just
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     * might not allow access to some of the locations in the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     * This implementation assumes the views are layed out in a logical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     * manner. That is, that the view at index x + 1 is visually after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     * the View at index x, and that the View at index x - 1 is visually
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     * before the View at x. There is support for reversing this behavior
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     * only if the passed in <code>View</code> is an instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     * <code>CompositeView</code>. The <code>CompositeView</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     * must then override the <code>flipEastAndWestAtEnds</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     * @param v View to query
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 9768
diff changeset
   975
     * @param pos the position to convert &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     * @param direction the direction from the current position that can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     *  be thought of as the arrow keys typically found on a keyboard;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     *  this may be one of the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     *  <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     *  <li><code>SwingConstants.WEST</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     *  <li><code>SwingConstants.EAST</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     *  <li><code>SwingConstants.NORTH</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     *  <li><code>SwingConstants.SOUTH</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     *  </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     * @param biasRet an array contain the bias that was checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     * @return the location within the model that best represents the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     *  location visual position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     * @exception BadLocationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     * @exception IllegalArgumentException if <code>direction</code> is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
    static int getNextVisualPositionFrom(View v, int pos, Position.Bias b,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
                                          Shape alloc, int direction,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
                                          Position.Bias[] biasRet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                             throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        if (v.getViewCount() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
            // Nothing to do.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
            return pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
        boolean top = (direction == SwingConstants.NORTH ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
                       direction == SwingConstants.WEST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        int retValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        if (pos == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
            // Start from the first View.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
            int childIndex = (top) ? v.getViewCount() - 1 : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
            View child = v.getView(childIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
            Shape childBounds = v.getChildAllocation(childIndex, alloc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
            retValue = child.getNextVisualPositionFrom(pos, b, childBounds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
                                                       direction, biasRet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
            if (retValue == -1 && !top && v.getViewCount() > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
                // Special case that should ONLY happen if first view
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
                // isn't valid (can happen when end position is put at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
                // beginning of line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
                child = v.getView(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                childBounds = v.getChildAllocation(1, alloc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                retValue = child.getNextVisualPositionFrom(-1, biasRet[0],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
                                                           childBounds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                                                           direction, biasRet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            int increment = (top) ? -1 : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
            int childIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
            if (b == Position.Bias.Backward && pos > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
                childIndex = v.getViewIndex(pos - 1, Position.Bias.Forward);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
                childIndex = v.getViewIndex(pos, Position.Bias.Forward);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
            View child = v.getView(childIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
            Shape childBounds = v.getChildAllocation(childIndex, alloc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            retValue = child.getNextVisualPositionFrom(pos, b, childBounds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
                                                       direction, biasRet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
            if ((direction == SwingConstants.EAST ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
                 direction == SwingConstants.WEST) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
                (v instanceof CompositeView) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
                ((CompositeView)v).flipEastAndWestAtEnds(pos, b)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
                increment *= -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            childIndex += increment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
            if (retValue == -1 && childIndex >= 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
                                  childIndex < v.getViewCount()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
                child = v.getView(childIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                childBounds = v.getChildAllocation(childIndex, alloc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
                retValue = child.getNextVisualPositionFrom(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                                     -1, b, childBounds, direction, biasRet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
                // If there is a bias change, it is a fake position
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
                // and we should skip it. This is usually the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
                // of two elements side be side flowing the same way.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
                if (retValue == pos && biasRet[0] != b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
                    return getNextVisualPositionFrom(v, pos, biasRet[0],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
                                                     alloc, direction,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
                                                     biasRet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
            else if (retValue != -1 && biasRet[0] != b &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                     ((increment == 1 && child.getEndOffset() == retValue) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
                      (increment == -1 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
                       child.getStartOffset() == retValue)) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
                     childIndex >= 0 && childIndex < v.getViewCount()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
                // Reached the end of a view, make sure the next view
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
                // is a different direction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
                child = v.getView(childIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
                childBounds = v.getChildAllocation(childIndex, alloc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
                Position.Bias originalBias = biasRet[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
                int nextPos = child.getNextVisualPositionFrom(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
                                    -1, b, childBounds, direction, biasRet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
                if (biasRet[0] == b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
                    retValue = nextPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
                    biasRet[0] = originalBias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
        return retValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
}