1 /***
2 * TableColumnType.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.Hashtable;
29
30 import javax.swing.table.TableColumn;
31
32 import org.jcreme.enumerations.Enumeration;
33 import org.jcreme.swing.table.SortableTableColumn;
34 import org.jcreme.swing.table.columnmodel.editor.SortableTableColumnEditor;
35 import org.jcreme.swing.table.columnmodel.editor.TableColumnEditor;
36 import org.jcreme.swing.table.columnmodel.state.BaseTableColumnMemento;
37 import org.jcreme.swing.table.columnmodel.state.SortableTableColumnMemento;
38
39 /***
40 * This class enables to link a TableColumn subclass to its editor and to its
41 * memento.
42 *
43 * @author $Author: dbregeon $
44 * @version $Revision: 1.2 $
45 */
46 public class TableColumnType extends Enumeration {
47 /***
48 * The TableColumnType instances by their name.
49 */
50 protected static final Hashtable fromName = new Hashtable();
51
52 /***
53 *
54 */
55 public static final TableColumnType TABLE_COLUMN = new TableColumnType(
56 "TableColumn");
57 static {
58 TABLE_COLUMN.setTableColumnClass(TableColumn.class);
59 TABLE_COLUMN.setTableColumnEditorClass(TableColumnEditor.class);
60 TABLE_COLUMN.setTableColumnMementoClass(BaseTableColumnMemento.class);
61 }
62
63 /***
64 *
65 */
66 public static final TableColumnType SORTABLE_TABLE_COLUMN = new TableColumnType(
67 "SortableTableColumn");
68 static {
69 SORTABLE_TABLE_COLUMN.setTableColumnClass(SortableTableColumn.class);
70 SORTABLE_TABLE_COLUMN
71 .setTableColumnEditorClass(SortableTableColumnEditor.class);
72 SORTABLE_TABLE_COLUMN
73 .setTableColumnMementoClass(SortableTableColumnMemento.class);
74 }
75
76 /***
77 * The class that extends the TableColumn.
78 */
79 private Class tableColumnClass = null;
80
81 /***
82 * The class that implements the TableColumnEditor.
83 */
84 private Class tableColumnEditorClass = null;
85
86 /***
87 * The class that implements the TableColumnMemento.
88 */
89 private Class tableColumnMementoClass = null;
90
91 /***
92 * Creates a new instance of TableColorModel
93 *
94 * @param name
95 */
96 protected TableColumnType(String name) {
97 super(name);
98 }
99
100 /***
101 * @see org.jcreme.enumerations.Enumeration#getFromName()
102 */
103 protected Hashtable getFromName() {
104 return fromName;
105 }
106
107 /***
108 * This method retrieves a TableColumnType from its name. It creates a new
109 * one if it does not already exist.
110 *
111 * @param name
112 * the name of the TableColumnType.
113 * @return the TableColumnType that is associated to this name.
114 */
115 public static TableColumnType get(String name) {
116 TableColumnType result = null;
117 if (name != null) {
118 result = (TableColumnType) fromName.get(name);
119 if (result == null) {
120 result = new TableColumnType(name);
121 }
122 }
123 return result;
124 }
125
126 /***
127 * Gives access to the implementation class of the TableColumn.
128 *
129 * @return the implementation class of the TableColumn.
130 */
131 public Class getTableColumnClass() {
132 return this.tableColumnClass;
133 }
134
135 /***
136 * Enables to set the actual Class that implements the TableColumn.
137 *
138 * @param clazz
139 * the implementation of the TableColumn.
140 */
141 public void setTableColumnClass(Class clazz) {
142 if ((clazz == null) || (TableColumn.class.isAssignableFrom(clazz))) {
143 this.tableColumnClass = clazz;
144 }
145 }
146
147 /***
148 * Gives access to the TableColumnEditor class to use with this
149 * TableColumnType.
150 *
151 * @return the TableColumnEditor class to use for this type of TableColumn.
152 */
153 public Class getTableColumnEditorClass() {
154 return this.tableColumnEditorClass;
155 }
156
157 /***
158 * Enables to set the TableColumnEditor to use for this type of TableColumn.
159 * If the provided class is not a subclass TableColumnEditor it is silently
160 * ignored.
161 *
162 * @param clazz
163 * the TableColumnEditor subclass to use.
164 */
165 public void setTableColumnEditorClass(Class clazz) {
166 if ((clazz == null)
167 || (TableColumnEditor.class.isAssignableFrom(clazz))) {
168 this.tableColumnEditorClass = clazz;
169 }
170 }
171
172 /***
173 * Gives access to the TableColumnMemento class to use with this
174 * TableColorModelType.
175 *
176 * @return the TableColumnMemento class to use for this type of
177 * TableColorModel.
178 */
179 public Class getTableColumnMementoClass() {
180 return this.tableColumnMementoClass;
181 }
182
183 /***
184 * Enables to set the TableColorModelMemento to use for this type of
185 * TableColorModel. If the provided class is not a subclass
186 * TableColorModelMemento it is silently ignored.
187 *
188 * @param clazz
189 * the TableColorModelMemento subclass to use.
190 */
191 public void setTableColumnMementoClass(Class clazz) {
192 if ((clazz == null)
193 || (BaseTableColumnMemento.class.isAssignableFrom(clazz))) {
194 this.tableColumnMementoClass = clazz;
195 }
196 }
197
198 /***
199 * This methods gives the instances of this class.
200 *
201 * @return all the instances of this class.
202 */
203 public static TableColumnType[] getTableColumns() {
204 return (TableColumnType[]) fromName.values().toArray(
205 new TableColumnType[0]);
206 }
207
208 /***
209 * This method enables to retrieve all the possible values of an Enumeration
210 * class.
211 *
212 * @return the values for this class.
213 */
214 public static Enumeration[] getValues() {
215 return getTableColumns();
216 }
217
218 /***
219 * Enables to find the TableColumnType associated to a TableColumn
220 * implementation.
221 *
222 * @param clazz
223 * the implementation class.
224 * @return the associated TableColumnType, or null if it does not exist.
225 */
226 public static TableColumnType getFromImplementation(Class clazz) {
227 TableColumnType result = null;
228 TableColumnType[] types = getTableColumns();
229 for (int i = 0; (result == null) && (i < types.length); i++) {
230 if (types[i].getTableColumnClass() == clazz) {
231 result = types[i];
232 }
233 }
234 return result;
235 }
236
237 }