APIdia Launch Partner
Top Description Inners Fields Constructors Methods
javax.swing.text

public Class ZoneView

extends BoxView
Class Inheritance
Imports
java.util.ArrayList, .Vector, java.awt.*, javax.swing.event.*

ZoneView is a View implementation that creates zones for which the child views are not created or stored until they are needed for display or model/view translations. This enables a substantial reduction in memory consumption for situations where the model being represented is very large, by building view objects only for the region being actively viewed/edited. The size of the children can be estimated in some way, or calculated asynchronously with only the result being saved.

ZoneView extends BoxView to provide a box that implements zones for its children. The zones are special View implementations (the children of an instance of this class) that represent only a portion of the model that an instance of ZoneView is responsible for. The zones don't create child views until an attempt is made to display them. A box shaped view is well suited to this because:

The default behavior is controlled by two properties, maxZoneSize and maxZonesLoaded. Setting maxZoneSize to Integer.MAX_VALUE would have the effect of causing only one zone to be created. This would effectively turn the view into an implementation of the decorator pattern. Setting maxZonesLoaded to a value of Integer.MAX_VALUE would cause zones to never be unloaded. For simplicity, zones are created on boundaries represented by the child elements of the element the view is responsible for. The zones can be any View implementation, but the default implementation is based upon AsyncBoxView which supports fairly large zones efficiently.

Author
Timothy Prinzing
Since
1.3
See Also
View

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv class
ZoneView.Zone

Internally created view that has the purpose of holding the views that represent the children of the ZoneView that have been arranged in a zone.

Field Summary

Modifier and TypeField and Description
pack-priv Vector<View>
pack-priv int
pack-priv int
Inherited from javax.swing.text.BoxView:
majorAllocValidmajorAxismajorOffsetsmajorRequestmajorReqValidmajorSpanmajorSpansminorAllocValidminorOffsetsminorRequestminorReqValidminorSpanminorSpanstempRect

Constructor Summary

AccessConstructor and Description
public
ZoneView(Element
the element this view is responsible for
elem
,
int
either View.X_AXIS or View.Y_AXIS
axis
)

Constructs a ZoneView.

Method Summary

Modifier and TypeMethod and Description
protected View

Returns:

a view to represent a zone for the given range within the model
createZone
(int
the start of the desired zone. This should be >= getStartOffset() and < getEndOffset(). This value should also be < p1.
p0
,
int
the end of the desired zone. This should be > getStartOffset() and <= getEndOffset(). This value should also be > p0.
p1
)

Create a view to represent a zone for the given range within the model (which should be within the range of this objects responsibility).

pack-priv int
getDesiredZoneEnd(int pos)

Returns the zone position to use for the end of a zone that starts at the given position.

public int

Returns:

the current maximum zone size
getMaximumZoneSize
()

Get the current maximum zone size.

public int

Returns:

current setting of the number of zones allowed to be loaded at the same time
getMaxZonesLoaded
()

Get the current setting of the number of zones allowed to be loaded at the same time.

protected int

Returns:

index of the view representing the given position, or -1 if no view represents that position
getViewIndexAtPosition
(int
the position >= 0
pos
)

Overrides javax.swing.text.CompositeView.getViewIndexAtPosition.

Returns the child view index representing the given position in the model.

pack-priv void
handleInsert(int pos, int length)

pack-priv void
handleRemove(int pos, int length)

public void
insertUpdate(DocumentEvent
the change information from the associated document
changes
,
Shape
the current allocation of the view
a
,
ViewFactory
the factory to use to rebuild if the view has children
f
)

Overrides javax.swing.text.View.insertUpdate.

Gives notification that something was inserted into the document in a location that this view is responsible for.

protected boolean

Returns:

whether or not the zone is in the loaded state.
isZoneLoaded
(View
the zone
zone
)

Determine if a zone is in the loaded state.

protected void
loadChildren(ViewFactory
the view factory
f
)

Overrides javax.swing.text.CompositeView.loadChildren.

Loads all of the children to initialize the view.

public void
removeUpdate(DocumentEvent
the change information from the associated document
changes
,
Shape
the current allocation of the view
a
,
ViewFactory
the factory to use to rebuild if the view has children
f
)

Overrides javax.swing.text.View.removeUpdate.

Gives notification that something was removed from the document in a location that this view is responsible for.

public void
setMaximumZoneSize(int
the number of characters the zone may represent before attempting to break the zone into a smaller size.
size
)

Set the desired maximum zone size.

