com.lightdev.app.shtm
Class SHTMLTableView

java.lang.Object
  extended by javax.swing.text.View
      extended by javax.swing.text.CompositeView
          extended by javax.swing.text.BoxView
              extended by com.lightdev.app.shtm.SHTMLTableView
All Implemented Interfaces:
javax.swing.SwingConstants, javax.swing.text.ViewFactory

public class SHTMLTableView
extends javax.swing.text.BoxView
implements javax.swing.text.ViewFactory

HTML table view copied from javax.swing.text.html.TableView.java. The original intention was to implement a box painter other than StyleSheet.BoxPainter which obviously is not complete. This could have been a simple task just overriding TableView.create with our own ViewFactory implementing an alternate BoxPainter. Unfortunately while create is public, TableView itself is not (why???). We have to copy the whole thing and just adapt one line to make inner class CellView a subclass of our own SHTMLBlockView instead of javax.swing.text.html.BlockView. To make things worse, TableView uses some methods from class CSS which are not public as well (again not clear why) so these had to be copied into this class too. All in all a totally ugly solution which hopefully can be corrected once Sun decided to tidy up the CSS part of Swing.


Nested Class Summary
 class SHTMLTableView.RowView
          View of a row in a row-centric table.
 
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
SHTMLTableView(javax.swing.text.Element elem)
          Constructs a TableView for the given element.
 
Method Summary
protected  javax.swing.SizeRequirements calculateMajorAxisRequirements(int axis, javax.swing.SizeRequirements r)
          Calculate the requirements for the major axis.
protected  javax.swing.SizeRequirements calculateMinorAxisRequirements(int axis, javax.swing.SizeRequirements r)
          Calculate the requirements for the minor axis.
 void changedUpdate(javax.swing.event.DocumentEvent e, java.awt.Shape a, javax.swing.text.ViewFactory f)
          Gives notification from the document that attributes were changed in a location that this view is responsible for.
 javax.swing.text.View create(javax.swing.text.Element elem)
          The table itself acts as a factory for the various views that actually represent pieces of the table.
protected  SHTMLTableView.RowView createTableRow(javax.swing.text.Element elem)
          Creates a new table row.
protected  void forwardUpdate(javax.swing.event.DocumentEvent.ElementChange ec, javax.swing.event.DocumentEvent e, java.awt.Shape a, javax.swing.text.ViewFactory f)
           
 javax.swing.text.AttributeSet getAttributes()
          Fetches the attributes to use when rendering.
 int getColumnCount()
          The number of columns in the table.
protected  int getColumnsOccupied(javax.swing.text.View v)
          Determines the number of columns occupied by the table cell represented by given element.
 int getColumnSpan(int col)
          Fetches the span (width) of the given column.
 int getMultiRowSpan(int row0, int row1)
          Fetch the span of multiple rows.
 int getRowCount()
          The number of rows in the table.
protected  int getRowsOccupied(javax.swing.text.View v)
          Determines the number of rows occupied by the table cell represented by given element.
 int getRowSpan(int row)
          Fetches the span (height) of the given row.
protected  javax.swing.text.html.StyleSheet getStyleSheet()
           
protected  javax.swing.text.View getViewAtPosition(int pos, java.awt.Rectangle a)
          Fetches the child view that represents the given position in the model.
 javax.swing.text.ViewFactory getViewFactory()
          Fetches the ViewFactory implementation that is feeding the view hierarchy.
 void insertUpdate(javax.swing.event.DocumentEvent e, java.awt.Shape a, javax.swing.text.ViewFactory f)
          Gives notification that something was inserted into the document in a location that this view is responsible for.
protected  void invalidateGrid()
           
protected  void layoutColumns(int targetSpan, int[] offsets, int[] spans, javax.swing.SizeRequirements[] reqs)
          Layout the columns to fit within the given target span.
