edu.northwestern.at.utils.swing
Class JustifiedTextRowView

java.lang.Object
  extended by javax.swing.text.View
      extended by javax.swing.text.CompositeView
          extended by javax.swing.text.BoxView
              extended by edu.northwestern.at.utils.swing.JustifiedTextRowView
All Implemented Interfaces:
javax.swing.SwingConstants

public class JustifiedTextRowView
extends javax.swing.text.BoxView

A variant of a BoxView which knows how to lay out fully justified styled text.


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
JustifiedTextRowView(javax.swing.text.Element elem)
          Create a justified text row view.
 
Method Summary
protected  javax.swing.SizeRequirements calculateMinorAxisRequirements(int axis, javax.swing.SizeRequirements r)
          Calculates the size requirements for the minor axis.
protected static int containsPaddableSpace(javax.swing.text.View view)
          Check if child view contains a paddable space.
 float getAlignment(int axis)
          Determines the desired alignment for this view along an axis.
 javax.swing.text.AttributeSet getAttributes()
          Get attributes for this view.
protected static java.lang.String getDocumentTextForView(javax.swing.text.View view)
          Get document text contained in a view.
 int getEndOffset()
          Get end of portion of the model for which this view is responsible.
 short getLeftInset()
          Override to make getLeftInset public.
 float getMaximumSpan(int axis)
          Determine maximum span for this view along an axis.
 float getMinimumSpan(int axis)
          Determine minimum span for this view along an axis.
protected  int[] getPadPixels(int space, int viewsWithBlanks, int direction)
          Determine distribution of pad pixels for text justification.
 float getPreferredSpan(int axis)
          Determine preferred span for this view along an axis.
 short getRightInset()
          Override to make getRightInset public.
 int getRowNumber()
          Get row number for this view.
 int getStartOffset()
          Get start of portion of the model for which this view is responsible.
 short getTopInset()
          Override to make getTopInset public.
protected  int getViewIndexAtPosition(int pos)
          Get child view index representing the given position in the model.
protected  void layoutMajorAxis(int targetSpan, int axis, int[] offsets, int[] spans)
          Perform layout for the major axis of the box.
protected  void layoutMinorAxis(int targetSpan, int axis, int[] offsets, int[] spans)
          Perform layout for the minor axis of the box.
protected  void loadChildren(javax.swing.text.ViewFactory f)
          Load child views.
 java.awt.Shape modelToView(int pos, java.awt.Shape a, javax.swing.text.Position.Bias b)
          Map from document model coordinate space to coordinate space of the view mapped to it.
 void setInsets(short topInset, short leftInset, short bottomInset, short rightInset)
          Override to make setInsets public.
 void setRowNumber(int rowNumber)
          Set row number of this view.
 
Methods inherited from class javax.swing.text.BoxView
baselineLayout, baselineRequirements, calculateMajorAxisRequirements, childAllocation, flipEastAndWestAtEnds, forwardUpdate, getAxis, getChildAllocation, getHeight, getOffset, getResizeWeight, getSpan, getViewAtPoint, getWidth, isAfter, isAllocationValid, isBefore, isLayoutValid, layout, layoutChanged, paint, paintChild, preferenceChanged, replace, setAxis, setSize, viewToModel
 
Methods inherited from class javax.swing.text.CompositeView
getBottomInset, getInsideAllocation, getNextEastWestVisualPositionFrom, getNextNorthSouthVisualPositionFrom, getNextVisualPositionFrom, getView, getViewAtPosition, getViewCount, getViewIndex, modelToView, setParagraphInsets, setParent
 
Methods inherited from class javax.swing.text.View
append, breakView, changedUpdate, createFragment, forwardUpdateToView, getBreakWeight, getContainer, getDocument, getElement, getGraphics, getParent, getToolTipText, getViewFactory, getViewIndex, insert, insertUpdate, isVisible, modelToView, remove, removeAll, removeUpdate, updateChildren, updateLayout, viewToModel
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JustifiedTextRowView

JustifiedTextRowView(javax.swing.text.Element elem)
Create a justified text row view.

Parameters:
elem - The text line element for which to create a view.
Method Detail

calculateMinorAxisRequirements

protected javax.swing.SizeRequirements calculateMinorAxisRequirements(int axis,
                                                                      javax.swing.SizeRequirements r)
Calculates the size requirements for the minor axis.

Overrides:
calculateMinorAxisRequirements in class javax.swing.text.BoxView
Parameters:
axis - The axis being studied.
r - The SizeRequirements object; if null one will be created.
Returns:
The newly initialized SizeRequirements object.
See Also:
SizeRequirements

containsPaddableSpace

protected static int containsPaddableSpace(javax.swing.text.View view)
Check if child view contains a paddable space.

Parameters:
view - The view.
Returns:
= 0: child does not have a paddable space. = 1: child has a trailing paddable space. = -1: child has a leading paddable space.

getAlignment

public float getAlignment(int axis)
Determines the desired alignment for this view along an axis.

Overrides:
getAlignment in class javax.swing.text.BoxView
Parameters:
axis - May be either View.X_AXIS or View.Y_AXIS .
Returns:
The desired alignment. 0.0 for left and fully justified alignment. 0.5 for center alignment. 1.0 for right alignment.
Throws:
java.lang.IllegalArgumentException - for an invalid axis .

getAttributes

public javax.swing.text.AttributeSet getAttributes()
Get attributes for this view.

Overrides:
getAttributes in class javax.swing.text.View
Returns:
The attributes. May be null.

getEndOffset