public void
setMaxZonesLoaded(int
the desired maximum number of zones to be actively loaded, must be greater than 0
mzl
)

Sets the current setting of the number of zones allowed to be loaded at the same time.

pack-priv void
splitZone(int index, int offs0, int offs1)

Break up the zone at the given index into pieces of an acceptable size.

pack-priv void
protected void
unloadZone(View
the child view desired to be set to an unloaded state.
zone
)

Unload a zone (Convert the zone to its memory saving state).

protected boolean
updateChildren(DocumentEvent.ElementChange ec, DocumentEvent e, ViewFactory f)

Overrides javax.swing.text.View.updateChildren.

The superclass behavior will try to update the child views which is not desired in this case, since the children are zones and not directly effected by the changes to the associated element.

protected void
zoneWasLoaded(View
the child view that was just loaded.
zone
)

Called by a zone when it gets loaded.

Inherited from javax.swing.text.BoxView:
baselineLayoutbaselineRequirementscalculateMajorAxisRequirementscalculateMinorAxisRequirementscheckRequestschildAllocationflipEastAndWestAtEndsforwardUpdategetAlignmentgetAxisgetChildAllocationgetHeightgetMaximumSpangetMinimumSpangetOffsetgetPreferredSpangetResizeWeightgetSpangetSpanOnAxisgetViewAtPointgetWidthisAfterisAllocationValidisBeforeisLayoutValidlayoutlayoutChangedlayoutMajorAxislayoutMinorAxismodelToViewpaintpaintChildpreferenceChangedreplacesetAxissetSizesetSpanOnAxisupdateChildSizesupdateLayoutArrayviewToModel

Field Detail

loadedZonesback to summary
pack-priv Vector<View> loadedZones
maxZoneSizeback to summary
pack-priv int maxZoneSize
maxZonesLoadedback to summary
pack-priv int maxZonesLoaded

Constructor Detail

ZoneViewback to summary
public ZoneView(Element elem, int axis)

Constructs a ZoneView.

Parameters
elem:Element

the element this view is responsible for

axis:int

either View.X_AXIS or View.Y_AXIS

Method Detail

createZoneback to summary
protected View createZone(int p0, int p1)

Create a view to represent a zone for the given range within the model (which should be within the range of this objects responsibility). This is called by the zone management logic to create new zones. Subclasses can provide a different implementation for a zone by changing this method.

Parameters
p0:int

the start of the desired zone. This should be >= getStartOffset() and < getEndOffset(). This value should also be < p1.

p1:int

the end of the desired zone. This should be > getStartOffset() and <= getEndOffset(). This value should also be > p0.

Returns:View

a view to represent a zone for the given range within the model

getDesiredZoneEndback to summary
pack-priv int getDesiredZoneEnd(int pos)

Returns the zone position to use for the end of a zone that starts at the given position. By default this returns something close to half the max zone size.

getMaximumZoneSizeback to summary
public int getMaximumZoneSize()

Get the current maximum zone size.

Returns:int

the current maximum zone size

getMaxZonesLoadedback to summary
public int getMaxZonesLoaded()

Get the current setting of the number of zones allowed to be loaded at the same time.

Returns:int

current setting of the number of zones allowed to be loaded at the same time

getViewIndexAtPositionback to summary
protected int getViewIndexAtPosition(int pos)

Overrides javax.swing.text.CompositeView.getViewIndexAtPosition.

Returns the child view index representing the given position in the model.

Parameters
pos:int

the position >= 0

Returns:int

index of the view representing the given position, or -1 if no view represents that position

handleInsertback to summary
pack-priv void handleInsert(int pos, int length)
handleRemoveback to summary
pack-priv void handleRemove(int pos, int length)
insertUpdateback to summary
public void insertUpdate(DocumentEvent changes, Shape a, ViewFactory f)

Overrides javax.swing.text.View.insertUpdate.

Gives notification that something was inserted into the document in a location that this view is responsible for. This is largely delegated to the superclass, but is reimplemented to update the relevant zone (i.e. determine if a zone needs to be split into a set of 2 or more zones).

Parameters
changes:DocumentEvent

the change information from the associated document

a:Shape

the current allocation of the view

f:ViewFactory

the factory to use to rebuild if the view has children

See Also
View#insertUpdate
isZoneLoadedback to summary
protected boolean isZoneLoaded(View zone)

Determine if a zone is in the loaded state. The zones are expected to represent a subset of the child elements of the element this view is responsible for. Therefore, the default implementation is to return true if the view has children. param zone the child view

Parameters
zone:View

