1 16 package org.apache.cocoon.woody.formmodel; 17 18 import org.apache.cocoon.woody.datatype.SelectionList; 19 import org.apache.cocoon.woody.validation.ValidationError; 20 import org.apache.cocoon.woody.validation.ValidationErrorAware; 21 import org.apache.cocoon.woody.event.WidgetEvent; 22 import org.apache.cocoon.woody.event.ValueChangedEvent; 23 import org.apache.cocoon.woody.Constants; 24 import org.apache.cocoon.woody.FormContext; 25 import org.apache.cocoon.woody.util.I18nMessage; 26 import org.apache.cocoon.xml.AttributesImpl; 27 import org.xml.sax.ContentHandler ; 28 import org.xml.sax.SAXException ; 29 30 import java.util.Locale ; 31 32 49 public class MultiValueField extends AbstractWidget implements ValidationErrorAware, SelectableWidget { 50 private SelectionList selectionList; 51 private MultiValueFieldDefinition fieldDefinition; 52 private String [] enteredValues; 53 private Object [] values; 54 private ValidationError validationError; 55 56 public MultiValueField(MultiValueFieldDefinition definition) { 57 super.setDefinition(definition); 58 this.fieldDefinition = definition; 59 setLocation(definition.getLocation()); 60 } 61 62 public String getId() { 63 return definition.getId(); 64 } 65 66 public void readFromRequest(FormContext formContext) { 67 enteredValues = formContext.getRequest().getParameterValues(getFullyQualifiedId()); 68 validationError = null; 69 values = null; 70 71 boolean conversionFailed = false; 72 if (enteredValues != null) { 73 Object [] tempValues = new Object [enteredValues.length]; 79 for (int i = 0; i < enteredValues.length; i++) { 80 String param = enteredValues[i]; 81 tempValues[i] = fieldDefinition.getDatatype().convertFromString(param, formContext.getLocale()); 82 if (tempValues[i] == null) { 83 conversionFailed = true; 84 break; 85 } 86 } 87 88 if (!conversionFailed) 89 values = tempValues; 90 else 91 values = null; 92 } else { 93 values = new Object [0]; 94 } 95 } 96 97 public boolean validate(FormContext formContext) { 98 if (values != null) 99 validationError = fieldDefinition.getDatatype().validate(values, new ExpressionContextImpl(this)); 100 else 101 validationError = new ValidationError(new I18nMessage("multivaluefield.conversionfailed", Constants.I18N_CATALOGUE)); 102 103 104 return validationError == null ? super.validate(formContext) : false; 105 } 106 107 private static final String MULTIVALUEFIELD_EL = "multivaluefield"; 108 private static final String VALUES_EL = "values"; 109 private static final String VALUE_EL = "value"; 110 private static final String VALIDATION_MSG_EL = "validation-message"; 111 112 public void generateSaxFragment(ContentHandler contentHandler, Locale locale) throws SAXException { 113 AttributesImpl attrs = new AttributesImpl(); 114 attrs.addCDATAAttribute("id", getFullyQualifiedId()); 115 contentHandler.startElement(Constants.WI_NS, MULTIVALUEFIELD_EL, Constants.WI_PREFIX_COLON + MULTIVALUEFIELD_EL, attrs); 116 117 contentHandler.startElement(Constants.WI_NS, VALUES_EL, Constants.WI_PREFIX_COLON + VALUES_EL, Constants.EMPTY_ATTRS); 118 if (values != null) { 119 for (int i = 0; i < values.length; i++) { 120 contentHandler.startElement(Constants.WI_NS, VALUE_EL, Constants.WI_PREFIX_COLON + VALUE_EL, Constants.EMPTY_ATTRS); 121 String value = fieldDefinition.getDatatype().getPlainConvertor().convertToString(values[i], locale, null); 122 contentHandler.characters(value.toCharArray(), 0, value.length()); 123 contentHandler.endElement(Constants.WI_NS, VALUE_EL, Constants.WI_PREFIX_COLON + VALUE_EL); 124 } 125 } else if (enteredValues != null) { 126 for (int i = 0; i < enteredValues.length; i++) { 127 contentHandler.startElement(Constants.WI_NS, VALUE_EL, Constants.WI_PREFIX_COLON + VALUE_EL, Constants.EMPTY_ATTRS); 128 String value = fieldDefinition.getDatatype().getPlainConvertor().convertToString(enteredValues[i], locale, null); 129 contentHandler.characters(value.toCharArray(), 0, value.length()); 130 contentHandler.endElement(Constants.WI_NS, VALUE_EL, Constants.WI_PREFIX_COLON + VALUE_EL); 131 } 132 } 133 contentHandler.endElement(Constants.WI_NS, VALUES_EL, Constants.WI_PREFIX_COLON + VALUES_EL); 134 135 definition.generateDisplayData(contentHandler); 137 138 if (this.selectionList != null) { 140 this.selectionList.generateSaxFragment(contentHandler, locale); 141 } else { 142 fieldDefinition.getSelectionList().generateSaxFragment(contentHandler, locale); 143 } 144 145 if (validationError != null) { 147 contentHandler.startElement(Constants.WI_NS, VALIDATION_MSG_EL, Constants.WI_PREFIX_COLON + VALIDATION_MSG_EL, Constants.EMPTY_ATTRS); 148 validationError.generateSaxFragment(contentHandler); 149 contentHandler.endElement(Constants.WI_NS, VALIDATION_MSG_EL, Constants.WI_PREFIX_COLON + VALIDATION_MSG_EL); 150 } 151 152 contentHandler.endElement(Constants.WI_NS, MULTIVALUEFIELD_EL, Constants.WI_PREFIX_COLON + MULTIVALUEFIELD_EL); 153 } 154 155 public void generateLabel(ContentHandler contentHandler) throws SAXException { 156 definition.generateLabel(contentHandler); 157 } 158 159 public Object getValue() { 160 return values; 161 } 162 163 public void setValue(Object value) { 164 if (value == null) { 165 setValues(new Object [0]); 166 } else if (value.getClass().isArray()) { 167 setValues((Object [])value); 168 } else { 169 throw new RuntimeException ("Cannot set value of field \"" + getFullyQualifiedId() + "\" with an object of type " + value.getClass().getName()); 170 } 171 } 172 173 public void setValues(Object [] values) { 174 for (int i = 0; i < values.length; i++) { 176 if (!fieldDefinition.getDatatype().getTypeClass().isAssignableFrom(values[i].getClass())) 177 throw new RuntimeException ("Cannot set value of field \"" + getFullyQualifiedId() + "\" with an object of type " + values[i].getClass().getName()); 178 } 179 this.values = values; 180 } 181 182 186 public void setSelectionList(SelectionList selectionList) { 187 if (selectionList != null && 188 selectionList.getDatatype() != null && 189 selectionList.getDatatype() != fieldDefinition.getDatatype()) { 190 191 throw new RuntimeException ("Tried to assign a SelectionList that is not associated with this widget's datatype."); 192 } 193 this.selectionList = selectionList; 194 } 195 196 205 public void setSelectionList(String uri) { 206 setSelectionList(this.fieldDefinition.buildSelectionList(uri)); 207 } 208 209 225 public void setSelectionList(Object model, String valuePath, String labelPath) { 226 setSelectionList(this.fieldDefinition.buildSelectionListFromModel(model, valuePath, labelPath)); 227 } 228 229 public void broadcastEvent(WidgetEvent event) { 230 this.fieldDefinition.fireValueChangedEvent((ValueChangedEvent)event); 231 } 232 233 public ValidationError getValidationError() { 234 return this.validationError; 235 } 236 237 public void setValidationError(ValidationError error) { 238 this.validationError = error; 239 } 240 } 241 | Popular Tags |