jdk/src/share/classes/javax/swing/text/CompositeView.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 20158 1c5d22e5b898
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.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.swing.SwingConstants;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * <code>CompositeView</code> is an abstract <code>View</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * implementation which manages one or more child views.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * (Note that <code>CompositeView</code> is intended
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * for managing relatively small numbers of child views.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <code>CompositeView</code> is intended to be used as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * a starting point for <code>View</code> implementations,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * such as <code>BoxView</code>, that will contain child
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <code>View</code>s. Subclasses that wish to manage the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * collection of child <code>View</code>s should use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * {@link #replace} method.  As <code>View</code> invokes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <code>replace</code> during <code>DocumentListener</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * notification, you normally won't need to directly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * invoke <code>replace</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <p>While <code>CompositeView</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * does not impose a layout policy on its child <code>View</code>s,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * it does allow for inseting the child <code>View</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * it will contain.  The insets can be set by either
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * {@link #setInsets} or {@link #setParagraphInsets}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <p>In addition to the abstract methods of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * {@link javax.swing.text.View},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * subclasses of <code>CompositeView</code> will need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * override:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <li>{@link #isBefore} - Used to test if a given
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *     <code>View</code> location is before the visual space
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *     of the <code>CompositeView</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <li>{@link #isAfter} - Used to test if a given
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *     <code>View</code> location is after the visual space
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *     of the <code>CompositeView</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * <li>{@link #getViewAtPoint} - Returns the view at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *     a given visual location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <li>{@link #childAllocation} - Returns the bounds of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *     a particular child <code>View</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *     <code>getChildAllocation</code> will invoke
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *     <code>childAllocation</code> after offseting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *     the bounds by the <code>Inset</code>s of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *     <code>CompositeView</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * @author  Timothy Prinzing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
public abstract class CompositeView extends View {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Constructs a <code>CompositeView</code> for the given element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @param elem  the element this view is responsible for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    public CompositeView(Element elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        super(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        children = new View[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        nchildren = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        childAlloc = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * Loads all of the children to initialize the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * This is called by the {@link #setParent}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * method.  Subclasses can reimplement this to initialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * their child views in a different manner.  The default
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * implementation creates a child view for each
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * child element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @param f the view factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @see #setParent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    protected void loadChildren(ViewFactory f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (f == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            // No factory. This most likely indicates the parent view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            // has changed out from under us, bail!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        Element e = getElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        int n = e.getElementCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            View[] added = new View[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                added[i] = f.create(e.getElement(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            replace(0, 0, added);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    // --- View methods ---------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * Sets the parent of the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * This is reimplemented to provide the superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * behavior as well as calling the <code>loadChildren</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * method if this view does not already have children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * The children should not be loaded in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * constructor because the act of setting the parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * may cause them to try to search up the hierarchy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * (to get the hosting <code>Container</code> for example).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * If this view has children (the view is being moved
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * from one place in the view hierarchy to another),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * the <code>loadChildren</code> method will not be called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @param parent the parent of the view, <code>null</code> if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public void setParent(View parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        super.setParent(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if ((parent != null) && (nchildren == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            ViewFactory f = getViewFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            loadChildren(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Returns the number of child views of this view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @return the number of views >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @see #getView
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    public int getViewCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        return nchildren;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Returns the n-th view in this container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @param n the number of the desired view, >= 0 && < getViewCount()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @return the view at index <code>n</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public View getView(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        return children[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Replaces child views.  If there are no views to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * this acts as an insert.  If there are no views to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * add this acts as a remove.  Views being removed will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * have the parent set to <code>null</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * and the internal reference to them removed so that they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * may be garbage collected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @param offset the starting index into the child views to insert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *   the new views; >= 0 and <= getViewCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @param length the number of existing child views to remove;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *   this should be a value >= 0 and <= (getViewCount() - offset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @param views the child views to add; this value can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *  <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *   to indicate no children are being added (useful to remove)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    public void replace(int offset, int length, View[] views) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        // make sure an array exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        if (views == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            views = ZERO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        // update parent reference on removed views
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        for (int i = offset; i < offset + length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            if (children[i].getParent() == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                // in FlowView.java view might be referenced
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                // from two super-views as a child. see logicalView
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                children[i].setParent(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            children[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        // update the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        int delta = views.length - length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        int src = offset + length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        int nmove = nchildren - src;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        int dest = src + delta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        if ((nchildren + delta) >= children.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            // need to grow the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            int newLength = Math.max(2*children.length, nchildren + delta);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            View[] newChildren = new View[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            System.arraycopy(children, 0, newChildren, 0, offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            System.arraycopy(views, 0, newChildren, offset, views.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            System.arraycopy(children, src, newChildren, dest, nmove);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            children = newChildren;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            // patch the existing array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            System.arraycopy(children, src, children, dest, nmove);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            System.arraycopy(views, 0, children, offset, views.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        nchildren = nchildren + delta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        // update parent reference on added views
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        for (int i = 0; i < views.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            views[i].setParent(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * Fetches the allocation for the given child view to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * render into. This enables finding out where various views
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * are located.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @param index the index of the child, >= 0 && < getViewCount()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * @param a  the allocation to this view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @return the allocation to the child
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    public Shape getChildAllocation(int index, Shape a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        Rectangle alloc = getInsideAllocation(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        childAllocation(index, alloc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        return alloc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * Provides a mapping from the document model coordinate space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * to the coordinate space of the view mapped to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @param pos the position to convert >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @param b a bias value of either <code>Position.Bias.Forward</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *  or <code>Position.Bias.Backward</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @return the bounding box of the given position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @exception BadLocationException  if the given position does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *   not represent a valid location in the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @see View#modelToView
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        boolean isBackward = (b == Position.Bias.Backward);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        int testPos = (isBackward) ? Math.max(0, pos - 1) : pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        if(isBackward && testPos < getStartOffset()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        int vIndex = getViewIndexAtPosition(testPos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        if ((vIndex != -1) && (vIndex < getViewCount())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            View v = getView(vIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            if(v != null && testPos >= v.getStartOffset() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
               testPos < v.getEndOffset()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                Shape childShape = getChildAllocation(vIndex, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                if (childShape == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    // We are likely invalid, fail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                Shape retShape = v.modelToView(pos, childShape, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                if(retShape == null && v.getEndOffset() == pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                    if(++vIndex < getViewCount()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                        v = getView(vIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                        retShape = v.modelToView(pos, getChildAllocation(vIndex, a), b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                return retShape;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        throw new BadLocationException("Position not represented by view",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                                       pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * Provides a mapping from the document model coordinate space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * to the coordinate space of the view mapped to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * @param p0 the position to convert >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * @param b0 the bias toward the previous character or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     *  next character represented by p0, in case the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *  position is a boundary of two views; either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     *  <code>Position.Bias.Forward</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *  <code>Position.Bias.Backward</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * @param p1 the position to convert >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @param b1 the bias toward the previous character or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *  next character represented by p1, in case the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     *  position is a boundary of two views
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @return the bounding box of the given position is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @exception BadLocationException  if the given position does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *   not represent a valid location in the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @exception IllegalArgumentException for an invalid bias argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @see View#viewToModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    public Shape modelToView(int p0, Position.Bias b0, int p1, Position.Bias b1, Shape a) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        if (p0 == getStartOffset() && p1 == getEndOffset()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        Rectangle alloc = getInsideAllocation(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        Rectangle r0 = new Rectangle(alloc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        View v0 = getViewAtPosition((b0 == Position.Bias.Backward) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                                    Math.max(0, p0 - 1) : p0, r0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        Rectangle r1 = new Rectangle(alloc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        View v1 = getViewAtPosition((b1 == Position.Bias.Backward) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                                    Math.max(0, p1 - 1) : p1, r1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        if (v0 == v1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            if (v0 == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            // Range contained in one view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            return v0.modelToView(p0, b0, p1, b1, r0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        // Straddles some views.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        int viewCount = getViewCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        int counter = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        while (counter < viewCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            View v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            // Views may not be in same order as model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            // v0 or v1 may be null if there is a gap in the range this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            // view contains.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            if ((v = getView(counter)) == v0 || v == v1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                View endView;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                Rectangle retRect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                Rectangle tempRect = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                if (v == v0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                    retRect = v0.modelToView(p0, b0, v0.getEndOffset(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                                             Position.Bias.Backward, r0).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                              getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                    endView = v1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    retRect = v1.modelToView(v1.getStartOffset(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                                             Position.Bias.Forward,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                                             p1, b1, r1).getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                    endView = v0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                // Views entirely covered by range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                while (++counter < viewCount &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                       (v = getView(counter)) != endView) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    tempRect.setBounds(alloc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                    childAllocation(counter, tempRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                    retRect.add(tempRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                // End view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                if (endView != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                    Shape endShape;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                    if (endView == v1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                        endShape = v1.modelToView(v1.getStartOffset(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                                                  Position.Bias.Forward,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                                                  p1, b1, r1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                        endShape = v0.modelToView(p0, b0, v0.getEndOffset(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                                                  Position.Bias.Backward, r0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                    if (endShape instanceof Rectangle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                        retRect.add((Rectangle)endShape);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                        retRect.add(endShape.getBounds());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                return retRect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            counter++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        throw new BadLocationException("Position not represented by view", p0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * Provides a mapping from the view coordinate space to the logical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * coordinate space of the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * @param x   x coordinate of the view location to convert >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * @param y   y coordinate of the view location to convert >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * @param bias either <code>Position.Bias.Forward</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     *  <code>Position.Bias.Backward</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * @return the location within the model that best represents the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     *  given point in the view >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * @see View#viewToModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    public int viewToModel(float x, float y, Shape a, Position.Bias[] bias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        Rectangle alloc = getInsideAllocation(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        if (isBefore((int) x, (int) y, alloc)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            // point is before the range represented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            int retValue = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                retValue = getNextVisualPositionFrom(-1, Position.Bias.Forward,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                                                     a, EAST, bias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            } catch (BadLocationException ble) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            catch (IllegalArgumentException iae) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            if(retValue == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                retValue = getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                bias[0] = Position.Bias.Forward;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            return retValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        } else if (isAfter((int) x, (int) y, alloc)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            // point is after the range represented.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            int retValue = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                retValue = getNextVisualPositionFrom(-1, Position.Bias.Forward,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                                                     a, WEST, bias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            } catch (BadLocationException ble) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            catch (IllegalArgumentException iae) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            if(retValue == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                // NOTE: this could actually use end offset with backward.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                retValue = getEndOffset() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                bias[0] = Position.Bias.Forward;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            return retValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            // locate the child and pass along the request
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            View v = getViewAtPoint((int) x, (int) y, alloc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            if (v != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
              return v.viewToModel(x, y, alloc, bias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * Provides a way to determine the next visually represented model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * location that one might place a caret.  Some views may not be visible,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * they might not be in the same order found in the model, or they just
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * might not allow access to some of the locations in the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * This is a convenience method for {@link #getNextNorthSouthVisualPositionFrom}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * and {@link #getNextEastWestVisualPositionFrom}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * @param pos the position to convert >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * @param b a bias value of either <code>Position.Bias.Forward</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     *  or <code>Position.Bias.Backward</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * @param direction the direction from the current position that can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     *  be thought of as the arrow keys typically found on a keyboard;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     *  this may be one of the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     *  <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     *  <li><code>SwingConstants.WEST</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     *  <li><code>SwingConstants.EAST</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     *  <li><code>SwingConstants.NORTH</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     *  <li><code>SwingConstants.SOUTH</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     *  </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * @param biasRet an array containing the bias that was checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * @return the location within the model that best represents the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     *  location visual position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * @exception BadLocationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * @exception IllegalArgumentException if <code>direction</code> is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    public int getNextVisualPositionFrom(int pos, Position.Bias b, Shape a,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                                         int direction, Position.Bias[] biasRet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
      throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        Rectangle alloc = getInsideAllocation(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        switch (direction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        case NORTH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            return getNextNorthSouthVisualPositionFrom(pos, b, a, direction,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                                                       biasRet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        case SOUTH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            return getNextNorthSouthVisualPositionFrom(pos, b, a, direction,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                                                       biasRet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        case EAST:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            return getNextEastWestVisualPositionFrom(pos, b, a, direction,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                                                     biasRet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        case WEST:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            return getNextEastWestVisualPositionFrom(pos, b, a, direction,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                                                     biasRet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            throw new IllegalArgumentException("Bad direction: " + direction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * Returns the child view index representing the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * position in the model.  This is implemented to call the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * <code>getViewIndexByPosition</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * method for backward compatibility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * @param pos the position >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * @return  index of the view representing the given position, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     *   -1 if no view represents that position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    public int getViewIndex(int pos, Position.Bias b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        if(b == Position.Bias.Backward) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            pos -= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        if ((pos >= getStartOffset()) && (pos < getEndOffset())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            return getViewIndexAtPosition(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    // --- local methods ----------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * Tests whether a point lies before the rectangle range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * @param x the X coordinate >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * @param y the Y coordinate >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * @param alloc the rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * @return true if the point is before the specified range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    protected abstract boolean isBefore(int x, int y, Rectangle alloc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * Tests whether a point lies after the rectangle range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * @param x the X coordinate >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * @param y the Y coordinate >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * @param alloc the rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * @return true if the point is after the specified range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    protected abstract boolean isAfter(int x, int y, Rectangle alloc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * Fetches the child view at the given coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * @param x the X coordinate >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * @param y the Y coordinate >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * @param alloc the parent's allocation on entry, which should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     *   be changed to the child's allocation on exit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * @return the child view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    protected abstract View getViewAtPoint(int x, int y, Rectangle alloc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * Returns the allocation for a given child.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * @param index the index of the child, >= 0 && < getViewCount()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * @param a  the allocation to the interior of the box on entry,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     *   and the allocation of the child view at the index on exit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    protected abstract void childAllocation(int index, Rectangle a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * Fetches the child view that represents the given position in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * the model.  This is implemented to fetch the view in the case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * where there is a child view for each child element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * @param pos the position >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * @param a  the allocation to the interior of the box on entry,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     *   and the allocation of the view containing the position on exit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * @return  the view representing the given position, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     *   <code>null</code> if there isn't one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    protected View getViewAtPosition(int pos, Rectangle a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        int index = getViewIndexAtPosition(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        if ((index >= 0) && (index < getViewCount())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            View v = getView(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            if (a != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                childAllocation(index, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            return v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * Fetches the child view index representing the given position in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * the model.  This is implemented to fetch the view in the case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * where there is a child view for each child element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * @param pos the position >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * @return  index of the view representing the given position, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     *   -1 if no view represents that position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    protected int getViewIndexAtPosition(int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        Element elem = getElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        return elem.getElementIndex(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * Translates the immutable allocation given to the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * to a mutable allocation that represents the interior
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * allocation (i.e. the bounds of the given allocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * with the top, left, bottom, and right insets removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * It is expected that the returned value would be further
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * mutated to represent an allocation to a child view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * This is implemented to reuse an instance variable so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * it avoids creating excessive Rectangles.  Typically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * the result of calling this method would be fed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * the <code>childAllocation</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * @param a the allocation given to the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * @return the allocation that represents the inside of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     *   view after the margins have all been removed; if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     *   given allocation was <code>null</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     *   the return value is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    protected Rectangle getInsideAllocation(Shape a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        if (a != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            // get the bounds, hopefully without allocating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            // a new rectangle.  The Shape argument should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            // not be modified... we copy it into the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            // child allocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            Rectangle alloc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            if (a instanceof Rectangle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                alloc = (Rectangle) a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                alloc = a.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            childAlloc.setBounds(alloc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            childAlloc.x += getLeftInset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            childAlloc.y += getTopInset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            childAlloc.width -= getLeftInset() + getRightInset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            childAlloc.height -= getTopInset() + getBottomInset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            return childAlloc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * Sets the insets from the paragraph attributes specified in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * the given attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * @param attr the attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    protected void setParagraphInsets(AttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        // Since version 1.1 doesn't have scaling and assumes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        // a pixel is equal to a point, we just cast the point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        // sizes to integers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        top = (short) StyleConstants.getSpaceAbove(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        left = (short) StyleConstants.getLeftIndent(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        bottom = (short) StyleConstants.getSpaceBelow(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        right = (short) StyleConstants.getRightIndent(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * Sets the insets for the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * @param top the top inset >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * @param left the left inset >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * @param bottom the bottom inset >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * @param right the right inset >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    protected void setInsets(short top, short left, short bottom, short right) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        this.top = top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        this.left = left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        this.right = right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        this.bottom = bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * Gets the left inset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * @return the inset >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    protected short getLeftInset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        return left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * Gets the right inset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * @return the inset >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
    protected short getRightInset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        return right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * Gets the top inset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * @return the inset >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    protected short getTopInset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        return top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * Gets the bottom inset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * @return the inset >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    protected short getBottomInset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        return bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * Returns the next visual position for the cursor, in either the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * north or south direction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * @param pos the position to convert >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * @param b a bias value of either <code>Position.Bias.Forward</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     *  or <code>Position.Bias.Backward</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     * @param direction the direction from the current position that can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     *  be thought of as the arrow keys typically found on a keyboard;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     *  this may be one of the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     *  <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     *  <li><code>SwingConstants.NORTH</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     *  <li><code>SwingConstants.SOUTH</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     *  </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     * @param biasRet an array containing the bias that was checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * @return the location within the model that best represents the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     *  north or south location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * @exception BadLocationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * @exception IllegalArgumentException if <code>direction</code> is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * @see #getNextVisualPositionFrom
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * @return the next position west of the passed in position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    protected int getNextNorthSouthVisualPositionFrom(int pos, Position.Bias b,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                                                      Shape a, int direction,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                                                      Position.Bias[] biasRet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                                                throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        return Utilities.getNextVisualPositionFrom(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                            this, pos, b, a, direction, biasRet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     * Returns the next visual position for the cursor, in either the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * east or west direction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
    * @param pos the position to convert >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     * @param b a bias value of either <code>Position.Bias.Forward</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     *  or <code>Position.Bias.Backward</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * @param direction the direction from the current position that can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     *  be thought of as the arrow keys typically found on a keyboard;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     *  this may be one of the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     *  <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     *  <li><code>SwingConstants.WEST</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     *  <li><code>SwingConstants.EAST</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     *  </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * @param biasRet an array containing the bias that was checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * @return the location within the model that best represents the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     *  west or east location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * @exception BadLocationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * @exception IllegalArgumentException if <code>direction</code> is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * @see #getNextVisualPositionFrom
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
    protected int getNextEastWestVisualPositionFrom(int pos, Position.Bias b,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                                                    Shape a,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                                                    int direction,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                                                    Position.Bias[] biasRet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                                                throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        return Utilities.getNextVisualPositionFrom(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                            this, pos, b, a, direction, biasRet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * Determines in which direction the next view lays.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * Consider the <code>View</code> at index n. Typically the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * <code>View</code>s are layed out from left to right,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * so that the <code>View</code> to the EAST will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * at index n + 1, and the <code>View</code> to the WEST
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * will be at index n - 1. In certain situations,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * such as with bidirectional text, it is possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * that the <code>View</code> to EAST is not at index n + 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * but rather at index n - 1, or that the <code>View</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * to the WEST is not at index n - 1, but index n + 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * In this case this method would return true, indicating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * <code>View</code>s are layed out in descending order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * This unconditionally returns false, subclasses should override this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     * method if there is the possibility for laying <code>View</code>s in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * descending order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     * @param position position into the model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * @param bias either <code>Position.Bias.Forward</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     *          <code>Position.Bias.Backward</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     * @return false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    protected boolean flipEastAndWestAtEnds(int position,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                                            Position.Bias bias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
    // ---- member variables ---------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
    private static View[] ZERO = new View[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
    private View[] children;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
    private int nchildren;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
    private short left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
    private short right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
    private short top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
    private short bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
    private Rectangle childAlloc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
}