View Javadoc

1   /*
2    * $Id: OptGroup.java 651946 2008-04-27 13:41:38Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * &lt;s:select label="My Selection"
61   *            name="mySelection"
62   *            value="%{'POPEYE'}"
63   *            list="%{#{'SUPERMAN':'Superman', 'SPIDERMAN':'spiderman'}}"&gt;
64   *    &lt;s:optgroup label="Adult"
65   *                 list="%{#{'SOUTH_PARK':'South Park'}}" /&gt;
66   *    &lt;s:optgroup label="Japanese"
67   *                 list="%{#{'POKEMON':'pokemon','DIGIMON':'digimon','SAILORMOON':'Sailormoon'}}" /&gt;
68   * &lt;/s:select&gt;
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 }