1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.custom.tabbedpane;
20
21 import java.io.IOException;
22
23 import javax.el.ELException;
24 import javax.faces.FacesException;
25 import javax.faces.component.UIComponent;
26 import javax.faces.view.facelets.FaceletContext;
27 import javax.faces.view.facelets.FaceletException;
28 import javax.faces.view.facelets.TagAttribute;
29 import javax.faces.view.facelets.TagConfig;
30 import javax.faces.view.facelets.TagHandler;
31
32 import org.apache.myfaces.shared_tomahawk.util.ClassUtils;
33
34
35
36
37
38
39 public class TabChangeListenerTagHandler extends TagHandler
40 {
41 private final TagAttribute typeAttr;
42
43 public TabChangeListenerTagHandler(TagConfig config)
44 {
45 super(config);
46 typeAttr = getRequiredAttribute("type");
47 }
48
49 public void apply(FaceletContext faceletContext, UIComponent parent)
50 throws IOException, FacesException, FaceletException, ELException
51 {
52
53 if (parent.getParent() == null)
54 {
55 if (parent instanceof HtmlPanelTabbedPane)
56 {
57 Object listenerRef;
58 if (typeAttr.isLiteral())
59 {
60 listenerRef = typeAttr.getValue();
61 }
62 else
63 {
64 listenerRef = typeAttr.getObject(faceletContext);
65 }
66
67 if (listenerRef instanceof String)
68 {
69 String className = (String) listenerRef;
70 TabChangeListener listener = (TabChangeListener) ClassUtils.newInstance(className);
71 ((HtmlPanelTabbedPane) parent).addTabChangeListener(listener);
72 }
73 else if (listenerRef instanceof TabChangeListener)
74 {
75 TabChangeListener listener = (TabChangeListener) listenerRef;
76 ((HtmlPanelTabbedPane) parent).addTabChangeListener(listener);
77 }
78 else if (listenerRef == null)
79 {
80 throw new FaceletException("Property 'type' must not be null.");
81 }
82 else
83 {
84 throw new FaceletException(
85 "Property 'type' must be either a string (containing a class name) " +
86 "or a TabChangeListener instance.");
87 }
88 }
89 else
90 {
91 throw new FaceletException(
92 "Component " + parent.getId() + " is not of type HtmlPanelTabbedPane");
93 }
94 }
95 }
96 }