JavaTM 2 Platform
Std. Ed. v1.3.1

javax.swing.text
Class AsyncBoxView

java.lang.Object
  |
  +--javax.swing.text.View
        |
        +--javax.swing.text.AsyncBoxView
All Implemented Interfaces:
SwingConstants

public class AsyncBoxView
extends View

A box that does layout asynchronously. This is useful to keep the GUI event thread moving by not doing any layout on it. The layout is done on a granularity of operations on the child views. After each child view is accessed for some part of layout (a potentially time consuming operation) the remaining tasks can be abandoned or a new higher priority task (i.e. to service a synchronous request or a visible area) can be taken on.

While the child view is being accessed a read lock is aquired on the associated document so that the model is stable while being accessed.

Since:
1.3

Inner Class Summary
 class AsyncBoxView.ChildLocator
          A class to manage the effective position of the child views in a localized area while changes are being made around the localized area.
 class AsyncBoxView.ChildState
          A record representing the layout state of a child view.
 
Field Summary
protected  AsyncBoxView.ChildLocator locator
          Object that manages the offsets of the children.
 
Fields inherited from class javax.swing.text.View
BadBreakWeight, ExcellentBreakWeight, ForcedBreakWeight, GoodBreakWeight, X_AXIS, Y_AXIS
 
Fields inherited from interface javax.swing.SwingConstants
BOTTOM, CENTER, EAST, HORIZONTAL, LEADING, LEFT, NORTH, NORTH_EAST, NORTH_WEST, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TOP, TRAILING, VERTICAL, WEST
 
Constructor Summary
AsyncBoxView(Element elem, int axis)
          Construct a box view that does asynchronous layout.
 
Method Summary
protected  AsyncBoxView.ChildState createChildState(View v)
          New ChildState records are created through this method to allow subclasses the extend the ChildState records to do/hold more
protected  void flushRequirementChanges()
          Publish the changes in preferences upward to the parent view.
 float getBottomInset()
          Get the bottom part of the margin around the view.
 Shape getChildAllocation(int index, Shape a)
          Fetches the allocation for the given child view.
protected  AsyncBoxView.ChildState getChildState(int index)
          Fetch the object representing the layout state of of the child at the given index.
protected  LayoutQueue getLayoutQueue()
          Fetch the queue to use for layout.
 float getLeftInset()
          Get the left part of the margin around the view.
 int getMajorAxis()
          Fetch the major axis (the axis the children are tiled along).
 float getMaximumSpan(int axis)
          Determines the maximum span for this view along an axis.
 float getMinimumSpan(int axis)
          Determines the minimum span for this view along an axis.
 int getMinorAxis()
          Fetch the minor axis (the axis orthoginal to the tiled axis).
 float getPreferredSpan(int axis)
          Determines the preferred span for this view along an axis.
 float getRightInset()
          Get the right part of the margin around the view.
 float getTopInset()
          Get the top part of the margin around the view.
 View getView(int n)
          Gets the nth child view.
 int getViewCount()
          Returns the number of views in this view.
 int getViewIndex(int pos, Position.Bias b)
          Returns the child view index representing the given position in the model.
protected  int getViewIndexAtPosition(int pos, Position.Bias b)
          Fetches the child view index representing the given position in the model.
protected  void loadChildren(ViewFactory f)
          Loads all of the children to initialize the view.
protected  void majorRequirementChange(AsyncBoxView.ChildState cs, float delta)
          Requirements changed along the major axis.
protected  void minorRequirementChange(AsyncBoxView.ChildState cs)
          Requirements changed along the minor axis.
 Shape modelToView(int pos, Shape a, Position.Bias b)
          Provides a mapping from the document model coordinate space to the coordinate space of the view mapped to it.
 void paint(Graphics g, Shape alloc)
          Render the view using the given allocation and rendering surface.
 void preferenceChanged(View child, boolean width, boolean height)
          Child views can call this on the parent to indicate that the preference has changed and should be reconsidered for layout.
 void replace(int offset, int length, View[] views)
          Calls the superclass to update the child views, and updates the status records for the children.
 void setBottomInset(float i)
          Set the bottom part of the margin around the view.
 void setLeftInset(float i)
          Set the left part of the margin around the view.
 void setParent(View parent)
          Sets the parent of the view.
 void setRightInset(float i)
          Set the right part of the margin around the view.
 void setSize(float width, float height)
          Sets the size of the view.
 void setTopInset(float i)
          Set the top part of the margin around the view.
