01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.woody.formmodel;
18:
19: import java.util.Iterator;
20:
21: import org.apache.cocoon.woody.Constants;
22: import org.apache.cocoon.woody.datatype.Datatype;
23: import org.apache.cocoon.woody.event.ValueChangedListener;
24: import org.apache.cocoon.woody.util.DomHelper;
25: import org.w3c.dom.Element;
26:
27: /**
28: * Builds {FieldDefinition}s.
29: *
30: * @version $Id: FieldDefinitionBuilder.java 433543 2006-08-22 06:22:54Z crossley $
31: */
32: public class FieldDefinitionBuilder extends
33: AbstractDatatypeWidgetDefinitionBuilder {
34:
35: public WidgetDefinition buildWidgetDefinition(Element widgetElement)
36: throws Exception {
37: FieldDefinition fieldDefinition = new FieldDefinition();
38: buildWidgetDefinition(fieldDefinition, widgetElement);
39: return fieldDefinition;
40: }
41:
42: protected void buildWidgetDefinition(
43: FieldDefinition fieldDefinition, Element widgetElement)
44: throws Exception {
45: setLocation(widgetElement, fieldDefinition);
46: setId(widgetElement, fieldDefinition);
47:
48: Element datatypeElement = DomHelper.getChildElement(
49: widgetElement, Constants.WD_NS, "datatype");
50: if (datatypeElement == null) {
51: throw new Exception(
52: "A nested datatype element is required for the widget at "
53: + DomHelper.getLocation(widgetElement));
54: }
55:
56: Datatype datatype = datatypeManager.createDatatype(
57: datatypeElement, false);
58: fieldDefinition.setDatatype(datatype);
59:
60: buildSelectionList(widgetElement, fieldDefinition);
61:
62: Iterator iter = buildEventListeners(widgetElement,
63: "on-value-changed", ValueChangedListener.class)
64: .iterator();
65: while (iter.hasNext()) {
66: fieldDefinition
67: .addValueChangedListener((ValueChangedListener) iter
68: .next());
69: }
70:
71: setDisplayData(widgetElement, fieldDefinition);
72: setValidators(widgetElement, fieldDefinition);
73:
74: boolean required = DomHelper.getAttributeAsBoolean(
75: widgetElement, "required", false);
76: fieldDefinition.setRequired(required);
77: }
78: }
|