Copyright © 2005 Scientific Applications. All Rights Reserved.

com.sciapp.table
Class TableAssistant

java.lang.Object
  extended by com.sciapp.table.TableAssistant

public class TableAssistant
extends Object

TableAssistant is a class that provides additional functions to a JTable. More specifically:

1) It automatically resizes the column of a table to the greatest preferred width of all cells under that column, when the column is double-clicked on its border.

2) Provides a popup through which the columns of the table can be dynamically added/removed. The popup is shown upon right-clicking on the table header.


Field Summary
protected  boolean allowEditColumn
          flag to indicate whether users are able to rename columns in the column dialog
protected  boolean allowReorder
          flag to indicate whether users are able to reorder columns in the column dialog
protected  int columnMargin
          the pixels to add to the columns that are resized
protected  int columnPopupOrder
          defines the ordering of column names in the column popup
static int CONTENT_RESIZE
          integer indicating that the column will be resized to its contents should the user double-click on its border
static int DEFAULT_COLUMN_MARGIN
          the default column margin
protected  Cursor defaultHeaderCursor
          the header's default cursor
protected  boolean groupingEnabled
          flag to indicate whether users are able to group/ungroup columns in the column dialog
static int HEADER_RESIZE
          integer indicating that the column will be resized to its header contents should the user double-click on its border
static int IF_LARGER_RESIZE
          integer indicating that the column will be resized only if the calculated width is larger than the current one
protected  int maxColumns
          the maximum number of columns that the column popup is going to display.
static int MODEL_ORDER
          defines a column ordering, according to the position of the columns as they appear in the table's data model
static int NO_RESIZE
          integer indicating that nothing will happen should the user double-click on a column border
static int PUSH_INVISIBLE_TO_END
          a modifier used by the column popup, in which non-visible columns are pushed towards the end of the popup
protected  int resizeMode
          the resize mode
protected  boolean showMore
          flag to indicate whether a menuitem is to be added to the column popup, that provides more column options.
static int UNLIMITED_COLUMNS
          integer that specifies that an unlimited number of columns will be added to the column popup.
static int VIEW_ORDER
          defines a column ordering, according to the position of the columns as they appear in the table's column model (the view)
 
Constructor Summary
TableAssistant(JTable table)
          Constructs a TableAssistant having table as the JTable on which this object acts.
 
Method Summary
protected  int calcContentWidth(int column)
          Calculates the preferred width for a column, based on the its cell contents.
protected  int calcHeaderWidth(int column)
          Calculates the preferred width for a column, based on the content of its header.
protected  JPopupMenu createPopup()
          Returns a new instance of the column popup menu.
 void deselect(int modelIndex)
          Unchecks the column with model index modelIndex from the column popup.
 boolean getAllowEditColumn()
          Determines whether users are able to rename columns in the column dialog.
 boolean getAllowReorder()
          Determines whether users are able to reorder columns in the column dialog.
 int getColumnMargin()
          Determines the pixels to increase the width of a column by, when it is being automatically resized with the resizeColumnToHeader(int) or resizeColumnToContents(int) methods.
 JPopupMenu getColumnPopup()
          Returns the popup menu displayed.
 int getColumnPopupOrder()
          Retrieves the mode according to which, columns in the column popup will be ordered.
 int[] getColumnPositions(TableColumn[] columns, boolean[] isVisible)
          Retrieves the position in the column popup for columns.
 Cursor getDefaultHeaderCursor()
          Retrieves the default header's cursor.
 boolean getGroupingEnabled()
          Determines whether users are able to group/ungroup columns in the column dialog.
 int getMaxColumns()
          Returns the maximum number of columns that the column popup is going to display.
 JDialog getMoreDialog()
          Returns an instance of the 'More...' dialog.
 int getResizeMode()
          Determines the current resize mode, when a user double clicks on a column border.
 boolean getShowMore()
          Returns a flag that is true if a menuitem (more) is to be added to the column popup, that provides more column options.
 boolean getShowPopup()
          Returns true if the column popup is to be shown.
 JTable getTable()
          Returns the table that is associated with the assistant.
 void refreshColumns()
          Recreates the column selection popup and dialog with values from the column model.
 void refreshPopup()
          Reinitializes the popup with values from the column model.
 void register(TableColumnModel columnModel)
          The component registers itself to the listener list of columnModel.
 void resizeColumn(int column)
          Resizes the given column according to the current resize mode.
 void resizeColumn(int column, int resizeMode)
          Resizes the given column according to the resize mode passed as argument to the method.
 void resizeColumns()
          Resizes all columns according to the current resize mode.
 void resizeColumns(int resizeMode)
          Resizes all columns according to the resize mode passed as argument to the method.
 void resizeColumnsToContents()
          Resizes all columns to their contents.
 void resizeColumnsToHeader()
          Resizes all columns to the content of their header.
 void resizeColumnToContents(int column)
          Resizes the given column to its contents.
 void resizeColumnToHeader(int column)
          Resizes the given column to the content of its header.
 void select(int modelIndex)
          Checks the column with model index modelIndex from the column popup.
 void setAllowEditColumn(boolean allowEditColumn)
          Sets a flag that is true if users are able to rename columns in the column dialog.
 void setAllowReorder(boolean allowReorder)
          Sets a flag that is true if users are able to reorder columns in the column dialog.
 void setColumnMargin(int columnMargin)
          Assigns the pixels to increase the width of a column by, when it is being automatically resized with the resizeColumnToHeader(int) or resizeColumnToContents(int) methods.
 void setColumnPopupOrder(int columnPopupOrder)
          Assigns the mode according to which, columns in the column popup will be ordered.
 void setColumnWidth(int column, int width)
          Sets a new column width for the specified column.
 void setDefaultHeaderCursor(Cursor cursor)
          Assigns the default headers' cursor.
 void setGroupingEnabled(boolean groupingEnabled)
          Sets a flag that is true if users are able to group/ungroup columns in the column dialog.
 void setHeader(JTableHeader newTableHeader)
          Associates newTableHeader with this instance of TableAssistant.
 void setMaxColumns(int maxColumns)
          Sets the maximum number of columns that the column popup is going to display.
 void setResizeMode(int resizeMode)
          Assigns the resize mode, when a user double clicks on a column border.
 void setShowMore(boolean showMore)
          Sets a flag that is true if a menuitem (more) is to be added to the column popup, that provides more column options.
 void setShowPopup(boolean showPopup)
          Sets a flag that is true if the column popup is to be shown.
 void setTable(JTable newTable)
          Assigns a new table to the assistant.
 void showColumnDialog()
          Makes the column dialog visible.
 void unregister(TableColumnModel columnModel)
          The component unregisters itself from the listener list of columnModel.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

