1 16 package org.apache.cocoon.forms.formmodel.tree.builder; 17 18 import java.util.Iterator ; 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 ; 30 31 36 public class TreeDefinitionBuilder extends AbstractWidgetDefinitionBuilder { 37 38 public WidgetDefinition buildWidgetDefinition(Element widgetElement) throws Exception { 39 TreeDefinition definition = new TreeDefinition(); 40 setupDefinition(widgetElement, definition); 41 definition.makeImmutable(); 42 return definition; 43 } 44 45 protected void setupDefinition(Element widgetElement, TreeDefinition definition) throws Exception { 46 super.setupDefinition(widgetElement, definition); 47 48 definition.setRootVisible(DomHelper.getAttributeAsBoolean(widgetElement, "root-visible", true)); 50 51 String selection = DomHelper.getAttribute(widgetElement, "selection", null); 53 if (selection == null) { 54 } 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 ("Invalid value selection value '" + selection + "' at " + 61 DomHelper.getLocation(widgetElement)); 62 } 63 64 Element modelElt = DomHelper.getChildElement(widgetElement, FormsConstants.DEFINITION_NS, "tree-model", false); 66 if (modelElt != null) { 67 String 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 Iterator iter = buildEventListeners(widgetElement, "on-selection-changed", TreeSelectionListener.class).iterator(); 82 while (iter.hasNext()) { 83 definition.addSelectionListener((TreeSelectionListener)iter.next()); 84 } 85 } 89 } 90 | Popular Tags |