KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > tree > TreeHandler


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.wcf.tree;
14
15 import javax.servlet.http.HttpSession JavaDoc;
16
17 import org.apache.commons.beanutils.PropertyUtils;
18 import org.w3c.dom.Element JavaDoc;
19
20 import com.tonbeller.wcf.controller.RequestContext;
21 import com.tonbeller.wcf.form.FormComponent;
22 import com.tonbeller.wcf.form.NodeHandlerSupport;
23 import com.tonbeller.wcf.form.XmlComponent;
24 import com.tonbeller.wcf.selection.DefaultSelectionModel;
25 import com.tonbeller.wcf.selection.SelectionChangeListener;
26 import com.tonbeller.wcf.selection.SelectionModel;
27 import com.tonbeller.wcf.ui.XoplonCtrl;
28 import com.tonbeller.wcf.utils.DomUtils;
29
30 /**
31  * Manages a TreeComponent inside a FormComponent
32  * @author av
33  */

34 public class TreeHandler extends NodeHandlerSupport {
35
36   TreeComponent tree;
37   FormComponent formComp;
38
39   public void initialize(RequestContext context, XmlComponent xmlComp, Element JavaDoc element) throws Exception JavaDoc {
40     super.initialize(context, xmlComp, element);
41
42     // must access the bean, so it has to be a FormComponent
43
formComp = (FormComponent) xmlComp;
44
45     String JavaDoc ref = XoplonCtrl.getModelReference(element);
46     Object JavaDoc bean = formComp.getBean();
47     TreeModel tm = (TreeModel) PropertyUtils.getProperty(bean, ref);
48     tree = new TreeComponent(element.getAttribute("id"), formComp, tm);
49
50     DefaultSelectionModel dsm = new DefaultSelectionModel();
51     String JavaDoc selmode = element.getAttribute("selmode");
52     if ("href".equals(selmode))
53       dsm.setMode(SelectionModel.SINGLE_SELECTION_HREF);
54     else if ("single".equals(selmode))
55       dsm.setMode(SelectionModel.SINGLE_SELECTION);
56     else if ("multi".equals(selmode))
57       dsm.setMode(SelectionModel.MULTIPLE_SELECTION);
58     else if ("button".equals(selmode))
59       dsm.setMode(SelectionModel.MULTIPLE_SELECTION_BUTTON);
60     else
61       dsm.setMode(SelectionModel.NO_SELECTION);
62     tree.setSelectionModel(dsm);
63     if (tm instanceof SelectionChangeListener)
64       dsm.addSelectionListener((SelectionChangeListener) tm);
65
66     // override border by setting an attribute
67
String JavaDoc border = element.getAttribute("border");
68     if (border.length() > 0)
69       tree.setBorder(border);
70
71     // override width by setting an attribute
72
String JavaDoc width = element.getAttribute("width");
73     if (width.length() > 0)
74       tree.setWidth(width);
75
76     // override renderId Attribute always
77
tree.setRenderId(element.getAttribute("id"));
78
79     // click in tree should validate the user input in form fields
80
tree.addFormListener(formComp);
81     formComp.addFormListener(tree);
82
83     tree.initialize(context);
84   }
85
86   public void destroy(HttpSession JavaDoc session) throws Exception JavaDoc {
87     tree.destroy(session);
88     super.destroy(session);
89   }
90
91   public void render(RequestContext context) throws Exception JavaDoc {
92     super.render(context);
93     Element JavaDoc parent = getElement();
94     DomUtils.removeChildElements(parent);
95     Element JavaDoc child = tree.render(context, parent.getOwnerDocument());
96     parent.appendChild(child);
97   }
98
99   /**
100    * @return
101    */

102   public FormComponent getFormComp() {
103     return formComp;
104   }
105
106   /**
107    * @return
108    */

109   public TreeComponent getTree() {
110     return tree;
111   }
112 }
113
Popular Tags