1 package net.sourceforge.formview; 2 3 import java.io.Serializable ; 4 import java.util.ArrayList ; 5 import java.util.Collections ; 6 import java.util.Iterator ; 7 import java.util.List ; 8 9 import org.apache.commons.collections.FastHashMap; 10 11 18 public class FormView implements Serializable , Cloneable { 19 20 private static final long serialVersionUID = 1L; 21 22 public FormView() { 23 this.lFields = new ArrayList (); 24 this.hFields = new FastHashMap(); 25 this.lStates = new ArrayList (); 26 this.hStates = new FastHashMap(); 27 } 28 29 33 protected String name = null; 34 35 40 protected Class updatedByClass = null; 41 42 45 protected String updatedByMethodName = null; 46 47 49 55 protected ArrayList lFields = null; 56 57 60 protected FastHashMap hFields = null; 61 62 protected ArrayList lStates = null; 63 protected FastHashMap hStates = null; 64 65 68 public String getName() { 69 return name; 70 } 71 72 75 public void setName(String name) { 76 this.name = name; 77 } 78 79 public Class getUpdatedByClass() { 80 return updatedByClass; 81 } 82 83 public void setUpdatedByClass(Class updatedByClass) { 84 this.updatedByClass = updatedByClass; 85 } 86 87 public String getUpdatedByMethodName() { 88 return updatedByMethodName; 89 } 90 91 public void setUpdatedByMethodName(String updatedByMethodName) { 92 this.updatedByMethodName = updatedByMethodName; 93 } 94 95 98 public void addField(FieldView f, Class updatedByClass, String 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 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 state) { 119 if (hStates != null) { 120 return (State)this.hStates.get(state); 121 } 122 return null; 123 } 124 125 public List getStates() { 126 return lStates; 127 } 128 129 133 public List getFields() { 134 return Collections.unmodifiableList(lFields); 135 } 136 137 public FieldView getField(String property) { 138 return (FieldView)this.hFields.get(property); 139 } 140 141 public FieldView getField(String property, String state) { 142 if (state != null) { 143 State formState = getState(state); 144 if (formState != null) { 145 FieldView fieldWithState = formState.getField(property); 147 if (fieldWithState != null) 148 return fieldWithState; 149 } 150 } 151 return getField(property); 152 } 153 154 public Object clone() { 155 FormView formCloned = new FormView(); 156 formCloned.setName(this.name); 157 formCloned.setUpdatedByClass(this.updatedByClass); 158 formCloned.setUpdatedByMethodName(this.updatedByMethodName); 159 for (Iterator iter = lFields.iterator(); iter.hasNext();) { 161 FieldView field = (FieldView) iter.next(); 162 formCloned.addField((FieldView)field.clone()); 163 164 } 165 for (Iterator 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 property = fieldView.getProperty(); 175 FieldView f = getField(property); 176 if (f != null) { 177 f.mergeFieldView(fieldView); 180 } 181 else { 182 addField(fieldView); 185 fieldView.mergeFieldView(fieldView); 187 } 188 } 189 190 193 public String toString() { 194 StringBuffer results = new StringBuffer (); 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 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 toHtml() { 214 StringBuffer results = new StringBuffer ("<tr>"); 215 results.append("<td>Form: </td><td>"); 216 results.append(name); 217 results.append("</td>"); 218 results.append("</tr>"); 219 results.append("</tr>"); 221 results.append("<td>updated by : " + getUpdatedByInformation() + "</td>"); 222 results.append("</tr>"); 223 results.append("<tr><table>"); 225 for (Iterator 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 getUpdatedByInformation() { 234 if (updatedByClass == null) 235 return ""; 236 StringBuffer s = new StringBuffer (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 |