1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.seasar.teeda.core.taglib.core; |
17 | |
|
18 | |
import javax.faces.component.ActionSource; |
19 | |
import javax.faces.component.UIComponent; |
20 | |
import javax.faces.context.FacesContext; |
21 | |
import javax.faces.el.ValueBinding; |
22 | |
import javax.faces.event.ActionListener; |
23 | |
import javax.faces.internal.ValueBindingUtil; |
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.seasar.framework.util.ClassUtil; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
public class ActionListenerTag extends TagSupport { |
35 | |
|
36 | |
private static final long serialVersionUID = 1L; |
37 | |
|
38 | 0 | private String type = null; |
39 | |
|
40 | 0 | public ActionListenerTag() { |
41 | 0 | } |
42 | |
|
43 | |
public void setType(String type) { |
44 | 0 | this.type = type; |
45 | 0 | } |
46 | |
|
47 | |
public String getType() { |
48 | 0 | return type; |
49 | |
} |
50 | |
|
51 | |
public int doStartTag() throws JspException { |
52 | 0 | UIComponentTag componentTag = UIComponentTag |
53 | |
.getParentUIComponentTag(pageContext); |
54 | 0 | if (componentTag == null) { |
55 | 0 | throw new JspException("Not nested in a UIComponentTag"); |
56 | |
} |
57 | 0 | if (!componentTag.getCreated()) { |
58 | 0 | return Tag.SKIP_BODY; |
59 | |
} |
60 | 0 | UIComponent component = componentTag.getComponentInstance(); |
61 | 0 | if (component instanceof ActionSource) { |
62 | 0 | String className = getActionListenerClassName(); |
63 | 0 | ActionListener actionListener = createActionListener(className); |
64 | 0 | ((ActionSource) component).addActionListener(actionListener); |
65 | |
} else { |
66 | 0 | throw new JspException("Component:" + component |
67 | |
+ " is not instance of ActionSource"); |
68 | |
} |
69 | 0 | return Tag.SKIP_BODY; |
70 | |
} |
71 | |
|
72 | |
public void release() { |
73 | 0 | type = null; |
74 | 0 | } |
75 | |
|
76 | |
protected String getActionListenerClassName() { |
77 | 0 | String className = type; |
78 | 0 | if (UIComponentTag.isValueReference(type)) { |
79 | 0 | FacesContext context = FacesContext.getCurrentInstance(); |
80 | 0 | ValueBinding vb = ValueBindingUtil |
81 | |
.createValueBinding(context, type); |
82 | 0 | className = (String) vb.getValue(context); |
83 | |
} |
84 | 0 | return className; |
85 | |
} |
86 | |
|
87 | |
protected ActionListener createActionListener(String className) { |
88 | 0 | return (ActionListener) ClassUtil.newInstance(className); |
89 | |
} |
90 | |
|
91 | |
} |