1 16 package org.apache.cocoon.woody.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.Locale ; 23 import java.util.Map ; 24 25 import org.apache.cocoon.woody.Constants; 26 import org.apache.cocoon.woody.FormContext; 27 import org.xml.sax.ContentHandler ; 28 import org.xml.sax.SAXException ; 29 30 36 public class ContainerDelegate { 37 private List widgets; 39 private Map widgetsById; 40 41 private static final String WIDGETS_EL = "widgets"; 42 43 public ContainerDelegate(WidgetDefinition definition) { 44 widgets = new ArrayList (); 45 widgetsById = new HashMap (); 46 } 48 49 public void addWidget(Widget widget) { 50 widgets.add(widget); 51 widgetsById.put(widget.getId(), widget); 52 } 53 54 public void readFromRequest(FormContext formContext) { 55 Iterator widgetIt = widgets.iterator(); 56 while (widgetIt.hasNext()) { 57 Widget widget = (Widget)widgetIt.next(); 58 widget.readFromRequest(formContext); 59 } 60 } 61 62 public boolean validate(FormContext formContext) { 63 boolean valid = true; 64 Iterator widgetIt = widgets.iterator(); 65 while (widgetIt.hasNext()) { 66 Widget widget = (Widget)widgetIt.next(); 67 valid = valid & widget.validate(formContext); 68 } 69 return valid; 70 } 71 72 public boolean hasWidget(String id) { 73 return widgetsById.containsKey(id); 74 } 75 76 public Widget getWidget(String id) { 77 return (Widget)widgetsById.get(id); 78 } 79 80 public Iterator iterator() { 81 return widgets.iterator(); 82 } 83 84 87 public boolean widgetsHaveValues() { 88 Iterator widgetsIt = widgets.iterator(); 89 while(widgetsIt.hasNext()) { 90 Widget widget = (Widget)widgetsIt.next(); 91 if (widget.getValue() == null) 92 return false; 93 } 94 return true; 95 } 96 97 public void generateSaxFragment(ContentHandler contentHandler, Locale locale) throws SAXException { 98 contentHandler.startElement(Constants.WI_NS, WIDGETS_EL, Constants.WI_PREFIX_COLON + WIDGETS_EL, Constants.EMPTY_ATTRS); 99 Iterator widgetIt = widgets.iterator(); 100 while (widgetIt.hasNext()) { 101 Widget widget = (Widget)widgetIt.next(); 102 widget.generateSaxFragment(contentHandler, locale); 103 } 104 contentHandler.endElement(Constants.WI_NS, WIDGETS_EL, Constants.WI_PREFIX_COLON + WIDGETS_EL); 105 } 106 } 107 108 | Popular Tags |