X-Git-Url: https://git.gag.com/?p=debian%2Fopenrocket;a=blobdiff_plain;f=core%2Fsrc%2Fnet%2Fsf%2Fopenrocket%2Fgui%2Fdialogs%2Fpreset%2FXTableColumnModel.java;fp=core%2Fsrc%2Fnet%2Fsf%2Fopenrocket%2Fgui%2Fdialogs%2Fpreset%2FXTableColumnModel.java;h=ac05c7894d5bfd3b1fc85e12163376b404eb8dd8;hp=0000000000000000000000000000000000000000;hb=9349577cdfdff682b2aabd6daa24fdc3a7449b58;hpb=30ba0a882f0c061176ba14dbf86d3d6fad096c02 diff --git a/core/src/net/sf/openrocket/gui/dialogs/preset/XTableColumnModel.java b/core/src/net/sf/openrocket/gui/dialogs/preset/XTableColumnModel.java new file mode 100644 index 00000000..ac05c789 --- /dev/null +++ b/core/src/net/sf/openrocket/gui/dialogs/preset/XTableColumnModel.java @@ -0,0 +1,242 @@ +package net.sf.openrocket.gui.dialogs.preset; + +import java.util.Enumeration; +import java.util.Vector; + +import javax.swing.table.DefaultTableColumnModel; +import javax.swing.table.TableColumn; + +public class XTableColumnModel extends DefaultTableColumnModel { + + /** Array of TableColumn objects in this model. + * Holds all column objects, regardless of their visibility + */ + protected Vector allTableColumns = new Vector(); + + /** + * Creates an extended table column model. + */ + XTableColumnModel() { + super(); + } + + /** + * Sets the visibility of the specified TableColumn. + * The call is ignored if the TableColumn is not found in this column model + * or its visibility status did not change. + *

