create changelog entry
[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 = 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 = 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 = 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         /** 
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>.
141          *
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]
147          */
148         @Override
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");
153                 
154                 TableColumn fromColumn = tableColumns.get(oldIndex);
155                 TableColumn toColumn = tableColumns.get(newIndex);
156                 
157                 int allColumnsOldIndex = allTableColumns.indexOf(fromColumn);
158                 int allColumnsNewIndex = allTableColumns.indexOf(toColumn);
159                 
160                 if (oldIndex != newIndex) {
161                         allTableColumns.remove(allColumnsOldIndex);
162                         allTableColumns.add(allColumnsNewIndex, fromColumn);
163                 }
164                 
165                 super.moveColumn(oldIndex, newIndex);
166         }
167         
168         /**
169          * Returns the total number of columns in this model.
170          *
171          * @param   onlyVisible   if set only visible columns will be counted
172          * @return      the number of columns in the <code>tableColumns</code> array
173          * @see #getColumns
174          */
175         public int getColumnCount(boolean onlyVisible) {
176                 Vector<TableColumn> columns = (onlyVisible ? tableColumns : allTableColumns);
177                 return columns.size();
178         }
179         
180         /**
181          * Returns an <code>Enumeration</code> of all the columns in the model.
182          *
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
185          */
186         public Enumeration<TableColumn> getColumns(boolean onlyVisible) {
187                 Vector<TableColumn> columns = (onlyVisible ? tableColumns : allTableColumns);
188                 
189                 return columns.elements();
190         }
191         
192         /**
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.
196          *
197          * @param       identifier   the identifier object to search for
198          * @param       onlyVisible  if set searches only visible columns
199          *
200          * @return              the index of the first column whose identifier
201          *                      equals <code>identifier</code>
202          *
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>
207          * @see         #getColumn
208          */
209         public int getColumnIndex(Object identifier, boolean onlyVisible) {
210                 if (identifier == null) {
211                         throw new IllegalArgumentException("Identifier is null");
212                 }
213                 
214                 Vector<TableColumn> columns = (onlyVisible ? tableColumns : allTableColumns);
215                 int noColumns = columns.size();
216                 TableColumn column;
217                 
218                 for (int columnIndex = 0; columnIndex < noColumns; ++columnIndex) {
219                         column = columns.get(columnIndex);
220                         
221                         if (identifier.equals(column.getIdentifier()))
222                                 return columnIndex;
223                 }
224                 
225                 throw new IllegalArgumentException("Identifier not found");
226         }
227         
228         /**
229          * Returns the <code>TableColumn</code> object for the column
230          * at <code>columnIndex</code>.
231          *
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
235          *
236          * @return      the <code>TableColumn</code> object for the column
237          *                              at <code>columnIndex</code>
238          */
239         public TableColumn getColumn(int columnIndex, boolean onlyVisible) {
240                 return tableColumns.elementAt(columnIndex);
241         }
242 }