KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > regis > forms > Field


1 package org.sapia.regis.forms;
2
3 import java.util.Map JavaDoc;
4
5 import org.sapia.regis.Node;
6 import org.sapia.regis.Property;
7 import org.sapia.regis.RWNode;
8 import org.sapia.regis.type.BuiltinType;
9
10 public class Field implements Comparable JavaDoc{
11   
12   private BuiltinType type;
13   private String JavaDoc name;
14   private boolean mandatory;
15   private int displayOrder;
16   
17   Field(BuiltinType type, String JavaDoc name){
18     this.type = type;
19     this.name = name;
20   }
21   
22   public String JavaDoc getName(){
23     return name;
24   }
25   
26   public BuiltinType getType(){
27     return type;
28   }
29   
30   public void setMandatory(boolean mandatory){
31     this.mandatory = mandatory;
32   }
33   
34   public boolean isMandatory(){
35     return mandatory;
36   }
37   
38   public void set(RWNode instance, String JavaDoc value){
39     type.parse(value);
40     instance.setProperty(name, value);
41   }
42   
43   public void set(RWNode instance, Object JavaDoc value){
44     if(!type.isAssignable(value)){
45       throw new IllegalArgumentException JavaDoc("Cannot assign value for field: " + name +
46           "; expected value of type: " + type.getName());
47     }
48     instance.setProperty(name, type.toString(value));
49   }
50   
51   public void set(RWNode instance, Map JavaDoc properties){
52     Object JavaDoc value = properties.get(name);
53     if(value == null && isMandatory()){
54       throw new IllegalStateException JavaDoc("Property: " + name + " is mandatory");
55     }
56     if(value instanceof String JavaDoc){
57       value = type.parse((String JavaDoc)value);
58     }
59     else if(!type.isAssignable(value)){
60       throw new IllegalArgumentException JavaDoc("Cannot assign value for field: " + name +
61           "; expected value of type: " + type.getName());
62     }
63     instance.setProperty(name, type.toString(value));
64   }
65   
66   public Object JavaDoc get(Node instance){
67     Property prop = instance.getProperty(name);
68     if(prop.isNull()) return null;
69     return type.parse(prop.getValue());
70   }
71   
72   public int getDisplayOrder() {
73     return displayOrder;
74   }
75
76   public void setDisplayOrder(int displayOrder) {
77     this.displayOrder = displayOrder;
78   }
79   
80   public int hashCode(){
81     return name.hashCode();
82   }
83   
84   public boolean equals(Object JavaDoc o){
85     if(o instanceof Field){
86       return ((Field)o).getName().equals(name);
87     }
88     return o == this;
89   }
90   
91   public int compareTo(Object JavaDoc f) {
92     return displayOrder - ((Field)f).getDisplayOrder();
93   }
94
95 }
96
Popular Tags