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.ValueExpression;
27 import javax.faces.component.UIComponent;
28 import javax.faces.view.facelets.FaceletContext;
29 import javax.faces.view.facelets.TagAttribute;
30 import javax.faces.view.facelets.TagAttributeException;
31 import javax.faces.view.facelets.TagConfig;
32 import javax.faces.view.facelets.TagException;
33 import javax.faces.view.facelets.TagHandler;
34 import java.io.IOException;
35
36 public class TabChangeListenerHandler extends TagHandler {
37
38 private Class listenerType;
39
40 private final TagAttribute type;
41
42 private final TagAttribute binding;
43
44
45 public TabChangeListenerHandler(final TagConfig config) {
46 super(config);
47 binding = getAttribute("binding");
48 type = getAttribute("type");
49 if (type != null) {
50 if (!type.isLiteral()) {
51 throw new TagAttributeException(tag, type, "Must be literal");
52 }
53 try {
54 this.listenerType = Class.forName(type.getValue());
55 } catch (final Exception e) {
56 throw new TagAttributeException(tag, type, e);
57 }
58 }
59 }
60
61 @Override
62 public void apply(final FaceletContext faceletContext, final UIComponent parent) throws IOException {
63 if (parent instanceof TabChangeSource) {
64
65 if (parent.getParent() == null) {
66 final TabChangeSource changeSource = (TabChangeSource) parent;
67 TabChangeListener listener = null;
68 ValueExpression valueExpression = null;
69 if (binding != null) {
70 valueExpression = binding.getValueExpression(faceletContext, TabChangeListener.class);
71 listener = (TabChangeListener) valueExpression.getValue(faceletContext);
72 }
73 if (listener == null) {
74 try {
75 listener = (TabChangeListener) listenerType.newInstance();
76 } catch (final Exception e) {
77 throw new TagAttributeException(tag, type, e);
78 }
79 if (valueExpression != null) {
80 valueExpression.setValue(faceletContext, listener);
81 }
82 }
83 if (valueExpression != null) {
84 changeSource.addTabChangeListener(
85 new ValueExpressionTabChangeListener(type.getValue(), valueExpression));
86 } else {
87 changeSource.addTabChangeListener(listener);
88 }
89 }
90 } else {
91 throw new TagException(tag, "Parent is not of type TabChangeSource, type is: " + parent);
92 }
93 }
94 }