protected  void updateLayout(DocumentEvent.ElementChange ec, DocumentEvent e, Shape a)
          Update the layout in response to receiving notification of change from the model.
 int viewToModel(float x, float y, Shape a, Position.Bias[] biasReturn)
          Provides a mapping from the view coordinate space to the logical coordinate space of the model.
 
Methods inherited from class javax.swing.text.View
append, breakView, changedUpdate, createFragment, forwardUpdate, forwardUpdateToView, getAlignment, getAttributes, getBreakWeight, getContainer, getDocument, getElement, getEndOffset, getGraphics, getNextVisualPositionFrom, getParent, getResizeWeight, getStartOffset, getViewFactory, insert, insertUpdate, isVisible, modelToView, modelToView, remove, removeAll, removeUpdate, updateChildren, viewToModel
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

locator

protected AsyncBoxView.ChildLocator locator
Object that manages the offsets of the children. All locking for management of child locations is on this object.
Constructor Detail

AsyncBoxView

public AsyncBoxView(Element elem,
                    int axis)
Construct a box view that does asynchronous layout.
Parameters:
elem - the element of the model to represent
axis - the axis to tile along. This can be either X_AXIS or Y_AXIS.
Method Detail

getMajorAxis

public int getMajorAxis()
Fetch the major axis (the axis the children are tiled along). This will have a value of either X_AXIS or Y_AXIS.

getMinorAxis

public int getMinorAxis()
Fetch the minor axis (the axis orthoginal to the tiled axis). This will have a value of either X_AXIS or Y_AXIS.

getTopInset

public float getTopInset()
Get the top part of the margin around the view.

setTopInset

public void setTopInset(float i)
Set the top part of the margin around the view.
Parameters:
i - the value of the inset

getBottomInset

public float getBottomInset()
Get the bottom part of the margin around the view.

setBottomInset

public void setBottomInset(float i)
Set the bottom part of the margin around the view.
Parameters:
i - the value of the inset

getLeftInset

public float getLeftInset()
Get the left part of the margin around the view.

setLeftInset

public void setLeftInset(float i)
Set the left part of the margin around the view.
Parameters:
i - the value of the inset

getRightInset

public float getRightInset()
Get the right part of the margin around the view.

setRightInset

public void setRightInset(float i)
Set the right part of the margin around the view.
Parameters:
i - the value of the inset

getChildState

protected AsyncBoxView.ChildState getChildState(int index)
Fetch the object representing the layout state of of the child at the given index.
Parameters:
index - the child index. This should be a value >= 0 and < getViewCount().

getLayoutQueue

protected LayoutQueue getLayoutQueue()
Fetch the queue to use for layout.

createChildState

protected AsyncBoxView.ChildState createChildState(View v)
New ChildState records are created through this method to allow subclasses the extend the ChildState records to do/hold more

majorRequirementChange

protected void majorRequirementChange(AsyncBoxView.ChildState cs,
                                      float delta)
Requirements changed along the major axis. This is called by the thread doing layout for the given ChildState object when it has completed fetching the child views new preferences. Typically this would be the layout thread, but might be the GUI thread if it is trying to update something immediately (such as to perform a model/view translation).

minorRequirementChange

protected void minorRequirementChange(AsyncBoxView.ChildState cs)
Requirements changed along the minor axis. This is called by the thread doing layout for the given ChildState object when it has completed fetching the child views new preferences. Typically this would be the layout thread, but might be the GUI thread if it is trying to update something immediately (such as to perform a model/view translation).

flushRequirementChanges

protected void flushRequirementChanges()
Publish the changes in preferences upward to the parent view. This is called by the layout thread.

replace

public void replace(int offset,
                    int length,
                    View[] views)
