1 16 package org.apache.cocoon.forms.formmodel; 17 18 import java.util.Locale ; 19 20 import org.apache.cocoon.forms.FormsConstants; 21 import org.apache.cocoon.forms.FormContext; 22 import org.apache.cocoon.forms.event.*; 23 import org.apache.cocoon.xml.AttributesImpl; 24 import org.apache.cocoon.xml.XMLUtils; 25 import org.xml.sax.ContentHandler ; 26 import org.xml.sax.SAXException ; 27 import org.apache.cocoon.forms.validation.ValidationErrorAware; 28 import org.apache.cocoon.forms.validation.ValidationError; 29 import org.apache.commons.lang.BooleanUtils; 30 31 44 public class BooleanField extends AbstractWidget 45 implements ValidationErrorAware, ValueChangedListenerEnabled { 46 47 private static final String BOOLEAN_FIELD_EL = "booleanfield"; 48 private static final String VALUE_EL = "value"; 49 private static final String VALIDATION_MSG_EL = "validation-message"; 50 51 private Boolean value = Boolean.FALSE; 54 private final BooleanFieldDefinition definition; 55 56 private ValueChangedListener listener; 57 protected ValidationError validationError; 58 59 60 public BooleanField(BooleanFieldDefinition definition) { 61 super(definition); 62 this.definition = definition; 63 this.listener = definition.getValueChangedListener(); 64 } 65 66 public WidgetDefinition getDefinition() { 67 return this.definition; 68 } 69 70 public void initialize() { 71 Boolean value = this.definition.getInitialValue(); 72 if (value != null) { 73 setValue(value); 74 } 75 super.initialize(); 76 } 77 78 public void readFromRequest(FormContext formContext) { 79 if (!getCombinedState().isAcceptingInputs()) { 80 return; 81 } 82 83 validationError = null; 84 Object oldValue = value; 85 String param = formContext.getRequest().getParameter(getRequestParameterName()); 86 87 value = BooleanUtils.toBooleanObject(definition.getTrueParamValue().equals(param)); 88 89 if (!value.equals(oldValue)) { 90 getForm().addWidgetEvent(new ValueChangedEvent(this, oldValue, value)); 91 } 92 } 93 94 98 public ValidationError getValidationError() { 99 return validationError; 100 } 101 102 108 public void setValidationError(ValidationError error) { 109 this.validationError = error; 110 getForm().addWidgetUpdate(this); 111 } 112 113 116 public String getXMLElementName() { 117 return BOOLEAN_FIELD_EL; 118 } 119 120 protected AttributesImpl getXMLElementAttributes() { 121 AttributesImpl attrs = super.getXMLElementAttributes(); 122 attrs.addCDATAAttribute("true-value", definition.getTrueParamValue()); 124 return attrs; 125 } 126 127 public void generateItemSaxFragment(ContentHandler contentHandler, Locale locale) throws SAXException { 128 contentHandler.startElement(FormsConstants.INSTANCE_NS, VALUE_EL, FormsConstants.INSTANCE_PREFIX_COLON + VALUE_EL, XMLUtils.EMPTY_ATTRIBUTES); 130 131 String stringValue = BooleanUtils.toBoolean(value) ? definition.getTrueParamValue() : "false"; 132 133 contentHandler.characters(stringValue.toCharArray(), 0, stringValue.length()); 134 contentHandler.endElement(FormsConstants.INSTANCE_NS, VALUE_EL, FormsConstants.INSTANCE_PREFIX_COLON + VALUE_EL); 135 if (validationError != null) { 137 contentHandler.startElement(FormsConstants.INSTANCE_NS, VALIDATION_MSG_EL, FormsConstants.INSTANCE_PREFIX_COLON + VALIDATION_MSG_EL, XMLUtils.EMPTY_ATTRIBUTES); 138 validationError.generateSaxFragment(contentHandler); 139 contentHandler.endElement(FormsConstants.INSTANCE_NS, VALIDATION_MSG_EL, FormsConstants.INSTANCE_PREFIX_COLON + VALIDATION_MSG_EL); 140 } 141 } 142 143 public Object getValue() { 144 return value; 145 } 146 147 151 public void setValue(Object object) { 152 if (object == null) { 153 object = Boolean.FALSE; 154 } 155 156 if (!(object instanceof Boolean )) { 157 throw new RuntimeException ("Cannot set value of boolean field \"" + getRequestParameterName() + "\" to a non-Boolean value."); 158 } 159 160 Object oldValue = value; 161 value = (Boolean )object; 162 if (!value.equals(oldValue)) { 163 Form form = getForm(); 164 if (hasValueChangedListeners() || this.getForm().hasFormHandler()) { 165 form.addWidgetEvent(new ValueChangedEvent(this, oldValue, value)); 166 } 167 form.addWidgetUpdate(this); 168 } 169 } 170 171 176 public void addValueChangedListener(ValueChangedListener listener) { 177 this.listener = WidgetEventMulticaster.add(this.listener, listener); 178 } 179 180 public void removeValueChangedListener(ValueChangedListener listener) { 181 this.listener = WidgetEventMulticaster.remove(this.listener, listener); 182 } 183 184 public boolean hasValueChangedListeners() { 185 return this.listener != null; 186 } 187 188 public void broadcastEvent(WidgetEvent event) { 189 if (event instanceof ValueChangedEvent) { 190 if (this.listener != null) { 191 this.listener.valueChanged((ValueChangedEvent)event); 192 } 193 } else { 194 super.broadcastEvent(event); 196 } 197 } 198 } 199 | Popular Tags |