the zone

Returns:boolean

whether or not the zone is in the loaded state.

loadChildrenback to summary
protected void loadChildren(ViewFactory f)

Overrides javax.swing.text.CompositeView.loadChildren.

Loads all of the children to initialize the view. This is called by the setParent method. This is reimplemented to not load any children directly (as they are created by the zones). This method creates the initial set of zones. Zones don't actually get populated however until an attempt is made to display them or to do model/view coordinate translation.

Parameters
f:ViewFactory

the view factory

removeUpdateback to summary
public void removeUpdate(DocumentEvent changes, Shape a, ViewFactory f)

Overrides javax.swing.text.View.removeUpdate.

Gives notification that something was removed from the document in a location that this view is responsible for. This is largely delegated to the superclass, but is reimplemented to update the relevant zones (i.e. determine if zones need to be removed or joined with another zone).

Parameters
changes:DocumentEvent

the change information from the associated document

a:Shape

the current allocation of the view

f:ViewFactory

the factory to use to rebuild if the view has children

See Also
View#removeUpdate
setMaximumZoneSizeback to summary
public void setMaximumZoneSize(int size)

Set the desired maximum zone size. A zone may get larger than this size if a single child view is larger than this size since zones are formed on child view boundaries.

Parameters
size:int

the number of characters the zone may represent before attempting to break the zone into a smaller size.

setMaxZonesLoadedback to summary
public void setMaxZonesLoaded(int mzl)

Sets the current setting of the number of zones allowed to be loaded at the same time. This will throw an IllegalArgumentException if mzl is less than 1.

Parameters
mzl:int

the desired maximum number of zones to be actively loaded, must be greater than 0

Exceptions
IllegalArgumentException:
if mzl is < 1
splitZoneback to summary
pack-priv void splitZone(int index, int offs0, int offs1)

Break up the zone at the given index into pieces of an acceptable size.

unloadOldZonesback to summary
pack-priv void unloadOldZones()
unloadZoneback to summary
protected void unloadZone(View zone)

Unload a zone (Convert the zone to its memory saving state). The zones are expected to represent a subset of the child elements of the element this view is responsible for. Therefore, the default implementation is to simple remove all the children.

Parameters
zone:View

the child view desired to be set to an unloaded state.

updateChildrenback to summary
protected boolean updateChildren(DocumentEvent.ElementChange ec, DocumentEvent e, ViewFactory f)

Overrides javax.swing.text.View.updateChildren.

The superclass behavior will try to update the child views which is not desired in this case, since the children are zones and not directly effected by the changes to the associated element. This is reimplemented to do nothing and return false.

zoneWasLoadedback to summary
protected void zoneWasLoaded(View zone)

Called by a zone when it gets loaded. This happens when an attempt is made to display or perform a model/view translation on a zone that was in an unloaded state. This is implemented to check if the maximum number of zones was reached and to unload the oldest zone if so.

Parameters
zone:View

the child view that was just loaded.

javax.swing.text back to summary

pack-priv Class ZoneView.Zone

extends AsyncBoxView
Class Inheritance

Internally created view that has the purpose of holding the views that represent the children of the ZoneView that have been arranged in a zone.

Field Summary

Modifier and TypeField and Description
private Position
private Position
Inherited from javax.swing.text.AsyncBoxView:
axisbottomInsetchangingestimatedMajorSpanflushTaskleftInsetlocatormajorChangedmajorSpanminorChangedminorSpanminRequestprefRequestrightInsetstatstopInset

Constructor Summary

AccessConstructor and Description
public
Zone(Element elem, Position start, Position end)

Method Summary

Modifier and TypeMethod and Description
public void
changedUpdate(DocumentEvent
the change information from the associated document
e
,
Shape
the current allocation of the view
a
,
ViewFactory
the factory to use to rebuild if the view has children
f
)

Overrides javax.swing.text.View.changedUpdate.

Gives notification from the document that attributes were changed in a location that this view is responsible for.

protected void
flushRequirementChanges()

Overrides javax.swing.text.AsyncBoxView.flushRequirementChanges.

Publish the changes in preferences upward to the parent view.

public AttributeSet
getAttributes()

Overrides javax.swing.text.View.getAttributes.

Fetches the attributes to use when rendering.

public int
getEndOffset()

Overrides javax.swing.text.View.getEndOffset.

End of the zones range.

public int
getStartOffset()

Overrides javax.swing.text.View.getStartOffset.

Start of the zones range.

public int

Returns:

