JIDE 3.5.15

com.jidesoft.grid
Class TransposeTableModel

java.lang.Object
  extended by javax.swing.table.AbstractTableModel
      extended by com.jidesoft.grid.TransposeTableModel
All Implemented Interfaces:
CategorizedTableModel, ColumnIdentifierTableModel, ColumnWidthTableModel, ContextSensitiveTableModel, EditableColumnTableModel, GroupableTableModel, HierarchicalTableModel, MultiTableModel, NavigableModel, RowModel, SpanModel, StyleModel, TableModelWrapper, Serializable, TableModel

public class TransposeTableModel
extends AbstractTableModel
implements ContextSensitiveTableModel, StyleModel, SpanModel, NavigableModel, MultiTableModel, CategorizedTableModel, ColumnIdentifierTableModel, ColumnWidthTableModel, EditableColumnTableModel, GroupableTableModel, HierarchicalTableModel, RowModel, TableModelWrapper

This is a table model to change the orientation of the table. With this model's help, you could transpose the original table model's row to column while the column to be row.

To have the functionality, please simply wrap your original table model with this class. You could then invoke setTransposed(boolean) to change the orientation as you wish. If isTransposed() returns false, it should behaves exactly the same as if there is only the original table model in the place.

After transposed, the default column name would be the first column values in the original table model. The default column class would be Object.class. You could either override getDefaultTransposedColumnClass(int) or invoke setTransposedColumnClasses(Class[]) to change the column classes.

See Also:
Serialized Form

Field Summary
 
Fields inherited from class javax.swing.table.AbstractTableModel
listenerList
 
Fields inherited from interface com.jidesoft.grid.MultiTableModel
FOOTER_COLUMN, HEADER_COLUMN, REGULAR_COLUMN
 
Constructor Summary
TransposeTableModel(TableModel model)
           
 
Method Summary
 TableModel getActualModel()
          Gets the underlying table model.
 Class<?> getCellClassAt(int rowIndex, int columnIndex)
          Gets the type at cell (row, column).
 CellSpan getCellSpanAt(int rowIndex, int columnIndex)
          Gets the cell span at the specified row and column.
 CellStyle getCellStyleAt(int rowIndex, int columnIndex)
          Gets the cell style at the specified row and column.
 Object getChildValueAt(int row)
          Gets the child object value associated with the row at row index.
 Class<?> getColumnClass(int columnIndex)
           
 int getColumnCount()
           
 TableCellEditor getColumnHeaderCellEditor(int columnIndex)
          Gets the column header cell editor.
 Object getColumnIdentifier(int columnIndex)
          Returns the identifier of the column in the model.
 String getColumnName(int columnIndex)
           
 int getColumnType(int columnIndex)
          Checks if the column at the columnIndex is a header column or footer column or just regular column.
 ConverterContext getConverterContextAt(int rowIndex, int columnIndex)
          Gets the converter context at cell (row, column).
protected  Class<?> getDefaultTransposedColumnClass(int columnIndex)
           
