KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > dataset > DataColumn


1 /*
2  * $Id: DataColumn.java,v 1.1 2005/02/23 17:51:31 rbair Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7 package org.jdesktop.dataset;
8 import java.beans.PropertyChangeListener JavaDoc;
9 import java.beans.PropertyChangeSupport JavaDoc;
10 import java.util.logging.Logger JavaDoc;
11 import org.jdesktop.dataset.NameGenerator;
12
13 /**
14  *
15  * @author rbair
16  */

17 public class DataColumn {
18     /**
19      * The Logger
20      */

21     private static final Logger JavaDoc LOG = Logger.getLogger(DataColumn.class.getName());
22     
23     //protected for testing
24
protected static final String JavaDoc DEFAULT_NAME_PREFIX = "DataColumn";
25     /**
26      * Used to generate a name for the DataColumn, since each DataColumn must
27      * have a name.
28      */

29     private static final NameGenerator NAMEGEN = new NameGenerator(DEFAULT_NAME_PREFIX);
30     
31     //used for communicating changes to this JavaBean, especially necessary for
32
//IDE tools, but also handy for any other component listening to this column
33
private PropertyChangeSupport JavaDoc pcs = new PropertyChangeSupport JavaDoc(this);
34     
35     /**
36      * The DataTable that created this DataColumn. This is an immutable property
37      * that is set in the constructor.
38      */

39     private DataTable table;
40     /**
41      * The name of this DataColumn. The name cannot contain any whitespace.
42      * In general, it should conform to the same rules as an identifier
43      * in Java.
44      */

45     private String JavaDoc name = NAMEGEN.generateName(this);
46     /**
47      * The Class of the data values for this DataColumn. This cannot be null. If
48      * the type is unknown, then this should be Object.class.
49      */

50     private Class JavaDoc type = Object JavaDoc.class;
51     /**
52      * Flag indicating whether the fields within this column are readonly or
53      * not.
54      */

55     private boolean readOnly = false;
56     /**
57      * Flag indicating whether the fields within this column are required.
58      * If a column is required, then the field must be filled in prior to
59      * a save, or an exception occurs.<br>
60      * TODO constraint logic isn't specified yet. When it is, make sure to
61      * include this check.
62      */

63     private boolean required = false;
64     
65     /**
66      * The default value for the column. When a new row is added, the various
67      * cells set their values to this default.
68      */

69     private Object JavaDoc defaultValue;
70
71     /**
72      * Indicates whether this DataColumn is a key column. Key Columns enforce
73      * a unique constraint on the DataColumn (no two values in the column can
74      * be the same, as determined by .equals()).
75      */

76     private boolean keyColumn;
77     
78     /**
79      * Create a new DataColumn. To construct a DataColumn, do not call
80      * <code>new DataColumn(table)</code> directly. Rather, call
81      * <code>table.addColumn()</code>.<br>
82      * @param table cannot be null. The DataTable that created this
83      * DataColumn.
84      */

85     protected DataColumn(DataTable table) {
86         assert table != null;
87         this.table = table;
88     }
89     
90     /**
91      * Returns the DataTable that this column belongs to
92      */

93     public DataTable getTable() {
94         return table;
95     }
96     
97     /**
98      * Returns the name of the DataColumn
99      */

100     public String JavaDoc getName() {
101         return name;
102     }
103
104     /**
105      * Sets the name of the DataColumn. The name must be valid., or the change
106      * will not be made. If the name is invalid, a warning will be posted.
107      */

108     public void setName(String JavaDoc name) {
109         if (this.name != name) {
110             assert DataSetUtils.isValidName(name);
111             assert !table.columns.containsKey(name) && !table.selectors.containsKey(name);
112             String JavaDoc oldName = this.name;
113             this.name = name;
114             pcs.firePropertyChange("name", oldName, name);
115         }
116     }
117
118     /**
119      * Returns the type of the values for this DataColumn
120      */

121     public Class JavaDoc getType() {
122         return type;
123     }
124
125     /**
126      * Sets the type for the values of this DataColumn.
127      * @param type If null, then the type is set to Object.class
128      */

129     public void setType(Class JavaDoc type) {
130         if (this.type != type) {
131             Class JavaDoc oldType = this.type;
132             this.type = type == null ? Object JavaDoc.class : type;
133             pcs.firePropertyChange("type", oldType, type);
134         }
135     }
136
137     /**
138      * @return true if this DataColumn is read-only
139      */

140     public boolean isReadOnly() {
141         return readOnly;
142     }
143
144     /**
145      * Sets whether this column is read-only or not.
146      * @param readOnly
147      */

148     public void setReadOnly(boolean readOnly) {
149         if (this.readOnly != readOnly) {
150             boolean oldValue = this.readOnly;
151             this.readOnly = readOnly;
152             pcs.firePropertyChange("readOnly", oldValue, readOnly);
153         }
154     }
155
156     /**
157      * @return true if the fields in this column need to have a value
158      * before they can be saved to the data store. The DataColumn is required
159      * if the required flag is set by the setRequired method, or if the
160      * DataColumn is a keyColumn. <br>
161      *
162      * TODO need to decide if that is true, or if it is always required!
163      */

164     public boolean isRequired() {
165         return required || keyColumn;
166     }
167
168     /**
169      * Specifies whether the fields in this column must have a value (cannot
170      * be null).
171      * @param required
172      */

173     public void setRequired(boolean required) {
174         if (this.required != required) {
175             boolean oldValue = this.required;
176             this.required = required;
177             pcs.firePropertyChange("required", oldValue, required);
178         }
179     }
180
181     /**
182      * @return the value to use as a default value when a new field for
183      * this column is created, such as when a new row is created.
184      */

185     public Object JavaDoc getDefaultValue() {
186         return defaultValue;
187     }
188     
189     /**
190      * Set the defaultValue
191      * @param defaultValue
192      */

193     public void setDefaultValue(Object JavaDoc defaultValue) {
194         if (this.defaultValue != defaultValue) {
195             Object JavaDoc oldVal = this.defaultValue;
196             this.defaultValue = defaultValue;
197             pcs.firePropertyChange("defaultValue", oldVal, defaultValue);
198         }
199     }
200     
201     /**
202      * Returns whether the column is a key column or not
203      */

204     public boolean isKeyColumn() {
205         return keyColumn;
206     }
207     
208     /**
209      * Sets this column to be a key column. This implicitly places a unique
210      * constraint on the column. When this flag is set, no checks are made to
211      * ensure correctness. However, the column will automatically be marked as
212      * being required.
213      *
214      * @param value
215      */

216     public void setKeyColumn(boolean value) {
217         if (value != keyColumn) {
218             boolean oldVal = keyColumn;
219             keyColumn = value;
220             pcs.firePropertyChange("keyColumn", oldVal, value);
221         }
222     }
223     
224     public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
225         pcs.addPropertyChangeListener(listener);
226     }
227     
228     public void addPropertyChangeListener(String JavaDoc property, PropertyChangeListener JavaDoc listener) {
229         pcs.addPropertyChangeListener(property, listener);
230     }
231     
232     public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
233         pcs.removePropertyChangeListener(listener);
234     }
235     
236     public void removePropertyChangeListener(String JavaDoc propertyName, PropertyChangeListener JavaDoc listener) {
237         pcs.removePropertyChangeListener(propertyName, listener);
238     }
239 }
Popular Tags