9968e247236136e62656d955b31999ae3485dfb0
[debian/openrocket] / core / src / net / sf / openrocket / gui / dialogs / preset / XTableColumnModel.java
1 package net.sf.openrocket.gui.dialogs.preset;
2
3 import java.util.Enumeration;
4 import java.util.Vector;
5
6 import javax.swing.table.DefaultTableColumnModel;
7 import javax.swing.table.TableColumn;
8
9 public class XTableColumnModel extends DefaultTableColumnModel  {
10
11         /** Array of TableColumn objects in this model.
12          *  Holds all column objects, regardless of their visibility
13          */
14         protected Vector<TableColumn> allTableColumns = new Vector<TableColumn>();
15
16         /**
17          * Creates an extended table column model.
18          */
19         XTableColumnModel() {
20                 super();
21         }
22
23         /**
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.
27          * <p>
28          *
29          * @param aColumn        the column to show/hide
30          * @param visible its new visibility status
31          */
32         // listeners will receive columnAdded()/columnRemoved() event
33         public void setColumnVisible(TableColumn column, boolean visible) {
34                 if(!visible) {
35                         super.removeColumn(column);
36                 }
37                 else {
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();
43                         int visibleIndex        = 0;
44
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);
48
49                                 if(testColumn == column) {
50                                         if(visibleColumn != column) {
51                                                 super.addColumn(column);
52                                                 super.moveColumn(tableColumns.size() - 1, visibleIndex);
53                                         }
54                                         return; // ####################
55                                 }
56                                 if(testColumn == visibleColumn) {
57                                         ++visibleIndex;
58                                 }
59                         }
60                 }
61         }
62
63         /**
64          * Makes all columns in this model visible
65          */
66         public void setAllColumnsVisible(boolean visible) {
67                 int noColumns       = allTableColumns.size();
68
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);
72                         if ( visible ) {
73
74                                 if(visibleColumn != invisibleColumn) {
75                                         super.addColumn(invisibleColumn);
76                                         super.moveColumn(tableColumns.size() - 1, columnIndex);
77                                 }
78                         } else {
79                                 super.removeColumn(invisibleColumn);
80                         }
81                 }
82         }
83
84         /**
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
91          */
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) {
96                                 return column;
97                         }
98                 }
99                 return null;
100         }
101
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?])
105          */    
106         public boolean isColumnVisible(TableColumn aColumn) {
107                 return (tableColumns.indexOf(aColumn) >= 0);
108         }
109
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
113          * @see #removeColumn
114          * @exception IllegalArgumentException if <code>column</code> is <code>null</code>
115          */    
116         @Override
117         public void addColumn(TableColumn column) {
118                 allTableColumns.add(column);
119                 super.addColumn(column);
120         }
121
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
126          * @see #addColumn
127          */    
128         @Override
129         public void removeColumn(TableColumn column) {
130                 int allColumnsIndex = allTableColumns.indexOf(column);
131                 if(allColumnsIndex != -1) {
132                         allTableColumns.remove(allColumnsIndex);
133                 }
134                 super.removeColumn(column);
135         }
136
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>.
140          *
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]
146          */
147         @Override
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");
152
153                 TableColumn fromColumn  = (TableColumn) tableColumns.get(oldIndex);
154                 TableColumn toColumn    = (TableColumn) tableColumns.get(newIndex);
155
156                 int allColumnsOldIndex  = allTableColumns.indexOf(fromColumn);
157                 int allColumnsNewIndex  = allTableColumns.indexOf(toColumn);
158
159                 if(oldIndex != newIndex) {
160                         allTableColumns.remove(allColumnsOldIndex);
161                         allTableColumns.add(allColumnsNewIndex, fromColumn);
162                 }
163
164                 super.moveColumn(oldIndex, newIndex);
165         }
166
167         /**
168          * Returns the total number of columns in this model.
169          *
170          * @param   onlyVisible   if set only visible columns will be counted
171          * @return      the number of columns in the <code>tableColumns</code> array
172          * @see #getColumns
173          */
174         public int getColumnCount(boolean onlyVisible) {
175                 Vector<TableColumn> columns = (onlyVisible ? tableColumns : allTableColumns);
176                 return columns.size();
177         }
178
179         /**
180          * Returns an <code>Enumeration</code> of all the columns in the model.
181          *
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
184          */
185         public Enumeration<TableColumn> getColumns(boolean onlyVisible) {
186                 Vector<TableColumn> columns = (onlyVisible ? tableColumns : allTableColumns);
187
188                 return columns.elements();
189         }
190
191         /**
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.
195          *
196          * @param       identifier   the identifier object to search for
197          * @param       onlyVisible  if set searches only visible columns
198          *
199          * @return              the index of the first column whose identifier
200          *                      equals <code>identifier</code>
201          *
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>
206          * @see         #getColumn
207          */
208         public int getColumnIndex(Object identifier, boolean onlyVisible) {
209                 if (identifier == null) {
210                         throw new IllegalArgumentException("Identifier is null");
211                 }
212
213                 Vector<TableColumn>      columns     = (onlyVisible ? tableColumns : allTableColumns);
214                 int         noColumns   = columns.size();
215                 TableColumn column;
216
217                 for(int columnIndex = 0; columnIndex < noColumns; ++columnIndex) {
218                         column = (TableColumn)columns.get(columnIndex);
219
220                         if(identifier.equals(column.getIdentifier()))
221                                 return columnIndex;
222                 }
223
224                 throw new IllegalArgumentException("Identifier not found");
225         }
226
227         /**
228          * Returns the <code>TableColumn</code> object for the column
229          * at <code>columnIndex</code>.
230          *
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
234          *
235          * @return      the <code>TableColumn</code> object for the column
236          *                              at <code>columnIndex</code>
237          */
238         public TableColumn getColumn(int columnIndex, boolean onlyVisible) {
239                 return (TableColumn)tableColumns.elementAt(columnIndex);
240         }
241 }