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.datatype.Datatype; 23 import org.apache.cocoon.forms.event.ValueChangedEvent; 24 import org.apache.cocoon.forms.event.ValueChangedListener; 25 import org.apache.cocoon.forms.event.ValueChangedListenerEnabled; 26 import org.apache.cocoon.forms.event.WidgetEvent; 27 import org.apache.cocoon.forms.event.WidgetEventMulticaster; 28 import org.apache.cocoon.xml.XMLUtils; 29 import org.apache.commons.lang.ObjectUtils; 30 import org.xml.sax.ContentHandler ; 31 import org.xml.sax.SAXException ; 32 33 42 public class Output extends AbstractWidget implements DataWidget, ValueChangedListenerEnabled { 43 44 private final OutputDefinition definition; 45 private Object value; 46 private ValueChangedListener listener; 47 48 public OutputDefinition getOutputDefinition() { 49 return definition; 50 } 51 52 public Datatype getDatatype() { 53 return definition.getDatatype(); 54 } 55 56 protected Output(OutputDefinition definition) { 57 super(definition); 58 this.definition = definition; 59 this.listener = definition.getValueChangedListener(); 60 } 61 62 public WidgetDefinition getDefinition() { 63 return definition; 64 } 65 66 public void readFromRequest(FormContext formContext) { 67 } 69 70 73 public boolean validate() { 74 return true; 75 } 76 77 80 public boolean isValid() { 81 return true; 82 } 83 84 private static final String OUTPUT_EL = "output"; 85 private static final String VALUE_EL = "value"; 86 87 88 91 public String getXMLElementName() { 92 return OUTPUT_EL; 93 } 94 95 protected void generateItemSaxFragment(ContentHandler contentHandler, Locale locale) throws SAXException { 96 if (value != null) { 98 contentHandler.startElement(FormsConstants.INSTANCE_NS, VALUE_EL, FormsConstants.INSTANCE_PREFIX_COLON + VALUE_EL, XMLUtils.EMPTY_ATTRIBUTES); 99 String stringValue; 100 stringValue = definition.getDatatype().convertToString(value, locale); 101 contentHandler.characters(stringValue.toCharArray(), 0, stringValue.length()); 102 contentHandler.endElement(FormsConstants.INSTANCE_NS, VALUE_EL, FormsConstants.INSTANCE_PREFIX_COLON + VALUE_EL); 103 } 104 } 105 106 public Object getValue() { 107 return value; 108 } 109 110 public void setValue(Object object) { 111 if (object != null && !definition.getDatatype().getTypeClass().isAssignableFrom(object.getClass())) { 112 throw new RuntimeException ("Tried to set value of output widget \"" 113 + getRequestParameterName() 114 + "\" with an object of an incorrect type: " 115 + "expected " + definition.getDatatype().getTypeClass() 116 + ", received " + object.getClass() + "."); 117 } 118 if (!ObjectUtils.equals(value, object)) { 119 Object oldValue = value; 120 value = object; 121 if (this.hasValueChangedListeners() || this.getForm().hasFormHandler()) { 122 getForm().addWidgetEvent(new ValueChangedEvent(this, oldValue, value)); 123 } 124 getForm().addWidgetUpdate(this); 125 } 126 } 127 128 public void addValueChangedListener(ValueChangedListener listener) { 129 this.listener = WidgetEventMulticaster.add(this.listener, listener); 130 } 131 132 public void removeValueChangedListener(ValueChangedListener listener) { 133 this.listener = WidgetEventMulticaster.remove(this.listener, listener); 134 } 135 136 public boolean hasValueChangedListeners() { 137 return this.listener != null; 138 } 139 140 public void broadcastEvent(WidgetEvent event) { 141 if (event instanceof ValueChangedEvent) { 142 if (this.listener != null) { 143 this.listener.valueChanged((ValueChangedEvent)event); 144 } 145 } else { 146 super.broadcastEvent(event); 148 } 149 } 150 } 151 | Popular Tags |