1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.custom.tree.taglib;
20
21 import javax.faces.component.UIComponent;
22 import javax.faces.context.FacesContext;
23 import javax.faces.el.ValueBinding;
24 import javax.servlet.jsp.JspException;
25
26 import org.apache.myfaces.custom.tree.HtmlTree;
27 import org.apache.myfaces.custom.tree.model.DefaultTreeModel;
28 import org.apache.myfaces.custom.tree.model.TreeModel;
29 import org.apache.myfaces.custom.tree.model.TreePath;
30 import org.apache.myfaces.shared_tomahawk.taglib.html.HtmlPanelGroupTag;
31
32
33
34
35
36
37
38
39
40 public class AbstractTreeTag extends HtmlPanelGroupTag {
41
42 private String value;
43
44 private String expandRoot;
45
46 public String getComponentType() {
47 return "org.apache.myfaces.HtmlTree";
48 }
49
50 public String getRendererType() {
51 return "org.apache.myfaces.HtmlTree";
52 }
53
54 public String getValue() {
55 return value;
56 }
57
58 public void setValue(String newValue) {
59 value = newValue;
60 }
61
62 public String isExpandRoot() {
63 return expandRoot;
64 }
65
66 public void setExpandRoot(String expandRoot) {
67 this.expandRoot = expandRoot;
68 }
69
70
71
72
73 public int doStartTag() throws JspException {
74 FacesContext context = FacesContext.getCurrentInstance();
75
76 if (value != null) {
77 ValueBinding valueBinding = context.getApplication()
78 .createValueBinding(value);
79 TreeModel treeModel = (TreeModel) (valueBinding.getValue(context));
80
81 if (treeModel == null) {
82
83 treeModel = new DefaultTreeModel();
84 valueBinding.setValue(context, treeModel);
85 }
86 }
87 int answer = super.doStartTag();
88 HtmlTree tree = (HtmlTree) getComponentInstance();
89
90 if (getCreated() && parseBoolean(expandRoot)) {
91
92 TreeModel model = tree.getModel(context);
93
94 if (model != null) {
95 tree.expandPath(new TreePath(new Object[] { model.getRoot() }),
96 context);
97 }
98 }
99
100 tree.addToModelListeners();
101 return answer;
102 }
103
104 private boolean parseBoolean(String s)
105 {
106 return ((s != null) && s.equalsIgnoreCase("true"));
107 }
108
109 public void release() {
110 super.release();
111 value = null;
112 expandRoot = null;
113 }
114
115
116
117
118 protected void setProperties(UIComponent component) {
119 super.setProperties(component);
120 FacesContext context = FacesContext.getCurrentInstance();
121
122 if (value != null) {
123 if (isValueReference(value)) {
124 ValueBinding binding = context.getApplication()
125 .createValueBinding(value);
126 component.setValueBinding("model", binding);
127 }
128 } else {
129 ValueBinding binding = component.getValueBinding("model");
130 if (binding == null) {
131 binding = context.getApplication().createValueBinding(
132 "#{sessionScope.tree}");
133 }
134 component.setValueBinding("model", binding);
135 }
136 }
137 }