columnPopupOrder

protected int columnPopupOrder
defines the ordering of column names in the column popup


MODEL_ORDER

public static final int MODEL_ORDER
defines a column ordering, according to the position of the columns as they appear in the table's data model

See Also:
Constant Field Values

VIEW_ORDER

public static final int VIEW_ORDER
defines a column ordering, according to the position of the columns as they appear in the table's column model (the view)

See Also:
Constant Field Values

PUSH_INVISIBLE_TO_END

public static final int PUSH_INVISIBLE_TO_END
a modifier used by the column popup, in which non-visible columns are pushed towards the end of the popup

See Also:
Constant Field Values

allowReorder

protected boolean allowReorder
flag to indicate whether users are able to reorder columns in the column dialog


groupingEnabled

protected boolean groupingEnabled
flag to indicate whether users are able to group/ungroup columns in the column dialog


allowEditColumn

protected boolean allowEditColumn
flag to indicate whether users are able to rename columns in the column dialog


NO_RESIZE

public static final int NO_RESIZE
integer indicating that nothing will happen should the user double-click on a column border

See Also:
Constant Field Values

CONTENT_RESIZE

public static final int CONTENT_RESIZE
integer indicating that the column will be resized to its contents should the user double-click on its border

See Also:
Constant Field Values

HEADER_RESIZE

public static final int HEADER_RESIZE
integer indicating that the column will be resized to its header contents should the user double-click on its border

See Also:
Constant Field Values

IF_LARGER_RESIZE

public static final int IF_LARGER_RESIZE
integer indicating that the column will be resized only if the calculated width is larger than the current one

See Also:
Constant Field Values

DEFAULT_COLUMN_MARGIN

public static final int DEFAULT_COLUMN_MARGIN
the default column margin

See Also:
Constant Field Values

columnMargin

protected int columnMargin
the pixels to add to the columns that are resized


resizeMode

protected int resizeMode
the resize mode


showMore

protected boolean showMore
flag to indicate whether a menuitem is to be added to the column popup, that provides more column options.


defaultHeaderCursor

protected Cursor defaultHeaderCursor
the header's default cursor


UNLIMITED_COLUMNS

public static final int UNLIMITED_COLUMNS
integer that specifies that an unlimited number of columns will be added to the column popup.

See Also:
Constant Field Values

maxColumns

protected int maxColumns
the maximum number of columns that the column popup is going to display.

Constructor Detail

TableAssistant

public TableAssistant(JTable table)
Constructs a TableAssistant having table as the JTable on which this object acts.

Parameters:
table - the table
Method Detail

calcContentWidth

protected int calcContentWidth(int column)
Calculates the preferred width for a column, based on the its cell contents.

