javax.swing.text

Class BoxView

Implemented Interfaces:
SwingConstants
Known Direct Subclasses:
BlockView, FlowView, FrameSetView, TableView, TableView.TableCell, TableView.TableRow, WrappedPlainView, ZoneView

public class BoxView
extends CompositeView

An implementation of CompositeView that arranges its children in a box along one axis. This is comparable to how the BoxLayout works, but for View children.

Field Summary

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

BoxView(Element element, int axis)
Creates a new BoxView for the given Element and axis.

Method Summary

protected void
baselineLayout(int span, int axis, int[] offsets, int[] spans)
Calculates the baseline layout of the children of this BoxView along the specified axis.
protected SizeRequirements
baselineRequirements(int axis, SizeRequirements sr)
Calculates size requirements for a baseline layout.
protected SizeRequirements
calculateMajorAxisRequirements(int axis, SizeRequirements sr)
Calculates the size requirements of this BoxView along its major axis, that is the axis specified in the constructor.
protected SizeRequirements
calculateMinorAxisRequirements(int axis, SizeRequirements sr)
Calculates the size requirements of this BoxView along its minor axis, that is the axis opposite to the axis specified in the constructor.
protected void
childAllocation(int index, Rectangle a)
Computes the allocation for a child View.
protected boolean
flipEastAndWestAtEnds(int position, Position.Bias bias)
protected void
forwardUpdate(DocumentEvent.ElementChange ec, DocumentEvent e, Shape a, ViewFactory vf)
Forwards the DocumentEvent to child views that need to get notified of the change to the model.
float
getAlignment(int axis)
Returns the alignment for this box view for the specified axis.
int
getAxis()
Returns the axis along which this BoxView is laid out.
Shape
getChildAllocation(int index, Shape a)
Returns the child allocation for the child view with the specified index.
int
getHeight()
Return the current height of the box.
float
getMaximumSpan(int axis)
Returns the maximum span of this view along the specified axis.
float
getMinimumSpan(int axis)
Returns the minimum span of this view along the specified axis.
protected int
getOffset(int axis, int childIndex)
Returns the offset for the child view with the given index for the specified axis.
float
getPreferredSpan(int axis)
Returns the preferred span of the content managed by this View along the specified axis.
int
getResizeWeight(int axis)
Returns the resize weight of this view.
protected int
getSpan(int axis, int childIndex)
Returns the span for the child view with the given index for the specified axis.
protected View
getViewAtPoint(int x, int y, Rectangle r)
Returns the child View at the specified location.
int
getWidth()
Return the current width of the box.
protected boolean
isAfter(int x, int y, Rectangle r)
Returns true if the specified point lies after the given Rectangle, false otherwise.
protected boolean
isAllocationValid()
Returns true if the cached allocations for the children are still valid, false otherwise.
protected boolean
isBefore(int x, int y, Rectangle r)
Returns true if the specified point lies before the given Rectangle, false otherwise.
protected boolean
isLayoutValid(int axis)
Returns true if the layout along the specified axis is valid, false otherwise.
protected void
layout(int width, int height)
Lays out the children of this BoxView with the specified bounds.
void
layoutChanged(int axis)
Marks the layout along the specified axis as invalid.
protected void
layoutMajorAxis(int targetSpan, int axis, int[] offsets, int[] spans)
Performs the layout along the major axis of a BoxView.
protected void
layoutMinorAxis(int targetSpan, int axis, int[] offsets, int[] spans)
Performs the layout along the minor axis of a BoxView.
Shape
modelToView(int pos, Shape a, Position.Bias bias)
Maps the document model position pos to a Shape in the view coordinate space.
void
paint(Graphics g, Shape a)
Renders the Element that is associated with this View.
protected void
paintChild(Graphics g, Rectangle alloc, int index)
Paints the child View at the specified index.
void
preferenceChanged(View child, boolean width, boolean height)
Called by a child View when its preferred span has changed.
void
replace(int offset, int length, View[] views)
Replaces child views by some other child views.
void
setAxis(int axis)
Sets the axis along which this BoxView is laid out.
void
setSize(float width, float height)
Sets the size of the view.
int
viewToModel(float x, float y, Shape a, Position.Bias[] bias)
Maps coordinates from the View's space into a position in the document model.

