KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > structure > PropertyUsage


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, is permitted provided that the following conditions are met: -
6  * Redistributions of source code must retain the above copyright notice, this
7  * list of conditions and the following disclaimer. - Redistributions in binary
8  * form must reproduce the above copyright notice, this list of conditions and
9  * the following disclaimer in the documentation and/or other materials
10  * provided with the distribution. - All advertising materials mentioning
11  * features or use of this software must display the following acknowledgment:
12  * "This product includes Djeneric." - Products derived from this software may
13  * not be called "Djeneric" nor may "Djeneric" appear in their names without
14  * prior written permission of Genimen BV. - Redistributions of any form
15  * whatsoever must retain the following acknowledgment: "This product includes
16  * Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG, OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.structure;
31
32 import com.genimen.djeneric.language.Messages;
33 import com.genimen.djeneric.repository.DjDomain;
34 import com.genimen.djeneric.repository.DjExtent;
35 import com.genimen.djeneric.repository.DjProperty;
36 import com.genimen.djeneric.repository.exceptions.CatalogException;
37 import com.genimen.djeneric.repository.exceptions.DjenericException;
38 import com.genimen.djeneric.repository.exceptions.ObjectNotDefinedException;
39 import com.genimen.djeneric.util.DjLogger;
40
41 /**
42  * Description of the Class
43  *
44  * @author Wido Riezebos @created 26 juni 2002
45  */

