CompositeViewpublic abstract class CompositeView extends View 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
View s. Subclasses that wish to manage the
collection of child View s should use the
{@link #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 View s,
it does allow for inseting the child View s
it will contain. The insets can be set by either
{@link #setInsets} or {@link #setParagraphInsets}.
In addition to the abstract methods of
{@link javax.swing.text.View},
subclasses of CompositeView will need to
override:
- {@link #isBefore} - Used to test if a given
View location is before the visual space
of the CompositeView .
- {@link #isAfter} - Used to test if a given
View location is after the visual space
of the CompositeView .
- {@link #getViewAtPoint} - Returns the view at
a given visual location.
- {@link #childAllocation} - Returns the bounds of
a particular child
View .
getChildAllocation will invoke
childAllocation after offseting
the bounds by the Inset s of the
CompositeView .
|
Fields Summary |
---|
private static View[] | ZERO | private View[] | children | private int | nchildren | private short | left | private short | right | private short | top | private short | bottom | private Rectangle | childAlloc |
Constructors Summary |
---|
public CompositeView(Element elem)Constructs a CompositeView for the given element.
super(elem);
children = new View[1];
nchildren = 0;
childAlloc = new Rectangle();
|
Methods Summary |
---|
protected abstract void | childAllocation(int index, java.awt.Rectangle a)Returns the allocation for a given child.
| protected boolean | flipEastAndWestAtEnds(int position, javax.swing.text.Position$Bias bias)Determines in which direction the next view lays.
Consider the View at index n. Typically the
View s are layed 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
View s are layed out in descending order.
This unconditionally returns false, subclasses should override this
method if there is the possibility for laying View s in
descending order.
return false;
| protected short | getBottomInset()Gets the bottom inset.
return bottom;
| public java.awt.Shape | getChildAllocation(int index, java.awt.Shape a)Fetches the allocation for the given child view to
render into. This enables finding out where various views
are located.
Rectangle alloc = getInsideAllocation(a);
childAllocation(index, alloc);
return alloc;
| protected java.awt.Rectangle | getInsideAllocation(java.awt.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.
if (a != null) {
// get the bounds, hopefully without allocating
// a new rectangle. The Shape argument should
// not be modified... we copy it into the
// child allocation.
Rectangle alloc;
if (a instanceof Rectangle) {
alloc = (Rectangle) a;
} else {
alloc = a.getBounds();
}
childAlloc.setBounds(alloc);
childAlloc.x += getLeftInset();
childAlloc.y += getTopInset();
childAlloc.width -= getLeftInset() + getRightInset();
childAlloc.height -= getTopInset() + getBottomInset();
return childAlloc;
}
return null;
| protected short | getLeftInset()Gets the left inset.
return left;
| protected int | getNextEastWestVisualPositionFrom(int pos, javax.swing.text.Position$Bias b, java.awt.Shape a, int direction, javax.swing.text.Position$Bias[] biasRet)Returns the next visual position for the cursor, in either the
east or west direction.
return Utilities.getNextVisualPositionFrom(
this, pos, b, a, direction, biasRet);
| protected int | getNextNorthSouthVisualPositionFrom(int pos, javax.swing.text.Position$Bias b, java.awt.Shape a, int direction, javax.swing.text.Position$Bias[] biasRet)Returns the next visual position for the cursor, in either the
north or south direction.
return Utilities.getNextVisualPositionFrom(
this, pos, b, a, direction, biasRet);
| public int | getNextVisualPositionFrom(int pos, javax.swing.text.Position$Bias b, java.awt.Shape a, int direction, javax.swing.text.Position$Bias[] biasRet)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 {@link #getNextNorthSouthVisualPositionFrom}
and {@link #getNextEastWestVisualPositionFrom}.
Rectangle alloc = getInsideAllocation(a);
switch (direction) {
case NORTH:
return getNextNorthSouthVisualPositionFrom(pos, b, a, direction,
biasRet);
case SOUTH:
return getNextNorthSouthVisualPositionFrom(pos, b, a, direction,
biasRet);
case EAST:
return getNextEastWestVisualPositionFrom(pos, b, a, direction,
biasRet);
case WEST:
return getNextEastWestVisualPositionFrom(pos, b, a, direction,
biasRet);
default:
throw new IllegalArgumentException("Bad direction: " + direction);
}
| protected short | getRightInset()Gets the right inset.
return right;
| protected short | getTopInset()Gets the top inset.
return top;
| public javax.swing.text.View | getView(int n)Returns the n-th view in this container.
return children[n];
| protected abstract javax.swing.text.View | getViewAtPoint(int x, int y, java.awt.Rectangle alloc)Fetches the child view at the given coordinates.
| protected javax.swing.text.View | getViewAtPosition(int pos, java.awt.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.
int index = getViewIndexAtPosition(pos);
if ((index >= 0) && (index < getViewCount())) {
View v = getView(index);
if (a != null) {
childAllocation(index, a);
}
return v;
}
return null;
| public int | getViewCount()Returns the number of child views of this view.
return nchildren;
| public int | getViewIndex(int pos, javax.swing.text.Position$Bias b)Returns the child view index representing the given
position in the model. This is implemented to call the
getViewIndexByPosition
method for backward compatibility.
if(b == Position.Bias.Backward) {
pos -= 1;
}
if ((pos >= getStartOffset()) && (pos < getEndOffset())) {
return getViewIndexAtPosition(pos);
}
return -1;
| 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.
Element elem = getElement();
return elem.getElementIndex(pos);
| protected abstract boolean | isAfter(int x, int y, java.awt.Rectangle alloc)Tests whether a point lies after the rectangle range.
| protected abstract boolean | isBefore(int x, int y, java.awt.Rectangle alloc)Tests whether a point lies before the rectangle range.
| protected void | loadChildren(javax.swing.text.ViewFactory f)Loads all of the children to initialize the view.
This is called by the {@link #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.
if (f == null) {
// No factory. This most likely indicates the parent view
// has changed out from under us, bail!
return;
}
Element e = getElement();
int n = e.getElementCount();
if (n > 0) {
View[] added = new View[n];
for (int i = 0; i < n; i++) {
added[i] = f.create(e.getElement(i));
}
replace(0, 0, added);
}
| public java.awt.Shape | modelToView(int pos, java.awt.Shape a, javax.swing.text.Position$Bias b)Provides a mapping from the document model coordinate space
to the coordinate space of the view mapped to it.
boolean isBackward = (b == Position.Bias.Backward);
int testPos = (isBackward) ? Math.max(0, pos - 1) : pos;
if(isBackward && testPos < getStartOffset()) {
return null;
}
int vIndex = getViewIndexAtPosition(testPos);
if ((vIndex != -1) && (vIndex < getViewCount())) {
View v = getView(vIndex);
if(v != null && testPos >= v.getStartOffset() &&
testPos < v.getEndOffset()) {
Shape childShape = getChildAllocation(vIndex, a);
if (childShape == null) {
// We are likely invalid, fail.
return null;
}
Shape retShape = v.modelToView(pos, childShape, b);
if(retShape == null && v.getEndOffset() == pos) {
if(++vIndex < getViewCount()) {
v = getView(vIndex);
retShape = v.modelToView(pos, getChildAllocation(vIndex, a), b);
}
}
return retShape;
}
}
throw new BadLocationException("Position not represented by view",
pos);
| public java.awt.Shape | modelToView(int p0, javax.swing.text.Position$Bias b0, int p1, javax.swing.text.Position$Bias b1, java.awt.Shape a)Provides a mapping from the document model coordinate space
to the coordinate space of the view mapped to it.
if (p0 == getStartOffset() && p1 == getEndOffset()) {
return a;
}
Rectangle alloc = getInsideAllocation(a);
Rectangle r0 = new Rectangle(alloc);
View v0 = getViewAtPosition((b0 == Position.Bias.Backward) ?
Math.max(0, p0 - 1) : p0, r0);
Rectangle r1 = new Rectangle(alloc);
View v1 = getViewAtPosition((b1 == Position.Bias.Backward) ?
Math.max(0, p1 - 1) : p1, r1);
if (v0 == v1) {
if (v0 == null) {
return a;
}
// Range contained in one view
return v0.modelToView(p0, b0, p1, b1, r0);
}
// Straddles some views.
int viewCount = getViewCount();
int counter = 0;
while (counter < viewCount) {
View v;
// Views may not be in same order as model.
// v0 or v1 may be null if there is a gap in the range this
// view contains.
if ((v = getView(counter)) == v0 || v == v1) {
View endView;
Rectangle retRect;
Rectangle tempRect = new Rectangle();
if (v == v0) {
retRect = v0.modelToView(p0, b0, v0.getEndOffset(),
Position.Bias.Backward, r0).
getBounds();
endView = v1;
}
else {
retRect = v1.modelToView(v1.getStartOffset(),
Position.Bias.Forward,
p1, b1, r1).getBounds();
endView = v0;
}
// Views entirely covered by range.
while (++counter < viewCount &&
(v = getView(counter)) != endView) {
tempRect.setBounds(alloc);
childAllocation(counter, tempRect);
retRect.add(tempRect);
}
// End view.
if (endView != null) {
Shape endShape;
if (endView == v1) {
endShape = v1.modelToView(v1.getStartOffset(),
Position.Bias.Forward,
p1, b1, r1);
}
else {
endShape = v0.modelToView(p0, b0, v0.getEndOffset(),
Position.Bias.Backward, r0);
}
if (endShape instanceof Rectangle) {
retRect.add((Rectangle)endShape);
}
else {
retRect.add(endShape.getBounds());
}
}
return retRect;
}
counter++;
}
throw new BadLocationException("Position not represented by view", p0);
| public void | replace(int offset, int length, javax.swing.text.View[] views)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.
// make sure an array exists
if (views == null) {
views = ZERO;
}
// update parent reference on removed views
for (int i = offset; i < offset + length; i++) {
if (children[i].getParent() == this) {
// in FlowView.java view might be referenced
// from two super-views as a child. see logicalView
children[i].setParent(null);
}
children[i] = null;
}
// update the array
int delta = views.length - length;
int src = offset + length;
int nmove = nchildren - src;
int dest = src + delta;
if ((nchildren + delta) >= children.length) {
// need to grow the array
int newLength = Math.max(2*children.length, nchildren + delta);
View[] newChildren = new View[newLength];
System.arraycopy(children, 0, newChildren, 0, offset);
System.arraycopy(views, 0, newChildren, offset, views.length);
System.arraycopy(children, src, newChildren, dest, nmove);
children = newChildren;
} else {
// patch the existing array
System.arraycopy(children, src, children, dest, nmove);
System.arraycopy(views, 0, children, offset, views.length);
}
nchildren = nchildren + delta;
// update parent reference on added views
for (int i = 0; i < views.length; i++) {
views[i].setParent(this);
}
| protected void | setInsets(short top, short left, short bottom, short right)Sets the insets for the view.
this.top = top;
this.left = left;
this.right = right;
this.bottom = bottom;
| protected void | setParagraphInsets(javax.swing.text.AttributeSet attr)Sets the insets from the paragraph attributes specified in
the given attributes.
// Since version 1.1 doesn't have scaling and assumes
// a pixel is equal to a point, we just cast the point
// sizes to integers.
top = (short) StyleConstants.getSpaceAbove(attr);
left = (short) StyleConstants.getLeftIndent(attr);
bottom = (short) StyleConstants.getSpaceBelow(attr);
right = (short) StyleConstants.getRightIndent(attr);
| public void | setParent(javax.swing.text.View parent)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.
super.setParent(parent);
if ((parent != null) && (nchildren == 0)) {
ViewFactory f = getViewFactory();
loadChildren(f);
}
| public int | viewToModel(float x, float y, java.awt.Shape a, javax.swing.text.Position$Bias[] bias)Provides a mapping from the view coordinate space to the logical
coordinate space of the model.
Rectangle alloc = getInsideAllocation(a);
if (isBefore((int) x, (int) y, alloc)) {
// point is before the range represented
int retValue = -1;
try {
retValue = getNextVisualPositionFrom(-1, Position.Bias.Forward,
a, EAST, bias);
} catch (BadLocationException ble) { }
catch (IllegalArgumentException iae) { }
if(retValue == -1) {
retValue = getStartOffset();
bias[0] = Position.Bias.Forward;
}
return retValue;
} else if (isAfter((int) x, (int) y, alloc)) {
// point is after the range represented.
int retValue = -1;
try {
retValue = getNextVisualPositionFrom(-1, Position.Bias.Forward,
a, WEST, bias);
} catch (BadLocationException ble) { }
catch (IllegalArgumentException iae) { }
if(retValue == -1) {
// NOTE: this could actually use end offset with backward.
retValue = getEndOffset() - 1;
bias[0] = Position.Bias.Forward;
}
return retValue;
} else {
// locate the child and pass along the request
View v = getViewAtPoint((int) x, (int) y, alloc);
if (v != null) {
return v.viewToModel(x, y, alloc, bias);
}
}
return -1;
|
|