KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > dataview > ObjEntityViewField


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.dataview;
21
22 import java.text.Format JavaDoc;
23
24 import org.apache.commons.lang.ObjectUtils;
25 import org.apache.commons.lang.Validate;
26 import org.apache.cayenne.DataObject;
27 import org.apache.cayenne.map.ObjAttribute;
28 import org.apache.cayenne.map.ObjRelationship;
29
30 /**
31  * Descriptor for a single view field.
32  *
33  * @since 1.1
34  * @author Andriy Shapochka
35  */

36 public class ObjEntityViewField {
37
38     //ObjEntityView this field belongs to (owner of this field)
39
private ObjEntityView owner;
40     //This field maps whether to an ObjAttribute of the ObjEntity
41
//the owner (view) corresponds to or an ObjRelationship with
42
//the ObjEntity as a source in case this field is a lookup field
43
private ObjAttribute objAttribute;
44
45     private ObjRelationship objRelationship;
46
47     //Field name - unique in the owner's context
48
private String JavaDoc name = "";
49
50     //field's data type, cannot be null
51
private DataTypeEnum dataType = DataTypeEnum.UNKNOWN_TYPE;
52
53     //field's calculation type, cannot be null
54
private CalcTypeEnum calcType = CalcTypeEnum.NO_CALC_TYPE;
55
56     //used for labeling (captioning) of this field in the GUI
57
//in a JTable or on the input form, for example
58
private String JavaDoc caption = "";
59
60     //display format of the values for this field,
61
//may differ from the edit format
62
//for example, 1234567.5 dollars can be displayed as $1,234,567.50
63
//but for edit it is more convenient to accept plain 1234567.5
64
private Format JavaDoc displayFormat = null;
65
66     //edit format of the values for this field
67
private Format JavaDoc editFormat = null;
68
69     //preferred index hints how the field should be placed in the ordered
70
//list of all the fields of the owner.
71
private int preferredIndex = -1;
72
73     //its actual index in the list of the fields
74
private int index = -1;
75
76     //editability hint to the GUI
77
private boolean editable = true;
78
79     //visibility hint to the GUI
80
private boolean visible = true;
81
82     //the field may have a default value
83
private Object JavaDoc defaultValue = null;
84
85     //if the calc type is lookup then the field must refer to another field
86
//to use its values
87
private ObjEntityViewField lookupField = null;
88
89     public ObjEntityViewField() {
90     }
91
92     public ObjEntityView getOwner() {
93         return owner;
94     }
95
96     public DataView getRootOwner() {
97         return getOwner().getOwner();
98     }
99
100     public int getIndex() {
101         return index;
102     }
103
104     public int getPreferredIndex() {
105         return preferredIndex;
106     }
107
108     public void setPreferredIndex(int preferredIndex) {
109         Validate.isTrue(preferredIndex >= -1);
110         this.preferredIndex = preferredIndex;
111     }
112
113     public boolean isVisible() {
114         return visible;
115     }
116
117     public void setVisible(boolean visible) {
118         this.visible = visible;
119     }
120
121     public String JavaDoc getName() {
122         return name;
123     }
124
125     public void setName(String JavaDoc name) {
126         Validate.notNull(name);
127         this.name = name;
128     }
129
130     public ObjAttribute getObjAttribute() {
131         return objAttribute;
132     }
133
134     public void setObjAttribute(ObjAttribute objAttribute) {
135         Validate.notNull(objAttribute);
136         this.objAttribute = objAttribute;
137     }
138
139     public boolean isEditable() {
140         return editable;
141     }
142
143     public void setEditable(boolean editable) {
144         this.editable = editable;
145     }
146
147     public String JavaDoc getCaption() {
148         return caption;
149     }
150
151     public void setCaption(String JavaDoc caption) {
152         this.caption = caption;
153     }
154
155     void setOwner(ObjEntityView owner) {
156         this.owner = owner;
157     }
158
159     void setIndex(int index) {
160         Validate.isTrue(owner == null || index >= 0);
161         this.index = index;
162     }
163
164     public Class JavaDoc getJavaClass() {
165         return getRootOwner().getDataTypeSpec().getJavaClass(dataType);
166     }
167
168     public Object JavaDoc getValue(DataObject obj) {
169         Object JavaDoc rawValue = getRawValue(obj);
170         return toValue(rawValue);
171     }
172
173     public Object JavaDoc toValue(Object JavaDoc rawValue) {
174         Object JavaDoc v = null;
175         if (isLookup()) {
176             if (rawValue instanceof DataObject)
177                 v = lookupField.getValue((DataObject) rawValue);
178             return v;
179         }
180         if (rawValue == null)
181             return null;
182
183         v = getRootOwner().getDataTypeSpec().toDataType(dataType, rawValue);
184         return v;
185     }
186
187     public Object JavaDoc toRawValue(Object JavaDoc value) {
188         if (value == null)
189             return null;
190         DataView rootOwner = getOwner().getOwner();
191         if (isLookup()) {
192             return rootOwner.getLookupCache().getDataObject(lookupField, value);
193         }
194         if (objAttribute == null)
195             return null;
196
197         String JavaDoc type = objAttribute.getType();
198         Object JavaDoc v = null;
199         try {
200             Class JavaDoc untypedValueClass = Class.forName(type);
201             v = rootOwner.getDataTypeSpec().fromDataType(
202                     untypedValueClass,
203                     dataType,
204                     value);
205         }
206         catch (ClassNotFoundException JavaDoc ex) {
207             ex.printStackTrace();
208         }
209         return v;
210     }
211
212     public void setValue(DataObject obj, Object JavaDoc value) {
213         Object JavaDoc rawValue = toRawValue(value);
214         Object JavaDoc oldValue = getValue(obj);
215         setRawValue(obj, rawValue);
216         getRootOwner().fireFieldValueChangeEvent(this, obj, oldValue, value);
217     }
218
219     public String JavaDoc getFormattedValue(DataObject obj) {
220         Object JavaDoc value = getRawValue(obj);
221         String JavaDoc formattedValue = null;
222         if (!isLookup()) {
223             Format JavaDoc f = (displayFormat != null ? displayFormat : editFormat);
224             if (f == null)
225                 formattedValue = ObjectUtils.toString(value);
226             else {
227                 try {
228                     formattedValue = f.format(value);
229                 }
230                 catch (Exception JavaDoc ex) {
231                     formattedValue = "";
232                 }
233             }
234         }
235         else {
236             formattedValue = lookupField.getFormattedValue((DataObject) value);
237         }
238         return formattedValue;
239     }
240
241     public Object JavaDoc getRawValue(DataObject obj) {
242         if (obj == null)
243             return null;
244         Object JavaDoc value = null;
245         if (!isLookup() && objAttribute != null) {
246             value = obj.readProperty(objAttribute.getName());
247         }
248         else if (isLookup() && objRelationship != null) {
249             value = obj.readProperty(objRelationship.getName());
250         }
251         return value;
252     }
253
254     public void setRawValue(DataObject obj, Object JavaDoc value) {
255         if (obj != null) {
256             if (!isLookup() && objAttribute != null) {
257                 obj.writeProperty(objAttribute.getName(), value);
258             }
259             else if (isLookup() && objRelationship != null) {
260                 obj.setToOneTarget(
261                         objRelationship.getName(),
262                         (DataObject) value,
263                         objRelationship.getReverseRelationship() != null);
264             }
265         }
266     }
267
268     public DataTypeEnum getDataType() {
269         return dataType;
270     }
271
272     public void setDataType(DataTypeEnum dataType) {
273         Validate.notNull(dataType);
274         this.dataType = dataType;
275     }
276
277     public CalcTypeEnum getCalcType() {
278         return calcType;
279     }
280
281     public void setCalcType(CalcTypeEnum calcType) {
282         Validate.notNull(calcType);
283         this.calcType = calcType;
284     }
285
286     public Object JavaDoc getDefaultValue() {
287         return defaultValue;
288     }
289
290     public void setDefaultValue(Object JavaDoc defaultValue) {
291         this.defaultValue = defaultValue;
292     }
293
294     public ObjRelationship getObjRelationship() {
295         return objRelationship;
296     }
297
298     public void setObjRelationship(ObjRelationship objRelationship) {
299         Validate.notNull(objRelationship);
300         this.objRelationship = objRelationship;
301     }
302
303     public boolean isLookup() {
304         return CalcTypeEnum.LOOKUP_TYPE.equals(calcType);
305     }
306
307     public Object JavaDoc[] getLookupValues() {
308         if (!isLookup())
309             return null;
310         return getRootOwner().getLookupCache().getCachedValues(lookupField);
311     }
312
313     public ObjEntityViewField getLookupField() {
314         return lookupField;
315     }
316
317     public void setLookupField(ObjEntityViewField lookupField) {
318         this.lookupField = lookupField;
319     }
320
321     public Format JavaDoc getDisplayFormat() {
322         if (displayFormat == null
323                 && isLookup()
324                 && lookupField != null
325                 && lookupField != this)
326             return lookupField.getDisplayFormat();
327         return displayFormat;
328     }
329
330     public void setDisplayFormat(Format JavaDoc displayFormat) {
331         this.displayFormat = displayFormat;
332     }
333
334     public Format JavaDoc getEditFormat() {
335         if (editFormat == null
336                 && isLookup()
337                 && lookupField != null
338                 && lookupField != this)
339             return lookupField.getEditFormat();
340         return editFormat;
341     }
342
343     public void setEditFormat(Format JavaDoc editFormat) {
344         this.editFormat = editFormat;
345     }
346
347     public boolean isSameObjAttribute(ObjEntityViewField field) {
348         if (objAttribute != null) {
349             ObjAttribute fieldAttribute = field.getObjAttribute();
350             if (fieldAttribute == null)
351                 return false;
352             boolean same = (objAttribute.equals(fieldAttribute) || (objAttribute
353                     .getEntity()
354                     .equals(fieldAttribute.getEntity()) && objAttribute.getName().equals(
355                     fieldAttribute.getName())));
356             return same;
357         }
358         else if (isLookup()) {
359             if (field.isLookup())
360                 return getLookupField().isSameObjAttribute(field.getLookupField());
361             else
362                 return getLookupField().isSameObjAttribute(field);
363         }
364         return false;
365     }
366 }
367
Popular Tags