Created
October 16, 2010 02:29
-
-
Save eugener/629318 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public final class TableEnumColumnSupport { | |
public interface ITableColumDefinition { | |
String getTitle(); | |
} | |
private TableEnumColumnSupport() {} | |
/** | |
* Finds column definition by index | |
* @param <T> | |
* @param cls | |
* @param index | |
* @return | |
*/ | |
public final static > T getDefinition( Class cls, int index ) { | |
return cls.getEnumConstants()[index]; | |
} | |
/** | |
* Returns titles for all column definitions. | |
* If column definition implements ITableColumDefinition interface then it's getTitle method is used, | |
* else enum name is converted to be used as a title | |
* @param <T> | |
* @param cls | |
* @return | |
*/ | |
public final static > String[] getTitles( Class cls ) { | |
T[] defs = cls.getEnumConstants(); | |
String[] titles = new String[defs.length]; | |
int i=0; | |
for( T c: defs) { | |
titles[i++] = getTitle(c); | |
} | |
return titles; | |
} | |
/** | |
* Returns column title for specified colum definition | |
* If column definition implements ITableColumDefinition interface then it's getTitle method is used, | |
* else enum name is converted to be used as a title | |
* @param <T> | |
* @param columnDef | |
* @return | |
*/ | |
public final static > String getTitle(T columnDef) { | |
return columnDef instanceof ITableColumDefinition? | |
((ITableColumDefinition)columnDef).getTitle(): enumToName(columnDef); | |
} | |
/** | |
* Returns column title for specified column index | |
* @param <T> | |
* @param cls | |
* @param columnIndex | |
* @return | |
*/ | |
public final static > String getTitle( Class cls, int columnIndex ) { | |
return getTitle( getDefinition( cls, columnIndex )); | |
} | |
/** | |
* Converts enum to readable column name | |
* @param <T> | |
* @param columnDef | |
* @return | |
*/ | |
public static > String enumToName(T columnDef) { | |
return columnDef == null? null: | |
StringUtils.capitalize( columnDef.name().replace('_', ' ').toLowerCase()); | |
} | |
/** | |
* Selects cell for specified rowIndex and column definition | |
* @param <T> | |
* @param table | |
* @param rowIndex | |
* @param columnDef | |
*/ | |
public final static > void setSelectedCell(JTable table, int rowIndex, T columnDef) { | |
table.changeSelection(rowIndex, columnDef.ordinal(), false, false); | |
} | |
/** | |
* Sets cell editor for specified column definitions in the table | |
* @param <T> | |
* @param table | |
* @param cellEditor | |
* @param columnDefs | |
* @return | |
*/ | |
public final static > JTable setColumnEditor(JTable table, TableCellEditor cellEditor, T... columnDefs) { | |
return TableUtils.setColumnEditor(table, cellEditor, toIndexes(columnDefs)); | |
} | |
/** | |
* Sets cell renderer for specified column definitions in the table | |
* @param <T> | |
* @param table | |
* @param cellRenderer | |
* @param columnDefs | |
* @return | |
*/ | |
public final static > JTable setColumnRenderer(JTable table, TableCellRenderer cellRenderer, T... columnDefs) { | |
return TableUtils.setColumnRenderer(table, cellRenderer, toIndexes(columnDefs)); | |
} | |
/** | |
* Converts column definitions to an array of actual column indexes | |
* @param <T> | |
* @param columnDefs | |
* @return | |
*/ | |
public final static > int[] toIndexes(Collection columnDefs) { | |
int[] colIndexes = new int[columnDefs.size()]; | |
int i = 0; | |
for( T def: columnDefs ) { | |
colIndexes[i++] = def.ordinal(); | |
} | |
return colIndexes; | |
} | |
/** | |
* Converts column definitions to an array of actual column indexes | |
* @param <T> | |
* @param columnDefs | |
* @return | |
*/ | |
public final static > int[] toIndexes(T... columnDefs) { | |
return toIndexes( Arrays.asList(columnDefs)); | |
} | |
/** | |
* Returns table column for specified colum definition | |
* @param <T> | |
* @param table | |
* @param columnDef | |
* @return | |
*/ | |
public final static > TableColumn getColumn( JTable table, T columnDef ) { | |
return table.getColumnModel().getColumn( columnDef.ordinal()); | |
} | |
/** | |
* Sets the identifiers on the table columns to match the specified column definition. | |
* This will allow to use getTableColumn(Obj id) to retrieve cols. This is preferrable to | |
* using indexes (not reliable-when hiding/showing cols) or the header string | |
* (which is not reliable b/c of renderers). | |
*/ | |
public final static > void setColumnIdentifiers( JXTable table, Class cls ) { | |
TableColumnModel columnModel = table.getColumnModel(); | |
Enumeration cols = columnModel.getColumns(); | |
for (Object c : cls.getEnumConstants()) | |
{ | |
cols.nextElement().setIdentifier( c ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment