jdk/src/java.desktop/share/classes/javax/swing/text/BoxView.java
author martin
Thu, 30 Oct 2014 07:31:41 -0700
changeset 28059 e576535359cc
parent 25859 3317bb8137f4
child 30462 507bcb03c954
permissions -rw-r--r--
8067377: My hobby: caning, then then canning, the the can-can Summary: Fix ALL the stutters! Reviewed-by: rriggs, mchung, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21278
diff changeset
     2
 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing.text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.PrintStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.swing.event.DocumentEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.swing.SizeRequirements;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * A view that arranges its children into a box shape by tiling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * its children along an axis.  The box is somewhat like that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * found in TeX where there is alignment of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * children, flexibility of the children is considered, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * This is a building block that might be useful to represent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * things like a collection of lines, paragraphs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * lists, columns, pages, etc.  The axis along which the children are tiled is
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20158
diff changeset
    41
 * considered the major axis.  The orthogonal axis is the minor axis.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * Layout for each axis is handled separately by the methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <code>layoutMajorAxis</code> and <code>layoutMinorAxis</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * Subclasses can change the layout algorithm by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * reimplementing these methods.    These methods will be called
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * as necessary depending upon whether or not there is cached
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * layout information and the cache is considered
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * valid.  These methods are typically called if the given size
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * along the axis changes, or if <code>layoutChanged</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * called to force an updated layout.  The <code>layoutChanged</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * method invalidates cached layout information, if there is any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * The requirements published to the parent view are calculated by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * the methods <code>calculateMajorAxisRequirements</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * and  <code>calculateMinorAxisRequirements</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * If the layout algorithm is changed, these methods will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * likely need to be reimplemented.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @author  Timothy Prinzing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
public class BoxView extends CompositeView {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Constructs a <code>BoxView</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * @param elem the element this view is responsible for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @param axis either <code>View.X_AXIS</code> or <code>View.Y_AXIS</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public BoxView(Element elem, int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        super(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        tempRect = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        this.majorAxis = axis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        majorOffsets = new int[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        majorSpans = new int[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        majorReqValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        majorAllocValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        minorOffsets = new int[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        minorSpans = new int[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        minorReqValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        minorAllocValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * Fetches the tile axis property.  This is the axis along which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * the child views are tiled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @return the major axis of the box, either
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     *  <code>View.X_AXIS</code> or <code>View.Y_AXIS</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public int getAxis() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        return majorAxis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * Sets the tile axis property.  This is the axis along which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * the child views are tiled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @param axis either <code>View.X_AXIS</code> or <code>View.Y_AXIS</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public void setAxis(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        boolean axisChanged = (axis != majorAxis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        majorAxis = axis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if (axisChanged) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            preferenceChanged(null, true, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Invalidates the layout along an axis.  This happens
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * automatically if the preferences have changed for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * any of the child views.  In some cases the layout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * may need to be recalculated when the preferences
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * have not changed.  The layout can be marked as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * invalid by calling this method.  The layout will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * be updated the next time the <code>setSize</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * is called on this view (typically in paint).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @param axis either <code>View.X_AXIS</code> or <code>View.Y_AXIS</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    public void layoutChanged(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        if (axis == majorAxis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            majorAllocValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            minorAllocValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Determines if the layout is valid along the given axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @param axis either <code>View.X_AXIS</code> or <code>View.Y_AXIS</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    protected boolean isLayoutValid(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        if (axis == majorAxis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            return majorAllocValid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            return minorAllocValid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * Paints a child.  By default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * that is all it does, but a subclass can use this to paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * things relative to the child.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @param g the graphics context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @param alloc the allocated region to paint into
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   157
     * @param index the child index, &gt;= 0 &amp;&amp; &lt; getViewCount()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    protected void paintChild(Graphics g, Rectangle alloc, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        View child = getView(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        child.paint(g, alloc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    // --- View methods ---------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * Invalidates the layout and resizes the cache of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * requests/allocations.  The child allocations can still
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * be accessed for the old layout, but the new children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * will have an offset and span of 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @param index the starting index into the child views to insert
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   173
     *   the new views; this should be a value &gt;= 0 and &lt;= getViewCount
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @param length the number of existing child views to remove;
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   175
     *   This should be a value &gt;= 0 and &lt;= (getViewCount() - offset)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @param elems the child views to add; this value can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *   <code>null</code>to indicate no children are being added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *   (useful to remove)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public void replace(int index, int length, View[] elems) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        super.replace(index, length, elems);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        // invalidate cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        int nInserted = (elems != null) ? elems.length : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        majorOffsets = updateLayoutArray(majorOffsets, index, nInserted);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        majorSpans = updateLayoutArray(majorSpans, index, nInserted);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        majorReqValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        majorAllocValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        minorOffsets = updateLayoutArray(minorOffsets, index, nInserted);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        minorSpans = updateLayoutArray(minorSpans, index, nInserted);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        minorReqValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        minorAllocValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * Resizes the given layout array to match the new number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * child views.  The current number of child views are used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * produce the new array.  The contents of the old array are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * inserted into the new array at the appropriate places so that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * the old layout information is transferred to the new array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @param oldArray the original layout array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @param offset location where new views will be inserted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @param nInserted the number of child views being inserted;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     *          therefore the number of blank spaces to leave in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *          new array at location <code>offset</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @return the new layout array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    int[] updateLayoutArray(int[] oldArray, int offset, int nInserted) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        int n = getViewCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        int[] newArray = new int[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        System.arraycopy(oldArray, 0, newArray, 0, offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        System.arraycopy(oldArray, offset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                         newArray, offset + nInserted, n - nInserted - offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        return newArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * Forwards the given <code>DocumentEvent</code> to the child views
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * that need to be notified of the change to the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * If a child changed its requirements and the allocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * was valid prior to forwarding the portion of the box
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * from the starting child to the end of the box will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * be repainted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * @param ec changes to the element this view is responsible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *  for (may be <code>null</code> if there were no changes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @param e the change information from the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @param a the current allocation of the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @param f the factory to use to rebuild if the view has children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @see #insertUpdate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @see #removeUpdate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @see #changedUpdate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    protected void forwardUpdate(DocumentEvent.ElementChange ec,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                                 DocumentEvent e, Shape a, ViewFactory f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        boolean wasValid = isLayoutValid(majorAxis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        super.forwardUpdate(ec, e, a, f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        // determine if a repaint is needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        if (wasValid && (! isLayoutValid(majorAxis))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            // Repaint is needed because one of the tiled children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            // have changed their span along the major axis.  If there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            // is a hosting component and an allocated shape we repaint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            Component c = getContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            if ((a != null) && (c != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                int pos = e.getOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                int index = getViewIndexAtPosition(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                Rectangle alloc = getInsideAllocation(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                if (majorAxis == X_AXIS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    alloc.x += majorOffsets[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                    alloc.width -= majorOffsets[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                    alloc.y += minorOffsets[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    alloc.height -= minorOffsets[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                c.repaint(alloc.x, alloc.y, alloc.width, alloc.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * This is called by a child to indicate its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * preferred span has changed.  This is implemented to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * throw away cached layout information so that new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * calculations will be done the next time the children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * need an allocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @param child the child view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @param width true if the width preference should change
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @param height true if the height preference should change
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    public void preferenceChanged(View child, boolean width, boolean height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        boolean majorChanged = (majorAxis == X_AXIS) ? width : height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        boolean minorChanged = (majorAxis == X_AXIS) ? height : width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        if (majorChanged) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            majorReqValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            majorAllocValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        if (minorChanged) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            minorReqValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            minorAllocValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        super.preferenceChanged(child, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * Gets the resize weight.  A value of 0 or less is not resizable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * @param axis may be either <code>View.X_AXIS</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *          <code>View.Y_AXIS</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @return the weight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @exception IllegalArgumentException for an invalid axis
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    public int getResizeWeight(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        checkRequests(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        if (axis == majorAxis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            if ((majorRequest.preferred != majorRequest.minimum) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                (majorRequest.preferred != majorRequest.maximum)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            if ((minorRequest.preferred != minorRequest.minimum) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                (minorRequest.preferred != minorRequest.maximum)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * Sets the size of the view along an axis.  This should cause
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * layout of the view along the given axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * @param axis may be either <code>View.X_AXIS</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *          <code>View.Y_AXIS</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * @param span the span to layout to >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    void setSpanOnAxis(int axis, float span) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        if (axis == majorAxis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            if (majorSpan != (int) span) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                majorAllocValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            if (! majorAllocValid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                // layout the major axis
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                majorSpan = (int) span;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                checkRequests(majorAxis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                layoutMajorAxis(majorSpan, axis, majorOffsets, majorSpans);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                majorAllocValid = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                // flush changes to the children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                updateChildSizes();
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
            if (((int) span) != minorSpan) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                minorAllocValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            if (! minorAllocValid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                // layout the minor axis
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                minorSpan = (int) span;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                checkRequests(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                layoutMinorAxis(minorSpan, axis, minorOffsets, minorSpans);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                minorAllocValid = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                // flush changes to the children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                updateChildSizes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * Propagates the current allocations to the child views.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    void updateChildSizes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        int n = getViewCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        if (majorAxis == X_AXIS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                View v = getView(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                v.setSize((float) majorSpans[i], (float) minorSpans[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                View v = getView(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                v.setSize((float) minorSpans[i], (float) majorSpans[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * Returns the size of the view along an axis.  This is implemented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * to return zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * @param axis may be either <code>View.X_AXIS</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     *          <code>View.Y_AXIS</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * @return the current span of the view along the given axis, >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    float getSpanOnAxis(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        if (axis == majorAxis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            return majorSpan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            return minorSpan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * Sets the size of the view.  This should cause
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * layout of the view if the view caches any layout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * information.  This is implemented to call the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * layout method with the sizes inside of the insets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     *
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   393
     * @param width the width &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   394
     * @param height the height &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    public void setSize(float width, float height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        layout(Math.max(0, (int)(width - getLeftInset() - getRightInset())),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
               Math.max(0, (int)(height - getTopInset() - getBottomInset())));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * Renders the <code>BoxView</code> using the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * rendering surface and area
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * on that surface.  Only the children that intersect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * the clip bounds of the given <code>Graphics</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * will be rendered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * @param g the rendering surface to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * @param allocation the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * @see View#paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    public void paint(Graphics g, Shape allocation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        Rectangle alloc = (allocation instanceof Rectangle) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                           (Rectangle)allocation : allocation.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        int n = getViewCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        int x = alloc.x + getLeftInset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        int y = alloc.y + getTopInset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        Rectangle clip = g.getClipBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            tempRect.x = x + getOffset(X_AXIS, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            tempRect.y = y + getOffset(Y_AXIS, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            tempRect.width = getSpan(X_AXIS, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            tempRect.height = getSpan(Y_AXIS, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            int trx0 = tempRect.x, trx1 = trx0 + tempRect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            int try0 = tempRect.y, try1 = try0 + tempRect.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            int crx0 = clip.x, crx1 = crx0 + clip.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            int cry0 = clip.y, cry1 = cry0 + clip.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            // We should paint views that intersect with clipping region
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            // even if the intersection has no inside points (is a line).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            // This is needed for supporting views that have zero width, like
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            // views that contain only combining marks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            if ((trx1 >= crx0) && (try1 >= cry0) && (crx1 >= trx0) && (cry1 >= try0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                paintChild(g, tempRect, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * Fetches the allocation for the given child view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * This enables finding out where various views
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * are located.  This is implemented to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * <code>null</code> if the layout is invalid,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * otherwise the superclass behavior is executed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     *
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   445
     * @param index the index of the child, &gt;= 0 &amp;&amp; &gt; getViewCount()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * @param a  the allocation to this view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * @return the allocation to the child; or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     *          if <code>a</code> is <code>null</code>;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     *          or <code>null</code> if the layout is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    public Shape getChildAllocation(int index, Shape a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        if (a != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            Shape ca = super.getChildAllocation(index, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            if ((ca != null) && (! isAllocationValid())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                // The child allocation may not have been set yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                Rectangle r = (ca instanceof Rectangle) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                    (Rectangle) ca : ca.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                if ((r.width == 0) && (r.height == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            return ca;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * Provides a mapping from the document model coordinate space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * to the coordinate space of the view mapped to it.  This makes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * sure the allocation is valid before calling the superclass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     *
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   472
     * @param pos the position to convert &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * @return the bounding box of the given position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * @exception BadLocationException  if the given position does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     *  not represent a valid location in the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * @see View#modelToView
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        if (! isAllocationValid()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            Rectangle alloc = a.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            setSize(alloc.width, alloc.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        return super.modelToView(pos, a, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * Provides a mapping from the view coordinate space to the logical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * coordinate space of the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     *
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   491
     * @param x   x coordinate of the view location to convert &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   492
     * @param y   y coordinate of the view location to convert &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * @return the location within the model that best represents the
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   495
     *  given point in the view &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * @see View#viewToModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    public int viewToModel(float x, float y, Shape a, Position.Bias[] bias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        if (! isAllocationValid()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            Rectangle alloc = a.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            setSize(alloc.width, alloc.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        return super.viewToModel(x, y, a, bias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * Determines the desired alignment for this view along an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * axis.  This is implemented to give the total alignment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * needed to position the children with the alignment points
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20158
diff changeset
   510
     * lined up along the axis orthogonal to the axis that is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * being tiled.  The axis being tiled will request to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * centered (i.e. 0.5f).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * @param axis may be either <code>View.X_AXIS</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     *   or <code>View.Y_AXIS</code>
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   516
     * @return the desired alignment &gt;= 0.0f &amp;&amp; &lt;= 1.0f; this should
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     *   be a value between 0.0 and 1.0 where 0 indicates alignment at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     *   origin and 1.0 indicates alignment to the full span
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     *   away from the origin; an alignment of 0.5 would be the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     *   center of the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * @exception IllegalArgumentException for an invalid axis
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    public float getAlignment(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        checkRequests(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        if (axis == majorAxis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            return majorRequest.alignment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            return minorRequest.alignment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * Determines the preferred span for this view along an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * @param axis may be either <code>View.X_AXIS</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     *           or <code>View.Y_AXIS</code>
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   538
     * @return   the span the view would like to be rendered into &gt;= 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     *           typically the view is told to render into the span
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     *           that is returned, although there is no guarantee;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     *           the parent may choose to resize or break the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * @exception IllegalArgumentException for an invalid axis type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    public float getPreferredSpan(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        checkRequests(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        float marginSpan = (axis == X_AXIS) ? getLeftInset() + getRightInset() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            getTopInset() + getBottomInset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        if (axis == majorAxis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            return ((float)majorRequest.preferred) + marginSpan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            return ((float)minorRequest.preferred) + marginSpan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * Determines the minimum span for this view along an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * @param axis may be either <code>View.X_AXIS</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     *           or <code>View.Y_AXIS</code>
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   561
     * @return  the span the view would like to be rendered into &gt;= 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     *           typically the view is told to render into the span
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     *           that is returned, although there is no guarantee;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     *           the parent may choose to resize or break the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * @exception IllegalArgumentException for an invalid axis type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    public float getMinimumSpan(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        checkRequests(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        float marginSpan = (axis == X_AXIS) ? getLeftInset() + getRightInset() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            getTopInset() + getBottomInset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        if (axis == majorAxis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            return ((float)majorRequest.minimum) + marginSpan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            return ((float)minorRequest.minimum) + marginSpan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * Determines the maximum span for this view along an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * @param axis may be either <code>View.X_AXIS</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     *           or <code>View.Y_AXIS</code>
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   584
     * @return   the span the view would like to be rendered into &gt;= 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     *           typically the view is told to render into the span
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     *           that is returned, although there is no guarantee;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     *           the parent may choose to resize or break the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * @exception IllegalArgumentException for an invalid axis type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    public float getMaximumSpan(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        checkRequests(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        float marginSpan = (axis == X_AXIS) ? getLeftInset() + getRightInset() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            getTopInset() + getBottomInset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        if (axis == majorAxis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            return ((float)majorRequest.maximum) + marginSpan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            return ((float)minorRequest.maximum) + marginSpan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    // --- local methods ----------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * Are the allocations for the children still
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * valid?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * @return true if allocations still valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    protected boolean isAllocationValid() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        return (majorAllocValid && minorAllocValid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * Determines if a point falls before an allocated region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     *
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   616
     * @param x the X coordinate &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   617
     * @param y the Y coordinate &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * @param innerAlloc the allocated region; this is the area
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     *   inside of the insets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * @return true if the point lies before the region else false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    protected boolean isBefore(int x, int y, Rectangle innerAlloc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        if (majorAxis == View.X_AXIS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            return (x < innerAlloc.x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            return (y < innerAlloc.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * Determines if a point falls after an allocated region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     *
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   633
     * @param x the X coordinate &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   634
     * @param y the Y coordinate &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * @param innerAlloc the allocated region; this is the area
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     *   inside of the insets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * @return true if the point lies after the region else false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    protected boolean isAfter(int x, int y, Rectangle innerAlloc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        if (majorAxis == View.X_AXIS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            return (x > (innerAlloc.width + innerAlloc.x));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            return (y > (innerAlloc.height + innerAlloc.y));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * Fetches the child view at the given coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     *
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   650
     * @param x the X coordinate &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   651
     * @param y the Y coordinate &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * @param alloc the parents inner allocation on entry, which should
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20158
diff changeset
   653
     *   be changed to the child's allocation on exit
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * @return the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    protected View getViewAtPoint(int x, int y, Rectangle alloc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        int n = getViewCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        if (majorAxis == View.X_AXIS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            if (x < (alloc.x + majorOffsets[0])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                childAllocation(0, alloc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                return getView(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                if (x < (alloc.x + majorOffsets[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                    childAllocation(i - 1, alloc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                    return getView(i - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            childAllocation(n - 1, alloc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            return getView(n - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            if (y < (alloc.y + majorOffsets[0])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                childAllocation(0, alloc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                return getView(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                if (y < (alloc.y + majorOffsets[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                    childAllocation(i - 1, alloc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                    return getView(i - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            childAllocation(n - 1, alloc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            return getView(n - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * Allocates a region for a child view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * @param index the index of the child view to
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   691
     *   allocate, &gt;= 0 &amp;&amp; &lt; getViewCount()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * @param alloc the allocated region
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    protected void childAllocation(int index, Rectangle alloc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        alloc.x += getOffset(X_AXIS, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        alloc.y += getOffset(Y_AXIS, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        alloc.width = getSpan(X_AXIS, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        alloc.height = getSpan(Y_AXIS, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * Perform layout on the box
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     *
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   704
     * @param width the width (inside of the insets) &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   705
     * @param height the height (inside of the insets) &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    protected void layout(int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        setSpanOnAxis(X_AXIS, width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        setSpanOnAxis(Y_AXIS, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * Returns the current width of the box.  This is the width that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * it was last allocated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * @return the current width of the box
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    public int getWidth() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        int span;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        if (majorAxis == X_AXIS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            span = majorSpan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            span = minorSpan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        span += getLeftInset() - getRightInset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        return span;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     * Returns the current height of the box.  This is the height that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * it was last allocated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * @return the current height of the box
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
    public int getHeight() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        int span;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        if (majorAxis == Y_AXIS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            span = majorSpan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            span = minorSpan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        span += getTopInset() - getBottomInset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        return span;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     * Performs layout for the major axis of the box (i.e. the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     * axis that it represents). The results of the layout (the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     * offset and span for each children) are placed in the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * arrays which represent the allocations to the children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     * along the major axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * @param targetSpan the total span given to the view, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     *  would be used to layout the children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * @param axis the axis being layed out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * @param offsets the offsets from the origin of the view for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     *  each of the child views; this is a return value and is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     *  filled in by the implementation of this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * @param spans the span of each child view; this is a return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     *  value and is filled in by the implementation of this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
    protected void layoutMajorAxis(int targetSpan, int axis, int[] offsets, int[] spans) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
         * first pass, calculate the preferred sizes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
         * and the flexibility to adjust the sizes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        long preferred = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        int n = getViewCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            View v = getView(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            spans[i] = (int) v.getPreferredSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            preferred += spans[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
         * Second pass, expand or contract by as much as possible to reach
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
         * the target span.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        // determine the adjustment to be made
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        long desiredAdjustment = targetSpan - preferred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        float adjustmentFactor = 0.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        int[] diffs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        if (desiredAdjustment != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            long totalSpan = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            diffs = new int[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                View v = getView(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                int tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                if (desiredAdjustment < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                    tmp = (int)v.getMinimumSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                    diffs[i] = spans[i] - tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                    tmp = (int)v.getMaximumSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                    diffs[i] = tmp - spans[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                totalSpan += tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            float maximumAdjustment = Math.abs(totalSpan - preferred);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                adjustmentFactor = desiredAdjustment / maximumAdjustment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                adjustmentFactor = Math.min(adjustmentFactor, 1.0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                adjustmentFactor = Math.max(adjustmentFactor, -1.0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        // make the adjustments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        int totalOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            offsets[i] = totalOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            if (desiredAdjustment != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                float adjF = adjustmentFactor * diffs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                spans[i] += Math.round(adjF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
            totalOffset = (int) Math.min((long) totalOffset + (long) spans[i], Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * Performs layout for the minor axis of the box (i.e. the
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20158
diff changeset
   819
     * axis orthogonal to the axis that it represents). The results
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * of the layout (the offset and span for each children) are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * placed in the given arrays which represent the allocations to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     * the children along the minor axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     * @param targetSpan the total span given to the view, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     *  would be used to layout the children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     * @param axis the axis being layed out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     * @param offsets the offsets from the origin of the view for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     *  each of the child views; this is a return value and is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     *  filled in by the implementation of this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     * @param spans the span of each child view; this is a return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     *  value and is filled in by the implementation of this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
    protected void layoutMinorAxis(int targetSpan, int axis, int[] offsets, int[] spans) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        int n = getViewCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
            View v = getView(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            int max = (int) v.getMaximumSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
            if (max < targetSpan) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                // can't make the child this wide, align it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                float align = v.getAlignment(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                offsets[i] = (int) ((targetSpan - max) * align);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                spans[i] = max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                // make it the target width, or as small as it can get.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                int min = (int)v.getMinimumSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                offsets[i] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                spans[i] = Math.max(min, targetSpan);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * Calculates the size requirements for the major axis
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * <code>axis</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     * @param axis the axis being studied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * @param r the <code>SizeRequirements</code> object;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     *          if <code>null</code> one will be created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * @return the newly initialized <code>SizeRequirements</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * @see javax.swing.SizeRequirements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
    protected SizeRequirements calculateMajorAxisRequirements(int axis, SizeRequirements r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        // calculate tiled request
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        float min = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        float pref = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        float max = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        int n = getViewCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
            View v = getView(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
            min += v.getMinimumSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
            pref += v.getPreferredSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
            max += v.getMaximumSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        if (r == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
            r = new SizeRequirements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        r.alignment = 0.5f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        r.minimum = (int) min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        r.preferred = (int) pref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        r.maximum = (int) max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * Calculates the size requirements for the minor axis
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * <code>axis</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * @param axis the axis being studied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * @param r the <code>SizeRequirements</code> object;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     *          if <code>null</code> one will be created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * @return the newly initialized <code>SizeRequirements</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * @see javax.swing.SizeRequirements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
    protected SizeRequirements calculateMinorAxisRequirements(int axis, SizeRequirements r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        int min = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        long pref = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        int max = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        int n = getViewCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
            View v = getView(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
            min = Math.max((int) v.getMinimumSpan(axis), min);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
            pref = Math.max((int) v.getPreferredSpan(axis), pref);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
            max = Math.max((int) v.getMaximumSpan(axis), max);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        if (r == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
            r = new SizeRequirements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
            r.alignment = 0.5f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        r.preferred = (int) pref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        r.minimum = min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        r.maximum = max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     * Checks the request cache and update if needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     * @param axis the axis being studied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     * @exception IllegalArgumentException if <code>axis</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     *  neither <code>View.X_AXIS</code> nor <code>View.Y_AXIS</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
    void checkRequests(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        if ((axis != X_AXIS) && (axis != Y_AXIS)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
            throw new IllegalArgumentException("Invalid axis: " + axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        if (axis == majorAxis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
            if (!majorReqValid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
                majorRequest = calculateMajorAxisRequirements(axis,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
                                                              majorRequest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                majorReqValid = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        } else if (! minorReqValid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
            minorRequest = calculateMinorAxisRequirements(axis, minorRequest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
            minorReqValid = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     * Computes the location and extent of each child view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     * in this <code>BoxView</code> given the <code>targetSpan</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * which is the width (or height) of the region we have to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * work with.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     * @param targetSpan the total span given to the view, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     *  would be used to layout the children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     * @param axis the axis being studied, either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     *          <code>View.X_AXIS</code> or <code>View.Y_AXIS</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     * @param offsets an empty array filled by this method with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     *          values specifying the location  of each child view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     * @param spans  an empty array filled by this method with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     *          values specifying the extent of each child view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
    protected void baselineLayout(int targetSpan, int axis, int[] offsets, int[] spans) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        int totalAscent = (int)(targetSpan * getAlignment(axis));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        int totalDescent = targetSpan - totalAscent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        int n = getViewCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
            View v = getView(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
            float align = v.getAlignment(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
            float viewSpan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
            if (v.getResizeWeight(axis) > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                // if resizable then resize to the best fit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                // the smallest span possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
                float minSpan = v.getMinimumSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                // the largest span possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                float maxSpan = v.getMaximumSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
                if (align == 0.0f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                    // if the alignment is 0 then we need to fit into the descent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
                    viewSpan = Math.max(Math.min(maxSpan, totalDescent), minSpan);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
                } else if (align == 1.0f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                    // if the alignment is 1 then we need to fit into the ascent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                    viewSpan = Math.max(Math.min(maxSpan, totalAscent), minSpan);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
                    // figure out the span that we must fit into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                    float fitSpan = Math.min(totalAscent / align,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
                                             totalDescent / (1.0f - align));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
                    // fit into the calculated span
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
                    viewSpan = Math.max(Math.min(maxSpan, fitSpan), minSpan);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
                // otherwise use the preferred spans
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
                viewSpan = v.getPreferredSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
            offsets[i] = totalAscent - (int)(viewSpan * align);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
            spans[i] = (int)viewSpan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     * Calculates the size requirements for this <code>BoxView</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     * by examining the size of each child view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     * @param axis the axis being studied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     * @param r the <code>SizeRequirements</code> object;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     *          if <code>null</code> one will be created
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     * @return the newly initialized <code>SizeRequirements</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
    protected SizeRequirements baselineRequirements(int axis, SizeRequirements r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        SizeRequirements totalAscent = new SizeRequirements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        SizeRequirements totalDescent = new SizeRequirements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        if (r == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
            r = new SizeRequirements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        r.alignment = 0.5f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
        int n = getViewCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        // loop through all children calculating the max of all their ascents and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        // descents at minimum, preferred, and maximum sizes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
            View v = getView(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            float align = v.getAlignment(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
            float span;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
            int ascent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
            int descent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
            // find the maximum of the preferred ascents and descents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
            span = v.getPreferredSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
            ascent = (int)(align * span);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
            descent = (int)(span - ascent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
            totalAscent.preferred = Math.max(ascent, totalAscent.preferred);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            totalDescent.preferred = Math.max(descent, totalDescent.preferred);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
            if (v.getResizeWeight(axis) > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
                // if the view is resizable then do the same for the minimum and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
                // maximum ascents and descents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
                span = v.getMinimumSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
                ascent = (int)(align * span);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
                descent = (int)(span - ascent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
                totalAscent.minimum = Math.max(ascent, totalAscent.minimum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
                totalDescent.minimum = Math.max(descent, totalDescent.minimum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
                span = v.getMaximumSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                ascent = (int)(align * span);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
                descent = (int)(span - ascent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                totalAscent.maximum = Math.max(ascent, totalAscent.maximum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
                totalDescent.maximum = Math.max(descent, totalDescent.maximum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
                // otherwise use the preferred
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
                totalAscent.minimum = Math.max(ascent, totalAscent.minimum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
                totalDescent.minimum = Math.max(descent, totalDescent.minimum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
                totalAscent.maximum = Math.max(ascent, totalAscent.maximum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
                totalDescent.maximum = Math.max(descent, totalDescent.maximum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
        // we now have an overall preferred, minimum, and maximum ascent and descent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
        // calculate the preferred span as the sum of the preferred ascent and preferred descent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        r.preferred = (int)Math.min((long)totalAscent.preferred + (long)totalDescent.preferred,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
                                    Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
        // calculate the preferred alignment as the preferred ascent divided by the preferred span
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        if (r.preferred > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
            r.alignment = (float)totalAscent.preferred / r.preferred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
        if (r.alignment == 0.0f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
            // if the preferred alignment is 0 then the minimum and maximum spans are simply
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
            // the minimum and maximum descents since there's nothing above the baseline
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
            r.minimum = totalDescent.minimum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
            r.maximum = totalDescent.maximum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
        } else if (r.alignment == 1.0f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
            // if the preferred alignment is 1 then the minimum and maximum spans are simply
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
            // the minimum and maximum ascents since there's nothing below the baseline
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
            r.minimum = totalAscent.minimum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
            r.maximum = totalAscent.maximum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
            // we want to honor the preferred alignment so we calculate two possible minimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
            // span values using 1) the minimum ascent and the alignment, and 2) the minimum
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
            // descent and the alignment. We'll choose the larger of these two numbers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
            r.minimum = Math.round(Math.max(totalAscent.minimum / r.alignment,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
                                          totalDescent.minimum / (1.0f - r.alignment)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
            // a similar calculation is made for the maximum but we choose the smaller number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
            r.maximum = Math.round(Math.min(totalAscent.maximum / r.alignment,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
                                          totalDescent.maximum / (1.0f - r.alignment)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     * Fetches the offset of a particular child's current layout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     * @param axis the axis being studied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     * @param childIndex the index of the requested child
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     * @return the offset (location) for the specified child
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
    protected int getOffset(int axis, int childIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        int[] offsets = (axis == majorAxis) ? majorOffsets : minorOffsets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        return offsets[childIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
    /**
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20158
diff changeset
  1105
     * Fetches the span of a particular child's current layout.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     * @param axis the axis being studied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
     * @param childIndex the index of the requested child
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     * @return the span (width or height) of the specified child
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
    protected int getSpan(int axis, int childIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
        int[] spans = (axis == majorAxis) ? majorSpans : minorSpans;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        return spans[childIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
     * Determines in which direction the next view lays.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
     * Consider the View at index n. Typically the <code>View</code>s
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
     * are layed out from left to right, so that the <code>View</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
     * to the EAST will be at index n + 1, and the <code>View</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
     * to the WEST will be at index n - 1. In certain situations,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
     * such as with bidirectional text, it is possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
     * that the <code>View</code> to EAST is not at index n + 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
     * but rather at index n - 1, or that the <code>View</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     * to the WEST is not at index n - 1, but index n + 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     * In this case this method would return true,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     * indicating the <code>View</code>s are layed out in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     * descending order. Otherwise the method would return false
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
     * indicating the <code>View</code>s are layed out in ascending order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
     * If the receiver is laying its <code>View</code>s along the
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 25859
diff changeset
  1131
     * <code>Y_AXIS</code>, this will return the value from
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
     * invoking the same method on the <code>View</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
     * responsible for rendering <code>position</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
     * <code>bias</code>. Otherwise this will return false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
     * @param position position into the model
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
     * @param bias either <code>Position.Bias.Forward</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
     *          <code>Position.Bias.Backward</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
     * @return true if the <code>View</code>s surrounding the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
     *          <code>View</code> responding for rendering
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
     *          <code>position</code> and <code>bias</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
     *          are layed out in descending order; otherwise false
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
    protected boolean flipEastAndWestAtEnds(int position,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
                                            Position.Bias bias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
        if(majorAxis == Y_AXIS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
            int testPos = (bias == Position.Bias.Backward) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
                          Math.max(0, position - 1) : position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
            int index = getViewIndexAtPosition(testPos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
            if(index != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
                View v = getView(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
                if(v != null && v instanceof CompositeView) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
                    return ((CompositeView)v).flipEastAndWestAtEnds(position,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
                                                                    bias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
    // --- variables ------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
    int majorAxis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
    int majorSpan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
    int minorSpan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     * Request cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
    boolean majorReqValid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
    boolean minorReqValid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
    SizeRequirements majorRequest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
    SizeRequirements minorRequest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
     * Allocation cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
    boolean majorAllocValid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
    int[] majorOffsets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
    int[] majorSpans;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
    boolean minorAllocValid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
    int[] minorOffsets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
    int[] minorSpans;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
    /** used in paint. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
    Rectangle tempRect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
}