KickJava   Java API By Example, From Geeks To Geeks.

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


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

16 public class FormSetView implements Serializable JavaDoc {
17
18     private static final long serialVersionUID = 1L;
19     
20     /**
21      * A <code>Map</code> of <code>Form</code>s
22      * using the name field of the <code>Form</code> as the key.
23      */

24     private Map JavaDoc forms = new HashMap JavaDoc();
25     private Map JavaDoc formsWithStateMap = new HashMap JavaDoc();
26
27     /**
28      * Add a <code>Form</code> to the <code>FormSet</code>.
29      */

30     public void addForm(FormView f, Class JavaDoc updatedByClass, String JavaDoc updatedByMethodName) {
31         f.setUpdatedByClass(updatedByClass);
32         f.setUpdatedByMethodName(updatedByMethodName);
33         forms.put(f.getName(), f);
34     }
35     
36     public void addForm(FormView f, Class JavaDoc updatedByClass) {
37         addForm(f, updatedByClass, "");
38     }
39
40     public void addForm(FormView f) {
41         addForm(f, FormView.class);
42     }
43     
44     public FormView getForm(String JavaDoc formName, String JavaDoc state) {
45         if (state != null && state.length() > 0) {
46             Object JavaDoc formWithState = formsWithStateMap.get(buildKey(formName, state));
47             if (formWithState == null) {
48                 // Compute form with state
49
computeFormWithState(formName, state);
50             }
51             formWithState = formsWithStateMap.get(buildKey(formName, state));
52             if (formWithState instanceof Boolean JavaDoc)
53                 return null;
54             return (FormView)formWithState;
55         }
56         return getForm(formName);
57     }
58     
59     /**
60      * Retrieve a <code>Form</code> based on the form name.
61      */

62     public FormView getForm(String JavaDoc formName) {
63         return (FormView) this.forms.get(formName);
64     }
65
66     /**
67      * A <code>Map</code> of <code>Form</code>s is returned as an
68      * unmodifiable <code>Map</code> with the key based on the form name.
69      */

70     public Map JavaDoc getForms() {
71         return Collections.unmodifiableMap(forms);
72     }
73     
74     private String JavaDoc buildKey(String JavaDoc formName, String JavaDoc state) {
75         return formName + "_" + state;
76     }
77     
78     private synchronized void computeFormWithState(String JavaDoc formName, String JavaDoc state) {
79         FormView form = getForm(formName);
80         if (form != null) {
81             // Clone form and update field with state
82
FormView formWithState = new FormView();
83             formWithState.setName(formName);
84             formWithState.setUpdatedByClass(FormView.class);
85             formWithState.setUpdatedByMethodName("computeFormWithState");
86             // get State of form (which contains list of fields with behaviour)
87
State formState = form.getState(state);
88             
89             // MERGE characteristics of field (defined after form)
90
// With characteristics of field (defined for this State)
91
// Loop for each field
92
List JavaDoc fields = form.getFields();
93             for (Iterator JavaDoc iter = fields.iterator(); iter.hasNext();) {
94                 // compute all fields form with state
95
FieldView field = (FieldView) iter.next();
96                 // Test if field is
97
formWithState.addField(computeFieldWithState(field, formState));
98             }
99             if (formState != null) {
100                 // ADD field defined into State and not defined into after Form.
101
// Loop for fields of State and add Field if field is not defined
102
// after form
103
List JavaDoc fieldsOfState = formState.getFields();
104                 if (fieldsOfState != null) {
105                     for (Iterator JavaDoc iter = fieldsOfState.iterator(); iter.hasNext();) {
106                         FieldView fieldState = (FieldView) iter.next();
107                         // Test if current field is into Form
108
if (formWithState.getField(fieldState.getProperty()) == null) {
109                             // Add this Field into form
110
formWithState.addField((FieldView)fieldState.clone());
111                         }
112                     }
113                 }
114             }
115             formsWithStateMap.put(buildKey(formName, state), formWithState);
116         }
117         else {
118             formsWithStateMap.put(buildKey(formName, state), new Boolean JavaDoc(false));
119         }
120     }
121     
122     private FieldView computeFieldWithState(FieldView f, State formState) {
123         FieldView fieldWithState = (FieldView)f.clone();
124         if (formState != null) {
125             // Test if field exist for this state
126
FieldView fieldView = formState.getField(f.getProperty());
127             if (fieldView != null) {
128                 // Field exist for this state merge value with field
129
if (fieldView.getBehaviour() != null) {
130                     fieldWithState.setBehaviour(fieldView.getBehaviour());
131                 }
132                 if (fieldView.getDate() != null) {
133                     fieldWithState.setDate(fieldView.getDate());
134                 }
135                 if (fieldView.getMaxlength() != null) {
136                     fieldWithState.setMaxlength(fieldView.getMaxlength());
137                 }
138                 if (fieldView.getRequired() != null) {
139                     fieldWithState.setRequired(fieldView.getRequired());
140                 }
141             }
142         }
143         return fieldWithState;
144     }
145 }
146
Popular Tags