KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: TabularDataMetaData.java,v 1.2 2004/09/07 18:16:02 davidson1 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.PropertyChangeListener JavaDoc;
11 import java.beans.PropertyChangeSupport JavaDoc;
12
13 import java.util.Iterator JavaDoc;
14
15 import org.jdesktop.swing.data.Converter;
16
17
18 /**
19  *
20  * This class will be going away once the DOMAdapter converts its API
21  * to use org.jdesktop.swing.data.MetaData.
22  *
23  * @version 1.0
24  */

25 public class TabularDataMetaData {
26     private Column columns[];
27
28     private PropertyChangeSupport JavaDoc pcs;
29
30     /**
31      * Creates a new meta data object with 0 columns.
32      */

33     public TabularDataMetaData() {
34         this(0);
35     }
36
37     /**
38      * Creates a new meta data object with the specified number of columns
39      * @param columnCount integer containing the number of columns
40      */

41     public TabularDataMetaData(int columnCount) {
42         pcs = new PropertyChangeSupport JavaDoc(this);
43         setColumnCount(columnCount);
44     }
45
46     /**
47      * Adds the specified property change listener to this meta data.
48      * This listener will be notified when either the number of columns
49      * change or properties on the columns are modified.
50      * @param listener PropertyChangeListener to be notified when meta data changes
51      */

52     public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
53         pcs.addPropertyChangeListener(listener);
54     }
55
56     /**
57      * Removes the specified property change listener from this meta data.
58      * @param listener PropertyChangeListener to be notified when meta data changes
59      */

60     public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
61         pcs.removePropertyChangeListener(listener);
62     }
63
64     /**
65      * Initializes the number of columns in this meta data object. If columns
66      * already exist when this method is called, they are discarded.
67      * @param columnCount integer containing the number of columns
68      */

69     public void setColumnCount(int columnCount) {
70         int oldColumnCount = columns != null? columns.length : 0;
71         columns = new Column[columnCount];
72         for (int i = 0; i < columnCount; i++) {
73             columns[i] = new Column("column"+i, java.lang.String JavaDoc.class, true);
74         }
75         pcs.firePropertyChange("columnCount", oldColumnCount, columnCount);
76     }
77
78     /**
79      * @return the number of columns
80      */

81     public int getColumnCount() {
82         return columns.length;
83     }
84
85     public int getColumnIndex(String JavaDoc name) {
86         int index = 0;
87         for (int i = 0; i < columns.length; i++) {
88             if (columns[i].name.equals(name)) {
89                 return i+1;
90             }
91         }
92         return index;
93     }
94
95     /**
96      * @param columnIndex integer index of the column (first is 1, second is 2...)
97      * @return String containing the column name at the specified index
98      */

99     public String JavaDoc getColumnName(int columnIndex) {
100         return columns[columnIndex-1].name;
101     }
102
103     /**
104      * Sets the column name at the specified index
105      * @param columnIndex integer index of the column (first is 1, second is 2...)
106      * @param columnName String containing the column name at the specified inde
107      */

108     public void setColumnName(int columnIndex, String JavaDoc columnName) {
109         String JavaDoc oldColumnName = columns[columnIndex-1].name;
110         columns[columnIndex-1].name = columnName;
111         pcs.firePropertyChange("columnName"+columnIndex, oldColumnName, columnName);
112     }
113
114     /**
115      * @param columnIndex integer index of the column (first is 1, second is 2...)
116      * @return String containing the column label at the specified index
117      */

118     public String JavaDoc getColumnLabel(int columnIndex) {
119         return columns[columnIndex-1].label;
120     }
121
122     /**
123      * Sets the column label at the specified index. The label is used
124      * to display this column to the end-user and should be localized.
125      *
126      * @param columnIndex index of the column (first is 1, second is 2...)
127      * @param columnLabel the column text to set
128      */

129     public void setColumnLabel(int columnIndex, String JavaDoc columnLabel) {
130         String JavaDoc oldColumnLabel = columns[columnIndex-1].label;
131         columns[columnIndex-1].label = columnLabel;
132         pcs.firePropertyChange("columnLabel"+columnIndex, oldColumnLabel, columnLabel);
133     }
134
135
136     /**
137      * @param columnIndex integer index of the column (first is 1, second is 2...)
138      * @return Class representing the column's type
139      */

140     public Class JavaDoc getColumnClass(int columnIndex) {
141         return columns[columnIndex-1].klass;
142     }
143
144     /**
145      * Sets the column class at the specified index
146      * @param columnIndex integer index of the column (first is 1, second is 2...)
147      * @param columnClass Class representing the column's type
148      */

149     public void setColumnClass(int columnIndex, Class JavaDoc columnClass) {
150         Class JavaDoc oldColumnClass = columns[columnIndex-1].klass;
151         columns[columnIndex-1].klass = columnClass;
152         pcs.firePropertyChange("columnClass"+columnIndex, oldColumnClass, columnClass);
153     }
154
155     public void setColumnDisplaySize(int columnIndex, int numChars) {
156         columns[columnIndex-1].displayLength = numChars;
157     }
158
159     public int getColumnDisplaySize(int columnIndex) {
160         return columns[columnIndex-1].displayLength;
161     }
162
163     /**
164      * @param columnIndex integer index of the column (first is 1, second is 2...)
165      * @return boolean indicating whether or not values in this column may be modified
166      */

