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.application.Application;
22 import javax.faces.component.UIComponent;
23 import javax.faces.context.FacesContext;
24 import javax.faces.el.ValueBinding;
25 import javax.faces.webapp.UIComponentTag;
26
27 import org.apache.myfaces.custom.tree.HtmlTree;
28 import org.apache.myfaces.custom.tree.model.DefaultTreeModel;
29 import org.apache.myfaces.custom.tree.model.TreeModel;
30 import org.apache.myfaces.custom.tree.model.TreePath;
31
32 import com.sun.facelets.FaceletContext;
33 import com.sun.facelets.tag.TagAttribute;
34 import com.sun.facelets.tag.jsf.ComponentConfig;
35 import com.sun.facelets.tag.jsf.html.HtmlComponentHandler;
36
37
38
39
40
41 public class TreeTagHandler extends HtmlComponentHandler
42 {
43
44 private TagAttribute valueAttr;
45 private TagAttribute expandRootAttr;
46
47 private boolean expandRoot;
48
49 public TreeTagHandler(ComponentConfig config)
50 {
51 super(config);
52 valueAttr = getRequiredAttribute("value");
53 expandRootAttr = getRequiredAttribute("expandRoot");
54 expandRoot = false;
55 }
56
57 protected void setAttributes(FaceletContext ctx, Object instance)
58 {
59 super.setAttributes(ctx, instance);
60
61 Application app = ctx.getFacesContext().getApplication();
62
63 HtmlTree tree = (HtmlTree) instance;
64
65 if(valueAttr != null)
66 {
67 String value = valueAttr.getValue();
68 if (value != null && UIComponentTag.isValueReference(value))
69 {
70 tree.setValueBinding("model", app.createValueBinding(valueAttr
71 .getValue()));
72 }
73 }
74 else
75 {
76 ValueBinding binding = tree.getValueBinding("model");
77 if (binding == null) {
78 binding = app.createValueBinding("#{sessionScope.tree}");
79 }
80 tree.setValueBinding("model", binding);
81 }
82
83 if (expandRootAttr != null)
84 {
85 expandRoot = expandRootAttr.getBoolean(ctx);
86 }
87 }
88
89 protected void onComponentCreated(FaceletContext ctx,
90 UIComponent component, UIComponent parent)
91 {
92 FacesContext context = ctx.getFacesContext();
93 Application app = ctx.getFacesContext().getApplication();
94
95 if (valueAttr != null) {
96 String value = valueAttr.getValue();
97 if (value != null){
98 ValueBinding valueBinding = app.createValueBinding(value);
99 TreeModel treeModel = (TreeModel) (valueBinding.getValue(context));
100
101 if (treeModel == null) {
102
103 treeModel = new DefaultTreeModel();
104 valueBinding.setValue(context, treeModel);
105 }
106 }
107 }
108
109 HtmlTree tree = (HtmlTree) component;
110
111 if (expandRoot)
112 {
113
114 TreeModel model = tree.getModel(context);
115
116 if (model != null) {
117 tree.expandPath(new TreePath(new Object[] { model.getRoot() }),
118 context);
119 }
120 }
121 tree.addToModelListeners();
122 }
123
124 }