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