KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sourceforge.formview;
2
3 import java.io.Serializable JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.util.Collections JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8
9 import org.apache.commons.collections.FastHashMap;
10
11 /**
12  * Description : Form view defintion.
13  *
14  * @version 1.0.0
15  * @author <a HREF="mailto:angelo.zerr@gmail.com">Angelo ZERR</a>
16  *
17  */

18 public class FormView implements Serializable JavaDoc, Cloneable JavaDoc {
19
20     private static final long serialVersionUID = 1L;
21     
22     public FormView() {
23         this.lFields = new ArrayList JavaDoc();
24         this.hFields = new FastHashMap();
25         this.lStates = new ArrayList JavaDoc();
26         this.hStates = new FastHashMap();
27     }
28     
29      /**
30      * The name/key the set of form view rules is
31      * stored under.
32      */

33     protected String JavaDoc name = null;
34
35     /////////// DEBUG Information
36
/**
37      * The class which has updated the form
38      * (Validator, FormView or user class)
39      */

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

45     protected String JavaDoc updatedByMethodName = null;
46     
47     /////////// End DEBUG
48

49     /**
50      * List of <code>Field</code>s. Used to maintain
51      * the order they were added in although individual
52      * <code>Field</code>s can be retrieved using
53      * <code>Map</code> of <code>Field</code>s.
54      */

55     protected ArrayList JavaDoc lFields = null;
56
57     /**
58      * Map of <code>Field</code>s keyed on their property value.
59      */

60     protected FastHashMap hFields = null;
61     
62     protected ArrayList JavaDoc lStates = null;
63     protected FastHashMap hStates = null;
64
65     /**
66      * Gets the name/key of the set of validation rules.
67      */

68     public String JavaDoc getName() {
69         return name;
70     }
71
72     /**
73      * Sets the name/key of the set of validation rules.
74      */

75     public void setName(String JavaDoc name) {
76         this.name = name;
77     }
78
79     public Class JavaDoc getUpdatedByClass() {
80         return updatedByClass;
81     }
82
83     public void setUpdatedByClass(Class JavaDoc updatedByClass) {
84         this.updatedByClass = updatedByClass;
85     }
86
87     public String JavaDoc getUpdatedByMethodName() {
88         return updatedByMethodName;
89     }
90
91     public void setUpdatedByMethodName(String JavaDoc updatedByMethodName) {
92         this.updatedByMethodName = updatedByMethodName;
93     }
94
95     /**
96      * Add a <code>Field</code> to the <code>Form</code>.
97      */

98     public void addField(FieldView f, Class JavaDoc updatedByClass, String JavaDoc updatedByMethodName) {
99         f.setUpdatedByClass(updatedByClass);
100         f.setUpdatedByMethodName(updatedByMethodName);
101         this.lFields.add(f);
102         this.hFields.put(f.getProperty(), f);
103     }
104     
105     public void addField(FieldView f, Class JavaDoc updatedByClass) {
106         addField(f, updatedByClass, "");
107     }
108  
109     public void addField(FieldView f) {
110         addField(f, FormView.class);
111     }
112     
113     public void addState(State state) {
114         this.lStates.add(state);
115         this.hStates.put(state.getName(), state);
116     }
117     
118     public State getState(String JavaDoc state) {
119         if (hStates != null) {
120             return (State)this.hStates.get(state);
121         }
122         return null;
123     }
124     
125     public List JavaDoc getStates() {
126         return lStates;
127     }
128
129     /**
130      * A <code>List</code> of <code>Field</code>s is returned as an
131      * unmodifiable <code>List</code>.
132      */

133     public List JavaDoc getFields() {
134         return Collections.unmodifiableList(lFields);
135     }
136  
137     public FieldView getField(String JavaDoc property) {
138         return (FieldView)this.hFields.get(property);
139     }
140     
141     public FieldView getField(String JavaDoc property, String JavaDoc state) {
142         if (state != null) {
143             State formState = getState(state);
144             if (formState != null) {
145                 // Test if field is associated with state
146
FieldView fieldWithState = formState.getField(property);
147                 if (fieldWithState != null)
148                     return fieldWithState;
149             }
150         }
151         return getField(property);
152     }
153     
154     public Object JavaDoc clone() {
155         FormView formCloned = new FormView();
156         formCloned.setName(this.name);
157         formCloned.setUpdatedByClass(this.updatedByClass);
158         formCloned.setUpdatedByMethodName(this.updatedByMethodName);
159         // Clone list of Fields
160
for (Iterator JavaDoc iter = lFields.iterator(); iter.hasNext();) {
161             FieldView field = (FieldView) iter.next();
162             formCloned.addField((FieldView)field.clone());
163             
164         }
165         // Clone list of States
166
for (Iterator JavaDoc iter = lStates.iterator(); iter.hasNext();) {
167             State state = (State) iter.next();
168             formCloned.addState((State)state.clone());
169         }
170         return formCloned;
171     }
172     
173     public void mergeFieldView(FieldView fieldView) {
174         String JavaDoc property = fieldView.getProperty();
175         FieldView f = getField(property);
176         if (f != null) {
177             // Field exist into form
178
// merge with it
179
f.mergeFieldView(fieldView);
180         }
181         else {
182             // Field doesn't exist into form
183
// Add it to form
184
addField(fieldView);
185             // and merge field to manage index
186
fieldView.mergeFieldView(fieldView);
187         }
188     }
189
190     /**
191      * Returns a string representation of the object.
192      */

193     public String JavaDoc toString() {
194         StringBuffer JavaDoc results = new StringBuffer JavaDoc();
195
196         results.append("Form: ");
197         results.append(name);
198         results.append("\n");
199         if (updatedByClass != null) {
200             results.append("\tupdatedByClass = " + updatedByClass.getName());
201             results.append("\n");
202         }
203         results.append("\tupdatedByMethodName = " + updatedByMethodName);
204         results.append("\n");
205         for (Iterator JavaDoc i = lFields.iterator(); i.hasNext();) {
206             results.append("\tField: \n");
207             results.append(i.next());
208             results.append("\n");
209         }
210         return results.toString();
211     }
212     
213     public String JavaDoc toHtml() {
214         StringBuffer JavaDoc results = new StringBuffer JavaDoc("<tr>");
215             results.append("<td>Form: </td><td>");
216             results.append(name);
217             results.append("</td>");
218         results.append("</tr>");
219         // Updated information
220
results.append("</tr>");
221             results.append("<td>updated by : " + getUpdatedByInformation() + "</td>");
222         results.append("</tr>");
223         // Fields
224
results.append("<tr><table>");
225             for (Iterator JavaDoc i = lFields.iterator(); i.hasNext();) {
226                 FieldView field = (FieldView)i.next();
227                 results.append(field.toHtml());
228             }
229         results.append("</table></tr>");
230         return results.toString();
231     }
232
233     private String JavaDoc getUpdatedByInformation() {
234         if (updatedByClass == null)
235             return "";
236         StringBuffer JavaDoc s = new StringBuffer JavaDoc(updatedByClass.getName());
237         if (updatedByMethodName != null && updatedByMethodName.length() > 0) {
238             s.append(" [method : " + updatedByMethodName + "]");
239         }
240         return s.toString();
241     }
242     
243
244 }
245
Popular Tags