index of the view representing the given position, or -1 if no view represents that position
getViewIndex
(int
the position >= 0
pos
,
Position.Bias b)

Overrides javax.swing.text.AsyncBoxView.getViewIndex.

Returns the child view index representing the given position in the model.

public void
insertUpdate(DocumentEvent
the change information from the associated document
e
,
Shape
the current allocation of the view
a
,
ViewFactory
the factory to use to rebuild if the view has children
f
)

Overrides javax.swing.text.View.insertUpdate.

Gives notification that something was inserted into the document in a location that this view is responsible for.

public boolean
isLoaded()

Determines if the zone is in the loaded state or not.

public void
load()

Creates the child views and populates the zone with them.

protected void
loadChildren(ViewFactory f)

Overrides javax.swing.text.AsyncBoxView.loadChildren.

This method is reimplemented to not build the children since the children are created when the zone is loaded rather then when it is placed in the view hierarchy.

public Shape

Returns:

the bounding box of the given position
modelToView
(int
the position to convert
pos
,
Shape
the allocated region to render into
a
,
Position.Bias b)

Overrides javax.swing.text.AsyncBoxView.modelToView.

Provides a mapping from the document model coordinate space to the coordinate space of the view mapped to it.

public void
paint(Graphics
the rendering surface to use
g
,
Shape
the allocated region to render into
a
)

Overrides javax.swing.text.AsyncBoxView.paint.

Renders using the given rendering surface and area on that surface.

public void
removeUpdate(DocumentEvent
the change information from the associated document
e
,
Shape
the current allocation of the view
a
,
ViewFactory
the factory to use to rebuild if the view has children
f
)

Overrides javax.swing.text.View.removeUpdate.

Gives notification that something was removed from the document in a location that this view is responsible for.

public void
unload()

Removes the child views and returns to a state of unloaded.

protected boolean
updateChildren(DocumentEvent.ElementChange ec, DocumentEvent e, ViewFactory f)

Overrides javax.swing.text.View.updateChildren.

Updates the child views in response to receiving notification that the model changed, and there is change record for the element this view is responsible for.

public int

Returns:

the location within the model that best represents the given point in the view >= 0
viewToModel
(float
x coordinate of the view location to convert >= 0
x
,
float
y coordinate of the view location to convert >= 0
y
,
Shape
the allocated region to render into
a
,
Position.Bias[] bias)

Overrides javax.swing.text.AsyncBoxView.viewToModel.

Provides a mapping from the view coordinate space to the logical coordinate space of the model.

Inherited from javax.swing.text.AsyncBoxView:
createChildStategetBottomInsetgetChildAllocationgetChildStategetEstimatedMajorSpangetInsetSpangetLayoutQueuegetLeftInsetgetMajorAxisgetMaximumSpangetMinimumSpangetMinorAxisgetNextVisualPositionFromgetPreferredSpangetRightInsetgetSpanOnAxisgetTopInsetgetViewgetViewCountgetViewIndexAtPositionmajorRequirementChangeminorRequirementChangepreferenceChangedreplacesetBottomInsetsetEstimatedMajorSpansetLeftInsetsetParentsetRightInsetsetSizesetSpanOnAxissetTopInsetupdateLayout

Field Detail

endback to summary
private Position end
startback to summary
private Position start

Constructor Detail

Zoneback to summary
public Zone(Element elem, Position start, Position end)

Method Detail

changedUpdateback to summary
public void changedUpdate(DocumentEvent e, Shape a, ViewFactory f)

Overrides javax.swing.text.View.changedUpdate.

Gives notification from the document that attributes were changed in a location that this view is responsible for. If the zone has been loaded, the superclass behavior is invoked, otherwise this does nothing.

Parameters
e:DocumentEvent

the change information from the associated document

a:Shape

the current allocation of the view

f:ViewFactory

the factory to use to rebuild if the view has children

See Also
View#removeUpdate
flushRequirementChangesback to summary
protected void flushRequirementChanges()

Overrides javax.swing.text.AsyncBoxView.flushRequirementChanges.

Publish the changes in preferences upward to the parent view.

This is reimplemented to stop the superclass behavior if the zone has not yet been loaded. If the zone is unloaded for example, the last seen major span is the best estimate and a calculated span for no children is undesirable.

getAttributesback to summary
public AttributeSet getAttributes()

Overrides javax.swing.text.View.getAttributes.

Fetches the attributes to use when rendering. This view isn't directly responsible for an element so it returns the outer classes attributes.

getEndOffsetback to summary
public int getEndOffset()

