KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56 package org.objectstyle.cayenne.dataview;
57
58 import java.text.Format JavaDoc;
59
60 import org.apache.commons.lang.ObjectUtils;
61 import org.apache.commons.lang.Validate;
62 import org.objectstyle.cayenne.DataObject;
63 import org.objectstyle.cayenne.map.ObjAttribute;
64 import org.objectstyle.cayenne.map.ObjRelationship;
65
66 /**
67  * Descriptor for a single view field.
68  *
69  * @since 1.1
70  * @author Andriy Shapochka
71  */

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