protected  void layoutMajorAxis(int targetSpan, int axis, int[] offsets, int[] spans)
          Perform layout for the major axis of the box (i.e.
protected  void layoutMinorAxis(int targetSpan, int axis, int[] offsets, int[] spans)
          Perform layout for the minor axis of the box (i.e.
 void paint(java.awt.Graphics g, java.awt.Shape allocation)
          Renders using the given rendering surface and area on that surface.
 void removeUpdate(javax.swing.event.DocumentEvent e, java.awt.Shape a, javax.swing.text.ViewFactory f)
          Gives notification that something was removed from the document in a location that this view is responsible for.
 void replace(int offset, int length, javax.swing.text.View[] views)
          Change the child views.
 void setParent(javax.swing.text.View parent)
          Establishes the parent view for this view.
protected  void setPropertiesFromAttributes()
          Update any cached values that come from attributes.
 
Methods inherited from class javax.swing.text.BoxView
baselineLayout, baselineRequirements, childAllocation, flipEastAndWestAtEnds, getAlignment, getAxis, getChildAllocation, getHeight, getMaximumSpan, getMinimumSpan, getOffset, getPreferredSpan, getResizeWeight, getSpan, getViewAtPoint, getWidth, isAfter, isAllocationValid, isBefore, isLayoutValid, layout, layoutChanged, modelToView, paintChild, preferenceChanged, setAxis, setSize, viewToModel
 
Methods inherited from class javax.swing.text.CompositeView
getBottomInset, getInsideAllocation, getLeftInset, getNextEastWestVisualPositionFrom, getNextNorthSouthVisualPositionFrom, getNextVisualPositionFrom, getRightInset, getTopInset, getView, getViewCount, getViewIndex, getViewIndexAtPosition, loadChildren, modelToView, setInsets, setParagraphInsets
 
Methods inherited from class javax.swing.text.View
append, breakView, createFragment, forwardUpdateToView, getBreakWeight, getContainer, getDocument, getElement, getEndOffset, getGraphics, getParent, getStartOffset, getToolTipText, getViewIndex, insert, isVisible, modelToView, remove, removeAll, updateChildren, updateLayout, viewToModel
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SHTMLTableView

public SHTMLTableView(javax.swing.text.Element elem)
Constructs a TableView for the given element.

Parameters:
elem - the element that this view is responsible for
Method Detail

createTableRow

protected SHTMLTableView.RowView createTableRow(javax.swing.text.Element elem)
Creates a new table row.

Parameters:
elem - an element
Returns:
the row

getColumnCount

public int getColumnCount()
The number of columns in the table.


getColumnSpan

public int getColumnSpan(int col)
Fetches the span (width) of the given column. This is used by the nested cells to query the sizes of grid locations outside of themselves.


getRowCount

public int getRowCount()
The number of rows in the table.


getMultiRowSpan

public int getMultiRowSpan(int row0,
                           int row1)
Fetch the span of multiple rows. This includes the border area.


getRowSpan

public int getRowSpan(int row)
Fetches the span (height) of the given row.


getColumnsOccupied

protected int getColumnsOccupied(javax.swing.text.View v)
Determines the number of columns occupied by the table cell represented by given element.


getRowsOccupied

protected int getRowsOccupied(javax.swing.text.View v)
Determines the number of rows occupied by the table cell represented by given element.


invalidateGrid

protected void invalidateGrid()

getStyleSheet

protected javax.swing.text.html.StyleSheet getStyleSheet()

setPropertiesFromAttributes

protected void setPropertiesFromAttributes()
Update any cached values that come from attributes.


layoutColumns

protected void layoutColumns(int targetSpan,
                             int[] offsets,
                             int[] spans,
                             javax.swing.SizeRequirements[] reqs)
Layout the columns to fit within the given target span.

Parameters:
targetSpan - the given span for total of all the table columns
reqs - the requirements desired for each column. This is the column maximum of the cells minimum, preferred, and maximum requested span
spans - the return value of how much to allocated to each column
offsets - the return value of the offset from the origin for each column

calculateMinorAxisRequirements

protected javax.swing.SizeRequirements calculateMinorAxisRequirements(int axis,
                                                                      javax.swing.SizeRequirements r)
Calculate the requirements for the minor axis. This is called by the superclass whenever the requirements need to be updated (i.e. a preferenceChanged was messaged through this view).

This is implemented to calculate the requirements as the sum of the requirements of the columns and then adjust it if the CSS width or height attribute is specified and applicable to the axis.

Overrides:
calculateMinorAxisRequirements in class javax.swing.text.BoxView

calculateMajorAxisRequirements

protected javax.swing.SizeRequirements calculateMajorAxisRequirements(int axis,
                                                                      javax.swing.SizeRequirements r)
Calculate the requirements for the major axis. This is called by the superclass whenever the requirements need to be updated (i.e. a preferenceChanged was messaged through this view).

This is implemented to provide the superclass behavior adjusted for multi-row table cells.

Overrides:
calculateMajorAxisRequirements in class javax.swing.text.BoxView

layoutMinorAxis

protected void layoutMinorAxis(int targetSpan,
                               int axis,
                               int[] offsets,
                               int[] spans)
Perform layout for the minor axis of the box (i.e. the axis orthoginal to the axis that it represents). The results of the layout should be placed in the given arrays which represent the allocations to the children along the minor axis. This is called by the superclass whenever the layout needs to be updated along the minor axis.

This is implemented to call the layoutColumns method, and then forward to the superclass to actually carry out the layout of the tables rows.

Overrides:
layoutMinorAxis in class javax.swing.text.BoxView
Parameters:
targetSpan - the total span given to the view, which whould 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 by the implementation of this method
spans - the span of each child view; this is a return value and is filled in by the implementation of this method

layoutMajorAxis

protected void layoutMajorAxis(int targetSpan,
                               int axis,
                               int[] offsets,
                               int[] spans)
Perform layout for the major axis of the box (i.e. the axis that it represents). The results of the layout should be placed in the given arrays which represent the allocations to the children along the minor axis. This is called by the superclass whenever the layout needs to be updated along the minor axis.

This method is where the layout of the table rows within the table takes place. This method is implemented to call the use the RowIterator and the CSS collapsing tile to layout with border spacing and border collapsing capabilities.

Overrides:
layoutMajorAxis in class javax.swing.text.BoxView
Parameters:
targetSpan - the total span given to the view, which whould 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 by the implementation of this method
spans - the span of each child view; this is a return value and is filled in by the implementation of this method

getViewAtPosition

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 walk through the children looking for a range that contains the given position. In this view the children do not necessarily have a one to one mapping with the child elements.

Overrides:
getViewAtPosition in class javax.swing.text.CompositeView
Parameters:
pos - the search position >= 0
a - the allocation to the table on entry, and the allocation of the view containing the position on exit
Returns:
the view representing the given position, or null if there isn't one

getAttributes

public javax.swing.text.AttributeSet getAttributes()
Fetches the attributes to use when rendering. This is implemented to multiplex the attributes specified in the model with a StyleSheet.

Overrides:
getAttributes in class javax.swing.text.View

paint

public void paint(java.awt.Graphics g,
                  java.awt.Shape allocation)
Renders using the given rendering surface and area on that surface. This is implemented to delegate to the css box painter to paint the border and background prior to the interior. The superclass culls rendering the children that don't directly intersect the clip and the row may have cells hanging from a row above in it. The table does not use the superclass rendering behavior and instead paints all of the rows and lets the rows cull those cells not intersecting the clip region.

Overrides:
paint in class javax.swing.text.BoxView
Parameters:
g - the rendering surface to use
allocation - the allocated region to render into
See Also:
SHTMLTableView.RowView.paint(java.awt.Graphics, java.awt.Shape)

setParent

public void setParent(javax.swing.text.View parent)
Establishes the parent view for this view. This is guaranteed to be called before any other methods if the parent view is functioning properly.

This is implemented to forward to the superclass as well as call the setPropertiesFromAttributes method to set the paragraph properties from the css attributes. The call is made at this time to ensure the ability to resolve upward through the parents view attributes.

Overrides:
setParent in class javax.swing.text.CompositeView
Parameters:
parent - the new parent, or null if the view is being removed from a parent it was previously added to

getViewFactory

public javax.swing.text.ViewFactory getViewFactory()
Fetches the ViewFactory implementation that is feeding the view hierarchy. This replaces the ViewFactory with an implementation that calls through to the createTableRow and createTableCell methods. If the element given to the factory isn't a table row or cell, the request is delegated to the factory produced by the superclass behavior.

Overrides:
getViewFactory in class javax.swing.text.View
Returns:
the factory, null if none

insertUpdate

public void insertUpdate(javax.swing.event.DocumentEvent e,
                         java.awt.Shape a,
                         javax.swing.text.ViewFactory f)
Gives notification that something was inserted into the document in a location that this view is responsible for. This replaces the ViewFactory with an implementation that calls through to the createTableRow and createTableCell methods. If the element given to the factory isn't a table row or cell, the request is delegated to the factory passed as an argument.

Overrides:
insertUpdate in class javax.swing.text.View
Parameters:
e - the change information from the associated document
a - the current allocation of the view
f - the factory to use to rebuild if the view has children
See Also:
View.insertUpdate(javax.swing.event.DocumentEvent, java.awt.Shape, javax.swing.text.ViewFactory)

removeUpdate

public void removeUpdate(javax.swing.event.DocumentEvent e,
                         java.awt.Shape a,
                         javax.swing.text.ViewFactory f)
Gives notification that something was removed from the document in a location that this view is responsible for. This replaces the ViewFactory with an implementation that calls through to the createTableRow and createTableCell methods. If the element given to the factory isn't a table row or cell, the request is delegated to the factory passed as an argument.

Overrides:
removeUpdate in class javax.swing.text.View
Parameters:
e - the change information from the associated document
a - the current allocation of the view
f - the factory to use to rebuild if the view has children
See Also:
View.removeUpdate(javax.swing.event.DocumentEvent, java.awt.Shape, javax.swing.text.ViewFactory)

changedUpdate

public void changedUpdate(javax.swing.event.DocumentEvent e,
                          java.awt.Shape a,
                          javax.swing.text.ViewFactory f)
Gives notification from the document that attributes were changed in a location that this view is responsible for. This replaces the ViewFactory with an implementation that calls through to the createTableRow and createTableCell methods. If the element given to the factory isn't a table row or cell, the request is delegated to the factory passed as an argument.

Overrides:
changedUpdate in class javax.swing.text.View
Parameters:
e - the change information from the associated document
a - the current allocation of the view
f - the factory to use to rebuild if the view has children
See Also:
SHTMLTableView.RowView.changedUpdate(javax.swing.event.DocumentEvent, java.awt.Shape, javax.swing.text.ViewFactory)

forwardUpdate

protected void forwardUpdate(javax.swing.event.DocumentEvent.ElementChange ec,
                             javax.swing.event.DocumentEvent e,
                             java.awt.Shape a,
                             javax.swing.text.ViewFactory f)
Overrides:
forwardUpdate in class javax.swing.text.BoxView

replace

public void replace(int offset,
                    int length,
                    javax.swing.text.View[] views)
Change the child views. This is implemented to provide the superclass behavior and invalidate the grid so that rows and columns will be recalculated.

Overrides:
replace in class javax.swing.text.BoxView

create

public javax.swing.text.View create(javax.swing.text.Element elem)
The table itself acts as a factory for the various views that actually represent pieces of the table. All other factory activity is delegated to the factory returned by the parent of the table.

Specified by:
create in interface javax.swing.text.ViewFactory