jdk/src/share/classes/javax/swing/text/AsyncBoxView.java
author malenkov
Tue, 29 Oct 2013 17:01:06 +0400
changeset 21278 ef8a3a2a72f2
parent 21256 dfb1f9090caa
child 23010 6dadb192ad81
permissions -rw-r--r--
8022746: List of spelling errors in API doc Reviewed-by: alexsch, smarks
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 7959
diff changeset
     2
 * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
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: 1639
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: 1639
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing.text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.util.*;
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
    28
import java.util.List;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.swing.SwingUtilities;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.swing.event.DocumentEvent;
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 box that does layout asynchronously.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * is useful to keep the GUI event thread moving by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * not doing any layout on it.  The layout is done
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * on a granularity of operations on the child views.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * After each child view is accessed for some part
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * of layout (a potentially time consuming operation)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * the remaining tasks can be abandoned or a new higher
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * priority task (i.e. to service a synchronous request
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * or a visible area) can be taken on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * While the child view is being accessed
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 21256
diff changeset
    45
 * a read lock is acquired on the associated document
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * so that the model is stable while being accessed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @author  Timothy Prinzing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @since   1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
public class AsyncBoxView extends View {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * Construct a box view that does asynchronous layout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * @param elem the element of the model to represent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * @param axis the axis to tile along.  This can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     *  either X_AXIS or Y_AXIS.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public AsyncBoxView(Element elem, int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        super(elem);
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
    62
        stats = new ArrayList<ChildState>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        this.axis = axis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        locator = new ChildLocator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        flushTask = new FlushTask();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        minorSpan = Short.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        estimatedMajorSpan = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * Fetch the major axis (the axis the children
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * are tiled along).  This will have a value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * either X_AXIS or Y_AXIS.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public int getMajorAxis() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        return axis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /**
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 21256
diff changeset
    80
     * Fetch the minor axis (the axis orthogonal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * to the tiled axis).  This will have a value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * either X_AXIS or Y_AXIS.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public int getMinorAxis() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        return (axis == X_AXIS) ? Y_AXIS : X_AXIS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * Get the top part of the margin around the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public float getTopInset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        return topInset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Set the top part of the margin around the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @param i the value of the inset
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public void setTopInset(float i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        topInset = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Get the bottom part of the margin around the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public float getBottomInset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        return bottomInset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * Set the bottom part of the margin around the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @param i the value of the inset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public void setBottomInset(float i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        bottomInset = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * Get the left part of the margin around the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public float getLeftInset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        return leftInset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Set the left part of the margin around the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @param i the value of the inset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public void setLeftInset(float i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        leftInset = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * Get the right part of the margin around the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public float getRightInset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        return rightInset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Set the right part of the margin around the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @param i the value of the inset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public void setRightInset(float i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        rightInset = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * Fetch the span along an axis that is taken up by the insets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @param axis the axis to determine the total insets along,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *  either X_AXIS or Y_AXIS.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    protected float getInsetSpan(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        float margin = (axis == X_AXIS) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            getLeftInset() + getRightInset() : getTopInset() + getBottomInset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        return margin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * Set the estimatedMajorSpan property that determines if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * major span should be treated as being estimated.  If this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * property is true, the value of setSize along the major axis
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * will change the requirements along the major axis and incremental
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * changes will be ignored until all of the children have been updated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * (which will cause the property to automatically be set to false).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * If the property is false the value of the majorSpan will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * considered to be accurate and incremental changes will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * added into the total as they are calculated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    protected void setEstimatedMajorSpan(boolean isEstimated) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        estimatedMajorSpan = isEstimated;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * Is the major span currently estimated?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    protected boolean getEstimatedMajorSpan() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        return estimatedMajorSpan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * Fetch the object representing the layout state of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * of the child at the given index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @param index the child index.  This should be a
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 9035
diff changeset
   196
     *   value &gt;= 0 and &lt; getViewCount().
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    protected ChildState getChildState(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        synchronized(stats) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            if ((index >= 0) && (index < stats.size())) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   201
                return stats.get(index);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * Fetch the queue to use for layout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    protected LayoutQueue getLayoutQueue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        return LayoutQueue.getDefaultQueue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * New ChildState records are created through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * this method to allow subclasses the extend
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * the ChildState records to do/hold more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    protected ChildState createChildState(View v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        return new ChildState(v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * Requirements changed along the major axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * This is called by the thread doing layout for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * the given ChildState object when it has completed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * fetching the child views new preferences.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * Typically this would be the layout thread, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * might be the event thread if it is trying to update
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * something immediately (such as to perform a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * model/view translation).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * This is implemented to mark the major axis as having
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * changed so that a future check to see if the requirements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * need to be published to the parent view will consider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * the major axis.  If the span along the major axis is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * not estimated, it is updated by the given delta to reflect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * the incremental change.  The delta is ignored if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * major span is estimated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    protected synchronized void majorRequirementChange(ChildState cs, float delta) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        if (estimatedMajorSpan == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            majorSpan += delta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        majorChanged = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * Requirements changed along the minor axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * This is called by the thread doing layout for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * the given ChildState object when it has completed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * fetching the child views new preferences.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * Typically this would be the layout thread, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * might be the GUI thread if it is trying to update
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * something immediately (such as to perform a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * model/view translation).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    protected synchronized void minorRequirementChange(ChildState cs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        minorChanged = true;
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
     * Publish the changes in preferences upward to the parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * view.  This is normally called by the layout thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    protected void flushRequirementChanges() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        AbstractDocument doc = (AbstractDocument) getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            doc.readLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            View parent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            boolean horizontal = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            boolean vertical = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                // perform tasks that iterate over the children while
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                // preventing the collection from changing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                synchronized(stats) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                    int n = getViewCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                    if ((n > 0) && (minorChanged || estimatedMajorSpan)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                        LayoutQueue q = getLayoutQueue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                        ChildState min = getChildState(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                        ChildState pref = getChildState(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                        float span = 0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                        for (int i = 1; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                            ChildState cs = getChildState(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                            if (minorChanged) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                                if (cs.min > min.min) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                                    min = cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                                if (cs.pref > pref.pref) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                                    pref = cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                            if (estimatedMajorSpan) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                                span += cs.getMajorSpan();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                        if (minorChanged) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                            minRequest = min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                            prefRequest = pref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                        if (estimatedMajorSpan) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                            majorSpan = span;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                            estimatedMajorSpan = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                            majorChanged = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                // message preferenceChanged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                if (majorChanged || minorChanged) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                    parent = getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                    if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                        if (axis == X_AXIS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                            horizontal = majorChanged;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                            vertical = minorChanged;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                            vertical = majorChanged;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                            horizontal = minorChanged;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                    majorChanged = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                    minorChanged = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            // propagate a preferenceChanged, using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            // layout thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                parent.preferenceChanged(this, horizontal, vertical);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                // probably want to change this to be more exact.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                Component c = getContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    c.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            doc.readUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * Calls the superclass to update the child views, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * updates the status records for the children.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * is expected to be called while a write lock is held
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * on the model so that interaction with the layout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * thread will not happen (i.e. the layout thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * acquires a read lock before doing anything).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     *
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 9035
diff changeset
   353
     * @param offset the starting offset into the child views &gt;= 0
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 9035
diff changeset
   354
     * @param length the number of existing views to replace &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * @param views the child views to insert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    public void replace(int offset, int length, View[] views) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        synchronized(stats) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            // remove the replaced state records
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            for (int i = 0; i < length; i++) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   361
                ChildState cs = stats.remove(offset);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                float csSpan = cs.getMajorSpan();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                cs.getChildView().setParent(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                if (csSpan != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                    majorRequirementChange(cs, -csSpan);
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
            // insert the state records for the new children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            LayoutQueue q = getLayoutQueue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            if (views != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                for (int i = 0; i < views.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                    ChildState s = createChildState(views[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                    stats.add(offset + i, s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                    q.addTask(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            // notify that the size changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            q.addTask(flushTask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * Loads all of the children to initialize the view.
7959
2e05332a8f5c 6589952: Swing: dead links in API documentation
rupashka
parents: 5506
diff changeset
   387
     * This is called by the {@link #setParent setParent}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * method.  Subclasses can reimplement this to initialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * their child views in a different manner.  The default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * implementation creates a child view for each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * child element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * Normally a write-lock is held on the Document while
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * the children are being changed, which keeps the rendering
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * and layout threads safe.  The exception to this is when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * the view is initialized to represent an existing element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * (via this method), so it is synchronized to exclude
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * preferenceChanged while we are initializing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * @param f the view factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * @see #setParent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    protected void loadChildren(ViewFactory f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        Element e = getElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        int n = e.getElementCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            View[] added = new View[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                added[i] = f.create(e.getElement(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            replace(0, 0, added);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * Fetches the child view index representing the given position in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * the model.  This is implemented to fetch the view in the case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * where there is a child view for each child element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     *
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 9035
diff changeset
   420
     * @param pos the position &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * @return  index of the view representing the given position, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     *   -1 if no view represents that position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    protected synchronized int getViewIndexAtPosition(int pos, Position.Bias b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        boolean isBackward = (b == Position.Bias.Backward);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        pos = (isBackward) ? Math.max(0, pos - 1) : pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        Element elem = getElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        return elem.getElementIndex(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * Update the layout in response to receiving notification of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * change from the model.  This is implemented to note the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * change on the ChildLocator so that offsets of the children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * will be correctly computed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * @param ec changes to the element this view is responsible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     *  for (may be null if there were no changes).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * @param e the change information from the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * @param a the current allocation of the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * @see #insertUpdate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * @see #removeUpdate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * @see #changedUpdate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    protected void updateLayout(DocumentEvent.ElementChange ec,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                                    DocumentEvent e, Shape a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        if (ec != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            // the newly inserted children don't have a valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            // offset so the child locator needs to be messaged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            // that the child prior to the new children has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            // changed size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            int index = Math.max(ec.getIndex() - 1, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            ChildState cs = getChildState(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            locator.childChanged(cs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    // --- View methods ------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * Sets the parent of the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * This is reimplemented to provide the superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * behavior as well as calling the <code>loadChildren</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * method if this view does not already have children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * The children should not be loaded in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * constructor because the act of setting the parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * may cause them to try to search up the hierarchy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * (to get the hosting Container for example).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * If this view has children (the view is being moved
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * from one place in the view hierarchy to another),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * the <code>loadChildren</code> method will not be called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * @param parent the parent of the view, null if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    public void setParent(View parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        super.setParent(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        if ((parent != null) && (getViewCount() == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            ViewFactory f = getViewFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            loadChildren(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * Child views can call this on the parent to indicate that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * the preference has changed and should be reconsidered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * for layout.  This is reimplemented to queue new work
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * on the layout thread.  This method gets messaged from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * multiple threads via the children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * @param child the child view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * @param width true if the width preference has changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * @param height true if the height preference has changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * @see javax.swing.JComponent#revalidate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    public synchronized void preferenceChanged(View child, boolean width, boolean height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        if (child == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            getParent().preferenceChanged(this, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            if (changing != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                View cv = changing.getChildView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                if (cv == child) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                    // size was being changed on the child, no need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                    // queue work for it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                    changing.preferenceChanged(width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            int index = getViewIndex(child.getStartOffset(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                                     Position.Bias.Forward);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            ChildState cs = getChildState(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            cs.preferenceChanged(width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            LayoutQueue q = getLayoutQueue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            q.addTask(cs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            q.addTask(flushTask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * Sets the size of the view.  This should cause
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * layout of the view if the view caches any layout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * Since the major axis is updated asynchronously and should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * the sum of the tiled children the call is ignored for the major
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * axis.  Since the minor axis is flexible, work is queued to resize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * the children if the minor span changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     *
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 9035
diff changeset
   528
     * @param width the width &gt;= 0
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 9035
diff changeset
   529
     * @param height the height &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    public void setSize(float width, float height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        setSpanOnAxis(X_AXIS, width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        setSpanOnAxis(Y_AXIS, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * Retrieves the size of the view along an axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * @param axis may be either <code>View.X_AXIS</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     *          <code>View.Y_AXIS</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * @return the current span of the view along the given axis, >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    float getSpanOnAxis(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        if (axis == getMajorAxis()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            return majorSpan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        return minorSpan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * Sets the size of the view along an axis.  Since the major
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * axis is updated asynchronously and should be the sum of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * tiled children the call is ignored for the major axis.  Since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * the minor axis is flexible, work is queued to resize the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * children if the minor span changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * @param axis may be either <code>View.X_AXIS</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     *          <code>View.Y_AXIS</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * @param span the span to layout to >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    void setSpanOnAxis(int axis, float span) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        float margin = getInsetSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        if (axis == getMinorAxis()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            float targetSpan = span - margin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            if (targetSpan != minorSpan) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                minorSpan = targetSpan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                // mark all of the ChildState instances as needing to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                // resize the child, and queue up work to fix them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                int n = getViewCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                if (n != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                    LayoutQueue q = getLayoutQueue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                    for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                        ChildState cs = getChildState(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                        cs.childSizeValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                        q.addTask(cs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                    q.addTask(flushTask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            // along the major axis the value is ignored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            // unless the estimatedMajorSpan property is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            // true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            if (estimatedMajorSpan) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                majorSpan = span - margin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * Render the view using the given allocation and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * rendering surface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * This is implemented to determine whether or not the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * desired region to be rendered (i.e. the unclipped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * area) is up to date or not.  If up-to-date the children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * are rendered.  If not up-to-date, a task to build
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * the desired area is placed on the layout queue as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * a high priority task.  This keeps by event thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * moving by rendering if ready, and postponing until
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * a later time if not ready (since paint requests
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * can be rescheduled).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * @param g the rendering surface to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * @param alloc the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * @see View#paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    public void paint(Graphics g, Shape alloc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        synchronized (locator) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            locator.setAllocation(alloc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            locator.paintChildren(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * Determines the preferred span for this view along an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * @param axis may be either View.X_AXIS or View.Y_AXIS
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 9035
diff changeset
   621
     * @return   the span the view would like to be rendered into &gt;= 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     *           Typically the view is told to render into the span
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     *           that is returned, although there is no guarantee.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     *           The parent may choose to resize or break the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * @exception IllegalArgumentException for an invalid axis type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    public float getPreferredSpan(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        float margin = getInsetSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        if (axis == this.axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            return majorSpan + margin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        if (prefRequest != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            View child = prefRequest.getChildView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            return child.getPreferredSpan(axis) + margin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        // nothing is known about the children yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        return margin + 30;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * Determines the minimum span for this view along an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * @param axis may be either View.X_AXIS or View.Y_AXIS
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 9035
diff changeset
   646
     * @return  the span the view would like to be rendered into &gt;= 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     *           Typically the view is told to render into the span
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     *           that is returned, although there is no guarantee.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     *           The parent may choose to resize or break the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * @exception IllegalArgumentException for an invalid axis type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    public float getMinimumSpan(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        if (axis == this.axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            return getPreferredSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        if (minRequest != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            View child = minRequest.getChildView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            return child.getMinimumSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        // nothing is known about the children yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        if (axis == X_AXIS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            return getLeftInset() + getRightInset() + 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            return getTopInset() + getBottomInset() + 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * Determines the maximum span for this view along an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * @param axis may be either View.X_AXIS or View.Y_AXIS
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 9035
diff changeset
   674
     * @return   the span the view would like to be rendered into &gt;= 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     *           Typically the view is told to render into the span
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     *           that is returned, although there is no guarantee.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     *           The parent may choose to resize or break the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * @exception IllegalArgumentException for an invalid axis type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    public float getMaximumSpan(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        if (axis == this.axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            return getPreferredSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        return Integer.MAX_VALUE;
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * Returns the number of views in this view.  Since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * the default is to not be a composite view this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * returns 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     *
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 9035
diff changeset
   693
     * @return the number of views &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * @see View#getViewCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    public int getViewCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        synchronized(stats) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            return stats.size();
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * Gets the nth child view.  Since there are no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * children by default, this returns null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     *
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 9035
diff changeset
   706
     * @param n the number of the view to get, &gt;= 0 &amp;&amp; &lt; getViewCount()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * @return the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    public View getView(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        ChildState cs = getChildState(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        if (cs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            return cs.getChildView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * Fetches the allocation for the given child view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * This enables finding out where various views
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * are located, without assuming the views store
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * their location.  This returns null since the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * default is to not have any child views.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     *
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 9035
diff changeset
   724
     * @param index the index of the child, &gt;= 0 &amp;&amp; &lt; getViewCount()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     * @param a  the allocation to this view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     * @return the allocation to the child
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    public Shape getChildAllocation(int index, Shape a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        Shape ca = locator.getChildAllocation(index, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        return ca;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * Returns the child view index representing the given position in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * the model.  By default a view has no children so this is implemented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * to return -1 to indicate there is no valid child index for any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     *
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 9035
diff changeset
   739
     * @param pos the position &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * @return  index of the view representing the given position, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     *   -1 if no view represents that position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
    public int getViewIndex(int pos, Position.Bias b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        return getViewIndexAtPosition(pos, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     * Provides a mapping from the document model coordinate space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * to the coordinate space of the view mapped to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     *
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 9035
diff changeset
   752
     * @param pos the position to convert &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * @param b the bias toward the previous character or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     *  next character represented by the offset, in case the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     *  position is a boundary of two views.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * @return the bounding box of the given position is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * @exception BadLocationException  if the given position does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     *   not represent a valid location in the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * @exception IllegalArgumentException for an invalid bias argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * @see View#viewToModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
    public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        int index = getViewIndex(pos, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        Shape ca = locator.getChildAllocation(index, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        // forward to the child view, and make sure we don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        // interact with the layout thread by synchronizing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        // on the child state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        ChildState cs = getChildState(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        synchronized (cs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            View cv = cs.getChildView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            Shape v = cv.modelToView(pos, ca, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            return v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     * Provides a mapping from the view coordinate space to the logical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * coordinate space of the model.  The biasReturn argument will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * filled in to indicate that the point given is closer to the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * character in the model or the previous character in the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * This is expected to be called by the GUI thread, holding a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * read-lock on the associated model.  It is implemented to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * locate the child view and determine it's allocation with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * lock on the ChildLocator object, and to call viewToModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * on the child view with a lock on the ChildState object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * to avoid interaction with the layout thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     *
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 9035
diff changeset
   791
     * @param x the X coordinate &gt;= 0
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 9035
diff changeset
   792
     * @param y the Y coordinate &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * @return the location within the model that best represents the
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 9035
diff changeset
   795
     *  given point in the view &gt;= 0.  The biasReturn argument will be
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * filled in to indicate that the point given is closer to the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * character in the model or the previous character in the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
    public int viewToModel(float x, float y, Shape a, Position.Bias[] biasReturn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        int pos;    // return position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        int index;  // child index to forward to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        Shape ca;   // child allocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        // locate the child view and it's allocation so that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        // we can forward to it.  Make sure the layout thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        // doesn't change anything by trying to flush changes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        // to the parent while the GUI thread is trying to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        // find the child and it's allocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        synchronized (locator) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            index = locator.getViewIndexAtPoint(x, y, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            ca = locator.getChildAllocation(index, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        // forward to the child view, and make sure we don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        // interact with the layout thread by synchronizing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        // on the child state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        ChildState cs = getChildState(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        synchronized (cs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            View v = cs.getChildView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            pos = v.viewToModel(x, y, ca, biasReturn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        return pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     * Provides a way to determine the next visually represented model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     * location that one might place a caret.  Some views may not be visible,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     * they might not be in the same order found in the model, or they just
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     * might not allow access to some of the locations in the model.
21256
dfb1f9090caa 7016396: (spec) JCK test mentioned in 6735293 is still failing
malenkov
parents: 20428
diff changeset
   830
     * This method enables specifying a position to convert
dfb1f9090caa 7016396: (spec) JCK test mentioned in 6735293 is still failing
malenkov
parents: 20428
diff changeset
   831
     * within the range of &gt;=0.  If the value is -1, a position
dfb1f9090caa 7016396: (spec) JCK test mentioned in 6735293 is still failing
malenkov
parents: 20428
diff changeset
   832
     * will be calculated automatically.  If the value &lt; -1,
dfb1f9090caa 7016396: (spec) JCK test mentioned in 6735293 is still failing
malenkov
parents: 20428
diff changeset
   833
     * the {@code BadLocationException} will be thrown.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     *
21256
dfb1f9090caa 7016396: (spec) JCK test mentioned in 6735293 is still failing
malenkov
parents: 20428
diff changeset
   835
     * @param pos the position to convert
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * @param direction the direction from the current position that can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     *  be thought of as the arrow keys typically found on a keyboard;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     *  this may be one of the following:
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 9035
diff changeset
   840
     *  <ul style="list-style-type:none">
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 9035
diff changeset
   841
     *  <li><code>SwingConstants.WEST</code></li>
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 9035
diff changeset
   842
     *  <li><code>SwingConstants.EAST</code></li>
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 9035
diff changeset
   843
     *  <li><code>SwingConstants.NORTH</code></li>
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 9035
diff changeset
   844
     *  <li><code>SwingConstants.SOUTH</code></li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     *  </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     * @param biasRet an array contain the bias that was checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     * @return the location within the model that best represents the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     *  location visual position
21256
dfb1f9090caa 7016396: (spec) JCK test mentioned in 6735293 is still failing
malenkov
parents: 20428
diff changeset
   849
     * @exception BadLocationException the given position is not a valid
dfb1f9090caa 7016396: (spec) JCK test mentioned in 6735293 is still failing
malenkov
parents: 20428
diff changeset
   850
     *                                 position within the document
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * @exception IllegalArgumentException if <code>direction</code> is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
    public int getNextVisualPositionFrom(int pos, Position.Bias b, Shape a,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                                         int direction,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                                         Position.Bias[] biasRet)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                                                  throws BadLocationException {
21256
dfb1f9090caa 7016396: (spec) JCK test mentioned in 6735293 is still failing
malenkov
parents: 20428
diff changeset
   857
        if (pos < -1) {
dfb1f9090caa 7016396: (spec) JCK test mentioned in 6735293 is still failing
malenkov
parents: 20428
diff changeset
   858
            throw new BadLocationException("invalid position", pos);
dfb1f9090caa 7016396: (spec) JCK test mentioned in 6735293 is still failing
malenkov
parents: 20428
diff changeset
   859
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        return Utilities.getNextVisualPositionFrom(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                            this, pos, b, a, direction, biasRet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
    // --- variables -----------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     * The major axis against which the children are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     * tiled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
    int axis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     * The children and their layout statistics.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     */
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   875
    List<ChildState> stats;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     * Current span along the major axis.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     * is also the value returned by getMinimumSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * getPreferredSize, and getMaximumSize along
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     * the major axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
    float majorSpan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * Is the span along the major axis estimated?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
    boolean estimatedMajorSpan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * Current span along the minor axis.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     * is what layout was done against (i.e. things
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * are flexible in this direction).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
    float minorSpan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     * Object that manages the offsets of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     * children.  All locking for management of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     * child locations is on this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
    protected ChildLocator locator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
    float topInset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
    float bottomInset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
    float leftInset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
    float rightInset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
    ChildState minRequest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
    ChildState prefRequest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
    boolean majorChanged;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
    boolean minorChanged;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
    Runnable flushTask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     * Child that is actively changing size.  This often
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     * causes a preferenceChanged, so this is a cache to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     * possibly speed up the marking the state.  It also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     * helps flag an opportunity to avoid adding to flush
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     * task to the layout queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
    ChildState changing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     * A class to manage the effective position of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     * child views in a localized area while changes are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     * being made around the localized area.  The AsyncBoxView
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     * may be continuously changing, but the visible area
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * needs to remain fairly stable until the layout thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     * decides to publish an update to the parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
    public class ChildLocator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
         * construct a child locator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        public ChildLocator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
            lastAlloc = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
            childAlloc = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
         * Notification that a child changed.  This can effect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
         * whether or not new offset calculations are needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
         * This is called by a ChildState object that has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
         * changed it's major span.  This can therefore be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
         * called by multiple threads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        public synchronized void childChanged(ChildState cs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
            if (lastValidOffset == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                lastValidOffset = cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
            } else if (cs.getChildView().getStartOffset() <
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
                       lastValidOffset.getChildView().getStartOffset()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                lastValidOffset = cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
         * Paint the children that intersect the clip area.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        public synchronized void paintChildren(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
            Rectangle clip = g.getClipBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
            float targetOffset = (axis == X_AXIS) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                clip.x - lastAlloc.x : clip.y - lastAlloc.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
            int index = getViewIndexAtVisualOffset(targetOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
            int n = getViewCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
            float offs = getChildState(index).getMajorOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
            for (int i = index; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
                ChildState cs = getChildState(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                cs.setMajorOffset(offs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                Shape ca = getChildAllocation(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                if (intersectsClip(ca, clip)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
                    synchronized (cs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                        View v = cs.getChildView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
                        v.paint(g, ca);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                    // done painting intersection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                offs += cs.getMajorSpan();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
         * Fetch the allocation to use for a child view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
         * This will update the offsets for all children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
         * not yet updated before the given index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        public synchronized Shape getChildAllocation(int index, Shape a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
            if (a == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
            setAllocation(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
            ChildState cs = getChildState(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
            if (lastValidOffset == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
                lastValidOffset = getChildState(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
            if (cs.getChildView().getStartOffset() >
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
                lastValidOffset.getChildView().getStartOffset()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
                // offsets need to be updated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
                updateChildOffsetsToIndex(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
            Shape ca = getChildAllocation(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
            return ca;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
         * Fetches the child view index at the given point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
         * This is called by the various View methods that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
         * need to calculate which child to forward a message
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
         * to.  This should be called by a block synchronized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
         * on this object, and would typically be followed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
         * with one or more calls to getChildAllocation that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
         * should also be in the synchronized block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
         *
20428
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 9035
diff changeset
  1018
         * @param x the X coordinate &gt;= 0
929cd48fca8a 8025249: [javadoc] fix some javadoc errors in javax/swing/
yan
parents: 9035
diff changeset
  1019
         * @param y the Y coordinate &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
         * @param a the allocation to the View
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
         * @return the nearest child index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        public int getViewIndexAtPoint(float x, float y, Shape a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
            setAllocation(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
            float targetOffset = (axis == X_AXIS) ? x - lastAlloc.x : y - lastAlloc.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
            int index = getViewIndexAtVisualOffset(targetOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
            return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
         * Fetch the allocation to use for a child view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
         * <em>This does not update the offsets in the ChildState
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
         * records.</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        protected Shape getChildAllocation(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
            ChildState cs = getChildState(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
            if (! cs.isLayoutValid()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
                cs.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
            if (axis == X_AXIS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
                childAlloc.x = lastAlloc.x + (int) cs.getMajorOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
                childAlloc.y = lastAlloc.y + (int) cs.getMinorOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
                childAlloc.width = (int) cs.getMajorSpan();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                childAlloc.height = (int) cs.getMinorSpan();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                childAlloc.y = lastAlloc.y + (int) cs.getMajorOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
                childAlloc.x = lastAlloc.x + (int) cs.getMinorOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
                childAlloc.height = (int) cs.getMajorSpan();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
                childAlloc.width = (int) cs.getMinorSpan();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
            childAlloc.x += (int)getLeftInset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
            childAlloc.y += (int)getRightInset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
            return childAlloc;
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
         * Copy the currently allocated shape into the Rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
         * used to store the current allocation.  This would be
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 21256
diff changeset
  1059
         * a floating point rectangle in a Java2D-specific implementation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        protected void setAllocation(Shape a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
            if (a instanceof Rectangle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
                lastAlloc.setBounds((Rectangle) a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
                lastAlloc.setBounds(a.getBounds());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
            setSize(lastAlloc.width, lastAlloc.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
         * Locate the view responsible for an offset into the box
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
         * along the major axis.  Make sure that offsets are set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
         * on the ChildState objects up to the given target span
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
         * past the desired offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
         * @return   index of the view representing the given visual
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
         *   location (targetOffset), or -1 if no view represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
         *   that location
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
        protected int getViewIndexAtVisualOffset(float targetOffset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
            int n = getViewCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
            if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
                boolean lastValid = (lastValidOffset != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
                if (lastValidOffset == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
                    lastValidOffset = getChildState(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
                if (targetOffset > majorSpan) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
                    // should only get here on the first time display.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
                    if (!lastValid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
                        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
                    int pos = lastValidOffset.getChildView().getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
                    int index = getViewIndex(pos, Position.Bias.Forward);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
                    return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
                } else if (targetOffset > lastValidOffset.getMajorOffset()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
                    // roll offset calculations forward
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
                    return updateChildOffsets(targetOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
                    // no changes prior to the needed offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
                    // this should be a binary search
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
                    float offs = 0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
                    for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
                        ChildState cs = getChildState(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
                        float nextOffs = offs + cs.getMajorSpan();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
                        if (targetOffset < nextOffs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
                            return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
                        offs = nextOffs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
            return n - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
         * Move the location of the last offset calculation forward
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
         * to the desired offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        int updateChildOffsets(float targetOffset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
            int n = getViewCount();
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  1122
            int targetIndex = n - 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
            int pos = lastValidOffset.getChildView().getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
            int startIndex = getViewIndex(pos, Position.Bias.Forward);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
            float start = lastValidOffset.getMajorOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
            float lastOffset = start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
            for (int i = startIndex; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
                ChildState cs = getChildState(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
                cs.setMajorOffset(lastOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
                lastOffset += cs.getMajorSpan();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
                if (targetOffset < lastOffset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
                    targetIndex = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
                    lastValidOffset = cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
            return targetIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
         * Move the location of the last offset calculation forward
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
         * to the desired index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
        void updateChildOffsetsToIndex(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
            int pos = lastValidOffset.getChildView().getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
            int startIndex = getViewIndex(pos, Position.Bias.Forward);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
            float lastOffset = lastValidOffset.getMajorOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
            for (int i = startIndex; i <= index; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
                ChildState cs = getChildState(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
                cs.setMajorOffset(lastOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
                lastOffset += cs.getMajorSpan();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
        boolean intersectsClip(Shape childAlloc, Rectangle clip) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
            Rectangle cs = (childAlloc instanceof Rectangle) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
                (Rectangle) childAlloc : childAlloc.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
            if (cs.intersects(clip)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
                // Make sure that lastAlloc also contains childAlloc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
                // this will be false if haven't yet flushed changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
                return lastAlloc.intersects(cs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
         * The location of the last offset calculation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
         * that is valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
        protected ChildState lastValidOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
         * The last seen allocation (for repainting when changes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
         * are flushed upward).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        protected Rectangle lastAlloc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
         * A shape to use for the child allocation to avoid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
         * creating a lot of garbage.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        protected Rectangle childAlloc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
     * A record representing the layout state of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
     * child view.  It is runnable as a task on another
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
     * thread.  All access to the child view that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     * based upon a read-lock on the model should synchronize
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     * on this object (i.e. The layout thread and the GUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
     * thread can both have a read lock on the model at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
     * same time and are not protected from each other).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
     * Access to a child view hierarchy is serialized via
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
     * synchronization on the ChildState instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
    public class ChildState implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
         * Construct a child status.  This needs to start
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
         * out as fairly large so we don't falsely begin with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
         * the idea that all of the children are visible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
         * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
        public ChildState(View v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
            child = v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
            minorValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
            majorValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
            childSizeValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
            child.setParent(AsyncBoxView.this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
         * Fetch the child view this record represents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
        public View getChildView() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
            return child;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
         * Update the child state.  This should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
         * called by the thread that desires to spend
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
         * time updating the child state (intended to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
         * be the layout thread).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
         * <p>
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 21256
diff changeset
  1227
         * This acquires a read lock on the associated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
         * document for the duration of the update to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
         * ensure the model is not changed while it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
         * operating.  The first thing to do would be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
         * to see if any work actually needs to be done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
         * The following could have conceivably happened
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
         * while the state was waiting to be updated:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
         * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
         * <li>The child may have been removed from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
         * view hierarchy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
         * <li>The child may have been updated by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
         * higher priority operation (i.e. the child
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
         * may have become visible).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
         * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
        public void run () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
            AbstractDocument doc = (AbstractDocument) getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
                doc.readLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
                if (minorValid && majorValid && childSizeValid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
                    // nothing to do
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
                if (child.getParent() == AsyncBoxView.this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
                    // this may overwrite anothers threads cached
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
                    // value for actively changing... but that just
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
                    // means it won't use the cache if there is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
                    // overwrite.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
                    synchronized(AsyncBoxView.this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
                        changing = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
                    updateChild();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
                    synchronized(AsyncBoxView.this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
                        changing = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
                    // setting the child size on the minor axis
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
                    // may have caused it to change it's preference
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
                    // along the major axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
                    updateChild();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
                doc.readUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
        void updateChild() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
            boolean minorUpdated = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
            synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
                if (! minorValid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
                    int minorAxis = getMinorAxis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
                    min = child.getMinimumSpan(minorAxis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
                    pref = child.getPreferredSpan(minorAxis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
                    max = child.getMaximumSpan(minorAxis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
                    minorValid = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
                    minorUpdated = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
            if (minorUpdated) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
                minorRequirementChange(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
            boolean majorUpdated = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
            float delta = 0.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
            synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
                if (! majorValid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
                    float old = span;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
                    span = child.getPreferredSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
                    delta = span - old;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
                    majorValid = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
                    majorUpdated = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
            if (majorUpdated) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
                majorRequirementChange(this, delta);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
                locator.childChanged(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
            synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
                if (! childSizeValid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
                    float w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
                    float h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
                    if (axis == X_AXIS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
                        w = span;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
                        h = getMinorSpan();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
                        w = getMinorSpan();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
                        h = span;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
                    childSizeValid = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
                    child.setSize(w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
         * What is the span along the minor axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
        public float getMinorSpan() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
            if (max < minorSpan) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
                return max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
            // make it the target width, or as small as it can get.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
            return Math.max(min, minorSpan);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
         * What is the offset along the minor axis
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
        public float getMinorOffset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
            if (max < minorSpan) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
                // can't make the child this wide, align it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
                float align = child.getAlignment(getMinorAxis());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
                return ((minorSpan - max) * align);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
            return 0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
         * What is the span along the major axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
        public float getMajorSpan() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
            return span;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
         * Get the offset along the major axis
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
        public float getMajorOffset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
            return offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
         * This method should only be called by the ChildLocator,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
         * it is simply a convenient place to hold the cached
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
         * location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
        public void setMajorOffset(float offs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
            offset = offs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
         * Mark preferences changed for this child.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
         * @param width true if the width preference has changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
         * @param height true if the height preference has changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
         * @see javax.swing.JComponent#revalidate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
        public void preferenceChanged(boolean width, boolean height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
            if (axis == X_AXIS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
                if (width) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
                    majorValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
                if (height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
                    minorValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
                if (width) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
                    minorValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
                if (height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
                    majorValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
            childSizeValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
         * Has the child view been laid out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
        public boolean isLayoutValid() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
            return (minorValid && majorValid && childSizeValid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
        // minor axis
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
        private float min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
        private float pref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
        private float max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
        private boolean minorValid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
        // major axis
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
        private float span;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
        private float offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
        private boolean majorValid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
        private View child;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
        private boolean childSizeValid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
     * Task to flush requirement changes upward
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
    class FlushTask implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
            flushRequirementChanges();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
}