Calls the superclass to update the child views, and updates the status records for the children. This is expected to be called while a write lock is held on the model so that interaction with the layout thread will not happen (i.e. the layout thread acquires a read lock before doing anything).
Overrides:
replace in class View
Parameters:
offset - the starting offset into the child views >= 0
length - the number of existing views to replace >= 0
views - the child views to insert

loadChildren

protected void loadChildren(ViewFactory f)
Loads all of the children to initialize the view. This is called by the setParent method. Subclasses can reimplement this to initialize their child views in a different manner. The default implementation creates a child view for each child element.

Normally a write-lock is held on the Document while the children are being changed, which keeps the rendering and layout threads safe. The exception to this is when the view is initialized to represent an existing element (via this method), so it is synchronized to exclude preferenceChanged while we are initializing.

Parameters:
f - the view factory
See Also:
setParent(javax.swing.text.View)

getViewIndexAtPosition

protected int getViewIndexAtPosition(int pos,
                                     Position.Bias b)
Fetches the child view index representing the given position in the model. This is implemented to fetch the view in the case where there is a child view for each child element.
Parameters:
pos - the position >= 0

updateLayout

protected void updateLayout(DocumentEvent.ElementChange ec,
                            DocumentEvent e,
                            Shape a)
Update the layout in response to receiving notification of change from the model. This is implemented to note the change on the ChildLocator so that offsets of the children will be correctly computed.
Overrides:
updateLayout in class View
Parameters:
ec - changes to the element this view is responsible for (may be null if there were no changes).
e - the change information from the associated document
a - the current allocation of the view
f - the factory to use to rebuild if the view has children
See Also:
View.insertUpdate(javax.swing.event.DocumentEvent, java.awt.Shape, javax.swing.text.ViewFactory), View.removeUpdate(javax.swing.event.DocumentEvent, java.awt.Shape, javax.swing.text.ViewFactory), View.changedUpdate(javax.swing.event.DocumentEvent, java.awt.Shape, javax.swing.text.ViewFactory)

setParent

public void setParent(View parent)
Sets the parent of the view. This is reimplemented to provide the superclass behavior as well as calling the loadChildren method if this view does not already have children. The children should not be loaded in the constructor because the act of setting the parent may cause them to try to search up the hierarchy (to get the hosting Container for example). If this view has children (the view is being moved from one place in the view hierarchy to another), the loadChildren method will not be called.
Overrides:
setParent in class View
Parameters:
parent - the parent of the view, null if none

preferenceChanged

public void preferenceChanged(View child,
                              boolean width,
                              boolean height)
Child views can call this on the parent to indicate that the preference has changed and should be reconsidered for layout. This is reimplemented to queue new work on the layout thread. This method gets messaged from multiple threads via the children.
Overrides:
preferenceChanged in class View
Parameters:
child - the child view
width - true if the width preference has changed
height - true if the height preference has changed
See Also:
JComponent.revalidate()

setSize

public void setSize(float width,
                    float height)
Sets the size of the view. This should cause layout of the view, if it has any layout duties.

This is implemented to check and see if there has been a change in the minor span (since the view is flexible along the minor axis). If there has been a change, this will add a high priorty task on the layout thread that will mark all of the ChildState records as needing to resize the child, and to spawn a bunch of low priority tasks to fixup the children.

This method will normally be called by the GUI event thread, which we don't want to slow down in any way if we can help it. Pushing the potentially time consuming task of marking each record frees the GUI thread, but also leaves the view open to paint attempts that can't be satisfied. The view is marked as resizing and the ResizeTask will turn off the flag when the children have all been marked.

Overrides:
setSize in class View
Parameters:
width - the width >= 0
height - the height >= 0

paint

public void paint(Graphics g,
                  Shape alloc)
Render the view using the given allocation and rendering surface.

This is implemented to determine whether or not the desired region to be rendered (i.e. the unclipped area) is up to date or not. If up-to-date the children are rendered. If not up-to-date, a task to build the desired area is placed on the layout queue as a high priority task. This keeps by event thread moving by rendering if ready, and postponing until a later time if not ready (since paint requests can be rescheduled).

