0N/A/*
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 *
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 *
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 *
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.
0N/A *
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
2362N/A * questions.
0N/A */
0N/Apackage javax.swing.text;
0N/A
0N/Aimport java.util.*;
611N/Aimport java.util.List;
0N/Aimport java.awt.*;
0N/Aimport javax.swing.SwingUtilities;
0N/Aimport javax.swing.event.DocumentEvent;
0N/A
0N/A/**
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 * <p>
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 *
0N/A * @author Timothy Prinzing
0N/A * @since 1.3
0N/A */
0N/Apublic class AsyncBoxView extends View {
0N/A
0N/A /**
0N/A * Construct a box view that does asynchronous layout.
0N/A *
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 */
0N/A public AsyncBoxView(Element elem, int axis) {
0N/A super(elem);
611N/A stats = new ArrayList<ChildState>();
0N/A this.axis = axis;
0N/A locator = new ChildLocator();
0N/A flushTask = new FlushTask();
0N/A minorSpan = Short.MAX_VALUE;
0N/A estimatedMajorSpan = false;
0N/A }
0N/A
0N/A /**
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 */
0N/A public int getMajorAxis() {
0N/A return axis;
0N/A }
0N/A
0N/A /**
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 */
0N/A public int getMinorAxis() {
0N/A return (axis == X_AXIS) ? Y_AXIS : X_AXIS;
0N/A }
0N/A
0N/A /**
0N/A * Get the top part of the margin around the view.
0N/A */
0N/A public float getTopInset() {
0N/A return topInset;
0N/A }
0N/A
0N/A /**
0N/A * Set the top part of the margin around the view.
0N/A *
0N/A * @param i the value of the inset
0N/A */
0N/A public void setTopInset(float i) {
0N/A topInset = i;
0N/A }
0N/A
0N/A /**
0N/A * Get the bottom part of the margin around the view.
0N/A */
0N/A public float getBottomInset() {
0N/A return bottomInset;
0N/A }
0N/A
0N/A /**
0N/A * Set the bottom part of the margin around the view.
0N/A *
0N/A * @param i the value of the inset
0N/A */
0N/A public void setBottomInset(float i) {
0N/A bottomInset = i;
0N/A }
0N/A
0N/A /**
0N/A * Get the left part of the margin around the view.
0N/A */
0N/A public float getLeftInset() {
0N/A return leftInset;
0N/A }
0N/A
0N/A /**
0N/A * Set the left part of the margin around the view.
0N/A *
0N/A * @param i the value of the inset
0N/A */
0N/A public void setLeftInset(float i) {
0N/A leftInset = i;
0N/A }
0N/A
0N/A /**
0N/A * Get the right part of the margin around the view.
0N/A */
0N/A public float getRightInset() {
0N/A return rightInset;
0N/A }
0N/A
0N/A /**
0N/A * Set the right part of the margin around the view.
0N/A *
0N/A * @param i the value of the inset
0N/A */
0N/A public void setRightInset(float i) {
0N/A rightInset = i;
0N/A }
0N/A
0N/A /**
0N/A * Fetch the span along an axis that is taken up by the insets.
0N/A *
0N/A * @param axis the axis to determine the total insets along,
0N/A * either X_AXIS or Y_AXIS.
0N/A * @since 1.4
0N/A */
0N/A protected float getInsetSpan(int axis) {
0N/A float margin = (axis == X_AXIS) ?
0N/A getLeftInset() + getRightInset() : getTopInset() + getBottomInset();
0N/A return margin;
0N/A }
0N/A
0N/A /**
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 *
0N/A * @since 1.4
0N/A */
0N/A protected void setEstimatedMajorSpan(boolean isEstimated) {
0N/A estimatedMajorSpan = isEstimated;
0N/A }
0N/A
0N/A /**
0N/A * Is the major span currently estimated?
0N/A *
0N/A * @since 1.4
0N/A */
0N/A protected boolean getEstimatedMajorSpan() {
0N/A return estimatedMajorSpan;
0N/A }
0N/A
0N/A /**
0N/A * Fetch the object representing the layout state of
0N/A * of the child at the given index.
0N/A *
0N/A * @param index the child index. This should be a
0N/A * value >= 0 and < getViewCount().
0N/A */
0N/A protected ChildState getChildState(int index) {
0N/A synchronized(stats) {
0N/A if ((index >= 0) && (index < stats.size())) {
611N/A return stats.get(index);
0N/A }
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Fetch the queue to use for layout.
0N/A */
0N/A protected LayoutQueue getLayoutQueue() {
0N/A return LayoutQueue.getDefaultQueue();
0N/A }
0N/A
0N/A /**
0N/A * New ChildState records are created through
0N/A * this method to allow subclasses the extend
0N/A * the ChildState records to do/hold more
0N/A */
0N/A protected ChildState createChildState(View v) {
0N/A return new ChildState(v);
0N/A }
0N/A
0N/A /**
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 * model/view translation).
0N/A * <p>
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 */
0N/A protected synchronized void majorRequirementChange(ChildState cs, float delta) {
0N/A if (estimatedMajorSpan == false) {
0N/A majorSpan += delta;
0N/A }
0N/A majorChanged = true;
0N/A }
0N/A
0N/A /**
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 * model/view translation).
0N/A */
0N/A protected synchronized void minorRequirementChange(ChildState cs) {
0N/A minorChanged = true;
0N/A }
0N/A
0N/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 */
0N/A protected void flushRequirementChanges() {
0N/A AbstractDocument doc = (AbstractDocument) getDocument();
0N/A try {
0N/A doc.readLock();
0N/A
0N/A View parent = null;
0N/A boolean horizontal = false;
0N/A boolean vertical = false;
0N/A
0N/A synchronized(this) {
0N/A // perform tasks that iterate over the children while
0N/A // preventing the collection from changing.
0N/A synchronized(stats) {
0N/A int n = getViewCount();
0N/A if ((n > 0) && (minorChanged || estimatedMajorSpan)) {
0N/A LayoutQueue q = getLayoutQueue();
0N/A ChildState min = getChildState(0);
0N/A ChildState pref = getChildState(0);
0N/A float span = 0f;
0N/A for (int i = 1; i < n; i++) {
0N/A ChildState cs = getChildState(i);
0N/A if (minorChanged) {
0N/A if (cs.min > min.min) {
0N/A min = cs;
0N/A }
0N/A if (cs.pref > pref.pref) {
0N/A pref = cs;
0N/A }
0N/A }
0N/A if (estimatedMajorSpan) {
0N/A span += cs.getMajorSpan();
0N/A }
0N/A }
0N/A
0N/A if (minorChanged) {
0N/A minRequest = min;
0N/A prefRequest = pref;
0N/A }
0N/A if (estimatedMajorSpan) {
0N/A majorSpan = span;
0N/A estimatedMajorSpan = false;
0N/A majorChanged = true;
0N/A }
0N/A }
0N/A }
0N/A
0N/A // message preferenceChanged
0N/A if (majorChanged || minorChanged) {
0N/A parent = getParent();
0N/A if (parent != null) {
0N/A if (axis == X_AXIS) {
0N/A horizontal = majorChanged;
0N/A vertical = minorChanged;
0N/A } else {
0N/A vertical = majorChanged;
0N/A horizontal = minorChanged;
0N/A }
0N/A }
0N/A majorChanged = false;
0N/A minorChanged = false;
0N/A }
0N/A }
0N/A
0N/A // propagate a preferenceChanged, using the
0N/A // layout thread.
0N/A if (parent != null) {
0N/A parent.preferenceChanged(this, horizontal, vertical);
0N/A
0N/A // probably want to change this to be more exact.
0N/A Component c = getContainer();
0N/A if (c != null) {
0N/A c.repaint();
0N/A }
0N/A }
0N/A } finally {
0N/A doc.readUnlock();
0N/A }
0N/A }
0N/A
0N/A /**
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 *
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 */
0N/A public void replace(int offset, int length, View[] views) {
0N/A synchronized(stats) {
0N/A // remove the replaced state records
0N/A for (int i = 0; i < length; i++) {
611N/A ChildState cs = stats.remove(offset);
0N/A float csSpan = cs.getMajorSpan();
0N/A
0N/A cs.getChildView().setParent(null);
0N/A if (csSpan != 0) {
0N/A majorRequirementChange(cs, -csSpan);
0N/A }
0N/A }
0N/A
0N/A // insert the state records for the new children
0N/A LayoutQueue q = getLayoutQueue();
0N/A if (views != null) {
0N/A for (int i = 0; i < views.length; i++) {
0N/A ChildState s = createChildState(views[i]);
0N/A stats.add(offset + i, s);
0N/A q.addTask(s);
0N/A }
0N/A }
0N/A
0N/A // notify that the size changed
0N/A q.addTask(flushTask);
0N/A }
0N/A }
0N/A
0N/A /**
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 * child element.
0N/A * <p>
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 *
0N/A * @param f the view factory
0N/A * @see #setParent
0N/A */
0N/A protected void loadChildren(ViewFactory f) {
0N/A Element e = getElement();
0N/A int n = e.getElementCount();
0N/A if (n > 0) {
0N/A View[] added = new View[n];
0N/A for (int i = 0; i < n; i++) {
0N/A added[i] = f.create(e.getElement(i));
0N/A }
0N/A replace(0, 0, added);
0N/A }
0N/A }
0N/A
0N/A /**
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 *
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 */
0N/A protected synchronized int getViewIndexAtPosition(int pos, Position.Bias b) {
0N/A boolean isBackward = (b == Position.Bias.Backward);
0N/A pos = (isBackward) ? Math.max(0, pos - 1) : pos;
0N/A Element elem = getElement();
0N/A return elem.getElementIndex(pos);
0N/A }
0N/A
0N/A /**
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 *
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 */
0N/A protected void updateLayout(DocumentEvent.ElementChange ec,
0N/A DocumentEvent e, Shape a) {
0N/A if (ec != null) {
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 // changed size.
0N/A int index = Math.max(ec.getIndex() - 1, 0);
0N/A ChildState cs = getChildState(index);
0N/A locator.childChanged(cs);
0N/A }
0N/A }
0N/A
0N/A // --- View methods ------------------------------------
0N/A
0N/A /**
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 *
0N/A * @param parent the parent of the view, null if none
0N/A */
0N/A public void setParent(View parent) {
0N/A super.setParent(parent);
0N/A if ((parent != null) && (getViewCount() == 0)) {
0N/A ViewFactory f = getViewFactory();
0N/A loadChildren(f);
0N/A }
0N/A }
0N/A
0N/A /**
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 *
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 */
0N/A public synchronized void preferenceChanged(View child, boolean width, boolean height) {
0N/A if (child == null) {
0N/A getParent().preferenceChanged(this, width, height);
0N/A } else {
0N/A if (changing != null) {
0N/A View cv = changing.getChildView();
0N/A if (cv == child) {
0N/A // size was being changed on the child, no need to
0N/A // queue work for it.
0N/A changing.preferenceChanged(width, height);
0N/A return;
0N/A }
0N/A }
0N/A int index = getViewIndex(child.getStartOffset(),
0N/A Position.Bias.Forward);
0N/A ChildState cs = getChildState(index);
0N/A cs.preferenceChanged(width, height);
0N/A LayoutQueue q = getLayoutQueue();
0N/A q.addTask(cs);
0N/A q.addTask(flushTask);
0N/A }
0N/A }
0N/A
0N/A /**
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 * information.
0N/A * <p>
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 *
0N/A * @param width the width >= 0
0N/A * @param height the height >= 0
0N/A */
0N/A public void setSize(float width, float height) {
0N/A setSpanOnAxis(X_AXIS, width);
0N/A setSpanOnAxis(Y_AXIS, height);
0N/A }
0N/A
0N/A /**
0N/A * Retrieves the size of the view along an axis.
0N/A *
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 */
0N/A float getSpanOnAxis(int axis) {
0N/A if (axis == getMajorAxis()) {
0N/A return majorSpan;
0N/A }
0N/A return minorSpan;
0N/A }
0N/A
0N/A /**
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 *
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 */
0N/A void setSpanOnAxis(int axis, float span) {
0N/A float margin = getInsetSpan(axis);
0N/A if (axis == getMinorAxis()) {
0N/A float targetSpan = span - margin;
0N/A if (targetSpan != minorSpan) {
0N/A minorSpan = targetSpan;
0N/A
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 int n = getViewCount();
0N/A if (n != 0) {
0N/A LayoutQueue q = getLayoutQueue();
0N/A for (int i = 0; i < n; i++) {
0N/A ChildState cs = getChildState(i);
0N/A cs.childSizeValid = false;
0N/A q.addTask(cs);
0N/A }
0N/A q.addTask(flushTask);
0N/A }
0N/A }
0N/A } else {
0N/A // along the major axis the value is ignored
0N/A // unless the estimatedMajorSpan property is
0N/A // true.
0N/A if (estimatedMajorSpan) {
0N/A majorSpan = span - margin;
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Render the view using the given allocation and
0N/A * rendering surface.
0N/A * <p>
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 *
0N/A * @param g the rendering surface to use
0N/A * @param alloc the allocated region to render into
0N/A * @see View#paint
0N/A */
0N/A public void paint(Graphics g, Shape alloc) {
0N/A synchronized (locator) {
0N/A locator.setAllocation(alloc);
0N/A locator.paintChildren(g);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Determines the preferred span for this view along an
0N/A * axis.
0N/A *
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 */
0N/A public float getPreferredSpan(int axis) {
0N/A float margin = getInsetSpan(axis);
0N/A if (axis == this.axis) {
0N/A return majorSpan + margin;
0N/A }
0N/A if (prefRequest != null) {
0N/A View child = prefRequest.getChildView();
0N/A return child.getPreferredSpan(axis) + margin;
0N/A }
0N/A
0N/A // nothing is known about the children yet
0N/A return margin + 30;
0N/A }
0N/A
0N/A /**
0N/A * Determines the minimum span for this view along an
0N/A * axis.
0N/A *
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 */
0N/A public float getMinimumSpan(int axis) {
0N/A if (axis == this.axis) {
0N/A return getPreferredSpan(axis);
0N/A }
0N/A if (minRequest != null) {
0N/A View child = minRequest.getChildView();
0N/A return child.getMinimumSpan(axis);
0N/A }
0N/A
0N/A // nothing is known about the children yet
0N/A if (axis == X_AXIS) {
0N/A return getLeftInset() + getRightInset() + 5;
0N/A } else {
0N/A return getTopInset() + getBottomInset() + 5;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Determines the maximum span for this view along an
0N/A * axis.
0N/A *
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 */
0N/A public float getMaximumSpan(int axis) {
0N/A if (axis == this.axis) {
0N/A return getPreferredSpan(axis);
0N/A }
0N/A return Integer.MAX_VALUE;
0N/A }
0N/A
0N/A
0N/A /**
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 * returns 0.
0N/A *
0N/A * @return the number of views >= 0
0N/A * @see View#getViewCount
0N/A */
0N/A public int getViewCount() {
0N/A synchronized(stats) {
0N/A return stats.size();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Gets the nth child view. Since there are no
0N/A * children by default, this returns null.
0N/A *
0N/A * @param n the number of the view to get, >= 0 && < getViewCount()
0N/A * @return the view
0N/A */
0N/A public View getView(int n) {
0N/A ChildState cs = getChildState(n);
0N/A if (cs != null) {
0N/A return cs.getChildView();
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /**
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 *
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 */
0N/A public Shape getChildAllocation(int index, Shape a) {
0N/A Shape ca = locator.getChildAllocation(index, a);
0N/A return ca;
0N/A }
0N/A
0N/A /**
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 * position.
0N/A *
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 * @since 1.3
0N/A */
0N/A public int getViewIndex(int pos, Position.Bias b) {
0N/A return getViewIndexAtPosition(pos, b);
0N/A }
0N/A
0N/A /**
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 *
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 */
0N/A public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException {
0N/A int index = getViewIndex(pos, b);
0N/A Shape ca = locator.getChildAllocation(index, a);
0N/A
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 ChildState cs = getChildState(index);
0N/A synchronized (cs) {
0N/A View cv = cs.getChildView();
0N/A Shape v = cv.modelToView(pos, ca, b);
0N/A return v;
0N/A }
0N/A }
0N/A
0N/A /**
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 * <p>
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 *
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 */
0N/A public int viewToModel(float x, float y, Shape a, Position.Bias[] biasReturn) {
0N/A int pos; // return position
0N/A int index; // child index to forward to
0N/A Shape ca; // child allocation
0N/A
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 synchronized (locator) {
0N/A index = locator.getViewIndexAtPoint(x, y, a);
0N/A ca = locator.getChildAllocation(index, a);
0N/A }
0N/A
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 ChildState cs = getChildState(index);
0N/A synchronized (cs) {
0N/A View v = cs.getChildView();
0N/A pos = v.viewToModel(x, y, ca, biasReturn);
0N/A }
0N/A return pos;
0N/A }
0N/A
0N/A /**
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 *
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 * <ul>
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 * </ul>
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 */
0N/A public int getNextVisualPositionFrom(int pos, Position.Bias b, Shape a,
0N/A int direction,
0N/A Position.Bias[] biasRet)
0N/A throws BadLocationException {
0N/A return Utilities.getNextVisualPositionFrom(
0N/A this, pos, b, a, direction, biasRet);
0N/A }
0N/A
0N/A // --- variables -----------------------------------------
0N/A
0N/A /**
0N/A * The major axis against which the children are
0N/A * tiled.
0N/A */
0N/A int axis;
0N/A
0N/A /**
0N/A * The children and their layout statistics.
0N/A */
611N/A List<ChildState> stats;
0N/A
0N/A /**
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 * the major axis.
0N/A */
0N/A float majorSpan;
0N/A
0N/A /**
0N/A * Is the span along the major axis estimated?
0N/A */
0N/A boolean estimatedMajorSpan;
0N/A
0N/A /**
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 */
0N/A float minorSpan;
0N/A
0N/A /**
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 */
0N/A protected ChildLocator locator;
0N/A
0N/A float topInset;
0N/A float bottomInset;
0N/A float leftInset;
0N/A float rightInset;
0N/A
0N/A ChildState minRequest;
0N/A ChildState prefRequest;
0N/A boolean majorChanged;
0N/A boolean minorChanged;
0N/A Runnable flushTask;
0N/A
0N/A /**
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 */
0N/A ChildState changing;
0N/A
0N/A /**
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 * @since 1.3
0N/A */
0N/A public class ChildLocator {
0N/A
0N/A /**
0N/A * construct a child locator.
0N/A */
0N/A public ChildLocator() {
0N/A lastAlloc = new Rectangle();
0N/A childAlloc = new Rectangle();
0N/A }
0N/A
0N/A /**
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 */
0N/A public synchronized void childChanged(ChildState cs) {
0N/A if (lastValidOffset == null) {
0N/A lastValidOffset = cs;
0N/A } else if (cs.getChildView().getStartOffset() <
0N/A lastValidOffset.getChildView().getStartOffset()) {
0N/A lastValidOffset = cs;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Paint the children that intersect the clip area.
0N/A */
0N/A public synchronized void paintChildren(Graphics g) {
0N/A Rectangle clip = g.getClipBounds();
0N/A float targetOffset = (axis == X_AXIS) ?
0N/A clip.x - lastAlloc.x : clip.y - lastAlloc.y;
0N/A int index = getViewIndexAtVisualOffset(targetOffset);
0N/A int n = getViewCount();
0N/A float offs = getChildState(index).getMajorOffset();
0N/A for (int i = index; i < n; i++) {
0N/A ChildState cs = getChildState(i);
0N/A cs.setMajorOffset(offs);
0N/A Shape ca = getChildAllocation(i);
0N/A if (intersectsClip(ca, clip)) {
0N/A synchronized (cs) {
0N/A View v = cs.getChildView();
0N/A v.paint(g, ca);
0N/A }
0N/A } else {
0N/A // done painting intersection
0N/A break;
0N/A }
0N/A offs += cs.getMajorSpan();
0N/A }
0N/A }
0N/A
0N/A /**
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 */
0N/A public synchronized Shape getChildAllocation(int index, Shape a) {
0N/A if (a == null) {
0N/A return null;
0N/A }
0N/A setAllocation(a);
0N/A ChildState cs = getChildState(index);
0N/A if (lastValidOffset == null) {
0N/A lastValidOffset = getChildState(0);
0N/A }
0N/A if (cs.getChildView().getStartOffset() >
0N/A lastValidOffset.getChildView().getStartOffset()) {
0N/A // offsets need to be updated
0N/A updateChildOffsetsToIndex(index);
0N/A }
0N/A Shape ca = getChildAllocation(index);
0N/A return ca;
0N/A }
0N/A
0N/A /**
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 *
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 */
0N/A public int getViewIndexAtPoint(float x, float y, Shape a) {
0N/A setAllocation(a);
0N/A float targetOffset = (axis == X_AXIS) ? x - lastAlloc.x : y - lastAlloc.y;
0N/A int index = getViewIndexAtVisualOffset(targetOffset);
0N/A return index;
0N/A }
0N/A
0N/A /**
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 * records.</em>
0N/A */
0N/A protected Shape getChildAllocation(int index) {
0N/A ChildState cs = getChildState(index);
0N/A if (! cs.isLayoutValid()) {
0N/A cs.run();
0N/A }
0N/A if (axis == X_AXIS) {
0N/A childAlloc.x = lastAlloc.x + (int) cs.getMajorOffset();
0N/A childAlloc.y = lastAlloc.y + (int) cs.getMinorOffset();
0N/A childAlloc.width = (int) cs.getMajorSpan();
0N/A childAlloc.height = (int) cs.getMinorSpan();
0N/A } else {
0N/A childAlloc.y = lastAlloc.y + (int) cs.getMajorOffset();
0N/A childAlloc.x = lastAlloc.x + (int) cs.getMinorOffset();
0N/A childAlloc.height = (int) cs.getMajorSpan();
0N/A childAlloc.width = (int) cs.getMinorSpan();
0N/A }
0N/A childAlloc.x += (int)getLeftInset();
0N/A childAlloc.y += (int)getRightInset();
0N/A return childAlloc;
0N/A }
0N/A
0N/A /**
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 */
0N/A protected void setAllocation(Shape a) {
0N/A if (a instanceof Rectangle) {
0N/A lastAlloc.setBounds((Rectangle) a);
0N/A } else {
0N/A lastAlloc.setBounds(a.getBounds());
0N/A }
0N/A setSize(lastAlloc.width, lastAlloc.height);
0N/A }
0N/A
0N/A /**
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 *
0N/A * @return index of the view representing the given visual
0N/A * location (targetOffset), or -1 if no view represents
0N/A * that location
0N/A */
0N/A protected int getViewIndexAtVisualOffset(float targetOffset) {
0N/A int n = getViewCount();
0N/A if (n > 0) {
0N/A boolean lastValid = (lastValidOffset != null);
0N/A
0N/A if (lastValidOffset == null) {
0N/A lastValidOffset = getChildState(0);
0N/A }
0N/A if (targetOffset > majorSpan) {
0N/A // should only get here on the first time display.
0N/A if (!lastValid) {
0N/A return 0;
0N/A }
0N/A int pos = lastValidOffset.getChildView().getStartOffset();
0N/A int index = getViewIndex(pos, Position.Bias.Forward);
0N/A return index;
0N/A } else if (targetOffset > lastValidOffset.getMajorOffset()) {
0N/A // roll offset calculations forward
0N/A return updateChildOffsets(targetOffset);
0N/A } else {
0N/A // no changes prior to the needed offset
0N/A // this should be a binary search
0N/A float offs = 0f;
0N/A for (int i = 0; i < n; i++) {
0N/A ChildState cs = getChildState(i);
0N/A float nextOffs = offs + cs.getMajorSpan();
0N/A if (targetOffset < nextOffs) {
0N/A return i;
0N/A }
0N/A offs = nextOffs;
0N/A }
0N/A }
0N/A }
0N/A return n - 1;
0N/A }
0N/A
0N/A /**
0N/A * Move the location of the last offset calculation forward
0N/A * to the desired offset.
0N/A */
0N/A int updateChildOffsets(float targetOffset) {
0N/A int n = getViewCount();
611N/A int targetIndex = n - 1;
0N/A int pos = lastValidOffset.getChildView().getStartOffset();
0N/A int startIndex = getViewIndex(pos, Position.Bias.Forward);
0N/A float start = lastValidOffset.getMajorOffset();
0N/A float lastOffset = start;
0N/A for (int i = startIndex; i < n; i++) {
0N/A ChildState cs = getChildState(i);
0N/A cs.setMajorOffset(lastOffset);
0N/A lastOffset += cs.getMajorSpan();
0N/A if (targetOffset < lastOffset) {
0N/A targetIndex = i;
0N/A lastValidOffset = cs;
0N/A break;
0N/A }
0N/A }
0N/A
0N/A return targetIndex;
0N/A }
0N/A
0N/A /**
0N/A * Move the location of the last offset calculation forward
0N/A * to the desired index.
0N/A */
0N/A void updateChildOffsetsToIndex(int index) {
0N/A int pos = lastValidOffset.getChildView().getStartOffset();
0N/A int startIndex = getViewIndex(pos, Position.Bias.Forward);
0N/A float lastOffset = lastValidOffset.getMajorOffset();
0N/A for (int i = startIndex; i <= index; i++) {
0N/A ChildState cs = getChildState(i);
0N/A cs.setMajorOffset(lastOffset);
0N/A lastOffset += cs.getMajorSpan();
0N/A }
0N/A }
0N/A
0N/A boolean intersectsClip(Shape childAlloc, Rectangle clip) {
0N/A Rectangle cs = (childAlloc instanceof Rectangle) ?
0N/A (Rectangle) childAlloc : childAlloc.getBounds();
0N/A if (cs.intersects(clip)) {
0N/A // Make sure that lastAlloc also contains childAlloc,
0N/A // this will be false if haven't yet flushed changes.
0N/A return lastAlloc.intersects(cs);
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * The location of the last offset calculation
0N/A * that is valid.
0N/A */
0N/A protected ChildState lastValidOffset;
0N/A
0N/A /**
0N/A * The last seen allocation (for repainting when changes
0N/A * are flushed upward).
0N/A */
0N/A protected Rectangle lastAlloc;
0N/A
0N/A /**
0N/A * A shape to use for the child allocation to avoid
0N/A * creating a lot of garbage.
0N/A */
0N/A protected Rectangle childAlloc;
0N/A }
0N/A
0N/A /**
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 * @since 1.3
0N/A */
0N/A public class ChildState implements Runnable {
0N/A
0N/A /**
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 * @since 1.4
0N/A */
0N/A public ChildState(View v) {
0N/A child = v;
0N/A minorValid = false;
0N/A majorValid = false;
0N/A childSizeValid = false;
0N/A child.setParent(AsyncBoxView.this);
0N/A }
0N/A
0N/A /**
0N/A * Fetch the child view this record represents
0N/A */
0N/A public View getChildView() {
0N/A return child;
0N/A }
0N/A
0N/A /**
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 * <p>
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 * <ol>
0N/A * <li>The child may have been removed from the
0N/A * view hierarchy.
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 * </ol>
0N/A */
0N/A public void run () {
0N/A AbstractDocument doc = (AbstractDocument) getDocument();
0N/A try {
0N/A doc.readLock();
0N/A if (minorValid && majorValid && childSizeValid) {
0N/A // nothing to do
0N/A return;
0N/A }
0N/A if (child.getParent() == AsyncBoxView.this) {
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 // overwrite.
0N/A synchronized(AsyncBoxView.this) {
0N/A changing = this;
0N/A }
0N/A updateChild();
0N/A synchronized(AsyncBoxView.this) {
0N/A changing = null;
0N/A }
0N/A
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 updateChild();
0N/A }
0N/A } finally {
0N/A doc.readUnlock();
0N/A }
0N/A }
0N/A
0N/A void updateChild() {
0N/A boolean minorUpdated = false;
0N/A synchronized(this) {
0N/A if (! minorValid) {
0N/A int minorAxis = getMinorAxis();
0N/A min = child.getMinimumSpan(minorAxis);
0N/A pref = child.getPreferredSpan(minorAxis);
0N/A max = child.getMaximumSpan(minorAxis);
0N/A minorValid = true;
0N/A minorUpdated = true;
0N/A }
0N/A }
0N/A if (minorUpdated) {
0N/A minorRequirementChange(this);
0N/A }
0N/A
0N/A boolean majorUpdated = false;
0N/A float delta = 0.0f;
0N/A synchronized(this) {
0N/A if (! majorValid) {
0N/A float old = span;
0N/A span = child.getPreferredSpan(axis);
0N/A delta = span - old;
0N/A majorValid = true;
0N/A majorUpdated = true;
0N/A }
0N/A }
0N/A if (majorUpdated) {
0N/A majorRequirementChange(this, delta);
0N/A locator.childChanged(this);
0N/A }
0N/A
0N/A synchronized(this) {
0N/A if (! childSizeValid) {
0N/A float w;
0N/A float h;
0N/A if (axis == X_AXIS) {
0N/A w = span;
0N/A h = getMinorSpan();
0N/A } else {
0N/A w = getMinorSpan();
0N/A h = span;
0N/A }
0N/A childSizeValid = true;
0N/A child.setSize(w, h);
0N/A }
0N/A }
0N/A
0N/A }
0N/A
0N/A /**
0N/A * What is the span along the minor axis.
0N/A */
0N/A public float getMinorSpan() {
0N/A if (max < minorSpan) {
0N/A return max;
0N/A }
0N/A // make it the target width, or as small as it can get.
0N/A return Math.max(min, minorSpan);
0N/A }
0N/A
0N/A /**
0N/A * What is the offset along the minor axis
0N/A */
0N/A public float getMinorOffset() {
0N/A if (max < minorSpan) {
0N/A // can't make the child this wide, align it
0N/A float align = child.getAlignment(getMinorAxis());
0N/A return ((minorSpan - max) * align);
0N/A }
0N/A return 0f;
0N/A }
0N/A
0N/A /**
0N/A * What is the span along the major axis.
0N/A */
0N/A public float getMajorSpan() {
0N/A return span;
0N/A }
0N/A
0N/A /**
0N/A * Get the offset along the major axis
0N/A */
0N/A public float getMajorOffset() {
0N/A return offset;
0N/A }
0N/A
0N/A /**
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 * location.
0N/A */
0N/A public void setMajorOffset(float offs) {
0N/A offset = offs;
0N/A }
0N/A
0N/A /**
0N/A * Mark preferences changed for this child.
0N/A *
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 */
0N/A public void preferenceChanged(boolean width, boolean height) {
0N/A if (axis == X_AXIS) {
0N/A if (width) {
0N/A majorValid = false;
0N/A }
0N/A if (height) {
0N/A minorValid = false;
0N/A }
0N/A } else {
0N/A if (width) {
0N/A minorValid = false;
0N/A }
0N/A if (height) {
0N/A majorValid = false;
0N/A }
0N/A }
0N/A childSizeValid = false;
0N/A }
0N/A
0N/A /**
0N/A * Has the child view been laid out.
0N/A */
0N/A public boolean isLayoutValid() {
0N/A return (minorValid && majorValid && childSizeValid);
0N/A }
0N/A
0N/A // minor axis
0N/A private float min;
0N/A private float pref;
0N/A private float max;
0N/A private boolean minorValid;
0N/A
0N/A // major axis
0N/A private float span;
0N/A private float offset;
0N/A private boolean majorValid;
0N/A
0N/A private View child;
0N/A private boolean childSizeValid;
0N/A }
0N/A
0N/A /**
0N/A * Task to flush requirement changes upward
0N/A */
0N/A class FlushTask implements Runnable {
0N/A
0N/A public void run() {
0N/A flushRequirementChanges();
0N/A }
0N/A
0N/A }
0N/A
0N/A}