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

public abstract Class CompositeView

extends View
Class Inheritance
Known Direct Subclasses
javax.swing.text.FlowView.LogicalView, javax.swing.JEditorPane.PlainEditorKit.PlainParagraph.LogicalView, javax.swing.plaf.basic.BasicTextAreaUI.PlainParagraph.LogicalView, javax.swing.text.BoxView
Imports
java.util.*, java.awt.*

CompositeView is an abstract View implementation which manages one or more child views. (Note that CompositeView is intended for managing relatively small numbers of child views.) CompositeView is intended to be used as a starting point for View implementations, such as BoxView, that will contain child Views. Subclasses that wish to manage the collection of child Views should use the replace method. As View invokes replace during DocumentListener notification, you normally won't need to directly invoke replace.

While CompositeView does not impose a layout policy on its child Views, it does allow for insetting the child Views it will contain. The insets can be set by either setInsets or setParagraphInsets.

In addition to the abstract methods of javax.swing.text.View, subclasses of CompositeView will need to override:

Author
Timothy Prinzing

Field Summary

Modifier and TypeField and Description
private short
private Rectangle
private View[]
private short
private int
private short
private short
private static View[]
Inherited from javax.swing.text.View:
BadBreakWeightExcellentBreakWeightfirstUpdateIndexForcedBreakWeightGoodBreakWeightlastUpdateIndexsharedBiasReturnX_AXISY_AXIS

Constructor Summary

AccessConstructor and Description
public
CompositeView(Element
the element this view is responsible for
elem
)

Constructs a CompositeView for the given element.

Method Summary

Modifier and TypeMethod and Description
protected abstract void
childAllocation(int
the index of the child, >= 0 && < getViewCount()
index
,
Rectangle
the allocation to the interior of the box on entry, and the allocation of the child view at the index on exit.
a
)

Returns the allocation for a given child.

protected boolean

Returns:

false
flipEastAndWestAtEnds
(int
position into the model
position
,
Position.Bias
either Position.Bias.Forward or Position.Bias.Backward
bias
)

Determines in which direction the next view lays.

protected short

Returns:

the inset >= 0
getBottomInset
()

Gets the bottom inset.

public Shape

Returns:

the allocation to the child
getChildAllocation
(int
the index of the child, >= 0 && < getViewCount()
index
,
Shape
the allocation to this view
a
)

Overrides javax.swing.text.View.getChildAllocation.

Fetches the allocation for the given child view to render into.

protected Rectangle

Returns:

the allocation that represents the inside of the view after the margins have all been removed; if the given allocation was null, the return value is null
getInsideAllocation
(Shape
the allocation given to the view
a
)