Parameters:
column - the column for which the width is calculated
Returns:
the preferred width

calcHeaderWidth

protected int calcHeaderWidth(int column)
Calculates the preferred width for a column, based on the content of its header.

Parameters:
column - the column for which the width is calculated
Returns:
the preferred width

createPopup

protected JPopupMenu createPopup()
Returns a new instance of the column popup menu.

Returns:
a new column popup menu instance.

deselect

public void deselect(int modelIndex)
Unchecks the column with model index modelIndex from the column popup.

Parameters:
modelIndex - the model index of the table's column

getAllowEditColumn

public boolean getAllowEditColumn()
Determines whether users are able to rename columns in the column dialog.

Returns:
true if column renaming is enabled, false otherwise

getAllowReorder

public boolean getAllowReorder()
Determines whether users are able to reorder columns in the column dialog.

Returns:
true if column reordering is enabled, false otherwise

getColumnMargin

public int getColumnMargin()
Determines the pixels to increase the width of a column by, when it is being automatically resized with the resizeColumnToHeader(int) or resizeColumnToContents(int) methods.

Returns:
the number of pixels

getColumnPopup

public JPopupMenu getColumnPopup()
Returns the popup menu displayed.

Returns:
the popup menu.

getColumnPopupOrder

public int getColumnPopupOrder()
Retrieves the mode according to which, columns in the column popup will be ordered.
The mode can have two values:
MODEL_ORDER, in which columns are ordered according to their position in the table's data model
VIEW_ORDER, in which columns are ordered according to their position in the table's column model (the view)
Additionally, if the PUSH_INVISIBLE_TO_END modifier is defined, non-visible columns will be pushed towards the end of the popup.

Returns:
the column popup's order mode

getColumnPositions

public int[] getColumnPositions(TableColumn[] columns,
                                boolean[] isVisible)
Retrieves the position in the column popup for columns. The isVisible array contains booleans, according to whether columns are visible or not. An integer value of -1 implies that the column should not be added to the column popup at all.
The behaviour is controlled with the columnPopupOrder mode, but can also be overriden by the user.

Parameters:
columns - an array containing the available columns to show in the popup
isVisible - denoting whether or not a column is visible
Returns:
the column positions in the popup

getDefaultHeaderCursor

public Cursor getDefaultHeaderCursor()
Retrieves the default header's cursor.

Returns:
the default header's cursor

getGroupingEnabled

public boolean getGroupingEnabled()
Determines whether users are able to group/ungroup columns in the column dialog.

Returns:
true if column grouping is enabled, false otherwise
Since:
3.4

getMaxColumns

public int getMaxColumns()
Returns the maximum number of columns that the column popup is going to display.

Returns:
the maximum number of columns that the column popup is going to display.

getMoreDialog

public JDialog getMoreDialog()
Returns an instance of the 'More...' dialog.

Returns:
the more dialog instance

getResizeMode

public int getResizeMode()
Determines the current resize mode, when a user double clicks on a column border.

The mode may be:
NO_RESIZE: nothing will happen
CONTENT_RESIZE: column will be resized to its contents
HEADER_RESIZE: column will be resized to its header contents
CONTENT_RESIZE | HEADER_RESIZE: column will be resized to the maximum width of its cell AND header contents
If the mask IF_LARGER_RESIZE is used, then the column will be resized only if the calculated width is larger than the current one

The default mode is CONTENT_RESIZE.

Returns:
an integer that denotes the resize mode

getShowMore

public boolean getShowMore()
Returns a flag that is true if a menuitem (more) is to be added to the column popup, that provides more column options.

Returns:
true if the more menuitem is to be shown, false otherwise.

getShowPopup

public boolean getShowPopup()
Returns true if the column popup is to be shown.

Returns:
the show popup flag

getTable

public JTable getTable()
Returns the table that is associated with the assistant.

Returns:
the table

refreshColumns

public void refreshColumns()
Recreates the column selection popup and dialog with values from the column model.


refreshPopup

public void refreshPopup()
Reinitializes the popup with values from the column model.


register

public void register(TableColumnModel columnModel)
The component registers itself to the listener list of columnModel.

Parameters:
columnModel - the TableColumnModel

resizeColumn

public void resizeColumn(int column)
Resizes the given column according to the current resize mode.

Parameters:
column - the table column to resize

resizeColumn

public void resizeColumn(int column,
                         int resizeMode)
Resizes the given column according to the resize mode passed as argument to the method.

Parameters:
column - the table column to resize
resizeMode - the resize mode

resizeColumns

public void resizeColumns()
Resizes all columns according to the current resize mode.


resizeColumns

public void resizeColumns(int resizeMode)
Resizes all columns according to the resize mode passed as argument to the method.

Parameters:
resizeMode - the resize mode

