1 16 package org.apache.cocoon.woody.formmodel; 17 18 import java.util.ArrayList ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 import java.util.Locale ; 22 23 import org.apache.cocoon.woody.Constants; 24 import org.apache.cocoon.woody.FormContext; 25 import org.apache.cocoon.woody.event.WidgetEvent; 26 import org.apache.cocoon.woody.validation.WidgetValidator; 27 import org.apache.cocoon.xml.AttributesImpl; 28 import org.xml.sax.ContentHandler ; 29 import org.xml.sax.SAXException ; 30 31 37 public abstract class AbstractWidget implements Widget { 38 private String location; 39 private Widget parent; 40 private Form form; 41 protected AbstractWidgetDefinition definition; 42 43 private List validators; 44 45 48 protected void setDefinition(AbstractWidgetDefinition definition) { 49 this.definition = definition; 50 } 51 52 55 public String getId() { 56 return definition.getId(); 57 } 58 59 62 protected void setLocation(String location) { 63 this.location = location; 64 } 65 66 69 public String getLocation() { 70 return this.location; 71 } 72 73 public Widget getParent() { 74 return parent; 75 } 76 77 public void setParent(Widget widget) { 78 this.parent = widget; 79 } 80 81 public Form getForm() { 82 if (this.form == null) { 83 if (parent == null) { 84 this.form = (Form)this; 85 } else { 86 this.form = parent.getForm(); 87 } 88 } 89 return this.form; 90 } 91 92 public String getNamespace() { 93 if (getParent() != null && getParent().getNamespace().length() > 0) { 94 return getParent().getNamespace() + "." + getId(); 95 } else { 96 return getId(); 97 } 98 } 99 100 public String getFullyQualifiedId() { 101 if (parent != null) { 102 String namespace = parent.getNamespace(); 103 if (namespace.length() > 0) { 104 return namespace + "." + getId(); 105 } 106 } 107 return getId(); 108 } 109 110 public Object getValue() { 111 return null; 112 } 113 114 public void setValue(Object object) { 115 throw new RuntimeException ("Cannot set the value of widget " + getFullyQualifiedId()); 116 } 117 118 public boolean isRequired() { 119 return false; 120 } 121 122 public Widget getWidget(String id) { 123 return null; 124 } 125 126 public void broadcastEvent(WidgetEvent event) { 127 throw new UnsupportedOperationException ("Widget " + this.getFullyQualifiedId() + " doesn't handle events."); 128 } 129 130 135 public void addValidator(WidgetValidator validator) { 136 if (this.validators == null) { 137 this.validators = new ArrayList (); 138 } 139 140 this.validators.add(validator); 141 } 142 143 149 public boolean removeValidator(WidgetValidator validator) { 150 return (this.validators == null)? false : this.validators.remove(validator); 151 } 152 153 public boolean validate(FormContext context) { 154 if (!this.definition.validate(this, context)) { 156 return false; 158 } else { 159 if (this.validators == null) { 161 return true; 163 } else { 164 Iterator iter = this.validators.iterator(); 166 while(iter.hasNext()) { 167 WidgetValidator validator = (WidgetValidator)iter.next(); 168 if (!validator.validate(this, context)) { 169 return false; 170 } 171 } 172 return true; 174 } 175 } 176 } 177 178 public void generateLabel(ContentHandler contentHandler) throws SAXException { 179 if (definition != null) { 180 definition.generateDisplayData("label", contentHandler); 181 } 182 } 183 184 public void generateItemSaxFragment(ContentHandler contentHandler, Locale locale) throws SAXException { 185 } 187 188 public void generateSaxFragment(ContentHandler contentHandler, Locale locale, String element, WidgetDefinition definition) 189 throws SAXException { 190 AttributesImpl attrs = new AttributesImpl(); 191 attrs.addCDATAAttribute("id", getFullyQualifiedId()); 192 contentHandler.startElement(Constants.WI_NS, element, Constants.WI_PREFIX_COLON + element, attrs); 193 generateItemSaxFragment(contentHandler, locale); 194 contentHandler.endElement(Constants.WI_NS, element, Constants.WI_PREFIX_COLON + element); 195 } 196 } 197 | Popular Tags |