Methods inherited from class javax.swing.text.CompositeView

childAllocation, flipEastAndWestAtEnds, getBottomInset, getChildAllocation, getInsideAllocation, getLeftInset, getNextEastWestVisualPositionFrom, getNextNorthSouthVisualPositionFrom, getNextVisualPositionFrom, getRightInset, getTopInset, getView, getViewAtPoint, getViewAtPosition, getViewCount, getViewIndex, getViewIndexAtPosition, isAfter, isBefore, loadChildren, modelToView, modelToView, replace, setInsets, setParagraphInsets, setParent, viewToModel

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

Constructor Details

BoxView

public BoxView(Element element,
               int axis)
Creates a new BoxView for the given Element and axis. Valid values for the axis are View.X_AXIS and View.Y_AXIS.
Parameters:
element - the element that is rendered by this BoxView
axis - the axis along which the box is laid out

Method Details

baselineLayout

protected void baselineLayout(int span,
                              int axis,
                              int[] offsets,
                              int[] spans)
Calculates the baseline layout of the children of this BoxView along the specified axis. This is not used by the BoxView itself, but by subclasses that wish to perform a baseline layout, like the FlowView's rows.
Parameters:
span - the target span
axis - the axis that is examined
offsets - an empty array, filled with the offsets of the children
spans - an empty array, filled with the spans of the children

baselineRequirements

protected SizeRequirements baselineRequirements(int axis,
                                                SizeRequirements sr)
Calculates size requirements for a baseline layout. This is not used by the BoxView itself, but by subclasses that wish to perform a baseline layout, like the FlowView's rows.
Parameters:
axis - the axis that is examined
sr - the SizeRequirements object to hold the result, if null, a new one is created
Returns:
the size requirements for this BoxView along the specified axis

calculateMajorAxisRequirements

protected SizeRequirements calculateMajorAxisRequirements(int axis,
                                                          SizeRequirements sr)
Calculates the size requirements of this BoxView along its major axis, that is the axis specified in the constructor.
Parameters:
axis - the axis that is examined
sr - the SizeRequirements object to hold the result, if null, a new one is created
Returns:
the size requirements for this BoxView along the specified axis

calculateMinorAxisRequirements

protected SizeRequirements calculateMinorAxisRequirements(int axis,
                                                          SizeRequirements sr)
Calculates the size requirements of this BoxView along its minor axis, that is the axis opposite to the axis specified in the constructor.
Parameters:
axis - the axis that is examined
sr - the SizeRequirements object to hold the result, if null, a new one is created
Returns:
the size requirements for this BoxView along the specified axis

childAllocation

protected void childAllocation(int index,
                               Rectangle a)
Computes the allocation for a child View. The parameter a stores the allocation of this CompositeView and is then adjusted to hold the allocation of the child view.
Overrides:
childAllocation in interface CompositeView
Parameters:
index - the index of the child View
a - the allocation of this CompositeView before the call, the allocation of the child on exit

flipEastAndWestAtEnds

protected boolean flipEastAndWestAtEnds(int position,
                                        Position.Bias bias)
Overrides:
flipEastAndWestAtEnds in interface CompositeView

forwardUpdate

protected void forwardUpdate(DocumentEvent.ElementChange ec,
                             DocumentEvent e,
                             Shape a,
                             ViewFactory vf)
Forwards the DocumentEvent to child views that need to get notified of the change to the model. This calles View.forwardUpdateToView(View,DocumentEvent,Shape,ViewFactory) for each View that must be forwarded to. If ec is not null (this means there have been structural changes to the element that this view is responsible for) this method should recognize this and don't notify newly added child views.
Overrides:
forwardUpdate in interface View
Parameters:
ec - the ElementChange describing the element changes (may be null if there were no changes)
vf - the ViewFactory used to create new Views
Since:
1.3

getAlignment

public float getAlignment(int axis)
Returns the alignment for this box view for the specified axis. The axis that is tiled (the major axis) will be requested to be aligned centered (0.5F). The minor axis alignment depends on the child view's total alignment.
Overrides:
getAlignment in interface View
Parameters:
axis - the axis which is examined
Returns:
the alignment for this box view for the specified axis

