KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > table > TableColumnExt


1 /*
2  * $Id: TableColumnExt.java,v 1.3 2004/08/12 06:10:07 aim Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.swing.table;
9
10 import java.beans.Expression JavaDoc;
11 import java.beans.PropertyChangeEvent JavaDoc;
12 import java.beans.PropertyChangeListener JavaDoc;
13 import java.beans.PropertyChangeSupport JavaDoc;
14
15 import java.awt.Component JavaDoc;
16
17 import java.lang.reflect.Constructor JavaDoc;
18
19 import java.util.Hashtable JavaDoc;
20
21 import javax.swing.JTable JavaDoc;
22 import javax.swing.table.TableCellEditor JavaDoc;
23 import javax.swing.table.TableCellRenderer JavaDoc;
24
25 import org.jdesktop.swing.LabelProperties;
26 import org.jdesktop.swing.data.Converters;
27 import org.jdesktop.swing.data.Converter;
28 import org.jdesktop.swing.decorator.Sorter;
29
30 /**
31  * TableColumn extension which adds support for view column configuration features
32  * including column-visibility, sorting, and prototype values.
33  *
34  * @author Ramesh Gupta
35  * @author Amy Fowler
36  */

37 public class TableColumnExt extends javax.swing.table.TableColumn JavaDoc
38     implements Cloneable JavaDoc {
39
40     protected boolean editable = true;
41     protected boolean visible = true;
42     protected Object JavaDoc prototypeValue = null;
43
44     private Hashtable JavaDoc clientProperties = null;
45
46     protected Sorter sorter = null;
47     private Constructor JavaDoc sorterConstructor = null;
48     private final static Constructor JavaDoc defaultSorterConstructor;
49     private final static Class JavaDoc[] sorterConstructorSignature =
50         new Class JavaDoc[]{int.class, boolean.class};
51
52     static {
53         Constructor JavaDoc constructor = null;
54         try {
55             Class JavaDoc sorterClass = Class.forName("org.jdesktop.swing.decorator.ShuttleSorter", true,
56                                               TableColumnExt.class.getClassLoader());
57             constructor = sorterClass.getConstructor(sorterConstructorSignature);
58         }
59         catch (Exception JavaDoc ex) {
60         }
61         defaultSorterConstructor = constructor;
62     }
63
64     /**
65      * Creates new table view column with a model index = 0.
66      */

67     public TableColumnExt() {
68         this(0);
69     }
70
71     /**
72      * Creates new table view column with the specified model index.
73      * @param modelIndex index of table model column to which this view column
74      * is bound.
75      */

76     public TableColumnExt(int modelIndex) {
77         this(modelIndex, 75); // default width taken from javax.swing.table.TableColumn
78
}
79
80     /**
81      * Creates new table view column with the specified model index and column width.
82      * @param modelIndex index of table model column to which this view column
83      * is bound.
84      * @param width pixel width of view column
85      */

86     public TableColumnExt(int modelIndex, int width) {
87         this(modelIndex, width, null, null);
88     }
89
90     /**
91      * Creates new table view column with the specified model index, column
92      * width, cell renderer and cell editor.
93      * @param modelIndex index of table model column to which this view column
94      * is bound.
95      * @param width pixel width of view column
96      * @param cellRenderer the cell renderer which will render all cells in this
97      * view column
98      * @param cellEditor the cell editor which will edit cells in this view column
99      */

100     public TableColumnExt(int modelIndex, int width,
101                           TableCellRenderer JavaDoc cellRenderer, TableCellEditor JavaDoc cellEditor) {
102         super(modelIndex, width, cellRenderer, cellEditor);
103         this.sorterConstructor = defaultSorterConstructor;
104     }
105
106     /**
107      * Sets the editable property. This property enables the table view to
108      * control whether or not the user is permitted to edit cell values in this
109      * view column, even if the model permits. If the table model column corresponding to this view column
110      * returns <code>true</code> for <code>isCellEditable</code> and this
111      * property is <code>false</code>, then the user will not be permitted to
112      * edit values from this view column, dispite the model setting.
113      * If the model's <code>isCellEditable</code> returns <code>false</code>,
114      * then this property will be ignored and cell edits will not be permitted
115      * in this view column.
116      * @see #isEditable
117      * @see javax.swing.table.TableModel#isCellEditable
118      * @param editable boolean indicating whether or not the user may edit cell
119      * values in this view column
120      */

121     public void setEditable(boolean editable) {
122         boolean oldEditable = this.editable;
123         this.editable = editable;
124         firePropertyChange("editable",
125                            Boolean.valueOf(oldEditable),
126                            Boolean.valueOf(editable));
127     }
128
129     /**
130      * @see #setEditable
131      * @return boolean indicating whether or not the user may edit cell
132      * values in this view column
133      */

134     public boolean isEditable() {
135         return editable;
136     }
137
138     /**
139      * Sets the prototypeValue property. The value should be of a type
140      * which corresponds to the column's class as defined by the table model.
141      * If non-null, the JXTable instance will use this property to calculate
142      * and set the initial preferredWidth of the column. Note that this
143      * initial preferredWidth will be overridden if the user resizes columns
144      * directly.
145      * @see #getPrototypeValue
146      * @see org.jdesktop.swing.JXTable#getPreferredScrollableViewportSize
147      * @param value Object containing the value of the prototype to be used
148      * to calculate the initial preferred width of the column
149      */

150     public void setPrototypeValue(Object JavaDoc value) {
151         Object JavaDoc oldPrototypeValue = this.prototypeValue;
152         this.prototypeValue = value;
153         firePropertyChange("prototypeValue",
154                            oldPrototypeValue,
155                            value);
156
157     }
158
159     /**
160      * @see #setPrototypeValue
161      * @return Object containing the value of the prototype to be used
162      * to calculate the initial preferred width of the column
163      */

164     public Object JavaDoc getPrototypeValue() {
165         return prototypeValue;
166     }
167
168     /**
169      * Sets a user-defined sorter for this column
170      * @param sorterClassName String containing the name of the class which
171      * performs sorting on this view column
172      */

173     public void setSorterClass(String JavaDoc sorterClassName) {
174         if ((sorterClassName == null) || (sorterClassName.length() == 0)){
175             sorterConstructor = null;
176         }
177         else {
178             try {
179                 Class JavaDoc sorterClass = Class.forName(sorterClassName, true,
180                                                   getClass().getClassLoader());
181                 sorterConstructor = sorterClass.getConstructor(sorterConstructorSignature);
182             }
183             catch (Exception JavaDoc ex) {
184                 sorterConstructor = null;
185             }
186         }
187     }
188
189     /**
190      *
191      * @return String containing the name of the class which
192      * performs sorting on this view column
193      */

194     public String JavaDoc getSorterClass() {
195         return sorterConstructor == null ? null :
196             sorterConstructor.getDeclaringClass().getName();
197     }
198
199     /**
200      *
201      * @return Sorter instance which performs sorting on this view column
202      */

203     public Sorter getSorter() {
204         if (sorter == null) {
205             if (sorterConstructor != null) {
206                 try {
207                     sorter = (Sorter) sorterConstructor.newInstance(
208                         new Object JavaDoc[] {
209                             new Integer JavaDoc(getModelIndex()),
210                             new Boolean JavaDoc(true)});
211                 }
212                 catch (Exception JavaDoc ex) {
213                 }
214             }
215         }
216         return sorter;
217     }
218
219     /**
220      *
221      * @return boolean indicating whether this view column is sortable
222      */

223     public boolean isSortable() {
224         return sorterConstructor != null;
225     }
226
227     /**
228      * Sets the title of this view column. This is a convenience
229      * wrapper for <code>setHeaderValue</code>.
230      * @param title String containing the title of this view column
231      */

232     public void setTitle(String JavaDoc title) {
233         setHeaderValue(title); // simple wrapper
234
}
235
236     /**
237      * Convenience method which returns the headerValue property after
238      * converting it to a string.
239      * @return String containing the title of this view column
240      */

241     public String JavaDoc getTitle() {
242         return getHeaderValue().toString(); // simple wrapper
243
}
244
245     /**
246      * Sets the visible property. This property controls whether or not
247      * this view column is currently visible in the table.
248      * @see #setVisible
249      * @param visible boolean indicating whether or not this view column is
250      * visible in the table
251      */

252     public void setVisible(boolean visible) {
253         boolean oldVisible = this.visible;
254         this.visible = visible;
255         firePropertyChange("visible",
256                            Boolean.valueOf(oldVisible),
257                            Boolean.valueOf(visible));
258
259     }
260
261     /**
262      * @see #setVisible
263      * @return boolean indicating whether or not this view column is
264      * visible in the table
265      */

266     public boolean isVisible() {
267         return visible;
268     }
269
270     /**
271      * Stores the object value using the specified key.
272      * @see #getClientProperty
273      * @param key Object which is used as key to retrieve value
274      * @param value Object containing value of client property
275      */

276     public void putClientProperty(Object JavaDoc key, Object JavaDoc value) {
277         if (key == null)
278             throw new IllegalArgumentException JavaDoc("null key");
279
280         if ((value == null) && (getClientProperty(key) == null)) {
281             return;
282         }
283
284         if (value == null) {
285             getClientProperties().remove(key);
286         }
287         else {
288             getClientProperties().put(key, value);
289         }
290
291         /** @todo Support firePropertyChange(key.toString(), oldValue, newValue);
292          * Make all fireXXX methods in TableColumn protected instead of private */

293     }
294
295     /**
296      * Retrieves the object value using the specified key.
297      * @see #putClientProperty
298      * @param key Object which is used as key to retrieve value
299      * @return Object containing value of client property
300      */

301     public Object JavaDoc getClientProperty(Object JavaDoc key) {
302         return ((key == null) || (clientProperties == null)) ?
303                 null : clientProperties.get(key);
304     }
305
306     private Hashtable JavaDoc getClientProperties() {
307         if (clientProperties == null) {
308             clientProperties = new Hashtable JavaDoc();
309         }
310         return clientProperties;
311     }
312
313     /**
314       * Returns a clone of this TableColumn. Some implementations of TableColumn
315       * may assume that all TableColumnModels are unique, therefore it is
316       * recommended that the same TableColumn instance not be added more than
317       * once to a TableColumnModel. To show TableColumns with the same column of
318       * data from the model, create a new instance with the same modelIndex.
319       *
320       * @return a clone of this TableColumn
321       */

322      public Object JavaDoc clone() {
323          final TableColumnExt copy = new TableColumnExt(
324              this.getModelIndex(), this.getWidth(),
325              this.getCellRenderer(), this.getCellEditor());
326
327          copy.setEditable(this.isEditable());
328          copy.setHeaderValue(this.getHeaderValue()); // no need to copy setTitle();
329
copy.setIdentifier(this.getIdentifier());
330          copy.setMaxWidth(this.getMaxWidth());
331          copy.setMinWidth(this.getMinWidth());
332          copy.setPreferredWidth(this.getPreferredWidth());
333          copy.setPrototypeValue(this.getPrototypeValue());
334          copy.setResizable(this.getResizable());
335          copy.setVisible(this.isVisible());
336          copy.setSorterClass(this.getSorterClass());
337          copy.sorterConstructor = sorterConstructor;
338          return copy;
339      }
340
341      protected void firePropertyChange(String JavaDoc propertyName, Object JavaDoc oldValue, Object JavaDoc newValue) {
342          if ((oldValue != null && !oldValue.equals(newValue)) ||
343               oldValue == null && newValue != null) {
344              PropertyChangeListener JavaDoc pcl[] = getPropertyChangeListeners();
345              if (pcl != null && pcl.length != 0) {
346                  PropertyChangeEvent JavaDoc pce = new PropertyChangeEvent JavaDoc(this,
347                      propertyName,
348                      oldValue, newValue);
349
350                  for (int i = 0; i < pcl.length; i++) {
351                      pcl[i].propertyChange(pce);
352                  }
353              }
354          }
355      }
356 }
357
Popular Tags