1 package net.sourceforge.formview; 2 3 import java.io.Serializable ; 4 import java.util.Collections ; 5 import java.util.HashMap ; 6 import java.util.Iterator ; 7 import java.util.List ; 8 import java.util.Map ; 9 10 16 public class FormSetView implements Serializable { 17 18 private static final long serialVersionUID = 1L; 19 20 24 private Map forms = new HashMap (); 25 private Map formsWithStateMap = new HashMap (); 26 27 30 public void addForm(FormView f, Class updatedByClass, String updatedByMethodName) { 31 f.setUpdatedByClass(updatedByClass); 32 f.setUpdatedByMethodName(updatedByMethodName); 33 forms.put(f.getName(), f); 34 } 35 36 public void addForm(FormView f, Class 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 formName, String state) { 45 if (state != null && state.length() > 0) { 46 Object formWithState = formsWithStateMap.get(buildKey(formName, state)); 47 if (formWithState == null) { 48 computeFormWithState(formName, state); 50 } 51 formWithState = formsWithStateMap.get(buildKey(formName, state)); 52 if (formWithState instanceof Boolean ) 53 return null; 54 return (FormView)formWithState; 55 } 56 return getForm(formName); 57 } 58 59 62 public FormView getForm(String formName) { 63 return (FormView) this.forms.get(formName); 64 } 65 66 70 public Map getForms() { 71 return Collections.unmodifiableMap(forms); 72 } 73 74 private String buildKey(String formName, String state) { 75 return formName + "_" + state; 76 } 77 78 private synchronized void computeFormWithState(String formName, String state) { 79 FormView form = getForm(formName); 80 if (form != null) { 81 FormView formWithState = new FormView(); 83 formWithState.setName(formName); 84 formWithState.setUpdatedByClass(FormView.class); 85 formWithState.setUpdatedByMethodName("computeFormWithState"); 86 State formState = form.getState(state); 88 89 List fields = form.getFields(); 93 for (Iterator iter = fields.iterator(); iter.hasNext();) { 94 FieldView field = (FieldView) iter.next(); 96 formWithState.addField(computeFieldWithState(field, formState)); 98 } 99 if (formState != null) { 100 List fieldsOfState = formState.getFields(); 104 if (fieldsOfState != null) { 105 for (Iterator iter = fieldsOfState.iterator(); iter.hasNext();) { 106 FieldView fieldState = (FieldView) iter.next(); 107 if (formWithState.getField(fieldState.getProperty()) == null) { 109 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 (false)); 119 } 120 } 121 122 private FieldView computeFieldWithState(FieldView f, State formState) { 123 FieldView fieldWithState = (FieldView)f.clone(); 124 if (formState != null) { 125 FieldView fieldView = formState.getField(f.getProperty()); 127 if (fieldView != null) { 128 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 |