1 package net.sf.openrocket.gui.dialogs.preset;
3 import java.util.Enumeration;
4 import java.util.Vector;
6 import javax.swing.table.DefaultTableColumnModel;
7 import javax.swing.table.TableColumn;
9 public class XTableColumnModel extends DefaultTableColumnModel {
11 /** Array of TableColumn objects in this model.
12 * Holds all column objects, regardless of their visibility
14 protected Vector<TableColumn> allTableColumns = new Vector<TableColumn>();
17 * Creates an extended table column model.
24 * Sets the visibility of the specified TableColumn.
25 * The call is ignored if the TableColumn is not found in this column model
26 * or its visibility status did not change.
29 * @param aColumn the column to show/hide
30 * @param visible its new visibility status
32 // listeners will receive columnAdded()/columnRemoved() event
33 public void setColumnVisible(TableColumn column, boolean visible) {
35 super.removeColumn(column);
38 // find the visible index of the column:
39 // iterate through both collections of visible and all columns, counting
40 // visible columns up to the one that's about to be shown again
41 int noVisibleColumns = tableColumns.size();
42 int noInvisibleColumns = allTableColumns.size();
45 for(int invisibleIndex = 0; invisibleIndex < noInvisibleColumns; ++invisibleIndex) {
46 TableColumn visibleColumn = (visibleIndex < noVisibleColumns ? (TableColumn)tableColumns.get(visibleIndex) : null);
47 TableColumn testColumn = (TableColumn)allTableColumns.get(invisibleIndex);
49 if(testColumn == column) {
50 if(visibleColumn != column) {
51 super.addColumn(column);
52 super.moveColumn(tableColumns.size() - 1, visibleIndex);
54 return; // ####################
56 if(testColumn == visibleColumn) {
64 * Makes all columns in this model visible
66 public void setAllColumnsVisible(boolean visible) {
67 int noColumns = allTableColumns.size();
69 for(int columnIndex = 0; columnIndex < noColumns; ++columnIndex) {
70 TableColumn visibleColumn = (columnIndex < tableColumns.size() ? (TableColumn)tableColumns.get(columnIndex) : null);
71 TableColumn invisibleColumn = (TableColumn)allTableColumns.get(columnIndex);
74 if(visibleColumn != invisibleColumn) {
75 super.addColumn(invisibleColumn);
76 super.moveColumn(tableColumns.size() - 1, columnIndex);
79 super.removeColumn(invisibleColumn);
85 * Maps the index of the column in the table model at
86 * <code>modelColumnIndex</code> to the TableColumn object.
87 * There may me multiple TableColumn objects showing the same model column, though this is uncommon.
88 * This method will always return the first visible or else the first invisible column with the specified index.
89 * @param modelColumnIndex index of column in table model
90 * @return table column object or null if no such column in this column model
92 public TableColumn getColumnByModelIndex(int modelColumnIndex) {
93 for (int columnIndex = 0; columnIndex < allTableColumns.size(); ++columnIndex) {
94 TableColumn column = (TableColumn)allTableColumns.get(columnIndex);
95 if(column.getModelIndex() == modelColumnIndex) {
102 /** Checks wether the specified column is currently visible.
103 * @param aColumn column to check
104 * @return visibility of specified column (false if there is no such column at all. [It's not visible, right?])
106 public boolean isColumnVisible(TableColumn aColumn) {
107 return (tableColumns.indexOf(aColumn) >= 0);
110 /** Append <code>column</code> to the right of exisiting columns.
111 * Posts <code>columnAdded</code> event.
112 * @param column The column to be added
114 * @exception IllegalArgumentException if <code>column</code> is <code>null</code>
117 public void addColumn(TableColumn column) {
118 allTableColumns.add(column);
119 super.addColumn(column);
122 /** Removes <code>column</code> from this column model.
123 * Posts <code>columnRemoved</code> event.
124 * Will do nothing if the column is not in this model.
125 * @param column the column to be added
129 public void removeColumn(TableColumn column) {
130 int allColumnsIndex = allTableColumns.indexOf(column);
131 if(allColumnsIndex != -1) {
132 allTableColumns.remove(allColumnsIndex);
134 super.removeColumn(column);
137 /** Moves the column from <code>oldIndex</code> to <code>newIndex</code>.
138 * Posts <code>columnMoved</code> event.
139 * Will not move any columns if <code>oldIndex</code> equals <code>newIndex</code>.
141 * @param oldIndex index of column to be moved
142 * @param newIndex new index of the column
143 * @exception IllegalArgumentException if either <code>oldIndex</code> or
144 * <code>newIndex</code>
145 * are not in [0, getColumnCount() - 1]
148 public void moveColumn(int oldIndex, int newIndex) {
149 if ((oldIndex < 0) || (oldIndex >= getColumnCount()) ||
150 (newIndex < 0) || (newIndex >= getColumnCount()))
151 throw new IllegalArgumentException("moveColumn() - Index out of range");
153 TableColumn fromColumn = (TableColumn) tableColumns.get(oldIndex);
154 TableColumn toColumn = (TableColumn) tableColumns.get(newIndex);
156 int allColumnsOldIndex = allTableColumns.indexOf(fromColumn);
157 int allColumnsNewIndex = allTableColumns.indexOf(toColumn);
159 if(oldIndex != newIndex) {
160 allTableColumns.remove(allColumnsOldIndex);
161 allTableColumns.add(allColumnsNewIndex, fromColumn);
164 super.moveColumn(oldIndex, newIndex);
168 * Returns the total number of columns in this model.
170 * @param onlyVisible if set only visible columns will be counted
171 * @return the number of columns in the <code>tableColumns</code> array
174 public int getColumnCount(boolean onlyVisible) {
175 Vector<TableColumn> columns = (onlyVisible ? tableColumns : allTableColumns);
176 return columns.size();
180 * Returns an <code>Enumeration</code> of all the columns in the model.
182 * @param onlyVisible if set all invisible columns will be missing from the enumeration.
183 * @return an <code>Enumeration</code> of the columns in the model
185 public Enumeration<TableColumn> getColumns(boolean onlyVisible) {
186 Vector<TableColumn> columns = (onlyVisible ? tableColumns : allTableColumns);
188 return columns.elements();
192 * Returns the position of the first column whose identifier equals <code>identifier</code>.
193 * Position is the the index in all visible columns if <code>onlyVisible</code> is true or
194 * else the index in all columns.
196 * @param identifier the identifier object to search for
197 * @param onlyVisible if set searches only visible columns
199 * @return the index of the first column whose identifier
200 * equals <code>identifier</code>
202 * @exception IllegalArgumentException if <code>identifier</code>
203 * is <code>null</code>, or if no
204 * <code>TableColumn</code> has this
205 * <code>identifier</code>
208 public int getColumnIndex(Object identifier, boolean onlyVisible) {
209 if (identifier == null) {
210 throw new IllegalArgumentException("Identifier is null");
213 Vector<TableColumn> columns = (onlyVisible ? tableColumns : allTableColumns);
214 int noColumns = columns.size();
217 for(int columnIndex = 0; columnIndex < noColumns; ++columnIndex) {
218 column = (TableColumn)columns.get(columnIndex);
220 if(identifier.equals(column.getIdentifier()))
224 throw new IllegalArgumentException("Identifier not found");
228 * Returns the <code>TableColumn</code> object for the column
229 * at <code>columnIndex</code>.
231 * @param columnIndex the index of the column desired
232 * @param onlyVisible if set columnIndex is meant to be relative to all visible columns only
233 * else it is the index in all columns
235 * @return the <code>TableColumn</code> object for the column
236 * at <code>columnIndex</code>
238 public TableColumn getColumn(int columnIndex, boolean onlyVisible) {
239 return (TableColumn)tableColumns.elementAt(columnIndex);