KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > formmodel > AbstractDatatypeWidgetDefinitionBuilder


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.forms.formmodel;
17
18 import java.util.Iterator JavaDoc;
19 import java.util.Locale JavaDoc;
20
21 import org.apache.avalon.framework.service.ServiceSelector;
22 import org.apache.cocoon.forms.FormsConstants;
23 import org.apache.cocoon.forms.datatype.Datatype;
24 import org.apache.cocoon.forms.datatype.SelectionList;
25 import org.apache.cocoon.forms.datatype.SelectionListBuilder;
26 import org.apache.cocoon.forms.datatype.convertor.ConversionResult;
27 import org.apache.cocoon.forms.event.ValueChangedListener;
28 import org.apache.cocoon.forms.util.DomHelper;
29 import org.apache.cocoon.i18n.I18nUtils;
30 import org.w3c.dom.Element JavaDoc;
31
32 /**
33  * Abstract base class for WidgetDefinitionBuilders that build widgets that have datatypes/selection lists.
34  *
35  * @version $Id: AbstractDatatypeWidgetDefinitionBuilder.java 326838 2005-10-20 06:26:53Z sylvain $
36  */

37 public abstract class AbstractDatatypeWidgetDefinitionBuilder extends AbstractWidgetDefinitionBuilder {
38     
39     protected void setupDefinition(Element JavaDoc widgetElement, AbstractDatatypeWidgetDefinition definition) throws Exception JavaDoc {
40         super.setupDefinition(widgetElement, definition);
41         // parse "label", "hint", etc.
42
setDisplayData(widgetElement, definition);
43
44         // parse "on-value-changed"
45
Iterator JavaDoc iter = buildEventListeners(widgetElement, "on-value-changed", ValueChangedListener.class).iterator();
46         while (iter.hasNext()) {
47             definition.addValueChangedListener((ValueChangedListener)iter.next());
48         }
49         
50         //---- parse "datatype"
51
Element JavaDoc datatypeElement = DomHelper.getChildElement(widgetElement, FormsConstants.DEFINITION_NS, "datatype");
52         if (datatypeElement != null) {
53             Datatype datatype = datatypeManager.createDatatype(datatypeElement, false);
54             
55             // ---- parse "initial-value"
56
Object JavaDoc initialValue = null;
57             Element JavaDoc initialValueElement = DomHelper.getChildElement(widgetElement, FormsConstants.DEFINITION_NS, "initial-value", false);
58             if (initialValueElement != null) {
59                 String JavaDoc localeValue = DomHelper.getAttribute(initialValueElement, "locale", null);
60                 Locale JavaDoc locale = localeValue == null ? null : I18nUtils.parseLocale(localeValue);
61                 String JavaDoc value = DomHelper.getElementText(initialValueElement);
62                 ConversionResult result = datatype.convertFromString(value, locale);
63                 if (!result.isSuccessful()) {
64                     throw new Exception JavaDoc("Cannot parse initial value '" + value + "' at " +
65                             DomHelper.getLocation(initialValueElement));
66                 }
67                 initialValue = result.getResult();
68             }
69             
70             definition.setDatatype(datatype, initialValue);
71         }
72         
73         
74         //---- parse "selection-list"
75
// FIXME: pass the manager to the definition as a side effect. Should be removed
76
// when definition are managed like components.
77
definition.service(this.serviceManager);
78
79         SelectionList list = buildSelectionList(widgetElement, definition, "selection-list");
80         if (list != null) {
81             definition.setSelectionList(list);
82         }
83     }
84     
85     protected SelectionList buildSelectionList(
86             Element JavaDoc widgetElement, AbstractDatatypeWidgetDefinition definition, String JavaDoc name) throws Exception JavaDoc {
87         Element JavaDoc selectionListElement = DomHelper.getChildElement(widgetElement, FormsConstants.DEFINITION_NS, name);
88         
89         if(selectionListElement != null && definition.getDatatype() == null)
90             throw new Exception JavaDoc("A widget with a selection list always requires a datatype as well! (at "+DomHelper.getLocation(selectionListElement)+" )");
91         
92         if (selectionListElement == null)
93             return null;
94
95         // Get an appropriate list builder
96
ServiceSelector builderSelector = (ServiceSelector)this.serviceManager.lookup(SelectionListBuilder.ROLE + "Selector");
97         SelectionListBuilder builder = null;
98         try {
99             // listType can be null, meaning we will use the default selection list
100
String JavaDoc listType = selectionListElement.getAttribute("type");
101             if ("".equals(listType)) {
102                 listType = null;
103             }
104
105             builder = (SelectionListBuilder)builderSelector.select(listType);
106             return builder.build(selectionListElement, definition.getDatatype());
107
108         } finally {
109             if (builder != null) {
110                 builderSelector.release(builder);
111             }
112             this.serviceManager.release(builderSelector);
113         }
114     }
115 }
116
Popular Tags