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.EditableValueHolder; |
19 | |
import javax.faces.component.UIComponent; |
20 | |
import javax.faces.context.FacesContext; |
21 | |
import javax.faces.el.ValueBinding; |
22 | |
import javax.faces.event.ValueChangeListener; |
23 | |
import javax.faces.internal.ValueBindingUtil; |
24 | |
import javax.faces.webapp.UIComponentTag; |
25 | |
import javax.servlet.jsp.JspException; |
26 | |
import javax.servlet.jsp.tagext.TagSupport; |
27 | |
|
28 | |
import org.seasar.framework.util.ClassUtil; |
29 | |
import org.seasar.teeda.core.exception.NoEditableValueHolderRuntimeException; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | 4 | public class ValueChangeListenerTag extends TagSupport { |
35 | |
|
36 | |
private static final long serialVersionUID = 1L; |
37 | |
|
38 | 4 | private String type = null; |
39 | |
|
40 | |
public void setType(String type) { |
41 | 3 | this.type = type; |
42 | 3 | } |
43 | |
|
44 | |
public String getType() { |
45 | 4 | return type; |
46 | |
} |
47 | |
|
48 | |
public int doStartTag() throws JspException { |
49 | 3 | final String type = getType(); |
50 | 3 | if (type == null) { |
51 | 1 | throw new JspException("type attribute not set"); |
52 | |
} |
53 | 2 | UIComponentTag tag = UIComponentTag |
54 | |
.getParentUIComponentTag(pageContext); |
55 | 2 | if (tag == null) { |
56 | 1 | throw new JspException("Not nested in a UIComponentTag"); |
57 | |
} |
58 | 1 | if (!tag.getCreated()) { |
59 | 1 | return SKIP_BODY; |
60 | |
} |
61 | |
|
62 | 0 | UIComponent component = tag.getComponentInstance(); |
63 | 0 | if (component == null) { |
64 | 0 | throw new JspException( |
65 | |
"No component associated with UIComponentTag"); |
66 | |
} |
67 | 0 | if (component instanceof EditableValueHolder) { |
68 | 0 | String className = null; |
69 | 0 | if (UIComponentTag.isValueReference(type)) { |
70 | 0 | FacesContext context = FacesContext.getCurrentInstance(); |
71 | 0 | ValueBinding vb = ValueBindingUtil.createValueBinding(context, |
72 | |
type); |
73 | 0 | className = (String) vb.getValue(context); |
74 | |
} else { |
75 | 0 | className = type; |
76 | |
} |
77 | 0 | ValueChangeListener listener = (ValueChangeListener) ClassUtil |
78 | |
.newInstance(className); |
79 | 0 | ((EditableValueHolder) component).addValueChangeListener(listener); |
80 | |
} else { |
81 | 0 | throw new NoEditableValueHolderRuntimeException(component |
82 | |
.getClass()); |
83 | |
} |
84 | 0 | return SKIP_BODY; |
85 | |
} |
86 | |
|
87 | |
public void release() { |
88 | 1 | super.release(); |
89 | 1 | type = null; |
90 | 1 | } |
91 | |
|
92 | |
} |