resizeColumnsToContents

public void resizeColumnsToContents()
Resizes all columns to their contents.


resizeColumnsToHeader

public void resizeColumnsToHeader()
Resizes all columns to the content of their header.


resizeColumnToContents

public void resizeColumnToContents(int column)
Resizes the given column to its contents.

Parameters:
column - the table column to resize

resizeColumnToHeader

public void resizeColumnToHeader(int column)
Resizes the given column to the content of its header.

Parameters:
column - the table column to resize

select

public void select(int modelIndex)
Checks the column with model index modelIndex from the column popup.

Parameters:
modelIndex - the model index of the table's column

setAllowEditColumn

public void setAllowEditColumn(boolean allowEditColumn)
Sets a flag that is true if users are able to rename columns in the column dialog.

Parameters:
allowEditColumn - true if column renaming is enabled, false otherwise

setAllowReorder

public void setAllowReorder(boolean allowReorder)
Sets a flag that is true if users are able to reorder columns in the column dialog.

Parameters:
allowReorder - true if column reordering is enabled, false otherwise

setColumnMargin

public void setColumnMargin(int columnMargin)
Assigns the pixels to increase the width of a column by, when it is being automatically resized with the resizeColumnToHeader(int) or resizeColumnToContents(int) methods.

Parameters:
columnMargin - the number of pixels

setColumnPopupOrder

public void setColumnPopupOrder(int columnPopupOrder)
Assigns the mode according to which, columns in the column popup will be ordered.
The mode can have two values:
MODEL_ORDER, in which columns are ordered according to their position in the table's data model
VIEW_ORDER, in which columns are ordered according to their position in the table's column model (the view)
Additionally, if the PUSH_INVISIBLE_TO_END modifier is defined, non-visible columns will be pushed towards the end of the popup.
The default setting is MODEL_ORDER.

The popup order mode is taken into account solely by the getColumnPositions method, which returns the column positions in the popup.

Examples for setting the popup order:
setColumnPopupOrder(VIEW_ORDER);
setColumnPopupOrder(MODEL_ORDER | PUSH_INVISIBLE_TO_END);

Parameters:
columnPopupOrder - the column popup's order mode

setColumnWidth

public void setColumnWidth(int column,
                           int width)
Sets a new column width for the specified column.

Parameters:
column - the table column to resize
width - the new column width to assign to the column

setDefaultHeaderCursor

public void setDefaultHeaderCursor(Cursor cursor)
Assigns the default headers' cursor.

Parameters:
cursor - the default header cursor to assign

setGroupingEnabled

public void setGroupingEnabled(boolean groupingEnabled)
Sets a flag that is true if users are able to group/ungroup columns in the column dialog.

Parameters:
groupingEnabled - true if column grouping is enabled, false otherwise
Since:
3.4

setHeader

public void setHeader(JTableHeader newTableHeader)
Associates newTableHeader with this instance of TableAssistant.

Parameters:
newTableHeader - the JTableHeader to associate with this TableAssistant.

setMaxColumns

public void setMaxColumns(int maxColumns)
Sets the maximum number of columns that the column popup is going to display. If maxColumns is UNLIMITED_COLUMNS, an unlimited number of columns will be added to the column popup.

Parameters:
maxColumns - the maximum number of columns that the column popup is going to display.

setResizeMode

public void setResizeMode(int resizeMode)
Assigns the resize mode, when a user double clicks on a column border.

The mode may be:
NO_RESIZE: nothing will happen
CONTENT_RESIZE: column will be resized to its contents
HEADER_RESIZE: column will be resized to its header contents
CONTENT_RESIZE | HEADER_RESIZE: column will be resized to the maximum width of its cell AND header contents
If the mask IF_LARGER_RESIZE is used, then the column will be resized only if the calculated width is larger than the current one

The default mode is CONTENT_RESIZE.

Parameters:
resizeMode - an integer that denotes the resize mode

setShowMore

public void setShowMore(boolean showMore)
Sets a flag that is true if a menuitem (more) is to be added to the column popup, that provides more column options.

Parameters:
showMore - true if the more menuitem is to be shown, false otherwise.

setShowPopup

public void setShowPopup(boolean showPopup)
Sets a flag that is true if the column popup is to be shown.

Parameters:
showPopup - the show popup value to set

setTable

public void setTable(JTable newTable)
Assigns a new table to the assistant.

Parameters:
newTable - the table to assign

showColumnDialog

public void showColumnDialog()
Makes the column dialog visible.

Since:
3.4

unregister

public void unregister(TableColumnModel columnModel)
The component unregisters itself from the listener list of columnModel.

Parameters:
columnModel - the TableColumnModel

Copyright © 2005 Scientific Applications. All Rights Reserved.