Translates the immutable allocation given to the view to a mutable allocation that represents the interior allocation (i.e. the bounds of the given allocation with the top, left, bottom, and right insets removed.

protected short

Returns:

the inset >= 0
getLeftInset
()

Gets the left inset.

protected int

Returns:

the location within the model that best represents the next west or east location
getNextEastWestVisualPositionFrom
(int
the position to convert >= 0
pos
,
Position.Bias
a bias value of either Position.Bias.Forward or Position.Bias.Backward
b
,
Shape
the allocated region to render into
a
,
int
the direction from the current position that can be thought of as the arrow keys typically found on a keyboard; this may be one of the following:
  • SwingConstants.WEST
  • SwingConstants.EAST
direction
,
Position.Bias[]
an array containing the bias that was checked
biasRet
)

Returns the next visual position for the cursor, in either the east or west direction.

protected int

Returns:

the location within the model that best represents the next north or south location
getNextNorthSouthVisualPositionFrom
(int
the position to convert >= 0
pos
,
Position.Bias
a bias value of either Position.Bias.Forward or Position.Bias.Backward
b
,
Shape
the allocated region to render into
a
,
int
the direction from the current position that can be thought of as the arrow keys typically found on a keyboard; this may be one of the following:
  • SwingConstants.NORTH
  • SwingConstants.SOUTH
direction
,
Position.Bias[]
an array containing the bias that was checked
biasRet
)

Returns the next visual position for the cursor, in either the north or south direction.

public int

Returns:

the location within the model that best represents the next location visual position
getNextVisualPositionFrom
(int
the position to convert
pos
,
Position.Bias
a bias value of either Position.Bias.Forward or Position.Bias.Backward
b
,
Shape
the allocated region to render into
a
,
int
the direction from the current position that can be thought of as the arrow keys typically found on a keyboard; this may be one of the following:
  • SwingConstants.WEST
  • SwingConstants.EAST
  • SwingConstants.NORTH
  • SwingConstants.SOUTH
direction
,
Position.Bias[]
an array containing the bias that was checked
biasRet
)

Overrides javax.swing.text.View.getNextVisualPositionFrom.

Provides a way to determine the next visually represented model location that one might place a caret.

protected short

Returns:

the inset >= 0
getRightInset
()

Gets the right inset.

protected short

Returns:

the inset >= 0
getTopInset
()

Gets the top inset.

public View

Returns:

the view at index n
getView
(int
the number of the desired view, >= 0 && < getViewCount()
n
)

Overrides javax.swing.text.View.getView.

Returns the n-th view in this container.

protected abstract View

Returns:

the child view
getViewAtPoint
(int
the X coordinate >= 0
x
,
int
the Y coordinate >= 0
y
,
Rectangle
the parent's allocation on entry, which should be changed to the child's allocation on exit
alloc
)

Fetches the child view at the given coordinates.

protected View

Returns:

the view representing the given position, or null if there isn't one
getViewAtPosition
(int
the position >= 0
pos
,
Rectangle
the allocation to the interior of the box on entry, and the allocation of the view containing the position on exit
a
)

Fetches the child view that represents the given position in the model.

public int

Returns:

the number of views >= 0
getViewCount
()

Overrides javax.swing.text.View.getViewCount.

Returns the number of child views of this view.

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.View.getViewIndex.

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

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
)

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

protected abstract boolean

Returns:

true if the point is after the specified range
isAfter
(int
the X coordinate >= 0
x
,
int
the Y coordinate >= 0
y
,
Rectangle
the rectangle
alloc
)

Tests whether a point lies after the rectangle range.

protected abstract boolean

Returns:

true if the point is before the specified range
isBefore
(int
the X coordinate >= 0
x
,
int
the Y coordinate >= 0
y
,
Rectangle
the rectangle
alloc
)

Tests whether a point lies before the rectangle range.

protected void
loadChildren(ViewFactory
the view factory
f
)

Loads all of the children to initialize the view.

public Shape

Returns:

the bounding box of the given position
modelToView
(int
the position to convert >= 0
pos
,
Shape
the allocated region to render into
a
,
Position.Bias
a bias value of either Position.Bias.Forward or Position.Bias.Backward
b
)

Implements abstract javax.swing.text.View.modelToView.

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

public Shape

Returns:

the bounding box of the given position is returned
modelToView
(int
the position to convert >= 0
p0
,
Position.Bias
the bias toward the previous character or the next character represented by p0, in case the position is a boundary of two views; either Position.Bias.Forward or Position.Bias.Backward
b0
,
int
the position to convert >= 0
p1
,
Position.Bias
the bias toward the previous character or the next character represented by p1, in case the position is a boundary of two views
b1
,
Shape
the allocated region to render into
a
)

Overrides javax.swing.text.View.modelToView.

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

public void
replace(int
the starting index into the child views to insert the new views; >= 0 and <= getViewCount
offset
,
int
the number of existing child views to remove; this should be a value >= 0 and <= (getViewCount() - offset)
length
,
View[]
the child views to add; this value can be null to indicate no children are being added (useful to remove)
views
)

Overrides javax.swing.text.View.replace.

Replaces child views.

