1 /***
2 * EditableTableColumnModel.java
3 *
4 * This file is part of the creme library.
5 * The creme library intends to ease the development effort of large
6 * applications by providing easy to use support classes.
7 *
8 * Copyright (C) 2003 Denis Bregeon
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 * contact information: dbregeon@sourceforge.net
25 */
26 package org.jcreme.swing.table.columnmodel;
27
28 import java.util.Enumeration;
29 import java.util.Hashtable;
30 import java.util.Vector;
31
32 import javax.swing.table.DefaultTableColumnModel;
33 import javax.swing.table.TableColumn;
34
35 /***
36 * This extension of the DefaultTableModel enables to handle hidden columns.
37 *
38 * @author $Author: dbregeon $
39 * @version $Revision: 1.1 $
40 */
41 public class EditableTableColumnModel extends DefaultTableColumnModel {
42 private final Hashtable hiddenColumns = new Hashtable();
43
44 /***
45 * Hide a column. If the column was present in the model it will not be
46 * displayed anymore.
47 *
48 * @param column
49 * the column to hide.
50 */
51 public void hideColumn(TableColumn column) {
52 if (column != null) {
53 if (column.getIdentifier() != null) {
54 this.hiddenColumns.put(column.getIdentifier(), column);
55 }
56 super.removeColumn(column);
57 }
58 }
59
60 /***
61 * Removes a column from the model (whether hidden or not).
62 *
63 * @param column
64 * the column to remove.
65 */
66 public void removeColumn(TableColumn column) {
67 if ((column != null) && (this.hiddenColumns.contains(column))) {
68 this.hiddenColumns.remove(column.getIdentifier());
69 } else {
70 super.removeColumn(column);
71 }
72 }
73
74 /***
75 * Shows a previously hidden column. If the column was not hidden, same as
76 * addColumn.
77 *
78 * @param column
79 * the column to show.
80 */
81 public void showColumn(TableColumn column) {
82 if (column != null) {
83 if (column.getIdentifier() != null) {
84 this.hiddenColumns.remove(column.getIdentifier());
85 }
86 addColumn(column);
87 }
88 }
89
90 /***
91 * Gives access to the hidden columns.
92 *
93 * @return the hidden columns.
94 */
95 public TableColumn[] getHiddenColumns() {
96 return (TableColumn[]) this.hiddenColumns.values().toArray(
97 new TableColumn[0]);
98 }
99
100 /***
101 * Provides the index of the column associated to the Identifier. Behaves as
102 * the superclass's implementation, except for hidden columns (returns -1).
103 *
104 * @param identifier
105 * the identifier of a column.
106 * @return the index of the column or -1 if the column is hidden.
107 */
108 public int getColumnIndex(Object identifier) {
109 int result = -1;
110 if ((identifier == null)
111 || (!this.hiddenColumns.keySet().contains(identifier))) {
112 result = super.getColumnIndex(identifier);
113 }
114 return result;
115 }
116
117 /***
118 * Gives access to a complete list of the columns in the model regardless of
119 * whether they are hidden or not.
120 *
121 * @return the TableColumns in the model.
122 */
123 public TableColumn[] getAllColumns() {
124 Vector result = new Vector(this.hiddenColumns.values());
125 Enumeration enum = getColumns();
126 while (enum.hasMoreElements()) {
127 result.add(enum.nextElement());
128 }
129 return (TableColumn[]) result.toArray(new TableColumn[0]);
130
131 }
132
133 }