getAxis

public int getAxis()
Returns the axis along which this BoxView is laid out.
Returns:
the axis along which this BoxView is laid out
Since:
1.3

getChildAllocation

public Shape getChildAllocation(int index,
                                Shape a)
Returns the child allocation for the child view with the specified index. If the layout is invalid, this returns null.
Overrides:
getChildAllocation in interface CompositeView
Parameters:
index - the child view index
a - the allocation to this view
Returns:
the child allocation for the child view with the specified index or null if the layout is invalid or a is null

getHeight

public int getHeight()
Return the current height of the box. This is the last allocated height.
Returns:
the current height of the box

getMaximumSpan

public float getMaximumSpan(int axis)
Returns the maximum span of this view along the specified axis. This returns Integer.MAX_VALUE for the minor axis and the preferred span for the major 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 of this view along the specified axis. This calculates the minimum span using calculateMajorAxisRequirements(int,SizeRequirements) or calculateMinorAxisRequirements(int,SizeRequirements) (depending on the axis) and returns the resulting minimum span.
Overrides:
getMinimumSpan in interface View
Parameters:
axis - the axis
Returns:
the minimum span of this view along the specified axis

getOffset

protected int getOffset(int axis,
                        int childIndex)
Returns the offset for the child view with the given index for the specified axis.
Parameters:
axis - the axis to examine, either X_AXIS or Y_AXIS
childIndex - the index of the child for for which to return the span
Returns:
the offset for the child view with the given index for the specified axis

getPreferredSpan

public float getPreferredSpan(int axis)
Returns the preferred span of the content managed by this View along the specified axis.
Overrides:
getPreferredSpan in interface View
Parameters:
axis - the axis
Returns:
the preferred span of this View.

getResizeWeight

public int getResizeWeight(int axis)
Returns the resize weight of this view. A value of 0 or less means this view is not resizeable. Positive values make the view resizeable. This implementation returns 0 for the major axis and 1 for the minor axis of this box view.
Overrides:
getResizeWeight in interface View
Parameters:
axis - the axis
Returns:
the resizability of this view along the specified axis
Throws:
IllegalArgumentException - if axis is invalid

getSpan

protected int getSpan(int axis,
                      int childIndex)
Returns the span for the child view with the given index for the specified axis.
Parameters:
axis - the axis to examine, either X_AXIS or Y_AXIS
childIndex - the index of the child for for which to return the span
Returns:
the span for the child view with the given index for the specified axis

getViewAtPoint

protected View getViewAtPoint(int x,
                              int y,
                              Rectangle r)
Returns the child View at the specified location.
Overrides:
getViewAtPoint in interface CompositeView
Parameters:
x - the X coordinate
y - the Y coordinate
r - the inner allocation of this BoxView on entry, the allocation of the found child on exit
Returns:
the child View at the specified location

getWidth

public int getWidth()
Return the current width of the box. This is the last allocated width.
Returns:
the current width of the box

isAfter

protected boolean isAfter(int x,
                          int y,
                          Rectangle r)
Returns true if the specified point lies after the given Rectangle, false otherwise. "After" is typically defined as being to the right or below.
Overrides:
isAfter in interface CompositeView
Parameters:
x - the X coordinate of the point
y - the Y coordinate of the point
r - the rectangle to test the point against
Returns:
true if the specified point lies after the given Rectangle, false otherwise

isAllocationValid

protected boolean isAllocationValid()
Returns true if the cached allocations for the children are still valid, false otherwise.
Returns:
true if the cached allocations for the children are still valid, false otherwise

isBefore

protected boolean isBefore(int x,
                           int y,
                           Rectangle r)
Returns true if the specified point lies before the given Rectangle, false otherwise. "Before" is typically defined as being to the left or above.
Overrides:
isBefore in interface CompositeView
Parameters:
x - the X coordinate of the point
y - the Y coordinate of the point
r - the rectangle to test the point against
Returns:
true if the specified point lies before the given Rectangle, false otherwise

isLayoutValid

protected boolean isLayoutValid(int axis)
Returns true if the layout along the specified axis is valid, false otherwise. Valid values for the axis are View.X_AXIS and View.Y_AXIS.
Parameters:
axis - the axis
Returns:
true if the layout along the specified axis is valid, false otherwise
Since:
1.4

layout

protected void layout(int width,
                      int height)
Lays out the children of this BoxView with the specified bounds.
Parameters:
width - the width of the allocated region for the children (that is the inner allocation of this BoxView
height - the height of the allocated region for the children (that is the inner allocation of this BoxView

layoutChanged

public void layoutChanged(int axis)
Marks the layout along the specified axis as invalid. This is triggered automatically when any of the child view changes its preferences via preferenceChanged(View,boolean,boolean). The layout will be updated the next time when setSize(float,float) is called, typically from within the paint(Graphics,Shape) method. Valid values for the axis are View.X_AXIS and View.Y_AXIS.
Parameters:
axis - an int value
Since:
1.3

layoutMajorAxis

protected void layoutMajorAxis(int targetSpan,
                               int axis,
                               int[] offsets,
                               int[] spans)
Performs the layout along the major axis of a BoxView.
Parameters:
targetSpan - the (inner) span of the BoxView in which to layout the children
axis - the axis along which the layout is performed
offsets - the array that holds the offsets of the children on exit
spans - the array that holds the spans of the children on exit

layoutMinorAxis

protected void layoutMinorAxis(int targetSpan,
                               int axis,
                               int[] offsets,
                               int[] spans)
Performs the layout along the minor axis of a BoxView.
Parameters:
targetSpan - the (inner) span of the BoxView in which to layout the children
axis - the axis along which the layout is performed
offsets - the array that holds the offsets of the children on exit
spans - the array that holds the spans of the children on exit

modelToView

public Shape modelToView(int pos,
                         Shape a,
                         Position.Bias bias)
            throws BadLocationException
Maps the document model position pos to a Shape in the view coordinate space. This method overrides CompositeView's method to make sure the children are allocated properly before calling the super's behaviour.
Overrides:
modelToView in interface CompositeView

paint

public void paint(Graphics g,
                  Shape a)
Renders the Element that is associated with this View.
Overrides:
paint in interface View
Parameters:
g - the Graphics context to render to
a - the allocated region for the Element

paintChild

protected void paintChild(Graphics g,
                          Rectangle alloc,
                          int index)
Paints the child View at the specified index. This method modifies the actual values in alloc so make sure you have a copy of the original values if you need them.
Parameters:
g - the Graphics context to paint to
alloc - the allocated region for the child to paint into
index - the index of the child to be painted

preferenceChanged

public void preferenceChanged(View child,
                              boolean width,
                              boolean height)
Called by a child View when its preferred span has changed.
Overrides:
preferenceChanged in interface View
Parameters:
child - the child View.
width - indicates that the preferred width of the child changed.
height - indicates that the preferred height of the child changed.

replace

public void replace(int offset,
                    int length,
                    View[] views)
Replaces child views by some other child views. If there are no views to remove (length == 0), the result is a simple insert, if there are no children to add (view == null) the result is a simple removal. In addition this invalidates the layout and resizes the internal cache for the child allocations. The old children's cached allocations can still be accessed (although they are not guaranteed to be valid), and the new children will have an initial offset and span of 0.
Overrides:
replace in interface CompositeView
Parameters:
offset - the start offset from where to remove children
length - the number of children to remove
views - the views that replace the removed children

setAxis

public void setAxis(int axis)
Sets the axis along which this BoxView is laid out. Valid values for the axis are View.X_AXIS and View.Y_AXIS.
Parameters:
axis - the axis along which this BoxView is laid out
Since:
1.3

setSize

public void setSize(float width,
                    float height)
Sets the size of the view. If the actual size has changed, the layout is updated accordingly.
Overrides:
setSize in interface View
Parameters:
width - the new width
height - the new height

viewToModel

public int viewToModel(float x,
                       float y,
                       Shape a,
                       Position.Bias[] bias)
Maps coordinates from the View's space into a position in the document model.
Overrides:
viewToModel in interface CompositeView
Parameters:
x - the x coordinate in the view space
y - the y coordinate in the view space
a - the allocation of this View
Returns:
the position in the document that corresponds to the screen coordinates x, y

BoxView.java -- An composite view Copyright (C) 2005, 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.