KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > formmodel > tree > builder > TreeDefinitionBuilder


1 /*
2  * Copyright 1999-2005 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.tree.builder;
17
18 import java.util.Iterator JavaDoc;
19
20 import org.apache.avalon.framework.service.ServiceSelector;
21 import org.apache.cocoon.forms.FormsConstants;
22 import org.apache.cocoon.forms.event.ValueChangedListener;
23 import org.apache.cocoon.forms.formmodel.AbstractWidgetDefinitionBuilder;
24 import org.apache.cocoon.forms.formmodel.WidgetDefinition;
25 import org.apache.cocoon.forms.formmodel.tree.Tree;
26 import org.apache.cocoon.forms.formmodel.tree.TreeDefinition;
27 import org.apache.cocoon.forms.formmodel.tree.TreeSelectionListener;
28 import org.apache.cocoon.forms.util.DomHelper;
29 import org.w3c.dom.Element JavaDoc;
30
31 /**
32  * Builds a {@link org.apache.cocoon.forms.formmodel.tree.Tree}.
33  *
34  * @version $Id: TreeDefinitionBuilder.java 326838 2005-10-20 06:26:53Z sylvain $
35  */

36 public class TreeDefinitionBuilder extends AbstractWidgetDefinitionBuilder {
37
38     public WidgetDefinition buildWidgetDefinition(Element JavaDoc widgetElement) throws Exception JavaDoc {
39         TreeDefinition definition = new TreeDefinition();
40         setupDefinition(widgetElement, definition);
41         definition.makeImmutable();
42         return definition;
43     }
44     
45     protected void setupDefinition(Element JavaDoc widgetElement, TreeDefinition definition) throws Exception JavaDoc {
46         super.setupDefinition(widgetElement, definition);
47         
48         // Get the optional "root-visible" attribute
49
definition.setRootVisible(DomHelper.getAttributeAsBoolean(widgetElement, "root-visible", true));
50         
51         // Get the optional "selection" attribute
52
String JavaDoc selection = DomHelper.getAttribute(widgetElement, "selection", null);
53         if (selection == null) {
54             // Nothing
55
} else if ("multiple".equals(selection)) {
56             definition.setSelectionModel(Tree.MULTIPLE_SELECTION);
57         } else if ("single".equals(selection)) {
58             definition.setSelectionModel(Tree.SINGLE_SELECTION);
59         } else {
60             throw new Exception JavaDoc("Invalid value selection value '" + selection + "' at " +
61                     DomHelper.getLocation(widgetElement));
62         }
63         
64         // Get the model optional element
65
Element JavaDoc modelElt = DomHelper.getChildElement(widgetElement, FormsConstants.DEFINITION_NS, "tree-model", false);
66         if (modelElt != null) {
67             String JavaDoc type = DomHelper.getAttribute(modelElt, "type");
68             ServiceSelector selector =
69                 (ServiceSelector)this.serviceManager.lookup(TreeModelDefinitionBuilder.ROLE + "Selector");
70             
71             TreeModelDefinitionBuilder builder = (TreeModelDefinitionBuilder)selector.select(type);
72             try {
73                 definition.setModelDefinition(builder.build(modelElt));
74             } finally {
75                 selector.release(builder);
76                 serviceManager.release(selector);
77             }
78         }
79         
80         // parse "on-selection-changed"
81
Iterator JavaDoc iter = buildEventListeners(widgetElement, "on-selection-changed", TreeSelectionListener.class).iterator();
82         while (iter.hasNext()) {
83             definition.addSelectionListener((TreeSelectionListener)iter.next());
84         }
85         //TODO: allow child widgets, that will be attached to each node of the tree
86
//It may be useful to add TreeModel.getNodeType(Object) so that the container holding child
87
//widgets can have a value used by a union widget.
88
}
89 }
90
Popular Tags