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.faces.webapp.UIComponentTag;
25 import javax.servlet.jsp.JspException;
26 import javax.servlet.jsp.tagext.Tag;
27 import javax.servlet.jsp.tagext.TagSupport;
28
29 import org.apache.myfaces.custom.tree.HtmlTree;
30 import org.apache.myfaces.custom.tree.event.TreeSelectionListener;
31 import org.apache.myfaces.shared_tomahawk.util.ClassUtils;
32
33
34
35
36
37
38
39
40
41
42
43
44
45 public class TreeSelectionListenerTag extends TagSupport
46 {
47 private static final long serialVersionUID = 7322612767746076403L;
48 private String type = null;
49
50
51 public TreeSelectionListenerTag()
52 {
53 }
54
55
56
57
58
59
60 public void setType(String type)
61 {
62 this.type = type;
63 }
64
65
66 public int doStartTag() throws JspException
67 {
68 if (type == null)
69 {
70 throw new JspException("type attribute not set");
71 }
72
73
74 UIComponentTag componentTag = UIComponentTag.getParentUIComponentTag(pageContext);
75 if (componentTag == null)
76 {
77 throw new JspException("TreeSelectionListenerTag has no UIComponentTag ancestor");
78 }
79
80 if (componentTag.getCreated())
81 {
82
83 UIComponent component = componentTag.getComponentInstance();
84 if (component instanceof HtmlTree)
85 {
86 String className;
87 if (UIComponentTag.isValueReference(type))
88 {
89 FacesContext facesContext = FacesContext.getCurrentInstance();
90 ValueBinding valueBinding = facesContext.getApplication().createValueBinding(type);
91 className = (String) valueBinding.getValue(facesContext);
92 } else
93 {
94 className = type;
95 }
96 TreeSelectionListener listener = (TreeSelectionListener) ClassUtils.newInstance(className);
97 ((HtmlTree) component).addTreeSelectionListener(listener);
98 } else
99 {
100 throw new JspException("Component " + component.getId() + " is no HtmlTree");
101 }
102 }
103
104 return Tag.SKIP_BODY;
105 }
106 }