javax.swing.text
Class AsyncBoxView

java.lang.Object
  extended by javax.swing.text.View
      extended by javax.swing.text.AsyncBoxView
All Implemented Interfaces:
SwingConstants

public class AsyncBoxView
extends View

A View implementation that lays out its child views in a box, either vertically or horizontally. The difference to 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 EditorKit with a 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
 class AsyncBoxView.ChildLocator
          Manages the effective position of child views.
 class AsyncBoxView.ChildState
          Represents the layout state of a child view.
 
Field Summary
protected  AsyncBoxView.ChildLocator locator
          The child locator for this view.
 
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, NEXT, NORTH, NORTH_EAST, NORTH_WEST, PREVIOUS, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TOP, TRAILING, VERTICAL, WEST
 
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.ChildState createChildState(View v)
          Creates a ChildState object for the specified view.
protected  void flushRequirementChanges()
          Flushes the requirements changes upwards to the parent view.
 float getBottomInset()
          Returns the bottom inset.
 Shape getChildAllocation(int index, Shape a)
          Returns the child allocation for the child view with the specified index.
protected  AsyncBoxView.ChildState getChildState(int index)
          Returns the ChildState object associated with the child view at the specified index.
protected  boolean getEstimatedMajorSpan()
          Determines whether the major span should be treated as estimated or as beeing accurate.
protected  float getInsetSpan(int axis)
          Returns the span along an axis that is taken up by the insets.
protected  LayoutQueue getLayoutQueue()
          Returns the LayoutQueue used for layouting the box view.
 float getLeftInset()
          Returns the left inset.
 int getMajorAxis()
          Returns the major layout axis.
 float getMaximumSpan(int axis)
          Returns the maximum span of this view along the specified axis.
 float getMinimumSpan(int axis)
          Returns the minimum span along the specified axis.
 int getMinorAxis()
          Returns the minor layout axis, that is the axis orthogonal to the major layout axis.
 float getPreferredSpan(int axis)
          Returns the preferred span of this view along the specified layout axis.
 float getRightInset()
          Returns the right inset.
 float getTopInset()
          Returns the top inset.
 View getView(int index)
          Returns the view at the specified index.
 int getViewCount()
          Returns the number of child views.
 int getViewIndex(int pos, Position.Bias bias)
          Returns the view index of the child view that represents the specified model position.
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.
protected  void loadChildren(ViewFactory f)
          Loads the child views of this view.
protected  void majorRequirementChange(AsyncBoxView.ChildState cs, float delta)
          Receives notification from the child states that the requirements along the major axis have changed.
protected  void minorRequirementChange(AsyncBoxView.ChildState cs)
          Receives notification from the child states that the requirements along the minor axis have changed.
 Shape modelToView(int pos, Shape a, Position.Bias b)
          Maps a model location to view coordinates.
 void paint(Graphics g, Shape s)
          Paints the view.
 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.
 void replace(int offset, int length, View[] views)
          Replaces child views with new child views.
 void setBottomInset(float bottom)
          Sets the bottom inset.
protected  void setEstimatedMajorSpan(boolean estimated)
          Sets the estimatedMajorSpan property that determines if the major span should be treated as beeing estimated.
 void setLeftInset(float left)
          Sets the left inset.
 void setParent(View parent)
          Sets the parent for this view.
 void setRightInset(float right)
          Sets the right inset.
 void setSize(float width, float height)
          Sets the size of this view.
 void setTopInset(float top)
          Sets the top inset.
protected  void updateLayout(DocumentEvent.ElementChange ec, DocumentEvent e, Shape a)
          Updates the layout for this view.
 int viewToModel(float x, float y, Shape a, Position.Bias[] b)
          Maps view coordinates to a model location.
 
Methods inherited from class javax.swing.text.View
append, breakView, changedUpdate, createFragment, dump, forwardUpdate, forwardUpdateToView, getAlignment, getAttributes, getBreakWeight, getContainer, getDocument, getElement, getEndOffset, getGraphics, getNextVisualPositionFrom, getParent, getResizeWeight, getStartOffset, getToolTipText, getViewFactory, getViewIndex, 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
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

getMajorAxis

public int getMajorAxis()
Returns the major layout axis.

Returns:
the major layout 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

getView

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

Overrides:
getView in class View
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.

Overrides:
getViewCount in class View
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.

Overrides:
getViewIndex in class View
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

getTopInset

public float getTopInset()
Returns the top inset.

Returns:
the top inset

setTopInset

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

Parameters:
top - the top inset

getBottomInset

public float getBottomInset()
Returns the bottom inset.

Returns:
the bottom inset

setBottomInset

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

Parameters:
bottom - the bottom inset

getLeftInset

public float getLeftInset()
Returns the left inset.

Returns:
the left inset

setLeftInset

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

Parameters:
left - the left inset

getRightInset

public float getRightInset()
Returns the right inset.

Returns:
the right inset

setRightInset

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

Parameters:
right - the right inset

loadChildren

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

Parameters:
f - the view factory to build child views with

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

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

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

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

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

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.

Overrides:
setParent in class View
Parameters:
parent - the new parent view; null if this view is removed from the view hierarchy
See Also:
View.setParent(View)

setSize

public void setSize(float width,
                    float height)
Sets the size of this view. This is ususally called before paint(java.awt.Graphics, java.awt.Shape) 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).

Overrides:
setSize in class View
Parameters:
width - the width of the view
height - the height of the view

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.

Overrides:
replace in class View
Parameters:
offset - the offset at which to remove/insert
length - the number of child views to remove
views - the new child views to insert

paint

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

Specified by:
paint in class View
Parameters:
g - the graphics context to use
s - the allocation for this view

getPreferredSpan

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

Specified by:
getPreferredSpan in class View
Parameters:
axis - the axis
Returns:
the preferred span of this view along the specified layout axis

modelToView

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

Specified by:
modelToView in class View
Parameters:
pos - the model location
a - the current allocation of this view
b - the bias
Returns:
the view allocation for the specified model location
Throws:
BadLocationException - if pos is invalid

viewToModel

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

Specified by:
viewToModel in class View
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
a - the allocation of this View
Returns:
the model location for the specified view location

getChildAllocation

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

Overrides:
getChildAllocation in class View
Parameters:
index - the index of the child view
a - the current allocation of this view
Returns:
the allocation of the child view

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 Short.MAX_VALUE for the minor axis.

Overrides:
getMaximumSpan in class View
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.

Overrides:
getMinimumSpan in class View
Parameters:
axis - the axis
Returns:
the minimum span along the specified axis

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.

Overrides:
preferenceChanged in class View
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

updateLayout

protected void updateLayout(DocumentEvent.ElementChange ec,
                            DocumentEvent e,
                            Shape a)
Updates the layout for this view. This is implemented to trigger AsyncBoxView.ChildLocator.childChanged(javax.swing.text.AsyncBoxView.ChildState) for the changed view, if there is any.

Overrides:
updateLayout in class View
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

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

getLayoutQueue

protected LayoutQueue getLayoutQueue()
Returns the LayoutQueue used for layouting the box view. This simply returns LayoutQueue.getDefaultQueue().

Returns:
the LayoutQueue used for layouting the box view

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

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.