1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.myfaces.tobago.facelets;
21
22 import org.apache.myfaces.tobago.event.TabChangeListener;
23 import org.apache.myfaces.tobago.event.TabChangeSource;
24 import org.apache.myfaces.tobago.event.ValueExpressionTabChangeListener;
25
26 import javax.el.ELException;
27 import javax.el.ValueExpression;
28 import javax.faces.FacesException;
29 import javax.faces.component.UIComponent;
30 import javax.faces.view.facelets.FaceletContext;
31 import javax.faces.view.facelets.TagAttribute;
32 import javax.faces.view.facelets.TagAttributeException;
33 import javax.faces.view.facelets.TagConfig;
34 import javax.faces.view.facelets.TagException;
35 import javax.faces.view.facelets.TagHandler;
36 import java.io.IOException;
37
38 public class TabChangeListenerHandler extends TagHandler {
39
40 private Class listenerType;
41
42 private final TagAttribute type;
43
44 private final TagAttribute binding;
45
46
47 public TabChangeListenerHandler(TagConfig config) {
48 super(config);
49 binding = getAttribute("binding");
50 type = getAttribute("type");
51 if (type != null) {
52 if (!type.isLiteral()) {
53 throw new TagAttributeException(tag, type, "Must be literal");
54 }
55 try {
56 this.listenerType = Class.forName(type.getValue());
57 } catch (Exception e) {
58 throw new TagAttributeException(tag, type, e);
59 }
60 }
61 }
62
63 public void apply(FaceletContext faceletContext, UIComponent parent)
64 throws IOException, FacesException, ELException {
65 if (parent instanceof TabChangeSource) {
66
67 if (parent.getParent() == null) {
68 TabChangeSource changeSource = (TabChangeSource) parent;
69 TabChangeListener listener = null;
70 ValueExpression valueExpression = null;
71 if (binding != null) {
72 valueExpression = binding.getValueExpression(faceletContext, TabChangeListener.class);
73 listener = (TabChangeListener) valueExpression.getValue(faceletContext);
74 }
75 if (listener == null) {
76 try {
77 listener = (TabChangeListener) listenerType.newInstance();
78 } catch (Exception e) {
79 throw new TagAttributeException(tag, type, e.getCause());
80 }
81 if (valueExpression != null) {
82 valueExpression.setValue(faceletContext, listener);
83 }
84 }
85 if (valueExpression != null) {
86 changeSource.addTabChangeListener(
87 new ValueExpressionTabChangeListener(type.getValue(), valueExpression));
88 } else {
89 changeSource.addTabChangeListener(listener);
90 }
91 }
92 } else {
93 throw new TagException(tag, "Parent is not of type TabChangeSource, type is: " + parent);
94 }
95 }
96 }