167     public boolean isColumnWritable(int columnIndex) {
168         return columns[columnIndex-1].writable;
169     }
170
171     /**
172      * Sets whether or not values in the column at the specified index may be modified.
173      * @param columnIndex integer index of the column (first is 1, second is 2...)
174      * @param writable boolean indicating whether or not values in this column
175      * may be modified
176      */

177     public void setColumnWritable(int columnIndex, boolean writable) {
178         columns[columnIndex-1].writable = writable;
179     }
180
181     /**
182       * @param columnIndex integer index of the column (first is 1, second is 2...)
183       * @return boolean indicating whether or not values in this column may be null
184       */

185     public boolean isColumnNullable(int columnIndex) {
186         return columns[columnIndex-1].nullable;
187     }
188
189     /**
190      * Sets whether or not values in the column may have a null value.
191      * @param columnIndex integer index of the column (first is 1, second is 2...)
192      * @param nullable boolean indicating whether or not values in the column
193      * may be null
194      */

195     public void setColumnNullable(int columnIndex, boolean nullable) {
196         columns[columnIndex-1].nullable = nullable;
197     }
198
199     /**
200      * @param columnIndex integer index of the column (first is 1, second is 2...)
201      * @return Object representing minimum value for values in the column, or null
202      * if no minimum value constraint exists
203     */

204     public Object JavaDoc getColumnMinimum(int columnIndex) {
205         return columns[columnIndex-1].minimum;
206     }
207
208     /**
209      * Sets the minimum value for values in the column. This may be used
210      * to provide optimal UI controls for editing to minimize erroneous input.
211      * @param columnIndex integer index of the column (first is 1, second is 2...)
212      * @param minimum Object representing minimum value for values in the column, or null
213      * if no minimum value constraint exists
214      */

215     public void setColumnMinimum(int columnIndex, Object JavaDoc minimum) {
216         columns[columnIndex-1].minimum = minimum;
217     }
218
219     /**
220      * @param columnIndex integer index of the column (first is 1, second is 2...)
221      * @return Object representing maximum value for values in the column, or null
222      * if no maximum value constraint exists
223     */

224     public Object JavaDoc getColumnMaximum(int columnIndex) {
225         return columns[columnIndex-1].maximum;
226     }
227
228     /**
229      * Sets the maximum value for values in the column. This may be used
230      * to provide optimal UI controls for editing to minimize erroneous input.
231      * @param columnIndex integer index of the column (first is 1, second is 2...)
232      * @param maximum Object representing maximum value for values in the column,
233      * or null if no maximum value constraint exists
234      */

235     public void setColumnMaximum(int columnIndex, Object JavaDoc maximum) {
236         columns[columnIndex-1].maximum = maximum;
237     }
238
239     /**
240       * @param columnIndex integer index of the column (first is 1, second is 2...)
241       * @return Iterator containing the set of valid values for this column, or
242       * null if the value is not constrained by a set
243      */

244     public Iterator JavaDoc getColumnValues(int columnIndex) {
245         final Object JavaDoc values[] = new Object JavaDoc[(columns[columnIndex-1].values.length)];
246
247         System.arraycopy(columns[columnIndex-1].values, 0,
248                          values, 0,
249                          columns[columnIndex-1].values.length);
250
251         return new Iterator JavaDoc() {
252             int current = 0;
253             public boolean hasNext() {
254                 return current < values.length;
255             }
256             public Object JavaDoc next() {
257                 return values[current++];
258             }
259             public void remove() {
260                 // not supported
261
}
262         };
263     }
264
265     /**
266      * Sets the set of valid values for this column. This may be used
267      * to provide optimal UI controls (picklist) for editing to minimize erroneous input.
268      * @param columnIndex integer index of the column (first is 1, second is 2...)
269      * @param values array containing set of valid values, or null if no value
270      * set constraint exists
271      */

272     public void setColumnValues(int columnIndex, Object JavaDoc values[]) {
273         Object JavaDoc newValues[] = new Object JavaDoc[values.length];
274         System.arraycopy(values, 0, newValues, 0, values.length);
275         columns[columnIndex-1].values = newValues;
276     }
277
278     /**
279      * Sets the converter object to be used when converting values in the
280      * column to and from <code>String</code>.
281      * @param columnIndex integer index of the column (first is 1, second is 2...)
282      * @param converter DataConverter object used to convert values in column to
283      * and from String
284      */

285     public void setColumnConverter(int columnIndex, Converter converter) {
286         columns[columnIndex-1].converter = converter;
287     }
288
289     /**
290      * @param columnIndex integer index of the column (first is 1, second is 2...)
291      * @return Converter object used to convert values in column to
292      * and from String, or null if no converter was specified
293      */

294     public Converter getColumnConverter(int columnIndex) {
295         return columns[columnIndex-1].converter;
296     }
297
298     // Data structure for holding column-specific meta data
299
private class Column {
300         public String JavaDoc name;
301         public Class JavaDoc klass;
302         public String JavaDoc label;
303         public Converter converter;
304         public boolean writable = false;
305         public boolean nullable = false;
306         public int displayLength = -1;
307         public Object JavaDoc minimum;
308         public Object JavaDoc maximum;
309         public Object JavaDoc[] values;
310
311         public Column(String JavaDoc name, Class JavaDoc klass, boolean writable) {
312             this.name = name;
313             this.klass = klass;
314             this.writable = writable;
315         }
316     }
317 }
318
Popular Tags