protected  String getDefaultTransposedColumnIdentifier(int columnIndex)
           
 EditorContext getEditorContextAt(int rowIndex, int columnIndex)
          Gets the editor context at cell (row, column).
 GrouperContext getGrouperContext(int columnIndex)
           
 int getMaximumWidth(int column)
          Returns the maximum width of the column.
 int getMinimumWidth(int column)
          Returns the minimum width of the column.
 int getPreferredWidth(int column)
          Returns the preferred width of the column.
 Row getRowAt(int rowIndex)
          /** Returns the Row at the row index specified by rowIndex.
 int getRowCount()
           
 int getRowIndex(Row row)
          Gets the index of the Row.
 int getTableIndex(int columnIndex)
          Gets the table index that this column belongs to.
 Class<?>[] getTransposedColumnClasses()
          Get the column classes to be displayed after transposed.
 String getTransposedColumnName()
          Get the first column name after transposed.
 Object getValueAt(int rowIndex, int columnIndex)
           
 boolean hasChild(int row)
          Checks if the row has child.
 boolean isCategoryRow(int rowIndex)
          Checks if the row at rowIndex is a category row.
 boolean isCellEditable(int rowIndex, int columnIndex)
           
 boolean isCellSpanOn()
          Checks if the span is on.
 boolean isCellStyleOn()
          Checks if the style is on.
 boolean isColumnHeaderEditable(int columnIndex)
          Checks if the column at the specified columnIndex is editable.
 boolean isExpandable(int row)
          Returns true if the row is expandable.
 boolean isHierarchical(int row)
          Returns true if the child component will be shown in a new row.
 boolean isNavigableAt(int rowIndex, int columnIndex)
          Returns if the cell at the given coordinates can be navigated or not.
 boolean isNavigationOn()
          Checks if the navigation is on.
 boolean isTransposed()
          Get the flag indicating if the table model is transposed.
 void setTransposed(boolean transposed)
          Set the flag indicating if the table model is transposed.
 void setTransposedColumnClasses(Class<?>[] transposedColumnClasses)
          Set the column classes to be displayed after transposed.
 void setTransposedColumnName(String transposedColumnName)
          Set the first column name after transposed.
 void setValueAt(Object aValue, int rowIndex, int columnIndex)
           
 
Methods inherited from class javax.swing.table.AbstractTableModel
addTableModelListener, findColumn, fireTableCellUpdated, fireTableChanged, fireTableDataChanged, fireTableRowsDeleted, fireTableRowsInserted, fireTableRowsUpdated, fireTableStructureChanged, getListeners, getTableModelListeners, removeTableModelListener
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface javax.swing.table.TableModel
addTableModelListener, removeTableModelListener
 

Constructor Detail

TransposeTableModel

public TransposeTableModel(TableModel model)
Method Detail

getConverterContextAt

public ConverterContext getConverterContextAt(int rowIndex,
                                              int columnIndex)
Description copied from interface: ContextSensitiveTableModel
Gets the converter context at cell (row, column).

For a special row index like -1, please return the default converter context for the entire column if there is any.

Specified by:
getConverterContextAt in interface ContextSensitiveTableModel
Parameters:
rowIndex - the row index
columnIndex - the column index
Returns:
converter context

getEditorContextAt

public EditorContext getEditorContextAt(int rowIndex,
                                        int columnIndex)
Description copied from interface: ContextSensitiveTableModel
Gets the editor context at cell (row, column).

For a special row index like -1, please return the default editor context for the entire column if there is any.

Specified by:
getEditorContextAt in interface ContextSensitiveTableModel
Parameters:
rowIndex - the row index
columnIndex - the column index
Returns:
editor context

getCellClassAt

public Class<?> getCellClassAt(int rowIndex,
                               int columnIndex)
Description copied from interface: ContextSensitiveTableModel
Gets the type at cell (row, column).

For a special row index like -1, please return the default column class for the entire column if there is any.

Specified by:
getCellClassAt in interface ContextSensitiveTableModel
Parameters:
rowIndex - the row index
columnIndex - the column index
Returns:
type

getRowCount

public int getRowCount()
Specified by:
getRowCount in interface TableModel

getColumnCount

public int getColumnCount()
Specified by:
getColumnCount in interface TableModel

getDefaultTransposedColumnIdentifier

protected String getDefaultTransposedColumnIdentifier(int columnIndex)

getDefaultTransposedColumnClass

protected Class<?> getDefaultTransposedColumnClass(int columnIndex)

getColumnName

public String getColumnName(int columnIndex)
Specified by:
getColumnName in interface TableModel
Overrides:
getColumnName in class AbstractTableModel

getColumnClass

public Class<?> getColumnClass(int columnIndex)
Specified by:
getColumnClass in interface TableModel
Overrides:
getColumnClass in class AbstractTableModel

isCellEditable

public boolean isCellEditable(int rowIndex,
                              int columnIndex)
Specified by:
isCellEditable in interface TableModel
Overrides:
isCellEditable in class AbstractTableModel

getValueAt

public Object getValueAt(int rowIndex,
                         int columnIndex)
Specified by:
getValueAt in interface TableModel

setValueAt

public void setValueAt(Object aValue,
                       int rowIndex,
                       int columnIndex)
Specified by:
setValueAt in interface TableModel
Overrides:
setValueAt in class AbstractTableModel

getCellSpanAt

public CellSpan getCellSpanAt(int rowIndex,
                              int columnIndex)
Description copied from interface: SpanModel
Gets the cell span at the specified row and column. To avoid creating many instances of CellSpan, you can return the same instance by changing its values only. For example, the code below is a good example.

 private final CellSpan span = new CellSpan(0,0,1,1);
 public CellSpan getCellSpanAt(int row, int col) {
     span.setRow(...);
     span.setColumn(...);
     span.setRowSpan(...);
     span.setColumnSpan(...);
     return span;
 }
 
For performance consideration, if the cell span has 1 row span and 1 column span, it is better to return null instead of new CellSpan(row, column, 1, 1).

Specified by:
getCellSpanAt in interface SpanModel
Parameters:
rowIndex - the row index.
columnIndex - the column index.
Returns:
CellSpan object.

isCellSpanOn

public boolean isCellSpanOn()
Description copied from interface: SpanModel
Checks if the span is on. The CellSpanTable will ignore all the CellSpans defined in this model if this method returns false.

Since 3.5.12, please fire table data changed event if the returned value of this method is changed. Otherwise the CellSpanTable might still use the cached value to improve performance.

Specified by:
isCellSpanOn in interface SpanModel
Returns:
true if span is on. Otherwise false.

getCellStyleAt

public CellStyle getCellStyleAt(int rowIndex,
                                int columnIndex)
Description copied from interface: StyleModel
Gets the cell style at the specified row and column.

Specified by:
getCellStyleAt in interface StyleModel
Parameters:
rowIndex - the row index
columnIndex - the column index
Returns:
CellStyle object.

isCellStyleOn

public boolean isCellStyleOn()
Description copied from interface: StyleModel
Checks if the style is on. The CellStyleTable will ignore all the CellStyles defined in this model if this method returns false.

Specified by:
isCellStyleOn in interface StyleModel
Returns:
true if style is on. Otherwise false.

getGrouperContext

public GrouperContext getGrouperContext(int columnIndex)
Specified by:
getGrouperContext in interface GroupableTableModel

isCategoryRow

public boolean isCategoryRow(int rowIndex)
Description copied from interface: CategorizedTableModel
Checks if the row at rowIndex is a category row.

Specified by:
isCategoryRow in interface CategorizedTableModel
Parameters:
rowIndex - the index of the row to be checked.
Returns:
true if the row at rowIndex is a category row.

getRowAt

public Row getRowAt(int rowIndex)
Description copied from interface: RowModel
/** Returns the Row at the row index specified by rowIndex.

Specified by:
getRowAt in interface RowModel
Parameters:
rowIndex - the row whose row is to be queried
Returns:
the row at the specified row index

getRowIndex

public int getRowIndex(Row row)
Description copied from interface: RowModel
Gets the index of the Row.

Specified by:
getRowIndex in interface RowModel
Parameters:
row - the Row
Returns:
the index of the Row. If the row is displayed in the table, it will return the index. Otherwise, it will return -1. So -1 could mean two things - the row is not displayed or the row is not in the tree hierarchy at all.

isNavigableAt

public boolean isNavigableAt(int rowIndex,
                             int columnIndex)
Description copied from interface: NavigableModel
Returns if the cell at the given coordinates can be navigated or not.

Specified by:
isNavigableAt in interface NavigableModel
Parameters:
rowIndex - The row index
columnIndex - The column index
Returns:
true if navigable, false otherwise

isNavigationOn

public boolean isNavigationOn()
Description copied from interface: NavigableModel
Checks if the navigation is on. If off, NavigableModel.isNavigableAt(int,int) should always return true for valid indexes.

Specified by:
isNavigationOn in interface NavigableModel
Returns:
true if on, false otherwise

getMinimumWidth

public int getMinimumWidth(int column)
Description copied from interface: ColumnWidthTableModel
Returns the minimum width of the column.

Specified by:
getMinimumWidth in interface ColumnWidthTableModel
Parameters:
column - the column index
Returns:
the minimum width. -1 means the minimum width is not configured yet.

getPreferredWidth

public int getPreferredWidth(int column)
Description copied from interface: ColumnWidthTableModel
Returns the preferred width of the column.

Specified by:
getPreferredWidth in interface ColumnWidthTableModel
Parameters:
column - the column index
Returns:
the preferred width. -1 means the preferred width is not configured yet.

getMaximumWidth

public int getMaximumWidth(int column)
Description copied from interface: ColumnWidthTableModel
Returns the maximum width of the column.

Specified by:
getMaximumWidth in interface ColumnWidthTableModel
Parameters:
column - the column index
Returns:
the maximum width. -1 means the maximum width is not configured yet.

isColumnHeaderEditable

public boolean isColumnHeaderEditable(int columnIndex)
Description copied from interface: EditableColumnTableModel
Checks if the column at the specified columnIndex is editable.

Specified by:
isColumnHeaderEditable in interface EditableColumnTableModel
Parameters:
columnIndex - the column index
Returns:
true if the column should be editable. Otherwise false.

getColumnHeaderCellEditor

public TableCellEditor getColumnHeaderCellEditor(int columnIndex)
Description copied from interface: EditableColumnTableModel
Gets the column header cell editor.

Specified by:
getColumnHeaderCellEditor in interface EditableColumnTableModel
Parameters:
columnIndex - the column index
Returns:
the column header cell editor. Returns null if you want to use the default cell editor registered on EditableTableHeader.

getColumnIdentifier

public Object getColumnIdentifier(int columnIndex)
Description copied from interface: ColumnIdentifierTableModel
Returns the identifier of the column in the model. A JideTable uses this method to get the unique identifier of this column.

Specified by:
getColumnIdentifier in interface ColumnIdentifierTableModel
Parameters:
columnIndex - the index of the column
Returns:
the unique identifier of the column.

getColumnType

public int getColumnType(int columnIndex)
Description copied from interface: MultiTableModel
Checks if the column at the columnIndex is a header column or footer column or just regular column. Header columns will be displayed at the left side of the table. Footer columns will be on the right side. Regular columns will in the middle and will have horizontal scroll bar if there are a lot of columns.

Specified by:
getColumnType in interface MultiTableModel
Parameters:
columnIndex - the column index
Returns:
column type. The possible values are REGULAR_COLUMN, HEADER_COLUMN, or FOOTER_COLUMN.

getTableIndex

public int getTableIndex(int columnIndex)
Description copied from interface: MultiTableModel
Gets the table index that this column belongs to. As convention, the table index starts from 0. So all columns that return 0 in this method will be grouped into the first table. Those return 1 will be on the second table, and so on.

Specified by:
getTableIndex in interface MultiTableModel
Parameters:
columnIndex - the column index
Returns:
the table index.

hasChild

public boolean hasChild(int row)
Description copied from interface: HierarchicalTableModel
Checks if the row has child.

Specified by:
hasChild in interface HierarchicalTableModel
Parameters:
row - the row index
Returns:
true if the row has child. Otherwise false.

isHierarchical

public boolean isHierarchical(int row)
Description copied from interface: HierarchicalTableModel
Returns true if the child component will be shown in a new row. Returns false is the child component will replace the current row.

Specified by:
isHierarchical in interface HierarchicalTableModel
Returns:
true if the child component will be shown in a new row. Otherwise false which means the child component will replace the current row when displaying.

getChildValueAt

public Object getChildValueAt(int row)
Description copied from interface: HierarchicalTableModel
Gets the child object value associated with the row at row index.

Specified by:
getChildValueAt in interface HierarchicalTableModel
Returns:
the value. If child components are the same except the displaying is different, it's better to return a value using this method. Then the component factory can create the same component and only load a different value to that component. You can also return null in this method. If so, the component factory can look up in the table model on fly and find out how to create the child component.

isExpandable

public boolean isExpandable(int row)
Description copied from interface: HierarchicalTableModel
Returns true if the row is expandable. This only makes sense when hasChild() return true. If there is child and but not expandable, you will see a gray "+" icon but click on it does nothing.

Specified by:
isExpandable in interface HierarchicalTableModel
Returns:
true if the row is expandable.

getActualModel

public TableModel getActualModel()
Description copied from interface: TableModelWrapper
Gets the underlying table model.

Specified by:
getActualModel in interface TableModelWrapper
Returns:
the underlying table model.

isTransposed

public boolean isTransposed()
Get the flag indicating if the table model is transposed.

Returns:
true if the table model is turned. Otherwise false.
See Also:
setTransposed(boolean)

setTransposed

public void setTransposed(boolean transposed)
Set the flag indicating if the table model is transposed.

By default, this flag is false to keep the same behavior with the original table model.

Parameters:
transposed - the flag

getTransposedColumnClasses

public Class<?>[] getTransposedColumnClasses()
Get the column classes to be displayed after transposed.

Returns:
the column classes after transposed.
See Also:
setTransposedColumnClasses(Class[])

setTransposedColumnClasses

public void setTransposedColumnClasses(Class<?>[] transposedColumnClasses)
Set the column classes to be displayed after transposed.

If this is not configured, getDefaultTransposedColumnClass(int) will be invoked to feed the column classes on transposed.

Parameters:
transposedColumnClasses - the column classes array

getTransposedColumnName

public String getTransposedColumnName()
Get the first column name after transposed.

Returns:
the column name
See Also:
setTransposedColumnName(String)

setTransposedColumnName

public void setTransposedColumnName(String transposedColumnName)
Set the first column name after transposed.

By default, it's null. Then we will display an empty string for the column.

Parameters:
transposedColumnName - the column name

JIDE 3.5.15