46 public class PropertyUsage implements Cloneable JavaDoc
47 {
48   DjExtent _extent;
49   DjProperty _property;
50   String JavaDoc _prompt;
51   String JavaDoc _alias = null;
52   String JavaDoc _defaultValue = null;
53   String JavaDoc _expression = null;
54   String JavaDoc _description = null;
55   String JavaDoc _customEditorClass = null;
56   String JavaDoc _relativePath = null;
57
58   int _componentType = DjDomain.COMP_TEXTFIELD;
59   int _seq;
60   int _sortOrder;
61
62   boolean _update = true;
63   boolean _required = false;
64   boolean _query = true;
65   boolean _displayed = true;
66
67   int _displayWidth = 0;
68   int _displayHeight = 0;
69
70   /**
71    * Constructor for the PropertyUsage object
72    *
73    * @param property
74    * Description of the Parameter
75    */

76   public PropertyUsage(DjProperty property)
77   {
78     _extent = property.getExtent();
79     _property = property;
80
81     _prompt = property.getPrompt();
82     _required = property.isRequired();
83     _seq = property.getSeq();
84     _query = property.isQueryable();
85     _displayed = true;
86
87     if (property.getType() instanceof DjDomain)
88     {
89       DjDomain dom = (DjDomain) property.getType();
90       _displayHeight = dom.getDisplayHeight();
91       _displayWidth = dom.getDisplayWidth();
92       _componentType = dom.getComponentType();
93     }
94     else if (property.getType() instanceof DjExtent)
95     {
96       _componentType = DjDomain.COMP_COMBOBOX;
97       _displayWidth = 150;
98     }
99   }
100
101   public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc
102   {
103     return super.clone();
104   }
105
106   public boolean equals(Object JavaDoc obj)
107   {
108     if (obj == this) return true;
109     if (!(obj instanceof PropertyUsage)) return false;
110     PropertyUsage other = (PropertyUsage) obj;
111
112     return _extent == other._extent && safeCompare(_property, other._property) && safeCompare(_prompt, other._prompt)
113            && safeCompare(_alias, other._alias) && safeCompare(_defaultValue, other._defaultValue)
114            && safeCompare(_expression, other._expression) && safeCompare(_description, other._description)
115            && safeCompare(_customEditorClass, other._customEditorClass)
116            && safeCompare(_relativePath, other._relativePath) && _componentType == other._componentType
117            && _seq == other._seq && _sortOrder == other._sortOrder && _update == other._update
118            && _required == other._required && _query == other._query && _displayed == other._displayed
119            && _displayWidth == other._displayWidth && _displayHeight == other._displayHeight;
120   }
121
122   public int hashCode()
123   {
124     int result = 0;
125     if (_extent != null) result += _extent.hashCode();
126     if (_property != null) result += _property.hashCode();
127     if (_prompt != null) result += _prompt.hashCode();
128     if (_alias != null) result += _alias.hashCode();
129
130     return result;
131   }
132
133   private boolean safeCompare(Object JavaDoc one, Object JavaDoc two)
134   {
135     if (one == null && two == null) return true;
136     if (one == null || two == null) return false;
137
138     return one.equals(two);
139   }
140
141   /**
142    * Constructor for the PropertyUsage object
143    *
144    * @param property
145    * Description of the Parameter
146    * @param alias
147    * Description of the Parameter
148    */

149   public PropertyUsage(DjProperty property, String JavaDoc alias)
150   {
151     this(property);
152     setAlias(alias);
153   }
154
155   /**
156    * Constructor for the PropertyUsage object
157    *
158    * @param expression
159    * Description of the Parameter
160    * @param alias
161    * Description of the Parameter
162    */

163   public PropertyUsage(String JavaDoc expression, String JavaDoc alias)
164   {
165     setAlias(alias);
166     setExpression(expression);
167   }
168
169   /**
170    * Returns the basedOnExpression of the PropertyUsage object
171    *
172    * @return The basedOnExpression value
173    */

174   public boolean isBasedOnExpression()
175   {
176     return _expression != null;
177   }
178
179   /**
180    * Sets the property of the PropertyUsage object
181    *
182    * @param prop
183    * The new property value
184    */

185   public void setProperty(DjProperty prop)
186   {
187     _property = prop;
188   }
189
190   // Will return null for usages based on an expression
191
/**
192    * Returns the property of the PropertyUsage object
193    *
194    * @return The property value
195    */

196   public DjProperty getProperty()
197   {
198     try
199     {
200       return doGetProperty();
201     }
202     catch (ObjectNotDefinedException e)
203     {
204       // This should never ever occur, when it does: just return the base
205
// property
206
DjLogger.log(e.getMessage());
207       return getBaseProperty();
208     }
209   }
210
211   private DjProperty doGetProperty() throws ObjectNotDefinedException
212   {
213     // Not based on a property in a extent but on an (possibly Sql related)
214
// expression?
215
if (_expression != null) return null;
216
217     DjProperty result = getBaseProperty();
218     if (getRelativePath() == null) return result;
219
220     if (!(result.getType() instanceof DjExtent))
221     {
222       // This should never ever occur, when it does: just return the base
223
// property
224
DjLogger.log("Property " + result + " is not of type DjExtent");
225       return getBaseProperty();
226     }
227
228     DjExtent extent = (DjExtent) result.getType();
229     return extent.getProperty(getRelativePath());
230   }
231
232   public boolean hasValidRelativePath()
233   {
234     try
235     {
236       doGetProperty();
237       return true;
238     }
239     catch (ObjectNotDefinedException e)
240     {
241       return false;
242     }
243   }
244
245   public DjProperty getBaseProperty()
246   {
247     try
248     {
249       return _extent.getPropertyByInternalId(_property.getInternalId());
250     }
251     catch (ObjectNotDefinedException onde)
252     {
253       // Might have another internal ID now because of an import/merge. Try
254
// finding it by name then
255
}
256     try
257     {
258       return _extent.getProperty(_property.getName());
259     }
260     catch (ObjectNotDefinedException onde)
261     {
262       // Deleted then? Return null
263
}
264     return null;
265   }
266
267   /**
268    * Sets the displayWidth of the PropertyUsage object
269    *
270    * @param w
271    * The new displayWidth value
272    */

273   public void setDisplayWidth(int w)
274   {
275     _displayWidth = w;
276   }
277
278   /**
279    * Returns the displayWidth of the PropertyUsage object
280    *
281    * @return The displayWidth value
282    */

283   public int getDisplayWidth()
284   {
285     if (_displayWidth > 0) return _displayWidth;
286     DjProperty prop = getProperty();
287     if (getComponentType() == DjDomain.COMP_CHECKBOX) return 40;
288     if (getComponentType() == DjDomain.COMP_COMBOBOX || getComponentType() == DjDomain.COMP_CHOOSER) return 160;
289     if (prop.getTypeCode() == DjDomain.DATE_TYPE) return 100;
290     if (prop.getLength() == 0) return 100;
291     if (prop.getLength() > 40) return 160;
292     return (prop.getLength() + prop.getDecimals()) * 8;
293   }
294
295   /**
296    * Sets the displayHeight of the PropertyUsage object
297    *
298    * @param h
299    * The new displayHeight value
300    */

301   public void setDisplayHeight(int h)
302   {
303     _displayHeight = h;
304   }
305
306   /**
307    * Returns the displayHeight of the PropertyUsage object
308    *
309    * @return The displayHeight value
310    */

311   public int getDisplayHeight()
312   {
313     return _displayHeight;
314   }
315
316   /**
317    * Sets the sortOrder of the PropertyUsage object
318    *
319    * @param s
320    * The new sortOrder value
321    */

322   public void setSortOrder(int s)
323   {
324     _sortOrder = s;
325   }
326
327   /**
328    * Returns the sortOrder of the PropertyUsage object
329    *
330    * @return The sortOrder value
331    */

332   public int getSortOrder()
333   {
334     return _sortOrder;
335   }
336
337   /**
338    * Returns the alias of the PropertyUsage object
339    *
340    * @return The alias value
341    */

342   public String JavaDoc getAlias()
343   {
344     return _alias;
345   }
346
347   /**
348    * Sets the alias of the PropertyUsage object
349    *
350    * @param alias
351    * The new alias value
352    */

353   public void setAlias(String JavaDoc alias)
354   {
355     _alias = alias;
356   }
357
358   /**
359    * Returns the defaultValue of the PropertyUsage object
360    *
361    * @return The defaultValue value
362    */

363   public String JavaDoc getDefaultValue()
364   {
365     return _defaultValue;
366   }
367
368   /**
369    * Sets the defaultValue of the PropertyUsage object
370    *
371    * @param value
372    * The new defaultValue value
373    */

374   public void setDefaultValue(String JavaDoc value)
375   {
376     if (value != null && value.trim().length() == 0) value = null;
377     // Fix backwards compatibility with [[$...]] notation. Is now {$...}
378
if (value != null && value.startsWith("[[$") && value.endsWith("]]"))
379     {
380       value = "{" + value.substring(2);
381       value = value.substring(0, value.length() - 2) + "}";
382     }
383
384     _defaultValue = value;
385   }
386
387   /**
388    * Returns the expression of the PropertyUsage object
389    *
390    * @return The expression value
391    */

392   public String JavaDoc getExpression()
393   {
394     return _expression;
395   }
396
397   /**
398    * Sets the expression of the PropertyUsage object
399    *
400    * @param expression
401    * The new expression value
402    */

403   public void setExpression(String JavaDoc expression)
404   {
405     _expression = expression;
406   }
407
408   /**
409    * Returns the name of the PropertyUsage object
410    *
411    * @return The name value
412    */

413   public String JavaDoc getPropertyName()
414   {
415     DjProperty baseProp = getBaseProperty();
416
417     if (_relativePath == null)
418     {
419       if (baseProp != null) return baseProp.getName();
420       return getAlias();
421     }
422     return baseProp.getName() + "." + _relativePath;
423
424   }
425
426   /**
427    * Sets the prompt of the PropertyUsage object
428    *
429    * @param prompt
430    * The new prompt value
431    */

432   public void setPrompt(String JavaDoc prompt)
433   {
434     _prompt = prompt;
435   }
436
437   /**
438    * Returns the prompt of the PropertyUsage object
439    *
440    * @return The prompt value
441    */

442   public String JavaDoc getPrompt()
443   {
444     return _prompt;
445   }
446
447   /**
448    * Returns the required of the PropertyUsage object
449    *
450    * @return The required value
451    */

452   public boolean isRequired()
453   {
454     return _required;
455   }
456
457   /**
458    * Returns the queryable of the PropertyUsage object
459    *
460    * @return The queryable value
461    */

462   public boolean isQueryable()
463   {
464     return _query;
465   }
466
467   /**
468    * Sets the required of the PropertyUsage object
469    *
470    * @param b
471    * The new required value
472    */

473   public void setRequired(boolean b)
474   {
475     _required = b;
476   }
477
478   public boolean isDisplayed()
479   {
480     return _displayed;
481   }
482
483   public void setDisplayed(boolean b)
484   {
485     _displayed = b;
486   }
487
488   /**
489    * Returns the updateable of the PropertyUsage object
490    *
491    * @return The updateable value
492    */

493   public boolean isUpdateable()
494   {
495     return _update;
496   }
497
498   /**
499    * Sets the queryable of the PropertyUsage object
500    *
501    * @param b
502    * The new queryable value
503    */

504   public void setQueryable(boolean b)
505   {
506     _query = b;
507   }
508
509   /**
510    * Sets the updateable of the PropertyUsage object
511    *
512    * @param b
513    * The new updateable value
514    */

515   public void setUpdateable(boolean b)
516   {
517     _update = b;
518   }
519
520   /**
521    * Sets the seq of the PropertyUsage object
522    *
523    * @param seq
524    * The new seq value
525    */

526   public void setSeq(int seq)
527   {
528     _seq = seq;
529   }
530
531   /**
532    * Returns the seq of the PropertyUsage object
533    *
534    * @return The seq value
535    */

536   public int getSeq()
537   {
538     return _seq;
539   }
540
541   /**
542    * Sets the componentType of the PropertyUsage object
543    *
544    * @param type
545    * The new componentType value
546    */

547   public void setComponentType(int type)
548   {
549     _componentType = type;
550   }
551
552   /**
553    * Returns the componentType of the PropertyUsage object
554    *
555    * @return The componentType value
556    */

557   public int getComponentType()
558   {
559     return _componentType;
560   }
561
562   public boolean isCustomComponent()
563   {
564     return getComponentType() == DjDomain.COMP_CUSTOM || getComponentType() == DjDomain.COMP_CUSTOM_MEMO;
565   }
566
567   /**
568    * Returns the componentTypeName of the PropertyUsage object
569    *
570    * @return The componentTypeName value
571    */

572   public String JavaDoc getComponentTypeName()
573   {
574     return DjDomain.int2componentType(_componentType);
575   }
576
577   /**
578    * Sets the componentType of the PropertyUsage object
579    *
580    * @param typeName
581    * The new componentType value
582    * @exception CatalogException
583    * Description of the Exception
584    */

585   public void setComponentType(String JavaDoc typeName) throws CatalogException
586   {
587     _componentType = DjDomain.componentType2int(typeName);
588   }
589
590   /**
591    * Description of the Method
592    *
593    * @return Description of the Return Value
594    */

595   public String JavaDoc toString()
596   {
597     if (isBasedOnExpression()) return "[E " + getExpression() + "=" + getAlias() + "]";
598     return "[C " + _extent.getName() + "." + getPropertyName() + "=" + getAlias() + "]";
599   }
600
601   /**
602    * Returns the _hint.
603    *
604    * @return String
605    */

606   public String JavaDoc getDescription()
607   {
608     if (getBaseProperty() == null) return Messages.getString("PropertyUsage.deleted");
609     if (_description == null) return getProperty().getDescription();
610     return _description;
611   }
612
613   /**
614    * Sets the _hint.
615    *
616    * @param _hint
617    * The _hint to set
618    */

619   public void setDescription(String JavaDoc description)
620   {
621     if ("".equals(description)) description = null;
622
623     DjProperty prop = getProperty();
624     if (prop != null)
625     {
626
627       String JavaDoc descr = getProperty().getDescription();
628       if (descr != null && descr.equals(description)) description = null;
629     }
630     _description = description;
631   }
632
633   public String JavaDoc getCustomEditorClass()
634   {
635     return _customEditorClass;
636   }
637
638   public void setCustomEditorClass(String JavaDoc customEditorClass)
639   {
640     _customEditorClass = customEditorClass;
641   }
642
643   public String JavaDoc getRelativePath()
644   {
645     return _relativePath;
646   }
647
648   public void setRelativePath(String JavaDoc relativePath)
649   {
650     if (relativePath != null && relativePath.trim().length() == 0) relativePath = null;
651     _relativePath = relativePath;
652   }
653
654   public void validate() throws DjenericException
655   {
656     if (!hasValidRelativePath())
657     {
658       throw new DjenericException(Messages.getString("PropertyUsageTableModel.InvalidPath", getRelativePath()));
659     }
660
661   }
662
663 }
Popular Tags