1 16 package org.apache.cocoon.forms.formmodel; 17 18 import java.util.ArrayList ; 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.Map ; 23 24 import org.apache.cocoon.forms.FormsConstants; 25 import org.apache.cocoon.forms.event.CreateEvent; 26 import org.apache.cocoon.forms.event.CreateListener; 27 import org.apache.cocoon.forms.event.WidgetEventMulticaster; 28 import org.apache.cocoon.forms.validation.WidgetValidator; 29 import org.apache.cocoon.util.location.Location; 30 import org.apache.cocoon.xml.XMLUtils; 31 import org.apache.excalibur.xml.sax.XMLizable; 32 import org.xml.sax.ContentHandler ; 33 import org.xml.sax.SAXException ; 34 35 40 public abstract class AbstractWidgetDefinition implements WidgetDefinition { 41 private FormDefinition formDefinition; 42 protected WidgetDefinition parent; 43 44 private Location location = Location.UNKNOWN; 46 private String id; 47 48 private boolean mutable = true; 49 50 private Map attributes; 51 private Map displayData; 52 private List validators; 53 private WidgetState state = WidgetState.ACTIVE; 54 55 protected CreateListener createListener; 56 57 public FormDefinition getFormDefinition() { 58 if (this.formDefinition == null) { 59 if (this instanceof FormDefinition) { 60 this.formDefinition = (FormDefinition)this; 61 } else { 62 this.formDefinition = this.parent.getFormDefinition(); 63 } 64 } 65 return this.formDefinition; 66 } 67 68 71 public void initializeFrom(WidgetDefinition definition) throws Exception { 72 if(definition instanceof AbstractWidgetDefinition) { 73 AbstractWidgetDefinition other = (AbstractWidgetDefinition)definition; 74 75 this.state = other.state; 76 this.createListener = other.createListener; 78 this.validators = new ArrayList (); 79 if(other.validators!=null) { 80 for(int i=0; i<other.validators.size(); i++) 81 this.validators.add(other.validators.get(i)); 82 } 83 84 if(other.attributes!=null) { 85 if(attributes==null) 86 attributes = new HashMap (); 87 copyMap(attributes,other.attributes); 88 } 89 if(other.displayData!=null) { 90 if(displayData==null) 91 displayData = new HashMap (); 92 copyMap(displayData,other.displayData); 93 } 94 } else { 95 throw new Exception ("Definition to inherit from is not of the right type! (at "+getLocation()+")"); 96 } 97 } 98 99 105 protected void copyMap(Map dest, Map src) { 106 dest.clear(); 107 Iterator it = src.entrySet().iterator(); 108 while(it.hasNext()) { 109 Map.Entry entry = (Map.Entry )it.next(); 110 dest.put(entry.getKey(),entry.getValue()); 111 } 112 } 113 114 117 public void checkCompleteness() throws IncompletenessException 118 { 119 if( (id==null || "".equals(id) && !(this instanceof FormDefinition) )) 121 throw new IncompletenessException("Widget found without an ID! "+this,this); 122 123 124 } 126 127 128 131 public void makeImmutable() { 132 this.mutable = false; 133 } 134 135 138 protected void checkMutable() { 139 if (!this.mutable) { 140 throw new IllegalStateException ("Attempt to modify an immutable WidgetDefinition"); 141 } 142 } 143 144 147 public void setParent(WidgetDefinition definition) { 148 this.parent = definition; 151 } 152 153 157 public WidgetDefinition getParent() { 158 return this.parent; 159 } 160 161 public WidgetState getState() { 162 return this.state; 163 } 164 165 public void setState(WidgetState state) { 166 checkMutable(); 167 this.state = state; 168 } 169 170 public void setLocation(Location location) { 171 checkMutable(); 172 this.location = location; 173 } 174 175 public Location getLocation() { 176 return location; 177 } 178 179 public String getId() { 180 return id; 181 } 182 183 protected void setId(String id) { 184 checkMutable(); 185 this.id = id; 186 } 187 188 protected void setAttributes(Map attributes) { 189 checkMutable(); 190 if(this.attributes==null) { 192 this.attributes = attributes; 193 return; 194 } 195 if(attributes==null) 196 return; 197 198 Iterator entries = attributes.entrySet().iterator(); 200 while(entries.hasNext()) { 201 Map.Entry entry = (Map.Entry )entries.next(); 202 Object key = entry.getKey(); 203 Object value = entry.getValue(); 204 this.attributes.put(key,value); 205 } 206 } 207 208 public Object getAttribute(String name) { 209 if (this.attributes != null) { 210 return this.attributes.get(name); 211 } 212 return null; 213 } 214 215 protected void addCreateListener(CreateListener listener) { 216 checkMutable(); 217 this.createListener = WidgetEventMulticaster.add(this.createListener, listener); 219 } 220 221 public void widgetCreated(Widget widget) { 222 if (this.createListener != null) { 223 widget.getForm().addWidgetEvent(new CreateEvent(widget)); 224 } 225 } 226 227 public void fireCreateEvent(CreateEvent event) { 228 if (event.getSourceWidget().getDefinition() != this) { 230 throw new IllegalArgumentException ("Widget was not created by this definition"); 231 } 232 if (this.createListener != null) { 233 this.createListener.widgetCreated(event); 234 } 235 } 236 237 public void generateLabel(ContentHandler contentHandler) throws SAXException { 238 generateDisplayData("label", contentHandler); 239 } 240 241 248 public void setDisplayData(Map displayData) { 249 checkMutable(); 250 252 if(this.displayData==null) { 253 this.displayData = displayData; 254 return; 255 } 256 if(displayData==null) 257 return; 258 259 Iterator entries = displayData.entrySet().iterator(); 261 while(entries.hasNext()) { 262 Map.Entry entry = (Map.Entry )entries.next(); 263 Object key = entry.getKey(); 264 Object value = entry.getValue(); 265 if(value!=null || !this.displayData.containsKey(key)) 266 this.displayData.put(key,value); 267 } 268 } 269 270 public void addValidator(WidgetValidator validator) { 271 checkMutable(); 272 if (this.validators == null) { 273 this.validators = new ArrayList (); 274 } 275 276 this.validators.add(validator); 277 } 278 279 public void generateDisplayData(String name, ContentHandler contentHandler) throws SAXException { 280 Object data = this.displayData.get(name); 281 if (data != null) { 282 ((XMLizable)data).toSAX(contentHandler); 283 } else if (!this.displayData.containsKey(name)) { 284 throw new IllegalArgumentException ("Unknown display data name '" + name + "'"); 285 } 286 } 287 288 public void generateDisplayData(ContentHandler contentHandler) throws SAXException { 289 Iterator iter = this.displayData.entrySet().iterator(); 291 while (iter.hasNext()) { 292 Map.Entry entry = (Map.Entry )iter.next(); 293 if (entry.getValue() != null) { 294 String name = (String )entry.getKey(); 295 296 contentHandler.startElement(FormsConstants.INSTANCE_NS, name, FormsConstants.INSTANCE_PREFIX_COLON + name, XMLUtils.EMPTY_ATTRIBUTES); 298 299 ((XMLizable)entry.getValue()).toSAX(contentHandler); 300 301 contentHandler.endElement(FormsConstants.INSTANCE_NS, name, FormsConstants.INSTANCE_PREFIX_COLON + name); 302 } 303 } 304 } 305 306 public boolean validate(Widget widget) { 307 if (this.validators == null) { 308 return true; 310 311 } 312 Iterator iter = this.validators.iterator(); 313 while(iter.hasNext()) { 314 WidgetValidator validator = (WidgetValidator)iter.next(); 315 if (! validator.validate(widget)) { 316 return false; 318 } 319 } 320 return true; 322 } 323 } 324 | Popular Tags |