KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > formview > FieldView


1 package net.sourceforge.formview;
2
3 import java.io.Serializable JavaDoc;
4
5 import org.apache.commons.collections.FastHashMap;
6 import org.apache.commons.lang.StringUtils;
7
8 /**
9  * Field.
10  * @version 1.0.0
11  * @author <a HREF="mailto:angelo.zerr@gmail.com">Angelo ZERR</a>
12  *
13  */

14 public class FieldView implements Cloneable JavaDoc, Serializable JavaDoc {
15
16     private static final long serialVersionUID = 1L;
17     
18     protected String JavaDoc property = null;
19     protected String JavaDoc maxlength = null;
20     private String JavaDoc behaviour = null;
21     private String JavaDoc required = null;
22     private String JavaDoc date = null;
23     
24     private String JavaDoc dependsOn = null;
25     
26     private Integer JavaDoc index = null;
27     
28     private String JavaDoc roles = null;
29     private String JavaDoc[] rolesArray = null;
30     private String JavaDoc roleDefinitionName = null;
31     
32     /**
33      * Map of <code>index</code>s keyed on their property value.
34      */

35     protected FastHashMap hFieldsIndexed = null;
36  
37     /////////// DEBUG Information
38
/**
39     * The class which has updated the form
40     * (Validator, FormView or user class)
41     */

42    protected Class JavaDoc updatedByClass = null;
43      
44    /**
45     * The method which has updated the form
46     */

47    protected String JavaDoc updatedByMethodName = null;
48    
49    /////////// End DEBUG
50

51     public FieldView() {
52         
53     }
54     
55     public FieldView(String JavaDoc property) {
56         this.property = property;
57     }
58     
59     /**
60      * Gets the property name of the field.
61      */

62     public String JavaDoc getProperty() {
63         return this.property;
64     }
65
66     /**
67      * Sets the property name of the field.
68      */

69     public void setProperty(String JavaDoc property) {
70         this.property = property;
71     }
72
73     public String JavaDoc getMaxlength() {
74         return maxlength;
75     }
76
77     public void setMaxlength(String JavaDoc maxlength) {
78         this.maxlength = maxlength;
79     }
80
81     public String JavaDoc getBehaviour() {
82         return behaviour;
83     }
84
85     public void setBehaviour(String JavaDoc behaviour) {
86         this.behaviour = behaviour;
87     }
88
89     public String JavaDoc getRequired() {
90         return required;
91     }
92
93     public void setRequired(String JavaDoc required) {
94         this.required = required;
95     }
96     
97     public boolean isFiedlRequired() {
98         return (required != null && required.equalsIgnoreCase("true"));
99     }
100     
101     public String JavaDoc getDate() {
102         return date;
103     }
104
105     public void setDate(String JavaDoc date) {
106         this.date = date;
107     }
108     
109     public boolean isFieldDate() {
110         return (date != null && date.equalsIgnoreCase("true"));
111     }
112  
113      public Class JavaDoc getUpdatedByClass() {
114         return updatedByClass;
115     }
116
117     public void setUpdatedByClass(Class JavaDoc updatedByClass) {
118         this.updatedByClass = updatedByClass;
119     }
120
121     public String JavaDoc getUpdatedByMethodName() {
122         return updatedByMethodName;
123     }
124
125     public void setUpdatedByMethodName(String JavaDoc updatedByMethodName) {
126         this.updatedByMethodName = updatedByMethodName;
127     }
128     
129     public Integer JavaDoc getIndex() {
130         return index;
131     }
132
133     public void setIndex(Integer JavaDoc index) {
134         this.index = index;
135     }
136
137     /**
138      * @return Returns the roles.
139      */

140     public String JavaDoc getRoles() {
141         return roles;
142     }
143     /**
144      * @param roles The roles to set.
145      */

146     public void setRoles(String JavaDoc roles) {
147         this.roles = roles;
148         this.rolesArray = null;
149     }
150     
151     
152     
153     public String JavaDoc[] getRolesArray() {
154         // Compute Roles Array
155
if (rolesArray == null &&
156             roles != null &&
157             roles.length() > 0) {
158             this.rolesArray = StringUtils.split(roles, ",");
159         }
160         return rolesArray;
161     }
162     
163         
164     /**
165      * @return Returns the roleDefinitionName.
166      */

167     public String JavaDoc getRoleDefinitionName() {
168         return roleDefinitionName;
169     }
170     /**
171      * @param roleDefinitionName The roleDefinitionName to set.
172      */

173     public void setRoleDefinitionName(String JavaDoc roleDefinitionName) {
174         this.roleDefinitionName = roleDefinitionName;
175     }
176     public void mergeFieldView(FieldView fieldView) {
177         Integer JavaDoc index = fieldView.getIndex();
178         if (index != null) {
179             // Merge fieldwiew indexed with information
180
// of this (fieldview) and put it to Map fields indexed
181
if (fieldView.getBehaviour() == null)
182                 fieldView.setBehaviour(this.behaviour);
183             if (fieldView.getDate() == null)
184                 fieldView.setDate(this.date);
185             if (fieldView.getMaxlength() == null)
186                 fieldView.setMaxlength(this.maxlength);
187             if (fieldView.getRequired() == null)
188                 fieldView.setRequired(this.required);
189             if (fieldView.getRoles() == null)
190                 fieldView.setRoles(this.roles);
191             
192             if (hFieldsIndexed == null) {
193                 hFieldsIndexed = new FastHashMap();
194             }
195             hFieldsIndexed.put(index, fieldView);
196         }
197         else {
198             // Merge this (fieldview)
199
// with information of parameter fieldwiew
200
merge(this, fieldView);
201         }
202     }
203     
204     public boolean isIndexed() {
205         return (hFieldsIndexed != null && hFieldsIndexed.size() > 0);
206     }
207     
208     public FieldView getFieldWithIndex(Integer JavaDoc index) {
209         if (hFieldsIndexed != null) {
210             return (FieldView)hFieldsIndexed.get(index);
211         }
212         return null;
213     }
214     
215     private void merge(FieldView fieldView1, FieldView fieldView2) {
216         if (fieldView2.getBehaviour() != null)
217             fieldView1.setBehaviour(fieldView2.getBehaviour());
218         if (fieldView2.getDate() != null)
219             fieldView1.setDate(fieldView2.getDate());
220         if (fieldView2.getMaxlength() != null)
221             fieldView1.setMaxlength(fieldView2.getMaxlength());
222         if (fieldView2.getRequired() != null)
223             fieldView1.setRequired(fieldView2.getRequired());
224         if (fieldView2.getRoles() != null)
225             fieldView1.setRoles(fieldView2.getRoles());
226     }
227
228     /**
229      * Returns a string representation of the object.
230      */

231     public String JavaDoc toString() {
232         StringBuffer JavaDoc results = new StringBuffer JavaDoc();
233         results.append("\t\tproperty = " + property + "\n");
234         results.append("\n");
235         if (updatedByClass != null)
236             results.append("\t\tupdatedByClass = " + updatedByClass.getName() + "\n");
237         results.append("\t\tupdatedByMethodName = " + updatedByMethodName + "\n");
238         results.append("\t\tmaxlength = " + maxlength + "\n");
239         results.append("\t\trequired = " + required + "\n");
240         results.append("\t\tdate = " + date + "\n");
241         results.append("\t\tbehaviour = " + behaviour + "\n");
242         results.append("\t\troles = " + roles + "\n");
243         results.append("\t\troleDefinitionName = " + roleDefinitionName + "\n");
244         return results.toString();
245     }
246     
247     public String JavaDoc toHtml() {
248         StringBuffer JavaDoc results = new StringBuffer JavaDoc("<tr>");
249         results.append("<td>property : " + property + "</td>");
250         results.append("<td>updated by : " + getUpdatedByInformation() + "</td>");
251         results.append("<td>maxlength : " + maxlength + "</td>");
252         results.append("<td>required : " + required + "</td>");
253         results.append("<td>date : " + date + "</td>");
254         results.append("<td>behaviour : " + behaviour + "</td>");
255         results.append("<td>roles : " + roles + "</td>");
256         results.append("<td>roleDefinitionName : " + roleDefinitionName + "</td>");
257         results.append("</tr>");
258         return results.toString();
259     }
260     
261     private String JavaDoc getUpdatedByInformation() {
262         if (updatedByClass == null)
263             return "";
264         StringBuffer JavaDoc s = new StringBuffer JavaDoc(updatedByClass.getName());
265         if (updatedByMethodName != null && updatedByMethodName.length() > 0) {
266             s.append(" [method : " + updatedByMethodName + "]");
267         }
268         return s.toString();
269     }
270     
271     /**
272      * Return true if Field depends on dependName (date, required or maxlength)
273      * @param dependName
274      * @return
275      */

276     public boolean isDependsOn(String JavaDoc dependName) {
277         return (getDependsOn().indexOf(dependName) != -1);
278     }
279     
280     private String JavaDoc getDependsOn() {
281         if (dependsOn != null)
282             return dependsOn;
283         dependsOn = "";
284         if ("true".equals(date))
285             dependsOn += "date,";
286         if (maxlength != null && maxlength.length() > 0)
287             dependsOn += "maxlength,";
288         if ("true".equals(required))
289             dependsOn += "required,";
290         return dependsOn;
291     }
292     
293     public Object JavaDoc clone() {
294         FieldView fieldCloned = new FieldView(this.property);
295         fieldCloned.setBehaviour(this.behaviour);
296         fieldCloned.setDate(this.date);
297         fieldCloned.setIndex(this.index);
298         fieldCloned.setMaxlength(this.maxlength);
299         fieldCloned.setRequired(this.required);
300         fieldCloned.setUpdatedByClass(this.updatedByClass);
301         fieldCloned.setUpdatedByMethodName(this.updatedByMethodName);
302         fieldCloned.setRoles(this.roles);
303         fieldCloned.setRoleDefinitionName(this.roleDefinitionName);
304         return fieldCloned;
305     }
306    
307
308 }
309
Popular Tags