jdk/src/share/classes/javax/swing/text/NavigationFilter.java
author xdono
Mon, 15 Dec 2008 16:55:25 -0800
changeset 1639 a97859015238
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6785258: Update copyright year Summary: Update copyright for files that have been modified starting July 2008 to Dec 2008 Reviewed-by: katleman, ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2000-2006 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing.text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.awt.Shape;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * <code>NavigationFilter</code> can be used to restrict where the cursor can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * be positioned. When the default cursor positioning actions attempt to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * reposition the cursor they will call into the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * <code>NavigationFilter</code>, assuming
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * the <code>JTextComponent</code> has a non-null
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <code>NavigationFilter</code> set. In this manner
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * the <code>NavigationFilter</code> can effectively restrict where the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * cursor can be positioned. Similarly <code>DefaultCaret</code> will call
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * into the <code>NavigationFilter</code> when the user is changing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * selection to further restrict where the cursor can be positioned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * Subclasses can conditionally call into supers implementation to restrict
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * where the cursor can be placed, or call directly into the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <code>FilterBypass</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @see javax.swing.text.Caret
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @see javax.swing.text.DefaultCaret
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @see javax.swing.text.View
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
public class NavigationFilter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * Invoked prior to the Caret setting the dot. The default implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * calls directly into the <code>FilterBypass</code> with the passed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * in arguments. Subclasses may wish to conditionally
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * call super with a different location, or invoke the necessary method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * on the <code>FilterBypass</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * @param fb FilterBypass that can be used to mutate caret position
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * @param dot the position >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * @param bias Bias to place the dot at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public void setDot(FilterBypass fb, int dot, Position.Bias bias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        fb.setDot(dot, bias);
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
     * Invoked prior to the Caret moving the dot. The default implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * calls directly into the <code>FilterBypass</code> with the passed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * in arguments. Subclasses may wish to conditionally
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * call super with a different location, or invoke the necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * methods on the <code>FilterBypass</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * @param fb FilterBypass that can be used to mutate caret position
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * @param dot the position >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @param bias Bias for new location
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public void moveDot(FilterBypass fb, int dot, Position.Bias bias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        fb.moveDot(dot, bias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Returns the next visual position to place the caret at from an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * existing position. The default implementation simply forwards the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * method to the root View. Subclasses may wish to further restrict the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * location based on additional criteria.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @param text JTextComponent containing text
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @param pos Position used in determining next position
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @param bias Bias used in determining next position
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @param direction the direction from the current position that can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *  be thought of as the arrow keys typically found on a keyboard.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *  This will be one of the following values:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * <li>SwingConstants.WEST
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * <li>SwingConstants.EAST
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * <li>SwingConstants.NORTH
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * <li>SwingConstants.SOUTH
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @param biasRet Used to return resulting Bias of next position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @return the location within the model that best represents the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *  location visual position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @exception BadLocationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @exception IllegalArgumentException if <code>direction</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *          doesn't have one of the legal values above
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public int getNextVisualPositionFrom(JTextComponent text, int pos,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                                         Position.Bias bias, int direction,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                                         Position.Bias[] biasRet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                                           throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        return text.getUI().getNextVisualPositionFrom(text, pos, bias,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                                                      direction, biasRet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * Used as a way to circumvent calling back into the caret to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * position the cursor. Caret implementations that wish to support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * a NavigationFilter must provide an implementation that will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * not callback into the NavigationFilter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public static abstract class FilterBypass {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
         * Returns the Caret that is changing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
         * @return Caret that is changing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        public abstract Caret getCaret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
         * Sets the caret location, bypassing the NavigationFilter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
         * @param dot the position >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
         * @param bias Bias to place the dot at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        public abstract void setDot(int dot, Position.Bias bias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
         * Moves the caret location, bypassing the NavigationFilter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
         * @param dot the position >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
         * @param bias Bias for new location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        public abstract void moveDot(int dot, Position.Bias bias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
}