protected void
setInsets(short
the top inset >= 0
top
,
short
the left inset >= 0
left
,
short
the bottom inset >= 0
bottom
,
short
the right inset >= 0
right
)

Sets the insets for the view.

protected void
setParagraphInsets(AttributeSet
the attributes
attr
)

Sets the insets from the paragraph attributes specified in the given attributes.

public void
setParent(View
the parent of the view, null if none
parent
)

Overrides javax.swing.text.View.setParent.

Sets the parent of the view.

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[]
either Position.Bias.Forward or Position.Bias.Backward
bias
)

Implements abstract javax.swing.text.View.viewToModel.

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

Inherited from javax.swing.text.View:
appendbreakViewcalculateUpdateIndexeschangedUpdatecreateFragmentforwardUpdateforwardUpdateToViewgetAlignmentgetAttributesgetBreakWeightgetContainergetDocumentgetElementgetEndOffsetgetGraphicsgetMaximumSpangetMinimumSpangetParentgetPreferredSpangetResizeWeightgetStartOffsetgetToolTipTextgetViewFactorygetViewIndexinsertinsertUpdateisVisiblemodelToViewpaintpreferenceChangedremoveremoveAllremoveUpdatesetSizeupdateAfterChangeupdateChildrenupdateLayoutviewToModel

Field Detail

bottomback to summary
private short bottom
childAllocback to summary
private Rectangle childAlloc
childrenback to summary
private View[] children
leftback to summary
private short left
nchildrenback to summary
private int nchildren
topback to summary
private short top
ZEROback to summary
private static View[] ZERO

Constructor Detail

CompositeViewback to summary
public CompositeView(Element elem)

Constructs a CompositeView for the given element.

Parameters
elem:Element

the element this view is responsible for

Method Detail

childAllocationback to summary
protected abstract void childAllocation(int index, Rectangle a)

Returns the allocation for a given child.

Parameters
index:int

the index of the child, >= 0 && < getViewCount()

a:Rectangle

the allocation to the interior of the box on entry, and the allocation of the child view at the index on exit.

flipEastAndWestAtEndsback to summary
protected boolean flipEastAndWestAtEnds(int position, Position.Bias bias)

Determines in which direction the next view lays. Consider the View at index n. Typically the Views are laid out from left to right, so that the View to the EAST will be at index n + 1, and the View to the WEST will be at index n - 1. In certain situations, such as with bidirectional text, it is possible that the View to EAST is not at index n + 1, but rather at index n - 1, or that the View to the WEST is not at index n - 1, but index n + 1. In this case this method would return true, indicating the Views are laid out in descending order.

This unconditionally returns false, subclasses should override this method if there is the possibility for laying Views in descending order.

Parameters
position:int

position into the model

bias:Position.Bias

either Position.Bias.Forward or Position.Bias.Backward

Returns:boolean

false

getBottomInsetback to summary
protected short getBottomInset()

Gets the bottom inset.

Returns:short

the inset >= 0

getChildAllocationback to summary
public Shape getChildAllocation(int index, Shape a)

Overrides javax.swing.text.View.getChildAllocation.

Fetches the allocation for the given child view to render into. This enables finding out where various views are located.

Parameters
index:int

the index of the child, >= 0 && < getViewCount()

a:Shape

the allocation to this view

Returns:Shape

the allocation to the child

getInsideAllocationback to summary
protected Rectangle getInsideAllocation(Shape a)