Overrides javax.swing.text.View.getEndOffset.

End of the zones range.

getStartOffsetback to summary
public int getStartOffset()

Overrides javax.swing.text.View.getStartOffset.

Start of the zones range.

See Also
View#getStartOffset
getViewIndexback to summary
public int getViewIndex(int pos, Position.Bias b)

Overrides javax.swing.text.AsyncBoxView.getViewIndex.

Returns the child view index representing the given position in the model. Since the zone contains a cluster of the overall set of child elements, we can determine the index fairly quickly from the model by subtracting the index of the start offset from the index of the position given.

Parameters
pos:int

the position >= 0

Returns:int

index of the view representing the given position, or -1 if no view represents that position

Since
1.3
insertUpdateback to summary
public void insertUpdate(DocumentEvent e, Shape a, ViewFactory f)

Overrides javax.swing.text.View.insertUpdate.

Gives notification that something was inserted into the document in a location that this view is responsible for. If the zone has been loaded, the superclass behavior is invoked, otherwise this does nothing.

Parameters
e:DocumentEvent

the change information from the associated document

a:Shape

the current allocation of the view

f:ViewFactory

the factory to use to rebuild if the view has children

See Also
View#insertUpdate
isLoadedback to summary
public boolean isLoaded()

Determines if the zone is in the loaded state or not.

loadback to summary
public void load()

Creates the child views and populates the zone with them. This is done by translating the positions to child element index locations and building views to those elements. If the zone is already loaded, this does nothing.

loadChildrenback to summary
protected void loadChildren(ViewFactory f)

Overrides javax.swing.text.AsyncBoxView.loadChildren.

This method is reimplemented to not build the children since the children are created when the zone is loaded rather then when it is placed in the view hierarchy. The major span is estimated at this point by building the first child (but not storing it), and calling setEstimatedMajorSpan(true) followed by setSpan for the major axis with the estimated span.

modelToViewback to summary
public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException

Overrides javax.swing.text.AsyncBoxView.modelToView.

Provides a mapping from the document model coordinate space to the coordinate space of the view mapped to it. This is implemented to provide the superclass behavior after first making sure the zone is loaded (The zone must be loaded to make this calculation).

Parameters
pos:int

the position to convert

a:Shape

the allocated region to render into

Returns:Shape

the bounding box of the given position

Exceptions
BadLocationException:
if the given position does not represent a valid location in the associated document
See Also
View#modelToView
paintback to summary
public void paint(Graphics g, Shape a)

Overrides javax.swing.text.AsyncBoxView.paint.

Renders using the given rendering surface and area on that surface. This is implemented to load the zone if its not already loaded, and then perform the superclass behavior.

Parameters
g:Graphics

the rendering surface to use

a:Shape

the allocated region to render into

See Also
View#paint
removeUpdateback to summary
public void removeUpdate(DocumentEvent e, Shape a, ViewFactory f)

Overrides javax.swing.text.View.removeUpdate.

Gives notification that something was removed from the document in a location that this view is responsible for. If the zone has been loaded, the superclass behavior is invoked, otherwise this does nothing.

Parameters
e:DocumentEvent

the change information from the associated document

a:Shape

the current allocation of the view

f:ViewFactory

the factory to use to rebuild if the view has children

See Also
View#removeUpdate
unloadback to summary
public void unload()

Removes the child views and returns to a state of unloaded.

updateChildrenback to summary
protected boolean updateChildren(DocumentEvent.ElementChange ec, DocumentEvent e, ViewFactory f)

Overrides javax.swing.text.View.updateChildren.

Doc from javax.swing.text.View.updateChildren.

Updates the child views in response to receiving notification that the model changed, and there is change record for the element this view is responsible for. This is implemented to assume the child views are directly responsible for the child elements of the element this view represents. The ViewFactory is used to create child views for each element specified as added in the ElementChange, starting at the index specified in the given ElementChange. The number of child views representing the removed elements specified are removed.

viewToModelback to summary
public int viewToModel(float x, float y, Shape a, Position.Bias[] bias)

Overrides javax.swing.text.AsyncBoxView.viewToModel.

Provides a mapping from the view coordinate space to the logical coordinate space of the model. This is implemented to first make sure the zone is loaded before providing the superclass behavior.

Parameters
x:float

x coordinate of the view location to convert >= 0

y:float

y coordinate of the view location to convert >= 0

a:Shape

the allocated region to render into

Returns:int

the location within the model that best represents the given point in the view >= 0

See Also
View#viewToModel