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