Translates the immutable allocation given to the view to a mutable allocation that represents the interior allocation (i.e. the bounds of the given allocation with the top, left, bottom, and right insets removed. It is expected that the returned value would be further mutated to represent an allocation to a child view. This is implemented to reuse an instance variable so it avoids creating excessive Rectangles. Typically the result of calling this method would be fed to the childAllocation method.

Parameters
a:Shape

the allocation given to the view

Returns:Rectangle

the allocation that represents the inside of the view after the margins have all been removed; if the given allocation was null, the return value is null

getLeftInsetback to summary
protected short getLeftInset()

Gets the left inset.

Returns:short

the inset >= 0

getNextEastWestVisualPositionFromback to summary
protected int getNextEastWestVisualPositionFrom(int pos, Position.Bias b, Shape a, int direction, Position.Bias[] biasRet) throws BadLocationException

Returns the next visual position for the cursor, in either the east or west direction.

Parameters
pos:int

the position to convert >= 0

b:Position.Bias

a bias value of either Position.Bias.Forward or Position.Bias.Backward

a:Shape

the allocated region to render into

direction:int

the direction from the current position that can be thought of as the arrow keys typically found on a keyboard; this may be one of the following:

  • SwingConstants.WEST
  • SwingConstants.EAST
biasRet:Position.Bias[]

an array containing the bias that was checked

Returns:int

the location within the model that best represents the next west or east location

Exceptions
BadLocationException:
for a bad location within a document model
IllegalArgumentException:
if direction is invalid
See Also
getNextVisualPositionFrom
getNextNorthSouthVisualPositionFromback to summary
protected int getNextNorthSouthVisualPositionFrom(int pos, Position.Bias b, Shape a, int direction, Position.Bias[] biasRet) throws BadLocationException

Returns the next visual position for the cursor, in either the north or south direction.

Parameters
pos:int

the position to convert >= 0

b:Position.Bias

a bias value of either Position.Bias.Forward or Position.Bias.Backward

a:Shape

the allocated region to render into

direction:int

the direction from the current position that can be thought of as the arrow keys typically found on a keyboard; this may be one of the following:

  • SwingConstants.NORTH
  • SwingConstants.SOUTH
biasRet:Position.Bias[]

an array containing the bias that was checked

Returns:int

the location within the model that best represents the next north or south location

Exceptions
BadLocationException:
for a bad location within a document model
IllegalArgumentException:
if direction is invalid
See Also
getNextVisualPositionFrom
getNextVisualPositionFromback to summary
public int getNextVisualPositionFrom(int pos, Position.Bias b, Shape a, int direction, Position.Bias[] biasRet) throws BadLocationException

Overrides javax.swing.text.View.getNextVisualPositionFrom.

Provides a way to determine the next visually represented model location that one might place a caret. Some views may not be visible, they might not be in the same order found in the model, or they just might not allow access to some of the locations in the model. This is a convenience method for getNextNorthSouthVisualPositionFrom and getNextEastWestVisualPositionFrom. This method enables specifying a position to convert within the range of >=0. If the value is -1, a position will be calculated automatically. If the value < -1, the BadLocationException will be thrown.

Parameters
pos:int

the position to convert

b:Position.Bias

a bias value of either Position.Bias.Forward or Position.Bias.Backward

a:Shape

the allocated region to render into

direction:int

the direction from the current position that can be thought of as the arrow keys typically found on a keyboard; this may be one of the following:

  • SwingConstants.WEST
  • SwingConstants.EAST
  • SwingConstants.NORTH
  • SwingConstants.SOUTH
biasRet:Position.Bias[]

an array containing the bias that was checked

Returns:int

the location within the model that best represents the next location visual position

Exceptions
BadLocationException:
the given position is not a valid position within the document
IllegalArgumentException:
if direction is invalid
getRightInsetback to summary
protected short getRightInset()

Gets the right inset.

Returns:short

the inset >= 0

getTopInsetback to summary
protected short getTopInset()

Gets the top inset.

Returns:short

the inset >= 0

getViewback to summary
public View getView(int n)

Overrides javax.swing.text.View.getView.

Returns the n-th view in this container.

Parameters
n:int

the number of the desired view, >= 0 && < getViewCount()

Returns:View

the view at index n

getViewAtPointback to summary
protected abstract View getViewAtPoint(int x, int y, Rectangle alloc)

Fetches the child view at the given coordinates.

Parameters
x:int

the X coordinate >= 0

y:int

the Y coordinate >= 0

alloc:Rectangle

the parent's allocation on entry, which should be changed to the child's allocation on exit

Returns:View

the child view

getViewAtPositionback to summary
protected View getViewAtPosition(int pos, Rectangle a)

Fetches the child view that represents 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:int

the position >= 0

a:Rectangle

the allocation to the interior of the box on entry, and the allocation of the view containing the position on exit

Returns:View

the view representing the given position, or null if there isn't one

getViewCountback to summary
public int getViewCount()

Overrides javax.swing.text.View.getViewCount.

Returns the number of child views of this view.

Returns:int

the number of views >= 0

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

Overrides javax.swing.text.View.getViewIndex.

Returns the child view index representing the given position in the model. This is implemented to call the getViewIndexByPosition method for backward compatibility.

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
getViewIndexAtPositionback to summary
protected int getViewIndexAtPosition(int pos)

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:int

the position >= 0

Returns:int

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

isAfterback to summary
protected abstract boolean isAfter(int x, int y, Rectangle alloc)

Tests whether a point lies after the rectangle range.

Parameters
x:int

the X coordinate >= 0

y:int

the Y coordinate >= 0

alloc:Rectangle

the rectangle

Returns:boolean

true if the point is after the specified range

isBeforeback to summary
protected abstract boolean isBefore(int x, int y, Rectangle alloc)

Tests whether a point lies before the rectangle range.

Parameters
x:int

the X coordinate >= 0

y:int

the Y coordinate >= 0

alloc:Rectangle

the rectangle

Returns:boolean

true if the point is before the specified range

loadChildrenback to summary
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.

Parameters
f:ViewFactory

the view factory

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

Implements abstract javax.swing.text.View.modelToView.

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

Parameters
pos:int

the position to convert >= 0

a:Shape

the allocated region to render into

b:Position.Bias

a bias value of either Position.Bias.Forward or Position.Bias.Backward

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
modelToViewback to summary
public Shape modelToView(int p0, Position.Bias b0, int p1, Position.Bias b1, Shape a) throws BadLocationException

Overrides javax.swing.text.View.modelToView.

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

Parameters
p0:int

the position to convert >= 0

b0:Position.Bias

the bias toward the previous character or the next character represented by p0, in case the position is a boundary of two views; either Position.Bias.Forward or Position.Bias.Backward

p1:int

the position to convert >= 0

b1:Position.Bias

the bias toward the previous character or the next character represented by p1, in case the position is a boundary of two views

a:Shape

the allocated region to render into

Returns:Shape

the bounding box of the given position is returned

Exceptions
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
replaceback to summary
public void replace(int offset, int length, View[] views)

Overrides javax.swing.text.View.replace.

Replaces child views. If there are no views to remove this acts as an insert. If there are no views to add this acts as a remove. Views being removed will have the parent set to null, and the internal reference to them removed so that they may be garbage collected.

Parameters
offset:int

the starting index into the child views to insert the new views; >= 0 and <= getViewCount

length:int

the number of existing child views to remove; this should be a value >= 0 and <= (getViewCount() - offset)

views:View[]

the child views to add; this value can be null to indicate no children are being added (useful to remove)

setInsetsback to summary
protected void setInsets(short top, short left, short bottom, short right)

Sets the insets for the view.

Parameters
top:short

the top inset >= 0

left:short

the left inset >= 0

bottom:short

the bottom inset >= 0

right:short

the right inset >= 0

setParagraphInsetsback to summary
protected void setParagraphInsets(AttributeSet attr)

Sets the insets from the paragraph attributes specified in the given attributes.

Parameters
attr:AttributeSet

the attributes

setParentback to summary
public void setParent(View parent)

Overrides javax.swing.text.View.setParent.

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.

Parameters
parent:View

the parent of the view, null if none

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

Implements abstract javax.swing.text.View.viewToModel.

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

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

bias:Position.Bias[]

either Position.Bias.Forward or Position.Bias.Backward

Returns:int

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

See Also
View#viewToModel