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 = 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 = 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 = 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);
138 * Moves the column from <code>oldIndex</code> to <code>newIndex</code>.
139 * Posts <code>columnMoved</code> event.
140 * Will not move any columns if <code>oldIndex</code> equals <code>newIndex</code>.
142 * @param oldIndex index of column to be moved
143 * @param newIndex new index of the column
144 * @exception IllegalArgumentException if either <code>oldIndex</code> or
145 * <code>newIndex</code>
146 * are not in [0, getColumnCount() - 1]
149 public void moveColumn(int oldIndex, int newIndex) {
150 if ((oldIndex < 0) || (oldIndex >= getColumnCount()) ||
151 (newIndex < 0) || (newIndex >= getColumnCount()))
152 throw new IllegalArgumentException("moveColumn() - Index out of range");
154 TableColumn fromColumn = tableColumns.get(oldIndex);
155 TableColumn toColumn = tableColumns.get(newIndex);
157 int allColumnsOldIndex = allTableColumns.indexOf(fromColumn);
158 int allColumnsNewIndex = allTableColumns.indexOf(toColumn);
160 if (oldIndex != newIndex) {
161 allTableColumns.remove(allColumnsOldIndex);
162 allTableColumns.add(allColumnsNewIndex, fromColumn);
165 super.moveColumn(oldIndex, newIndex);
169 * Returns the total number of columns in this model.
171 * @param onlyVisible if set only visible columns will be counted
172 * @return the number of columns in the <code>tableColumns</code> array
175 public int getColumnCount(boolean onlyVisible) {
176 Vector<TableColumn> columns = (onlyVisible ? tableColumns : allTableColumns);
177 return columns.size();
181 * Returns an <code>Enumeration</code> of all the columns in the model.
183 * @param onlyVisible if set all invisible columns will be missing from the enumeration.
184 * @return an <code>Enumeration</code> of the columns in the model
186 public Enumeration<TableColumn> getColumns(boolean onlyVisible) {
187 Vector<TableColumn> columns = (onlyVisible ? tableColumns : allTableColumns);
189 return columns.elements();
193 * Returns the position of the first column whose identifier equals <code>identifier</code>.
194 * Position is the the index in all visible columns if <code>onlyVisible</code> is true or
195 * else the index in all columns.
197 * @param identifier the identifier object to search for
198 * @param onlyVisible if set searches only visible columns
200 * @return the index of the first column whose identifier
201 * equals <code>identifier</code>
203 * @exception IllegalArgumentException if <code>identifier</code>
204 * is <code>null</code>, or if no
205 * <code>TableColumn</code> has this
206 * <code>identifier</code>
209 public int getColumnIndex(Object identifier, boolean onlyVisible) {
210 if (identifier == null) {
211 throw new IllegalArgumentException("Identifier is null");
214 Vector<TableColumn> columns = (onlyVisible ? tableColumns : allTableColumns);
215 int noColumns = columns.size();
218 for (int columnIndex = 0; columnIndex < noColumns; ++columnIndex) {
219 column = columns.get(columnIndex);
221 if (identifier.equals(column.getIdentifier()))
225 throw new IllegalArgumentException("Identifier not found");
229 * Returns the <code>TableColumn</code> object for the column
230 * at <code>columnIndex</code>.
232 * @param columnIndex the index of the column desired
233 * @param onlyVisible if set columnIndex is meant to be relative to all visible columns only
234 * else it is the index in all columns
236 * @return the <code>TableColumn</code> object for the column
237 * at <code>columnIndex</code>
239 public TableColumn getColumn(int columnIndex, boolean onlyVisible) {
240 return tableColumns.elementAt(columnIndex);