javax.swing.text

Class AsyncBoxView

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, getChildAllocation, getContainer, getDocument, getElement, getEndOffset, getGraphics, getMaximumSpan, getMinimumSpan, getNextVisualPositionFrom, getParent, getPreferredSpan, getResizeWeight, getStartOffset, getToolTipText, getView, getViewCount, getViewFactory, getViewIndex, getViewIndex, insert, insertUpdate, isVisible, modelToView, modelToView, modelToView, paint, preferenceChanged, remove, removeAll, removeUpdate, replace, setParent, setSize, updateChildren, updateLayout, viewToModel, viewToModel

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Field Details

locator

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

Constructor Details

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 Details

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.
Overrides:
getChildAllocation in interface View
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 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 Short.MAX_VALUE for the minor axis.
Overrides:
getMaximumSpan in interface 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 interface View

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

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 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)
            throws BadLocationException
Maps a model location to view coordinates.
Overrides:
modelToView in interface 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

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.
Overrides:
paint in interface View
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.
Overrides:
preferenceChanged in interface 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

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 interface 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

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.
Overrides:
setParent in interface View
Parameters:
parent - the new parent view; null if this view is removed from the view hierarchy

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 paint(Graphics,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 interface 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 AsyncBoxView.ChildLocator.childChanged(AsyncBoxView.ChildState) for the changed view, if there is any.
Overrides:
updateLayout in interface 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

viewToModel

public int viewToModel(float x,
                       float y,
                       Shape a,
                       Position.Bias[] b)
Maps view coordinates to a model location.
Overrides:
viewToModel in interface 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
Returns:
the model location for the specified view location

AsyncBoxView.java -- A box view that performs layout asynchronously Copyright (C) 2006 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.