jdk/src/share/classes/javax/swing/text/Utilities.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8533 0537e84f6e69
child 9232 9e29d6359705
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8533
diff changeset
     2
 * Copyright (c) 1997, 2011, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * @param x  the X origin >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * @param y  the Y origin >= 0
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.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @param startOffset starting offset of the text in the document >= 0
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @param x  the X origin >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @param y  the Y origin >= 0
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.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @param startOffset starting offset of the text in the document >= 0
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @param x  the X origin >= 0
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.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @param startOffset starting offset of the text in the document >= 0
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     *   of the given text >= 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @param x  the target view location to translate to an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *   offset into the text >= 0.
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.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @param startOffset starting offset of the text in the document >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @return  the offset into the text >= 0
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
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        int currX = x0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        int nextX = currX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        // s may be a shared segment, so it is copied prior to calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        // the tab expander
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        char[] txt = s.array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        int txtOffset = s.offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        int txtCount = s.count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        int spaceAddon = 0 ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        int spaceAddonLeftoverEnd = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        int startJustifiableContent = 0 ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        int endJustifiableContent = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        if (justificationData != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            int offset = - startOffset + txtOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            View parent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            if (view != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                  && (parent = view.getParent()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                offset += parent.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            spaceAddon =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                justificationData[Row.SPACE_ADDON];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            spaceAddonLeftoverEnd =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                justificationData[Row.SPACE_ADDON_LEFTOVER_END] + offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            startJustifiableContent =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                justificationData[Row.START_JUSTIFIABLE] + offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            endJustifiableContent =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                justificationData[Row.END_JUSTIFIABLE] + offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        int n = s.offset + s.count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        for (int i = s.offset; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            if (txt[i] == '\t'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                || ((spaceAddon != 0 || i <= spaceAddonLeftoverEnd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                    && (txt[i] == ' ')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                    && startJustifiableContent <= i
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                    && i <= endJustifiableContent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                    )){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                if (txt[i] == '\t') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                    if (e != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                        nextX = (int) e.nextTabStop((float) nextX,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                                                    startOffset + i - txtOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                        nextX += metrics.charWidth(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                } else if (txt[i] == ' ') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                    nextX += metrics.charWidth(' ') + spaceAddon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                    if (i <= spaceAddonLeftoverEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                        nextX++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                nextX += metrics.charWidth(txt[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            if ((x >= currX) && (x < nextX)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                // found the hit position... return the appropriate side
8533
0537e84f6e69 6760148: Certain fonts are not correctly soft wrapped when using JTextComponent.print()
rupashka
parents: 7668
diff changeset
   393
                int offset = ((round == false) || ((x - currX) < (nextX - x))) ?
0537e84f6e69 6760148: Certain fonts are not correctly soft wrapped when using JTextComponent.print()
rupashka
parents: 7668
diff changeset
   394
                        (i - txtOffset) : (i + 1 - txtOffset);
0537e84f6e69 6760148: Certain fonts are not correctly soft wrapped when using JTextComponent.print()
rupashka
parents: 7668
diff changeset
   395
                // 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
   396
                // 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
   397
                // fractional metrics are enabled; and we must guard from this.
0537e84f6e69 6760148: Certain fonts are not correctly soft wrapped when using JTextComponent.print()
rupashka
parents: 7668
diff changeset
   398
                while (metrics.charsWidth(txt, txtOffset, offset + 1) > (x - x0)) {
0537e84f6e69 6760148: Certain fonts are not correctly soft wrapped when using JTextComponent.print()
rupashka
parents: 7668
diff changeset
   399
                    offset--;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                }
8533
0537e84f6e69 6760148: Certain fonts are not correctly soft wrapped when using JTextComponent.print()
rupashka
parents: 7668
diff changeset
   401
                return (offset < 0 ? 0 : offset);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            currX = nextX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        // didn't find, return end offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        return txtCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * Determine where to break the given text to fit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * within the given span. This tries to find a word boundary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * @param s  the source of the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * @param metrics the font metrics to use for the calculation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * @param x0 the starting view location representing the start
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     *   of the given text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * @param x  the target view location to translate to an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     *   offset into the text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * @param e  how to expand the tabs.  If this value is null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     *   tabs will be expanded as a space character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * @param startOffset starting offset in the document of the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * @return  the offset into the given text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    public static final int getBreakLocation(Segment s, FontMetrics metrics,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                                             int x0, int x, TabExpander e,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                                             int startOffset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        char[] txt = s.array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        int txtOffset = s.offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        int txtCount = s.count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        int index = Utilities.getTabbedTextOffset(s, metrics, x0, x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                                                  e, startOffset, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        if (index >= txtCount - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            return txtCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        for (int i = txtOffset + index; i >= txtOffset; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            char ch = txt[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            if (ch < 256) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                // break on whitespace
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                if (Character.isWhitespace(ch)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                    index = i - txtOffset + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                // a multibyte char found; use BreakIterator to find line break
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                BreakIterator bit = BreakIterator.getLineInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                bit.setText(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                int breakPos = bit.preceding(i + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                if (breakPos > txtOffset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                    index = breakPos - txtOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * Determines the starting row model position of the row that contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * the specified model position.  The component given must have a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * size to compute the result.  If the component doesn't have a size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * a value of -1 will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * @param c the editor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * @param offs the offset in the document >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * @return the position >= 0 if the request can be computed, otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     *  a value of -1 will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * @exception BadLocationException if the offset is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    public static final int getRowStart(JTextComponent c, int offs) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        Rectangle r = c.modelToView(offs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        if (r == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        int lastOffs = offs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        int y = r.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        while ((r != null) && (y == r.y)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            // Skip invisible elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            if(r.height !=0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                offs = lastOffs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            lastOffs -= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            r = (lastOffs >= 0) ? c.modelToView(lastOffs) : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        return offs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * Determines the ending row model position of the row that contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * the specified model position.  The component given must have a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * size to compute the result.  If the component doesn't have a size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * a value of -1 will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * @param c the editor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * @param offs the offset in the document >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * @return the position >= 0 if the request can be computed, otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     *  a value of -1 will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * @exception BadLocationException if the offset is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    public static final int getRowEnd(JTextComponent c, int offs) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        Rectangle r = c.modelToView(offs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        if (r == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        int n = c.getDocument().getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        int lastOffs = offs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        int y = r.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        while ((r != null) && (y == r.y)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            // Skip invisible elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            if (r.height !=0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                offs = lastOffs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            lastOffs += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            r = (lastOffs <= n) ? c.modelToView(lastOffs) : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        return offs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * Determines the position in the model that is closest to the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * view location in the row above.  The component given must have a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * size to compute the result.  If the component doesn't have a size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * a value of -1 will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * @param c the editor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * @param offs the offset in the document >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * @param x the X coordinate >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * @return the position >= 0 if the request can be computed, otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     *  a value of -1 will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * @exception BadLocationException if the offset is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    public static final int getPositionAbove(JTextComponent c, int offs, int x) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        int lastOffs = getRowStart(c, offs) - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        if (lastOffs < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        int bestSpan = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        int y = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        Rectangle r = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        if (lastOffs >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            r = c.modelToView(lastOffs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            y = r.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        while ((r != null) && (y == r.y)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            int span = Math.abs(r.x - x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            if (span < bestSpan) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                offs = lastOffs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                bestSpan = span;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            lastOffs -= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            r = (lastOffs >= 0) ? c.modelToView(lastOffs) : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        return offs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * Determines the position in the model that is closest to the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * view location in the row below.  The component given must have a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * size to compute the result.  If the component doesn't have a size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * a value of -1 will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * @param c the editor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * @param offs the offset in the document >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * @param x the X coordinate >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * @return the position >= 0 if the request can be computed, otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     *  a value of -1 will be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * @exception BadLocationException if the offset is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    public static final int getPositionBelow(JTextComponent c, int offs, int x) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        int lastOffs = getRowEnd(c, offs) + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        if (lastOffs <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        int bestSpan = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        int n = c.getDocument().getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        int y = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        Rectangle r = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        if (lastOffs <= n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            r = c.modelToView(lastOffs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            y = r.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        while ((r != null) && (y == r.y)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            int span = Math.abs(x - r.x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            if (span < bestSpan) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                offs = lastOffs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                bestSpan = span;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            lastOffs += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            r = (lastOffs <= n) ? c.modelToView(lastOffs) : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        return offs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * Determines the start of a word for the given model location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * Uses BreakIterator.getWordInstance() to actually get the words.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * @param c the editor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * @param offs the offset in the document >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * @return the location in the model of the word start >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * @exception BadLocationException if the offset is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    public static final int getWordStart(JTextComponent c, int offs) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        Document doc = c.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        Element line = getParagraphElement(c, offs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        if (line == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            throw new BadLocationException("No word at " + offs, offs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        int lineStart = line.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        int lineEnd = Math.min(line.getEndOffset(), doc.getLength());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        Segment seg = SegmentCache.getSharedSegment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        doc.getText(lineStart, lineEnd - lineStart, seg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        if(seg.count > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            BreakIterator words = BreakIterator.getWordInstance(c.getLocale());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            words.setText(seg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            int wordPosition = seg.offset + offs - lineStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            if(wordPosition >= words.last()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                wordPosition = words.last() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            words.following(wordPosition);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            offs = lineStart + words.previous() - seg.offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        SegmentCache.releaseSharedSegment(seg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        return offs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * Determines the end of a word for the given location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * Uses BreakIterator.getWordInstance() to actually get the words.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * @param c the editor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * @param offs the offset in the document >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * @return the location in the model of the word end >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * @exception BadLocationException if the offset is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    public static final int getWordEnd(JTextComponent c, int offs) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        Document doc = c.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        Element line = getParagraphElement(c, offs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        if (line == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            throw new BadLocationException("No word at " + offs, offs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        int lineStart = line.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        int lineEnd = Math.min(line.getEndOffset(), doc.getLength());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        Segment seg = SegmentCache.getSharedSegment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        doc.getText(lineStart, lineEnd - lineStart, seg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        if(seg.count > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            BreakIterator words = BreakIterator.getWordInstance(c.getLocale());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            words.setText(seg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            int wordPosition = offs - lineStart + seg.offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            if(wordPosition >= words.last()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                wordPosition = words.last() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            offs = lineStart + words.following(wordPosition) - seg.offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        SegmentCache.releaseSharedSegment(seg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        return offs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * Determines the start of the next word for the given location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * Uses BreakIterator.getWordInstance() to actually get the words.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * @param c the editor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * @param offs the offset in the document >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * @return the location in the model of the word start >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * @exception BadLocationException if the offset is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    public static final int getNextWord(JTextComponent c, int offs) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        int nextWord;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        Element line = getParagraphElement(c, offs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        for (nextWord = getNextWordInParagraph(c, line, offs, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
             nextWord == BreakIterator.DONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
             nextWord = getNextWordInParagraph(c, line, offs, true)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            // didn't find in this line, try the next line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            offs = line.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            line = getParagraphElement(c, offs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        return nextWord;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * Finds the next word in the given elements text.  The first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * parameter allows searching multiple paragraphs where even
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * the first offset is desired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * Returns the offset of the next word, or BreakIterator.DONE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * if there are no more words in the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    static int getNextWordInParagraph(JTextComponent c, Element line, int offs, boolean first) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        if (line == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            throw new BadLocationException("No more words", offs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        Document doc = line.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        int lineStart = line.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        int lineEnd = Math.min(line.getEndOffset(), doc.getLength());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        if ((offs >= lineEnd) || (offs < lineStart)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            throw new BadLocationException("No more words", offs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        Segment seg = SegmentCache.getSharedSegment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        doc.getText(lineStart, lineEnd - lineStart, seg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        BreakIterator words = BreakIterator.getWordInstance(c.getLocale());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        words.setText(seg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        if ((first && (words.first() == (seg.offset + offs - lineStart))) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            (! Character.isWhitespace(seg.array[words.first()]))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            return offs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        int wordPosition = words.following(seg.offset + offs - lineStart);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        if ((wordPosition == BreakIterator.DONE) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            (wordPosition >= seg.offset + seg.count)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                // there are no more words on this line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                return BreakIterator.DONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        // if we haven't shot past the end... check to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        // see if the current boundary represents whitespace.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        // if so, we need to try again
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        char ch = seg.array[wordPosition];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        if (! Character.isWhitespace(ch)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            return lineStart + wordPosition - seg.offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        // it was whitespace, try again.  The assumption
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        // is that it must be a word start if the last
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        // one had whitespace following it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        wordPosition = words.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        if (wordPosition != BreakIterator.DONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            offs = lineStart + wordPosition - seg.offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            if (offs != lineEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                return offs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        SegmentCache.releaseSharedSegment(seg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        return BreakIterator.DONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * Determine the start of the prev word for the given location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * Uses BreakIterator.getWordInstance() to actually get the words.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     * @param c the editor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     * @param offs the offset in the document >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     * @return the location in the model of the word start >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     * @exception BadLocationException if the offset is out of range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    public static final int getPreviousWord(JTextComponent c, int offs) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        int prevWord;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        Element line = getParagraphElement(c, offs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        for (prevWord = getPrevWordInParagraph(c, line, offs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
             prevWord == BreakIterator.DONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
             prevWord = getPrevWordInParagraph(c, line, offs)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            // didn't find in this line, try the prev line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            offs = line.getStartOffset() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            line = getParagraphElement(c, offs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        return prevWord;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * Finds the previous word in the given elements text.  The first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * parameter allows searching multiple paragraphs where even
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * the first offset is desired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * Returns the offset of the next word, or BreakIterator.DONE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     * if there are no more words in the element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    static int getPrevWordInParagraph(JTextComponent c, Element line, int offs) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        if (line == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            throw new BadLocationException("No more words", offs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        Document doc = line.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        int lineStart = line.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        int lineEnd = line.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        if ((offs > lineEnd) || (offs < lineStart)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            throw new BadLocationException("No more words", offs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        Segment seg = SegmentCache.getSharedSegment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        doc.getText(lineStart, lineEnd - lineStart, seg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        BreakIterator words = BreakIterator.getWordInstance(c.getLocale());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        words.setText(seg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        if (words.following(seg.offset + offs - lineStart) == BreakIterator.DONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            words.last();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        int wordPosition = words.previous();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        if (wordPosition == (seg.offset + offs - lineStart)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            wordPosition = words.previous();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        if (wordPosition == BreakIterator.DONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            // there are no more words on this line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            return BreakIterator.DONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        // if we haven't shot past the end... check to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        // see if the current boundary represents whitespace.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        // if so, we need to try again
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        char ch = seg.array[wordPosition];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        if (! Character.isWhitespace(ch)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            return lineStart + wordPosition - seg.offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        // it was whitespace, try again.  The assumption
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        // is that it must be a word start if the last
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        // one had whitespace following it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        wordPosition = words.previous();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        if (wordPosition != BreakIterator.DONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            return lineStart + wordPosition - seg.offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        SegmentCache.releaseSharedSegment(seg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        return BreakIterator.DONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * Determines the element to use for a paragraph/line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * @param c the editor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * @param offs the starting offset in the document >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * @return the element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    public static final Element getParagraphElement(JTextComponent c, int offs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        Document doc = c.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        if (doc instanceof StyledDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            return ((StyledDocument)doc).getParagraphElement(offs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        Element map = doc.getDefaultRootElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        int index = map.getElementIndex(offs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        Element paragraph = map.getElement(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        if ((offs >= paragraph.getStartOffset()) && (offs < paragraph.getEndOffset())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            return paragraph;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
    static boolean isComposedTextElement(Document doc, int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        Element elem = doc.getDefaultRootElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        while (!elem.isLeaf()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            elem = elem.getElement(elem.getElementIndex(offset));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        return isComposedTextElement(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
    static boolean isComposedTextElement(Element elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        AttributeSet as = elem.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        return isComposedTextAttributeDefined(as);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
    static boolean isComposedTextAttributeDefined(AttributeSet as) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        return ((as != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                (as.isDefined(StyleConstants.ComposedTextAttribute)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * Draws the given composed text passed from an input method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * @param view View hosting text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * @param attr the attributes containing the composed text
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * @param g  the graphics context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * @param x  the X origin
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     * @param y  the Y origin
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     * @param p0 starting offset in the composed text to be rendered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     * @param p1 ending offset in the composed text to be rendered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     * @return  the new insertion position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
    static int drawComposedText(View view, AttributeSet attr, Graphics g,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                                int x, int y, int p0, int p1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                                     throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        Graphics2D g2d = (Graphics2D)g;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        AttributedString as = (AttributedString)attr.getAttribute(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
            StyleConstants.ComposedTextAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        as.addAttribute(TextAttribute.FONT, g.getFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        if (p0 >= p1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
            return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        AttributedCharacterIterator aci = as.getIterator(null, p0, p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        return x + (int)SwingUtilities2.drawString(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
                             getJComponent(view), g2d,aci,x,y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     * Paints the composed text in a GlyphView
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
    static void paintComposedText(Graphics g, Rectangle alloc, GlyphView v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        if (g instanceof Graphics2D) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
            Graphics2D g2d = (Graphics2D) g;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
            int p0 = v.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
            int p1 = v.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            AttributeSet attrSet = v.getElement().getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
            AttributedString as =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                (AttributedString)attrSet.getAttribute(StyleConstants.ComposedTextAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
            int start = v.getElement().getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
            int y = alloc.y + alloc.height - (int)v.getGlyphPainter().getDescent(v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
            int x = alloc.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            //Add text attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
            as.addAttribute(TextAttribute.FONT, v.getFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            as.addAttribute(TextAttribute.FOREGROUND, v.getForeground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
            if (StyleConstants.isBold(v.getAttributes())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                as.addAttribute(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
            if (StyleConstants.isItalic(v.getAttributes())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                as.addAttribute(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
            if (v.isUnderline()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                as.addAttribute(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
            if (v.isStrikeThrough()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                as.addAttribute(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
            if (v.isSuperscript()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                as.addAttribute(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUPER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            if (v.isSubscript()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
                as.addAttribute(TextAttribute.SUPERSCRIPT, TextAttribute.SUPERSCRIPT_SUB);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
            // draw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
            AttributedCharacterIterator aci = as.getIterator(null, p0 - start, p1 - start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
            SwingUtilities2.drawString(getJComponent(v),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
                                       g2d,aci,x,y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     * Convenience function for determining ComponentOrientation.  Helps us
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     * avoid having Munge directives throughout the code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
    static boolean isLeftToRight( java.awt.Component c ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        return c.getComponentOrientation().isLeftToRight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     * Provides a way to determine the next visually represented model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     * location that one might place a caret.  Some views may not be visible,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     * they might not be in the same order found in the model, or they just
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     * might not allow access to some of the locations in the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     * This implementation assumes the views are layed out in a logical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     * manner. That is, that the view at index x + 1 is visually after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * the View at index x, and that the View at index x - 1 is visually
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * before the View at x. There is support for reversing this behavior
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * only if the passed in <code>View</code> is an instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     * <code>CompositeView</code>. The <code>CompositeView</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     * must then override the <code>flipEastAndWestAtEnds</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     * @param v View to query
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     * @param pos the position to convert >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     * @param direction the direction from the current position that can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     *  be thought of as the arrow keys typically found on a keyboard;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     *  this may be one of the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     *  <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     *  <li><code>SwingConstants.WEST</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     *  <li><code>SwingConstants.EAST</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     *  <li><code>SwingConstants.NORTH</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
     *  <li><code>SwingConstants.SOUTH</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     *  </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     * @param biasRet an array contain the bias that was checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     * @return the location within the model that best represents the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     *  location visual position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     * @exception BadLocationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     * @exception IllegalArgumentException if <code>direction</code> is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
    static int getNextVisualPositionFrom(View v, int pos, Position.Bias b,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                                          Shape alloc, int direction,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                                          Position.Bias[] biasRet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
                             throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        if (v.getViewCount() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
            // Nothing to do.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
            return pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
        boolean top = (direction == SwingConstants.NORTH ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
                       direction == SwingConstants.WEST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        int retValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        if (pos == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
            // Start from the first View.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
            int childIndex = (top) ? v.getViewCount() - 1 : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
            View child = v.getView(childIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
            Shape childBounds = v.getChildAllocation(childIndex, alloc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
            retValue = child.getNextVisualPositionFrom(pos, b, childBounds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
                                                       direction, biasRet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
            if (retValue == -1 && !top && v.getViewCount() > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
                // Special case that should ONLY happen if first view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
                // isn't valid (can happen when end position is put at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
                // beginning of line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
                child = v.getView(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
                childBounds = v.getChildAllocation(1, alloc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
                retValue = child.getNextVisualPositionFrom(-1, biasRet[0],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
                                                           childBounds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
                                                           direction, biasRet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
            int increment = (top) ? -1 : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
            int childIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
            if (b == Position.Bias.Backward && pos > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
                childIndex = v.getViewIndex(pos - 1, Position.Bias.Forward);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
                childIndex = v.getViewIndex(pos, Position.Bias.Forward);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
            View child = v.getView(childIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
            Shape childBounds = v.getChildAllocation(childIndex, alloc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
            retValue = child.getNextVisualPositionFrom(pos, b, childBounds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
                                                       direction, biasRet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
            if ((direction == SwingConstants.EAST ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
                 direction == SwingConstants.WEST) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
                (v instanceof CompositeView) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
                ((CompositeView)v).flipEastAndWestAtEnds(pos, b)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
                increment *= -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
            childIndex += increment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
            if (retValue == -1 && childIndex >= 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
                                  childIndex < v.getViewCount()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                child = v.getView(childIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
                childBounds = v.getChildAllocation(childIndex, alloc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
                retValue = child.getNextVisualPositionFrom(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
                                     -1, b, childBounds, direction, biasRet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
                // If there is a bias change, it is a fake position
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
                // and we should skip it. This is usually the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
                // of two elements side be side flowing the same way.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
                if (retValue == pos && biasRet[0] != b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
                    return getNextVisualPositionFrom(v, pos, biasRet[0],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
                                                     alloc, direction,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
                                                     biasRet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
            else if (retValue != -1 && biasRet[0] != b &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
                     ((increment == 1 && child.getEndOffset() == retValue) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
                      (increment == -1 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
                       child.getStartOffset() == retValue)) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
                     childIndex >= 0 && childIndex < v.getViewCount()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
                // Reached the end of a view, make sure the next view
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
                // is a different direction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
                child = v.getView(childIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
                childBounds = v.getChildAllocation(childIndex, alloc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
                Position.Bias originalBias = biasRet[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
                int nextPos = child.getNextVisualPositionFrom(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
                                    -1, b, childBounds, direction, biasRet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
                if (biasRet[0] == b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                    retValue = nextPos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
                    biasRet[0] = originalBias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        return retValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
}