1 16 package org.apache.cocoon.woody.formmodel; 17 18 import org.apache.cocoon.woody.Constants; 19 import org.apache.cocoon.woody.FormContext; 20 import org.apache.cocoon.woody.event.WidgetEvent; 21 import org.apache.cocoon.woody.event.ValueChangedEvent; 22 import org.apache.cocoon.xml.AttributesImpl; 23 import org.xml.sax.ContentHandler ; 24 import org.xml.sax.SAXException ; 25 26 import java.util.Locale ; 27 28 41 public class BooleanField extends AbstractWidget { 42 private Boolean value = Boolean.FALSE; 45 private BooleanFieldDefinition definition; 46 47 public BooleanField(BooleanFieldDefinition definition) { 48 this.definition = definition; 49 setLocation(definition.getLocation()); 50 } 51 52 public String getId() { 53 return definition.getId(); 54 } 55 56 public void readFromRequest(FormContext formContext) { 57 Object oldValue = value; 58 String param = formContext.getRequest().getParameter(getFullyQualifiedId()); 59 if (param != null && param.equalsIgnoreCase("true")) 60 value = Boolean.TRUE; 61 else 62 value = Boolean.FALSE; 63 64 if (value != oldValue) { 65 getForm().addWidgetEvent(new ValueChangedEvent(this, oldValue, value)); 66 } 67 } 68 69 74 public boolean validate(FormContext formContext) { 75 return true; 77 } 78 79 private static final String BOOLEAN_FIELD_EL = "booleanfield"; 80 private static final String VALUE_EL = "value"; 81 82 public void generateSaxFragment(ContentHandler contentHandler, Locale locale) throws SAXException { 83 AttributesImpl fieldAttrs = new AttributesImpl(); 84 fieldAttrs.addCDATAAttribute("id", getFullyQualifiedId()); 85 contentHandler.startElement(Constants.WI_NS, BOOLEAN_FIELD_EL, Constants.WI_PREFIX_COLON + BOOLEAN_FIELD_EL, fieldAttrs); 86 87 contentHandler.startElement(Constants.WI_NS, VALUE_EL, Constants.WI_PREFIX_COLON + VALUE_EL, Constants.EMPTY_ATTRS); 89 String stringValue = String.valueOf(value != null && value.booleanValue() == true? "true": "false"); 90 contentHandler.characters(stringValue.toCharArray(), 0, stringValue.length()); 91 contentHandler.endElement(Constants.WI_NS, VALUE_EL, Constants.WI_PREFIX_COLON + VALUE_EL); 92 93 definition.generateDisplayData(contentHandler); 95 96 contentHandler.endElement(Constants.WI_NS, BOOLEAN_FIELD_EL, Constants.WI_PREFIX_COLON + BOOLEAN_FIELD_EL); 97 } 98 99 public void generateLabel(ContentHandler contentHandler) throws SAXException { 100 definition.generateLabel(contentHandler); 101 } 102 103 public Object getValue() { 104 return value; 105 } 106 107 111 public void setValue(Object object) { 112 if (object == null) { 113 object = Boolean.FALSE; 114 } 115 116 if (!(object instanceof Boolean )) { 117 throw new RuntimeException ("Cannot set value of boolean field \"" + getFullyQualifiedId() + "\" to a non-Boolean value."); 118 } 119 120 Object oldValue = value; 121 value = (Boolean )object; 122 if (value != oldValue) { 123 getForm().addWidgetEvent(new ValueChangedEvent(this, oldValue, value)); 124 } 125 } 126 127 public void broadcastEvent(WidgetEvent event) { 128 this.definition.fireValueChangedEvent((ValueChangedEvent)event); 129 } 130 } 131 | Popular Tags |