javax.swing.text

Class AsyncBoxView

public class AsyncBoxView extends View

A {@link View} implementation that lays out its child views in a box, either vertically or horizontally. The difference to {@link BoxView} is that the layout is performed in an asynchronous manner. This helps to keep the eventqueue free from non-GUI related tasks. This view is currently not used in standard text components. In order to use it you would have to implement a special {@link EditorKit} with a {@link ViewFactory} that returns this view. For example:
 static class AsyncEditorKit extends StyledEditorKit implements ViewFactory
 {
   public View create(Element el)
   {
     if (el.getName().equals(AbstractDocument.SectionElementName))
       return new AsyncBoxView(el, View.Y_AXIS);
     return super.getViewFactory().create(el);
   }
   public ViewFactory getViewFactory() {
     return this;
   }
 }
 

Since: 1.3

Nested Class Summary
classAsyncBoxView.ChildLocator
Manages the effective position of child views.
classAsyncBoxView.ChildState
Represents the layout state of a child view.
Field Summary
protected AsyncBoxView.ChildLocatorlocator
The child locator for this view.
Constructor Summary
AsyncBoxView(Element elem, int axis)
Creates a new AsyncBoxView that represents the specified element and layouts its children along the specified axis.
Method Summary
protected AsyncBoxView.ChildStatecreateChildState(View v)
Creates a ChildState object for the specified view.
protected voidflushRequirementChanges()
Flushes the requirements changes upwards to the parent view.
floatgetBottomInset()
Returns the bottom inset.
ShapegetChildAllocation(int index, Shape a)
Returns the child allocation for the child view with the specified index.
protected AsyncBoxView.ChildStategetChildState(int index)
Returns the ChildState object associated with the child view at the specified index.
protected booleangetEstimatedMajorSpan()
Determines whether the major span should be treated as estimated or as beeing accurate.
protected floatgetInsetSpan(int axis)
Returns the span along an axis that is taken up by the insets.
protected LayoutQueuegetLayoutQueue()
Returns the LayoutQueue used for layouting the box view.
floatgetLeftInset()
Returns the left inset.
intgetMajorAxis()
Returns the major layout axis.
floatgetMaximumSpan(int axis)
Returns the maximum span of this view along the specified axis.
floatgetMinimumSpan(int axis)
Returns the minimum span along the specified axis.
intgetMinorAxis()
Returns the minor layout axis, that is the axis orthogonal to the major layout axis.
floatgetPreferredSpan(int axis)
Returns the preferred span of this view along the specified layout axis.
floatgetRightInset()
Returns the right inset.
floatgetTopInset()
Returns the top inset.
ViewgetView(int index)
Returns the view at the specified index.
intgetViewCount()
Returns the number of child views.
intgetViewIndex(int pos, Position.Bias bias)
Returns the view index of the child view that represents the specified model position.
protected intgetViewIndexAtPosition(int pos, Position.Bias b)
Returns the child view index of the view that represents the specified position in the document model.
protected voidloadChildren(ViewFactory f)
Loads the child views of this view.
protected voidmajorRequirementChange(AsyncBoxView.ChildState cs, float delta)
Receives notification from the child states that the requirements along the major axis have changed.
protected voidminorRequirementChange(AsyncBoxView.ChildState cs)
Receives notification from the child states that the requirements along the minor axis have changed.
ShapemodelToView(int pos, Shape a, Position.Bias b)
Maps a model location to view coordinates.
voidpaint(Graphics g, Shape s)
Paints the view.
voidpreferenceChanged(View view, boolean width, boolean height)
Receives notification that one of the child views has changed its layout preferences along one or both axis.
voidreplace(int offset, int length, View[] views)
Replaces child views with new child views.
voidsetBottomInset(float bottom)
Sets the bottom inset.
protected voidsetEstimatedMajorSpan(boolean estimated)
Sets the estimatedMajorSpan property that determines if the major span should be treated as beeing estimated.
voidsetLeftInset(float left)
Sets the left inset.
voidsetParent(View parent)
Sets the parent for this view.
voidsetRightInset(float right)
Sets the right inset.
voidsetSize(float width, float height)
Sets the size of this view.
voidsetTopInset(float top)
Sets the top inset.
protected voidupdateLayout(DocumentEvent.ElementChange ec, DocumentEvent e, Shape a)
Updates the layout for this view.
intviewToModel(float x, float y, Shape a, Position.Bias[] b)
Maps view coordinates to a model location.

Field Detail

locator

protected AsyncBoxView.ChildLocator locator
The child locator for this view.

Constructor Detail

AsyncBoxView

public AsyncBoxView(Element elem, int axis)
Creates a new AsyncBoxView that represents the specified element and layouts its children along the specified axis.

Parameters: elem the element axis the layout axis

Method Detail

createChildState

protected AsyncBoxView.ChildState createChildState(View v)
Creates a ChildState object for the specified view.

Parameters: v the view for which to create a child state object

Returns: the created child state

flushRequirementChanges

protected void flushRequirementChanges()
Flushes the requirements changes upwards to the parent view. This is called from the layout thread.

getBottomInset

public float getBottomInset()
Returns the bottom inset.

Returns: the bottom inset

getChildAllocation

public Shape getChildAllocation(int index, Shape a)
Returns the child allocation for the child view with the specified index.

Parameters: index the index of the child view a the current allocation of this view

Returns: the allocation of the child view

getChildState

protected AsyncBoxView.ChildState getChildState(int index)
Returns the ChildState object associated with the child view at the specified index.

Parameters: index the index of the child view for which to query the state

Returns: the child state for the specified child view

getEstimatedMajorSpan

protected boolean getEstimatedMajorSpan()
Determines whether the major span should be treated as estimated or as beeing accurate.

