3909N/A * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 2362N/A * published by the Free Software Foundation. Oracle designates this 0N/A * particular file as subject to the "Classpath" exception as provided 2362N/A * by Oracle in the LICENSE file that accompanied this code. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2362N/A * or visit www.oracle.com if you need additional information or have any 0N/A * A box that does layout asynchronously. This 0N/A * is useful to keep the GUI event thread moving by 0N/A * not doing any layout on it. The layout is done 0N/A * on a granularity of operations on the child views. 0N/A * After each child view is accessed for some part 0N/A * of layout (a potentially time consuming operation) 0N/A * the remaining tasks can be abandoned or a new higher 0N/A * priority task (i.e. to service a synchronous request 0N/A * or a visible area) can be taken on. 0N/A * While the child view is being accessed 0N/A * a read lock is aquired on the associated document 0N/A * so that the model is stable while being accessed. 0N/A * @author Timothy Prinzing 0N/A * Construct a box view that does asynchronous layout. 0N/A * @param elem the element of the model to represent 0N/A * @param axis the axis to tile along. This can be 0N/A * either X_AXIS or Y_AXIS. 0N/A * Fetch the major axis (the axis the children 0N/A * are tiled along). This will have a value of 0N/A * either X_AXIS or Y_AXIS. 0N/A * Fetch the minor axis (the axis orthoginal 0N/A * to the tiled axis). This will have a value of 0N/A * either X_AXIS or Y_AXIS. 0N/A * Get the top part of the margin around the view. 0N/A * Set the top part of the margin around the view. 0N/A * @param i the value of the inset 0N/A * Get the bottom part of the margin around the view. 0N/A * Set the bottom part of the margin around the view. 0N/A * @param i the value of the inset 0N/A * Get the left part of the margin around the view. 0N/A * Set the left part of the margin around the view. 0N/A * @param i the value of the inset 0N/A * Get the right part of the margin around the view. 0N/A * Set the right part of the margin around the view. 0N/A * @param i the value of the inset 0N/A * Fetch the span along an axis that is taken up by the insets. 0N/A * @param axis the axis to determine the total insets along, 0N/A * either X_AXIS or Y_AXIS. 0N/A * Set the estimatedMajorSpan property that determines if the 0N/A * major span should be treated as being estimated. If this 0N/A * property is true, the value of setSize along the major axis 0N/A * will change the requirements along the major axis and incremental 0N/A * changes will be ignored until all of the children have been updated 0N/A * (which will cause the property to automatically be set to false). 0N/A * If the property is false the value of the majorSpan will be 0N/A * considered to be accurate and incremental changes will be 0N/A * added into the total as they are calculated. 0N/A * Is the major span currently estimated? 0N/A * Fetch the object representing the layout state of 0N/A * of the child at the given index. 0N/A * @param index the child index. This should be a 0N/A * value >= 0 and < getViewCount(). 0N/A * Fetch the queue to use for layout. 0N/A * New ChildState records are created through 0N/A * this method to allow subclasses the extend 0N/A * Requirements changed along the major axis. 0N/A * This is called by the thread doing layout for 0N/A * the given ChildState object when it has completed 0N/A * fetching the child views new preferences. 0N/A * Typically this would be the layout thread, but 0N/A * might be the event thread if it is trying to update 0N/A * something immediately (such as to perform a 0N/A * This is implemented to mark the major axis as having 0N/A * changed so that a future check to see if the requirements 0N/A * need to be published to the parent view will consider 0N/A * the major axis. If the span along the major axis is 0N/A * not estimated, it is updated by the given delta to reflect 0N/A * the incremental change. The delta is ignored if the 0N/A * major span is estimated. 0N/A * Requirements changed along the minor axis. 0N/A * This is called by the thread doing layout for 0N/A * the given ChildState object when it has completed 0N/A * fetching the child views new preferences. 0N/A * Typically this would be the layout thread, but 0N/A * might be the GUI thread if it is trying to update 0N/A * something immediately (such as to perform a 0N/A * Publish the changes in preferences upward to the parent 0N/A * view. This is normally called by the layout thread. 0N/A synchronized(
this) {
0N/A // perform tasks that iterate over the children while 0N/A // preventing the collection from changing. 0N/A for (
int i =
1; i < n; i++) {
0N/A // message preferenceChanged 0N/A // propagate a preferenceChanged, using the 0N/A // probably want to change this to be more exact. 0N/A * Calls the superclass to update the child views, and 0N/A * updates the status records for the children. This 0N/A * is expected to be called while a write lock is held 0N/A * on the model so that interaction with the layout 0N/A * thread will not happen (i.e. the layout thread 0N/A * acquires a read lock before doing anything). 0N/A * @param offset the starting offset into the child views >= 0 0N/A * @param length the number of existing views to replace >= 0 0N/A * @param views the child views to insert 0N/A // remove the replaced state records 0N/A // insert the state records for the new children 0N/A // notify that the size changed 0N/A * Loads all of the children to initialize the view. 3370N/A * This is called by the {@link #setParent setParent} 0N/A * method. Subclasses can reimplement this to initialize 0N/A * their child views in a different manner. The default 0N/A * implementation creates a child view for each 0N/A * Normally a write-lock is held on the Document while 0N/A * the children are being changed, which keeps the rendering 0N/A * and layout threads safe. The exception to this is when 0N/A * the view is initialized to represent an existing element 0N/A * (via this method), so it is synchronized to exclude 0N/A * preferenceChanged while we are initializing. 0N/A * @param f the view factory 0N/A for (
int i =
0; i < n; i++) {
0N/A * Fetches the child view index representing the given position in 0N/A * the model. This is implemented to fetch the view in the case 0N/A * where there is a child view for each child element. 0N/A * @param pos the position >= 0 0N/A * @return index of the view representing the given position, or 0N/A * -1 if no view represents that position 0N/A * Update the layout in response to receiving notification of 0N/A * change from the model. This is implemented to note the 0N/A * change on the ChildLocator so that offsets of the children 0N/A * will be correctly computed. 0N/A * @param ec changes to the element this view is responsible 0N/A * for (may be null if there were no changes). 0N/A * @param e the change information from the associated document 0N/A * @param a the current allocation of the view 0N/A * @see #insertUpdate 0N/A * @see #removeUpdate 0N/A * @see #changedUpdate 0N/A // the newly inserted children don't have a valid 0N/A // offset so the child locator needs to be messaged 0N/A // that the child prior to the new children has 0N/A // --- View methods ------------------------------------ 0N/A * Sets the parent of the view. 0N/A * This is reimplemented to provide the superclass 0N/A * behavior as well as calling the <code>loadChildren</code> 0N/A * method if this view does not already have children. 0N/A * The children should not be loaded in the 0N/A * constructor because the act of setting the parent 0N/A * may cause them to try to search up the hierarchy 0N/A * (to get the hosting Container for example). 0N/A * If this view has children (the view is being moved 0N/A * from one place in the view hierarchy to another), 0N/A * the <code>loadChildren</code> method will not be called. 0N/A * @param parent the parent of the view, null if none 0N/A * Child views can call this on the parent to indicate that 0N/A * the preference has changed and should be reconsidered 0N/A * for layout. This is reimplemented to queue new work 0N/A * on the layout thread. This method gets messaged from 0N/A * multiple threads via the children. 0N/A * @param child the child view 0N/A * @param width true if the width preference has changed 0N/A * @param height true if the height preference has changed 0N/A * @see javax.swing.JComponent#revalidate 0N/A // size was being changed on the child, no need to 0N/A // queue work for it. 0N/A * Sets the size of the view. This should cause 0N/A * layout of the view if the view caches any layout 0N/A * Since the major axis is updated asynchronously and should be 0N/A * the sum of the tiled children the call is ignored for the major 0N/A * axis. Since the minor axis is flexible, work is queued to resize 0N/A * the children if the minor span changes. 0N/A * @param width the width >= 0 0N/A * @param height the height >= 0 0N/A * Retrieves the size of the view along an axis. 0N/A * @param axis may be either <code>View.X_AXIS</code> or 0N/A * <code>View.Y_AXIS</code> 0N/A * @return the current span of the view along the given axis, >= 0 0N/A * Sets the size of the view along an axis. Since the major 0N/A * axis is updated asynchronously and should be the sum of the 0N/A * tiled children the call is ignored for the major axis. Since 0N/A * the minor axis is flexible, work is queued to resize the 0N/A * children if the minor span changes. 0N/A * @param axis may be either <code>View.X_AXIS</code> or 0N/A * <code>View.Y_AXIS</code> 0N/A * @param span the span to layout to >= 0 0N/A // mark all of the ChildState instances as needing to 0N/A // resize the child, and queue up work to fix them. 0N/A for (
int i =
0; i < n; i++) {
0N/A // along the major axis the value is ignored 0N/A // unless the estimatedMajorSpan property is 0N/A * Render the view using the given allocation and 0N/A * rendering surface. 0N/A * This is implemented to determine whether or not the 0N/A * desired region to be rendered (i.e. the unclipped 0N/A * area) is up to date or not. If up-to-date the children 0N/A * are rendered. If not up-to-date, a task to build 0N/A * the desired area is placed on the layout queue as 0N/A * a high priority task. This keeps by event thread 0N/A * moving by rendering if ready, and postponing until 0N/A * a later time if not ready (since paint requests 0N/A * can be rescheduled). 0N/A * @param g the rendering surface to use 0N/A * @param alloc the allocated region to render into 0N/A * Determines the preferred span for this view along an 0N/A * @param axis may be either View.X_AXIS or View.Y_AXIS 0N/A * @return the span the view would like to be rendered into >= 0. 0N/A * Typically the view is told to render into the span 0N/A * that is returned, although there is no guarantee. 0N/A * The parent may choose to resize or break the view. 0N/A * @exception IllegalArgumentException for an invalid axis type 0N/A // nothing is known about the children yet 0N/A * Determines the minimum span for this view along an 0N/A * @param axis may be either View.X_AXIS or View.Y_AXIS 0N/A * @return the span the view would like to be rendered into >= 0. 0N/A * Typically the view is told to render into the span 0N/A * that is returned, although there is no guarantee. 0N/A * The parent may choose to resize or break the view. 0N/A * @exception IllegalArgumentException for an invalid axis type 0N/A // nothing is known about the children yet 0N/A * Determines the maximum span for this view along an 0N/A * @param axis may be either View.X_AXIS or View.Y_AXIS 0N/A * @return the span the view would like to be rendered into >= 0. 0N/A * Typically the view is told to render into the span 0N/A * that is returned, although there is no guarantee. 0N/A * The parent may choose to resize or break the view. 0N/A * @exception IllegalArgumentException for an invalid axis type 0N/A * Returns the number of views in this view. Since 0N/A * the default is to not be a composite view this 0N/A * @return the number of views >= 0 0N/A * @see View#getViewCount 0N/A * Gets the nth child view. Since there are no 0N/A * children by default, this returns null. 0N/A * @param n the number of the view to get, >= 0 && < getViewCount() 0N/A * Fetches the allocation for the given child view. 0N/A * This enables finding out where various views 0N/A * are located, without assuming the views store 0N/A * their location. This returns null since the 0N/A * default is to not have any child views. 0N/A * @param index the index of the child, >= 0 && < getViewCount() 0N/A * @param a the allocation to this view. 0N/A * @return the allocation to the child 0N/A * Returns the child view index representing the given position in 0N/A * the model. By default a view has no children so this is implemented 0N/A * to return -1 to indicate there is no valid child index for any 0N/A * @param pos the position >= 0 0N/A * @return index of the view representing the given position, or 0N/A * -1 if no view represents that position 0N/A * Provides a mapping from the document model coordinate space 0N/A * to the coordinate space of the view mapped to it. 0N/A * @param pos the position to convert >= 0 0N/A * @param a the allocated region to render into 0N/A * @param b the bias toward the previous character or the 0N/A * next character represented by the offset, in case the 0N/A * position is a boundary of two views. 0N/A * @return the bounding box of the given position is returned 0N/A * @exception BadLocationException if the given position does 0N/A * not represent a valid location in the associated document 0N/A * @exception IllegalArgumentException for an invalid bias argument 0N/A * @see View#viewToModel 0N/A // forward to the child view, and make sure we don't 0N/A // interact with the layout thread by synchronizing 0N/A // on the child state. 0N/A * Provides a mapping from the view coordinate space to the logical 0N/A * coordinate space of the model. The biasReturn argument will be 0N/A * filled in to indicate that the point given is closer to the next 0N/A * character in the model or the previous character in the model. 0N/A * This is expected to be called by the GUI thread, holding a 0N/A * read-lock on the associated model. It is implemented to 0N/A * locate the child view and determine it's allocation with a 0N/A * lock on the ChildLocator object, and to call viewToModel 0N/A * on the child view with a lock on the ChildState object 0N/A * to avoid interaction with the layout thread. 0N/A * @param x the X coordinate >= 0 0N/A * @param y the Y coordinate >= 0 0N/A * @param a the allocated region to render into 0N/A * @return the location within the model that best represents the 0N/A * given point in the view >= 0. The biasReturn argument will be 0N/A * filled in to indicate that the point given is closer to the next 0N/A * character in the model or the previous character in the model. 0N/A // locate the child view and it's allocation so that 0N/A // we can forward to it. Make sure the layout thread 0N/A // doesn't change anything by trying to flush changes 0N/A // to the parent while the GUI thread is trying to 0N/A // find the child and it's allocation. 0N/A // forward to the child view, and make sure we don't 0N/A // interact with the layout thread by synchronizing 0N/A // on the child state. 0N/A * Provides a way to determine the next visually represented model 0N/A * location that one might place a caret. Some views may not be visible, 0N/A * they might not be in the same order found in the model, or they just 0N/A * might not allow access to some of the locations in the model. 0N/A * @param pos the position to convert >= 0 0N/A * @param a the allocated region to render into 0N/A * @param direction the direction from the current position that can 0N/A * be thought of as the arrow keys typically found on a keyboard; 0N/A * this may be one of the following: 0N/A * <code>SwingConstants.WEST</code> 0N/A * <code>SwingConstants.EAST</code> 0N/A * <code>SwingConstants.NORTH</code> 0N/A * <code>SwingConstants.SOUTH</code> 0N/A * @param biasRet an array contain the bias that was checked 0N/A * @return the location within the model that best represents the next 0N/A * location visual position 0N/A * @exception BadLocationException 0N/A * @exception IllegalArgumentException if <code>direction</code> is invalid 0N/A // --- variables ----------------------------------------- 0N/A * The major axis against which the children are 0N/A * The children and their layout statistics. 0N/A * Current span along the major axis. This 0N/A * is also the value returned by getMinimumSize, 0N/A * getPreferredSize, and getMaximumSize along 0N/A * Is the span along the major axis estimated? 0N/A * Current span along the minor axis. This 0N/A * is what layout was done against (i.e. things 0N/A * are flexible in this direction). 0N/A * Object that manages the offsets of the 0N/A * children. All locking for management of 0N/A * child locations is on this object. 0N/A * Child that is actively changing size. This often 0N/A * causes a preferenceChanged, so this is a cache to 0N/A * possibly speed up the marking the state. It also 0N/A * helps flag an opportunity to avoid adding to flush 0N/A * task to the layout queue. 0N/A * A class to manage the effective position of the 0N/A * child views in a localized area while changes are 0N/A * being made around the localized area. The AsyncBoxView 0N/A * may be continuously changing, but the visible area 0N/A * needs to remain fairly stable until the layout thread 0N/A * decides to publish an update to the parent. 0N/A * construct a child locator. 0N/A * Notification that a child changed. This can effect 0N/A * whether or not new offset calculations are needed. 0N/A * This is called by a ChildState object that has 0N/A * changed it's major span. This can therefore be 0N/A * called by multiple threads. 0N/A * Paint the children that intersect the clip area. 0N/A // done painting intersection 0N/A * Fetch the allocation to use for a child view. 0N/A * This will update the offsets for all children 0N/A * not yet updated before the given index. 0N/A // offsets need to be updated 0N/A * Fetches the child view index at the given point. 0N/A * This is called by the various View methods that 0N/A * need to calculate which child to forward a message 0N/A * to. This should be called by a block synchronized 0N/A * on this object, and would typically be followed 0N/A * with one or more calls to getChildAllocation that 0N/A * should also be in the synchronized block. 0N/A * @param x the X coordinate >= 0 0N/A * @param y the Y coordinate >= 0 0N/A * @param a the allocation to the View 0N/A * @return the nearest child index 0N/A * Fetch the allocation to use for a child view. 0N/A * <em>This does not update the offsets in the ChildState 0N/A * Copy the currently allocated shape into the Rectangle 0N/A * used to store the current allocation. This would be 0N/A * a floating point rectangle in a Java2D-specific implmentation. 0N/A * Locate the view responsible for an offset into the box 0N/A * along the major axis. Make sure that offsets are set 0N/A * on the ChildState objects up to the given target span 0N/A * past the desired offset. 0N/A * @return index of the view representing the given visual 0N/A * location (targetOffset), or -1 if no view represents 0N/A // should only get here on the first time display. 0N/A // roll offset calculations forward 0N/A // no changes prior to the needed offset 0N/A // this should be a binary search 0N/A for (
int i =
0; i < n; i++) {
0N/A * Move the location of the last offset calculation forward 0N/A * to the desired offset. 0N/A * Move the location of the last offset calculation forward 0N/A * to the desired index. 0N/A // Make sure that lastAlloc also contains childAlloc, 0N/A // this will be false if haven't yet flushed changes. 0N/A * The location of the last offset calculation 0N/A * The last seen allocation (for repainting when changes 0N/A * are flushed upward). 0N/A * A shape to use for the child allocation to avoid 0N/A * creating a lot of garbage. 0N/A * A record representing the layout state of a 0N/A * child view. It is runnable as a task on another 0N/A * thread. All access to the child view that is 0N/A * based upon a read-lock on the model should synchronize 0N/A * on this object (i.e. The layout thread and the GUI 0N/A * thread can both have a read lock on the model at the 0N/A * same time and are not protected from each other). 0N/A * Access to a child view hierarchy is serialized via 0N/A * synchronization on the ChildState instance. 0N/A * Construct a child status. This needs to start 0N/A * out as fairly large so we don't falsely begin with 0N/A * the idea that all of the children are visible. 0N/A * Fetch the child view this record represents 0N/A * Update the child state. This should be 0N/A * called by the thread that desires to spend 0N/A * time updating the child state (intended to 0N/A * be the layout thread). 0N/A * This aquires a read lock on the associated 0N/A * document for the duration of the update to 0N/A * ensure the model is not changed while it is 0N/A * operating. The first thing to do would be 0N/A * to see if any work actually needs to be done. 0N/A * The following could have conceivably happened 0N/A * while the state was waiting to be updated: 0N/A * <li>The child may have been removed from the 0N/A * <li>The child may have been updated by a 0N/A * higher priority operation (i.e. the child 0N/A * may have become visible). 0N/A // this may overwrite anothers threads cached 0N/A // value for actively changing... but that just 0N/A // means it won't use the cache if there is an 0N/A // setting the child size on the minor axis 0N/A // may have caused it to change it's preference 0N/A // along the major axis. 0N/A synchronized(
this) {
0N/A synchronized(
this) {
0N/A synchronized(
this) {
0N/A * What is the span along the minor axis. 0N/A // make it the target width, or as small as it can get. 0N/A * What is the offset along the minor axis 0N/A // can't make the child this wide, align it 0N/A * What is the span along the major axis. 0N/A * Get the offset along the major axis 0N/A * This method should only be called by the ChildLocator, 0N/A * it is simply a convenient place to hold the cached 0N/A * Mark preferences changed for this child. 0N/A * @param width true if the width preference has changed 0N/A * @param height true if the height preference has changed 0N/A * @see javax.swing.JComponent#revalidate 0N/A * Has the child view been laid out. 0N/A * Task to flush requirement changes upward