1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts2.components;
22
23 import java.io.Writer;
24 import java.util.ArrayList;
25 import java.util.List;
26
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.apache.struts2.views.annotations.StrutsTag;
33 import org.apache.struts2.views.annotations.StrutsTagAttribute;
34
35 import com.opensymphony.xwork2.inject.Container;
36 import com.opensymphony.xwork2.inject.Inject;
37 import com.opensymphony.xwork2.util.ValueStack;
38
39 /***
40 * <!-- START SNIPPET: javadoc -->
41 *
42 * Create a optgroup component which needs to resides within a select tag.
43 *
44 * <!-- END SNIPPET: javadoc -->
45 *
46 * <p/>
47 *
48 * <!-- START SNIPPET: notice -->
49 *
50 * This component is to be used within a Select component.
51 *
52 * <!-- END SNIPPET: notice -->
53 *
54 * <p/>
55 *
56 * <pre>
57 * <!-- START SNIPPET: example -->
58 *
59 * <s:select label="My Selection"
60 * name="mySelection"
61 * value="%{'POPEYE'}"
62 * list="%{#{'SUPERMAN':'Superman', 'SPIDERMAN':'spiderman'}}">
63 * <s:optgroup label="Adult"
64 * list="%{#{'SOUTH_PARK':'South Park'}}" />
65 * <s:optgroup label="Japanese"
66 * list="%{#{'POKEMON':'pokemon','DIGIMON':'digimon','SAILORMOON':'Sailormoon'}}" />
67 * </s:select>
68 *
69 * <!-- END SNIPPET: example -->
70 * </pre>
71 *
72 */
73 @StrutsTag(name="optgroup", tldTagClass="org.apache.struts2.views.jsp.ui.OptGroupTag", description="Renders a Select Tag's OptGroup Tag")
74 public class OptGroup extends Component {
75
76 public static final String INTERNAL_LIST_UI_BEAN_LIST_PARAMETER_KEY = "optGroupInternalListUiBeanList";
77
78 private static Log _log = LogFactory.getLog(OptGroup.class);
79
80 protected HttpServletRequest req;
81 protected HttpServletResponse res;
82
83 protected ListUIBean internalUiBean;
84
85 public OptGroup(ValueStack stack, HttpServletRequest req, HttpServletResponse res) {
86 super(stack);
87 this.req = req;
88 this.res = res;
89 internalUiBean = new ListUIBean(stack, req, res) {
90 protected String getDefaultTemplate() {
91 return "empty";
92 }
93 };
94 }
95
96 @Inject
97 public void setContainer(Container container) {
98 container.inject(internalUiBean);
99 }
100
101 public boolean end(Writer writer, String body) {
102 Select select = (Select) findAncestor(Select.class);
103 if (select == null) {
104 _log.error("incorrect use of OptGroup component, this component must be used within a Select component",
105 new IllegalStateException("incorrect use of OptGroup component, this component must be used within a Select component"));
106 return false;
107 }
108 internalUiBean.start(writer);
109 internalUiBean.end(writer, body);
110
111 List listUiBeans = (List) select.getParameters().get(INTERNAL_LIST_UI_BEAN_LIST_PARAMETER_KEY);
112 if (listUiBeans == null) {
113 listUiBeans = new ArrayList();
114 }
115 listUiBeans.add(internalUiBean);
116 select.addParameter(INTERNAL_LIST_UI_BEAN_LIST_PARAMETER_KEY, listUiBeans);
117
118 return false;
119 }
120
121 @StrutsTagAttribute(description="Set the label attribute")
122 public void setLabel(String label) {
123 internalUiBean.setLabel(label);
124 }
125
126 @StrutsTagAttribute(description="Set the disable attribute.")
127 public void setDisabled(String disabled) {
128 internalUiBean.setDisabled(disabled);
129 }
130
131 @StrutsTagAttribute(description="Set the list attribute.")
132 public void setList(String list) {
133 internalUiBean.setList(list);
134 }
135
136 @StrutsTagAttribute(description="Set the listKey attribute.")
137 public void setListKey(String listKey) {
138 internalUiBean.setListKey(listKey);
139 }
140
141 @StrutsTagAttribute(description="Set the listValue attribute.")
142 public void setListValue(String listValue) {
143 internalUiBean.setListValue(listValue);
144 }
145 }