Returns: true if the major span should be treated as estimated, false if the major span should be treated as accurate

Since: 1.4

getInsetSpan

protected float getInsetSpan(int axis)
Returns the span along an axis that is taken up by the insets.

Parameters: axis the axis

Returns: the span along an axis that is taken up by the insets

Since: 1.4

getLayoutQueue

protected LayoutQueue getLayoutQueue()
Returns the LayoutQueue used for layouting the box view. This simply returns {@link LayoutQueue#getDefaultQueue()}.

Returns: the LayoutQueue used for layouting the box view

getLeftInset

public float getLeftInset()
Returns the left inset.

Returns: the left inset

getMajorAxis

public int getMajorAxis()
Returns the major layout axis.

Returns: the major layout axis

getMaximumSpan

public float getMaximumSpan(int axis)
Returns the maximum span of this view along the specified axis. This is implemented to return the preferredSpan for the major axis (that means the box can't be resized along the major axis) and {@link Short#MAX_VALUE} for the minor axis.

Parameters: axis the axis

Returns: the maximum span of this view along the specified axis

getMinimumSpan

public float getMinimumSpan(int axis)
Returns the minimum span along the specified axis.

getMinorAxis

public int getMinorAxis()
Returns the minor layout axis, that is the axis orthogonal to the major layout axis.

Returns: the minor layout axis

getPreferredSpan

public float getPreferredSpan(int axis)
Returns the preferred span of this view along the specified layout axis.

Returns: the preferred span of this view along the specified layout axis

getRightInset

public float getRightInset()
Returns the right inset.

Returns: the right inset

getTopInset

public float getTopInset()
Returns the top inset.

Returns: the top inset

getView

public View getView(int index)
Returns the view at the specified index.

Parameters: index the index of the requested child view

Returns: the view at the specified index

getViewCount

public int getViewCount()
Returns the number of child views.

Returns: the number of child views

getViewIndex

public int getViewIndex(int pos, Position.Bias bias)
Returns the view index of the child view that represents the specified model position.

Parameters: pos the model position for which we search the view index bias the bias

Returns: the view index of the child view that represents the specified model position

getViewIndexAtPosition

protected int getViewIndexAtPosition(int pos, Position.Bias b)
Returns the child view index of the view that represents the specified position in the document model.

Parameters: pos the position in the model b the bias

Returns: the child view index of the view that represents the specified position in the document model

loadChildren

protected void loadChildren(ViewFactory f)
Loads the child views of this view. This is triggered by {@link #setParent(View)}.

Parameters: f the view factory to build child views with

majorRequirementChange

protected void majorRequirementChange(AsyncBoxView.ChildState cs, float delta)
Receives notification from the child states that the requirements along the major axis have changed.

Parameters: cs the child state from which this notification is messaged

minorRequirementChange

protected void minorRequirementChange(AsyncBoxView.ChildState cs)
Receives notification from the child states that the requirements along the minor axis have changed.

Parameters: cs the child state from which this notification is messaged

modelToView

public Shape modelToView(int pos, Shape a, Position.Bias b)
Maps a model location to view coordinates.

Parameters: pos the model location a the current allocation of this view b the bias

Returns: the view allocation for the specified model location

paint

public void paint(Graphics g, Shape s)
Paints the view. This requests the {@link ChildLocator} to paint the views after setting the allocation on it.

Parameters: g the graphics context to use s the allocation for this view

preferenceChanged

public void preferenceChanged(View view, boolean width, boolean height)
Receives notification that one of the child views has changed its layout preferences along one or both axis. This queues a layout request for that child view if necessary.

Parameters: view the view that has changed its preferences width true if the width preference has changed height true if the height preference has changed

replace

public void replace(int offset, int length, View[] views)
Replaces child views with new child views. This creates ChildState objects for all the new views and adds layout requests for them to the layout queue.

Parameters: offset the offset at which to remove/insert length the number of child views to remove views the new child views to insert

setBottomInset

public void setBottomInset(float bottom)
Sets the bottom inset.

Parameters: bottom the bottom inset

setEstimatedMajorSpan

protected void setEstimatedMajorSpan(boolean estimated)
Sets the estimatedMajorSpan property that determines if the major span should be treated as beeing estimated.

Parameters: estimated if the major span should be treated as estimated or not

Since: 1.4

setLeftInset

public void setLeftInset(float left)
Sets the left inset.

Parameters: left the left inset

setParent

public void setParent(View parent)
Sets the parent for this view. This calls loadChildren if parent is not null and there have not been any child views initializes.

Parameters: parent the new parent view; null if this view is removed from the view hierarchy

See Also: setParent

setRightInset

public void setRightInset(float right)
Sets the right inset.

Parameters: right the right inset

setSize

public void setSize(float width, float height)
Sets the size of this view. This is ususally called before {@link #paint} is called to make sure the view has a valid layout. This implementation queues layout requests for every child view if the minor axis span has changed. (The major axis span is requested to never change for this view).

Parameters: width the width of the view height the height of the view

setTopInset

public void setTopInset(float top)
Sets the top inset.

Parameters: top the top inset

updateLayout

protected void updateLayout(DocumentEvent.ElementChange ec, DocumentEvent e, Shape a)
Updates the layout for this view. This is implemented to trigger {@link ChildLocator#childChanged} for the changed view, if there is any.

Parameters: ec the element change, may be null if there were no changes to the element of this view e the document event a the current allocation of this view

viewToModel

public int viewToModel(float x, float y, Shape a, Position.Bias[] b)
Maps view coordinates to a model location.

Parameters: x the x coordinate (relative to a) y the y coordinate (relative to a) b holds the bias of the model location on method exit

Returns: the model location for the specified view location