+ * + * @param aColumn the column to show/hide + * @param visible its new visibility status + */ + // listeners will receive columnAdded()/columnRemoved() event + public void setColumnVisible(TableColumn column, boolean visible) { + if (!visible) { + super.removeColumn(column); + } + else { + // find the visible index of the column: + // iterate through both collections of visible and all columns, counting + // visible columns up to the one that's about to be shown again + int noVisibleColumns = tableColumns.size(); + int noInvisibleColumns = allTableColumns.size(); + int visibleIndex = 0; + + for (int invisibleIndex = 0; invisibleIndex < noInvisibleColumns; ++invisibleIndex) { + TableColumn visibleColumn = (visibleIndex < noVisibleColumns ? (TableColumn) tableColumns.get(visibleIndex) : null); + TableColumn testColumn = allTableColumns.get(invisibleIndex); + + if (testColumn == column) { + if (visibleColumn != column) { + super.addColumn(column); + super.moveColumn(tableColumns.size() - 1, visibleIndex); + } + return; // #################### + } + if (testColumn == visibleColumn) { + ++visibleIndex; + } + } + } + } + + /** + * Makes all columns in this model visible + */ + public void setAllColumnsVisible(boolean visible) { + int noColumns = allTableColumns.size(); + + for (int columnIndex = 0; columnIndex < noColumns; ++columnIndex) { + TableColumn visibleColumn = (columnIndex < tableColumns.size() ? (TableColumn) tableColumns.get(columnIndex) : null); + TableColumn invisibleColumn = allTableColumns.get(columnIndex); + if (visible) { + + if (visibleColumn != invisibleColumn) { + super.addColumn(invisibleColumn); + super.moveColumn(tableColumns.size() - 1, columnIndex); + } + } else { + super.removeColumn(invisibleColumn); + } + } + } + + /** + * Maps the index of the column in the table model at + * modelColumnIndex to the TableColumn object. + * There may me multiple TableColumn objects showing the same model column, though this is uncommon. + * This method will always return the first visible or else the first invisible column with the specified index. + * @param modelColumnIndex index of column in table model + * @return table column object or null if no such column in this column model + */ + public TableColumn getColumnByModelIndex(int modelColumnIndex) { + for (int columnIndex = 0; columnIndex < allTableColumns.size(); ++columnIndex) { + TableColumn column = allTableColumns.get(columnIndex); + if (column.getModelIndex() == modelColumnIndex) { + return column; + } + } + return null; + } + + /** Checks wether the specified column is currently visible. + * @param aColumn column to check + * @return visibility of specified column (false if there is no such column at all. [It's not visible, right?]) + */ + public boolean isColumnVisible(TableColumn aColumn) { + return (tableColumns.indexOf(aColumn) >= 0); + } + + /** Append column to the right of exisiting columns. + * Posts columnAdded event. + * @param column The column to be added + * @see #removeColumn + * @exception IllegalArgumentException if column is null + */ + @Override + public void addColumn(TableColumn column) { + allTableColumns.add(column); + super.addColumn(column); + } + + /** Removes column from this column model. + * Posts columnRemoved event. + * Will do nothing if the column is not in this model. + * @param column the column to be added + * @see #addColumn + */ + @Override + public void removeColumn(TableColumn column) { + int allColumnsIndex = allTableColumns.indexOf(column); + if (allColumnsIndex != -1) { + allTableColumns.remove(allColumnsIndex); + } + super.removeColumn(column); + } + + /** + * Moves the column from oldIndex to newIndex. + * Posts columnMoved event. + * Will not move any columns if oldIndex equals newIndex. + * + * @param oldIndex index of column to be moved + * @param newIndex new index of the column + * @exception IllegalArgumentException if either oldIndex or + * newIndex + * are not in [0, getColumnCount() - 1] + */ + @Override + public void moveColumn(int oldIndex, int newIndex) { + if ((oldIndex < 0) || (oldIndex >= getColumnCount()) || + (newIndex < 0) || (newIndex >= getColumnCount())) + throw new IllegalArgumentException("moveColumn() - Index out of range"); + + TableColumn fromColumn = tableColumns.get(oldIndex); + TableColumn toColumn = tableColumns.get(newIndex); + + int allColumnsOldIndex = allTableColumns.indexOf(fromColumn); + int allColumnsNewIndex = allTableColumns.indexOf(toColumn); + + if (oldIndex != newIndex) { + allTableColumns.remove(allColumnsOldIndex); + allTableColumns.add(allColumnsNewIndex, fromColumn); + } + + super.moveColumn(oldIndex, newIndex); + } + + /** + * Returns the total number of columns in this model. + * + * @param onlyVisible if set only visible columns will be counted + * @return the number of columns in the tableColumns array + * @see #getColumns + */ + public int getColumnCount(boolean onlyVisible) { + Vector columns = (onlyVisible ? tableColumns : allTableColumns); + return columns.size(); + } + + /** + * Returns an Enumeration of all the columns in the model. + * + * @param onlyVisible if set all invisible columns will be missing from the enumeration. + * @return an Enumeration of the columns in the model + */ + public Enumeration getColumns(boolean onlyVisible) { + Vector columns = (onlyVisible ? tableColumns : allTableColumns); + + return columns.elements(); + } + + /** + * Returns the position of the first column whose identifier equals identifier. + * Position is the the index in all visible columns if onlyVisible is true or + * else the index in all columns. + * + * @param identifier the identifier object to search for + * @param onlyVisible if set searches only visible columns + * + * @return the index of the first column whose identifier + * equals identifier + * + * @exception IllegalArgumentException if identifier + * is null, or if no + * TableColumn has this + * identifier + * @see #getColumn + */ + public int getColumnIndex(Object identifier, boolean onlyVisible) { + if (identifier == null) { + throw new IllegalArgumentException("Identifier is null"); + } + + Vector columns = (onlyVisible ? tableColumns : allTableColumns); + int noColumns = columns.size(); + TableColumn column; + + for (int columnIndex = 0; columnIndex < noColumns; ++columnIndex) { + column = columns.get(columnIndex); + + if (identifier.equals(column.getIdentifier())) + return columnIndex; + } + + throw new IllegalArgumentException("Identifier not found"); + } + + /** + * Returns the TableColumn object for the column + * at columnIndex. + * + * @param columnIndex the index of the column desired + * @param onlyVisible if set columnIndex is meant to be relative to all visible columns only + * else it is the index in all columns + * + * @return the TableColumn object for the column + * at columnIndex + */ + public TableColumn getColumn(int columnIndex, boolean onlyVisible) { + return tableColumns.elementAt(columnIndex); + } +}