public int getEndOffset()
Get end of portion of the model for which this view is responsible.

Overrides:
getEndOffset in class javax.swing.text.View
Returns:
The ending offset into the model >= 0 .

getLeftInset

public short getLeftInset()
Override to make getLeftInset public.

Overrides:
getLeftInset in class javax.swing.text.CompositeView

getMaximumSpan

public float getMaximumSpan(int axis)
Determine maximum span for this view along an axis.

Overrides:
getMaximumSpan in class javax.swing.text.BoxView
Parameters:
axis - Either View.X_AXIS or View.Y_AXIS .
Returns:
Maximum span the view can be rendered into.

getMinimumSpan

public float getMinimumSpan(int axis)
Determine minimum span for this view along an axis.

Overrides:
getMinimumSpan in class javax.swing.text.BoxView
Parameters:
axis - Either View.X_AXIS or View.Y_AXIS .
Returns:
Minimum span the view can be rendered into.

getPadPixels

protected int[] getPadPixels(int space,
                             int viewsWithBlanks,
                             int direction)
Determine distribution of pad pixels for text justification.

Parameters:
space - The number of pad pixels required to justify a text line.
viewsWithBlanks - The number of views with paddable blanks to which we can allocate pad pixels.
direction - = 0: Pad lefthand spaces more heavily = 1: Pad righthand spaces more heavily.
Returns:
An array of containing the number of pad pixels to add to each view with paddable blanks.

Successive lines of displayed text alternate between direction=0 and direction=1 . This helps to even out the appearance of the padding by having it on the left end on one line and the right end on the next.


getPreferredSpan

public float getPreferredSpan(int axis)
Determine preferred span for this view along an axis.

Overrides:
getPreferredSpan in class javax.swing.text.BoxView
Parameters:
axis - Either View.X_AXIS or View.Y_AXIS .
Returns:
Preferred span the view can be rendered into.

getRightInset

public short getRightInset()
Override to make getRightInset public.

Overrides:
getRightInset in class javax.swing.text.CompositeView

getRowNumber

public int getRowNumber()
Get row number for this view.

Returns:
Row number of this view.

getStartOffset

public int getStartOffset()
Get start of portion of the model for which this view is responsible.

Overrides:
getStartOffset in class javax.swing.text.View
Returns:
The starting offset into the model >= 0 .

getTopInset

public short getTopInset()
Override to make getTopInset public.

Overrides:
getTopInset in class javax.swing.text.CompositeView

getViewIndexAtPosition

protected int getViewIndexAtPosition(int pos)
Get child view index representing the given position in the model.

Overrides:
getViewIndexAtPosition in class javax.swing.text.CompositeView
Parameters:
pos - The position >= 0 .
Returns:
Index of the view at the given position, or -1 if no view is at that position.

getDocumentTextForView

protected static java.lang.String getDocumentTextForView(javax.swing.text.View view)
Get document text contained in a view.

Parameters:
view - The view.
Returns:
The document text. Empty if none.

layoutMajorAxis

protected void layoutMajorAxis(int targetSpan,
                               int axis,
                               int[] offsets,
                               int[] spans)
Perform layout for the major axis of the box.

Overrides:
layoutMajorAxis in class javax.swing.text.BoxView
Parameters:
targetSpan - The total span given to the view, which should be used to layout the children.
axis - The axis being layed out.
offsets - The offsets from the origin of the view for each of the child views. This is a return value and is filled in here.
spans - The span of each child view. This is a return value and is filled in here.

The offset and span for each child view is stored in the offsets and spans parameters.

This is where the actual text justification occurs. Left, center, and right alignment are handled by the superclass in the usual fashion. The superclass is set up to treat full alignment the same as left alignment.

Once the text has been left aligned, this method determines how many extra pixels (if any) are needed to fully justify each line. Those pixels are distributed as evenly as possible to the existing blank spaces in the text. This results in fully justified text.


layoutMinorAxis

protected void layoutMinorAxis(int targetSpan,
                               int axis,
                               int[] offsets,
                               int[] spans)
Perform layout for the minor axis of the box.

Overrides:
layoutMinorAxis in class javax.swing.text.BoxView
Parameters:
targetSpan - The total span given to the view, which should be used to layout the children.
axis - The axis being layed out.
offsets - The offsets from the origin of the view for each of the child views. This is a return value and is filled in here.
spans - The span of each child view. This is a return value and is filled in here.

The offset and span for each child view is stored in the offsets and spans parameters.


loadChildren

protected void loadChildren(javax.swing.text.ViewFactory f)
Load child views. No-op here.

Overrides:
loadChildren in class javax.swing.text.CompositeView

modelToView

public java.awt.Shape modelToView(int pos,
                                  java.awt.Shape a,
                                  javax.swing.text.Position.Bias b)
                           throws javax.swing.text.BadLocationException
Map from document model coordinate space to coordinate space of the view mapped to it.

Overrides:
modelToView in class javax.swing.text.BoxView
Parameters:
pos - The position to convert >= 0.
a - The allocated region to render into.
b - The position bias value of either Position.Bias.Forward or Position.Bias.Backward .
Returns:
The bounding box of the given position.
Throws:
javax.swing.text.BadLocationException - If the given position does not represent a valid location in the associated document.

setInsets

public void setInsets(short topInset,
                      short leftInset,
                      short bottomInset,
                      short rightInset)
Override to make setInsets public.

Overrides:
setInsets in class javax.swing.text.CompositeView

setRowNumber

public void setRowNumber(int rowNumber)
Set row number of this view.

Parameters:
rowNumber - The row number of this view.