Overrides:
paint in class View
Parameters:
g - the rendering surface to use
alloc - the allocated region to render into
See Also:
View.paint(java.awt.Graphics, java.awt.Shape)

getPreferredSpan

public float getPreferredSpan(int axis)
Determines the preferred span for this view along an axis.
Overrides:
getPreferredSpan in class View
Parameters:
axis - may be either View.X_AXIS or View.Y_AXIS
Throws:
IllegalArgumentException - for an invalid axis type

getMinimumSpan

public float getMinimumSpan(int axis)
Determines the minimum span for this view along an axis.
Overrides:
getMinimumSpan in class View
Parameters:
axis - may be either View.X_AXIS or View.Y_AXIS
Throws:
IllegalArgumentException - for an invalid axis type

getMaximumSpan

public float getMaximumSpan(int axis)
Determines the maximum span for this view along an axis.
Overrides:
getMaximumSpan in class View
Parameters:
axis - may be either View.X_AXIS or View.Y_AXIS
Throws:
IllegalArgumentException - for an invalid axis type

getViewCount

public int getViewCount()
Returns the number of views in this view. Since the default is to not be a composite view this returns 0.
Overrides:
getViewCount in class View
Returns:
the number of views >= 0
See Also:
View.getViewCount()

getView

public View getView(int n)
Gets the nth child view. Since there are no children by default, this returns null.
Overrides:
getView in class View
Parameters:
n - the number of the view to get, >= 0 && < getViewCount()
Returns:
the view

getChildAllocation

public Shape getChildAllocation(int index,
                                Shape a)
Fetches the allocation for the given child view. This enables finding out where various views are located, without assuming the views store their location. This returns null since the default is to not have any child views.
Overrides:
getChildAllocation in class View
Parameters:
index - the index of the child, >= 0 && < getViewCount()
a - the allocation to this view.
Returns:
the allocation to the child

getViewIndex

public int getViewIndex(int pos,
                        Position.Bias b)
Returns the child view index representing the given position in the model. By default a view has no children so this is implemented to return -1 to indicate there is no valid child index for any position.
Overrides:
getViewIndex in class View
Parameters:
pos - the position >= 0
Returns:
index of the view representing the given position, or -1 if no view represents that position
Since:
1.3

modelToView

public Shape modelToView(int pos,
                         Shape a,
                         Position.Bias b)
                  throws BadLocationException
Provides a mapping from the document model coordinate space to the coordinate space of the view mapped to it.
Overrides:
modelToView in class View
Parameters:
pos - the position to convert >= 0
a - the allocated region to render into
b - the bias toward the previous character or the next character represented by the offset, in case the position is a boundary of two views.
Returns:
the bounding box of the given position is returned
Throws:
BadLocationException - if the given position does not represent a valid location in the associated document
IllegalArgumentException - for an invalid bias argument
See Also:
View.viewToModel(float, float, java.awt.Shape, javax.swing.text.Position.Bias[])

viewToModel

public int viewToModel(float x,
                       float y,
                       Shape a,
                       Position.Bias[] biasReturn)
Provides a mapping from the view coordinate space to the logical coordinate space of the model. The biasReturn argument will be filled in to indicate that the point given is closer to the next character in the model or the previous character in the model.

This is expected to be called by the GUI thread, holding a read-lock on the associated model. It is implemented to locate the child view and determine it's allocation with a lock on the ChildLocator object, and to call viewToModel on the child view with a lock on the ChildState object to avoid interaction with the layout thread.

Overrides:
viewToModel in class View
Parameters:
x - the X coordinate >= 0
y - the Y coordinate >= 0
a - the allocated region to render into
Returns:
the location within the model that best represents the given point in the view >= 0. The biasReturn argument will be filled in to indicate that the point given is closer to the next character in the model or the previous character in the model.

JavaTM 2 Platform
Std. Ed. v1.3.1

Submit a bug or feature
For further API reference and developer documentation, see Java 2 SDK SE Developer Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.

Java, Java 2D, and JDBC are trademarks or registered trademarks of Oracle and/or its affiliates, in the US and other countries.
Copyright © 1995, 2010 Oracle and/or its affiliates. All rights reserved.