1 16 package org.apache.cocoon.forms.formmodel; 17 18 import org.apache.cocoon.forms.FormContext; 19 import org.apache.cocoon.forms.event.ValueChangedEvent; 20 import org.apache.cocoon.forms.event.ValueChangedListener; 21 import org.apache.cocoon.forms.event.ValueChangedListenerEnabled; 22 import org.apache.commons.lang.ObjectUtils; 23 24 33 public class Union extends AbstractContainerWidget { 34 35 private static final String UNION_EL = "field"; 38 39 private Widget caseWidget; 40 private String caseValue; 41 42 private final UnionDefinition definition; 43 44 public Union(UnionDefinition definition) { 45 super(definition); 46 this.definition = definition; 47 } 50 51 public WidgetDefinition getDefinition() { 52 return definition; 53 } 54 55 60 public void initialize() { 61 String caseWidgetId = definition.getCaseWidgetId(); 62 this.caseWidget = getParent().lookupWidget(caseWidgetId); 63 if(this.caseWidget == null) { 64 throw new RuntimeException ("Could not find case widget \"" 65 + caseWidgetId + "\" for union \"" + getId() + "\" at " + getLocation()); 66 } 67 ((ValueChangedListenerEnabled)caseWidget).addValueChangedListener( 68 new ValueChangedListener() { 69 public void valueChanged(ValueChangedEvent event) { 70 String newValue = (String )event.getNewValue(); 71 if (!ObjectUtils.equals(Union.this.caseValue, newValue)) { 72 Union.this.caseValue = newValue; 73 getForm().addWidgetUpdate(Union.this); 74 } 75 } 76 } 77 ); 78 } 79 80 83 public String getXMLElementName() { 84 return UNION_EL; 85 } 86 87 public Object getValue() { 88 return this.caseWidget.getValue(); 89 } 90 91 public void readFromRequest(FormContext formContext) { 92 this.caseWidget.readFromRequest(formContext); 94 95 Widget widget; 96 String newValue = (String )getValue(); 98 if (newValue != null && !newValue.equals("")) { 99 100 if (getForm().getSubmitWidget() == this.caseWidget && !newValue.equals(this.caseValue)) { 101 widget = getChild(this.caseValue); 105 } else { 106 widget = getChild(newValue); 108 } 109 110 if (widget != null && getCombinedState().isAcceptingInputs()) { 111 widget.readFromRequest(formContext); 112 } 113 } 114 115 if (!ObjectUtils.equals(this.caseValue, newValue)) { 116 this.caseValue = newValue; 117 getForm().addWidgetUpdate(this); 118 } 119 } 120 121 public boolean validate() { 123 if (!getCombinedState().isValidatingValues()) { 124 this.wasValid = true; 125 return true; 126 } 127 Widget widget; 128 boolean valid = true; 129 String value = (String )getValue(); 131 if (value != null && !value.equals("")) { 132 if ((widget = getChild(value)) != null) { 133 valid = valid & widget.validate(); 134 } 135 } 136 this.wasValid = valid; 137 return valid; 138 } 139 140 public Widget getChild(String id) { 141 if (!widgets.hasWidget(id) && ((ContainerDefinition)definition).hasWidget(id)) { 142 ((ContainerDefinition)definition).createWidget(this, id); 143 Widget child = super.getChild(id); 144 child.initialize(); 145 return child; 146 } 147 return super.getChild(id); 148 } 149 150 153 158 } 159 | Popular Tags |