1 16 package org.apache.cocoon.forms.formmodel; 17 18 import java.util.ArrayList ; 19 import java.util.Collections ; 20 import java.util.HashMap ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 import java.util.Locale ; 24 import java.util.Map ; 25 26 import org.apache.cocoon.forms.FormsConstants; 27 import org.apache.cocoon.forms.FormContext; 28 import org.apache.cocoon.xml.XMLUtils; 29 import org.xml.sax.ContentHandler ; 30 import org.xml.sax.SAXException ; 31 32 39 public class WidgetList { 40 41 private static final String WIDGETS_EL = "widgets"; 42 43 48 private List widgets; 49 50 53 private Map widgetsById; 54 55 56 60 public WidgetList() { 61 widgets = new ArrayList (); 62 widgetsById = new HashMap (); 63 } 64 65 70 public List getWidgetList() { 71 return Collections.unmodifiableList(this.widgets); 72 } 73 74 79 public Map getWidgetMap() { 80 return Collections.unmodifiableMap(this.widgetsById); 81 } 82 83 88 public void addWidget(Widget widget) { 89 widgets.add(widget); 90 widgetsById.put(widget.getId(), widget); 91 } 92 93 94 102 public void readFromRequest(FormContext formContext) { 103 Iterator widgetIt = iterator(); 104 while (widgetIt.hasNext()) { 105 Widget widget = (Widget)widgetIt.next(); 106 widget.readFromRequest(formContext); 107 } 108 } 109 110 117 public boolean validate() { 118 boolean valid = true; 119 Iterator widgetIt = iterator(); 120 while (widgetIt.hasNext()) { 121 Widget widget = (Widget)widgetIt.next(); 122 valid = valid & widget.validate(); 123 } 124 return valid; 125 } 126 127 133 public boolean hasWidget(String id) { 134 return widgetsById.containsKey(id); 135 } 136 137 143 public Widget getWidget(String id) { 144 return (Widget)widgetsById.get(id); 145 } 146 147 150 public Iterator iterator() { 151 return widgets.iterator(); 152 } 153 154 157 public boolean widgetsHaveValues() { 158 Iterator widgetsIt = iterator(); 159 while(widgetsIt.hasNext()) { 160 Widget widget = (Widget)widgetsIt.next(); 161 if (widget.getValue() == null) 162 return false; 163 } 164 return true; 165 } 166 167 176 public void generateSaxFragment(ContentHandler contentHandler, Locale locale) throws SAXException { 177 contentHandler.startElement(FormsConstants.INSTANCE_NS, WIDGETS_EL, FormsConstants.INSTANCE_PREFIX_COLON + WIDGETS_EL, XMLUtils.EMPTY_ATTRIBUTES); 178 Iterator widgetIt = widgets.iterator(); 179 while (widgetIt.hasNext()) { 180 Widget widget = (Widget)widgetIt.next(); 181 widget.generateSaxFragment(contentHandler, locale); 182 } 183 contentHandler.endElement(FormsConstants.INSTANCE_NS, WIDGETS_EL, FormsConstants.INSTANCE_PREFIX_COLON + WIDGETS_EL); 184 } 185 } 186 187 | Popular Tags |