View Javadoc

1   /*
2    * $Id: OptGroup.java 451544 2006-09-30 05:38:02Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.components;
19  
20  import java.io.Writer;
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpServletResponse;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  
30  import com.opensymphony.xwork2.util.ValueStack;
31  
32  /***
33   * <!-- START SNIPPET: javadoc -->
34   *
35   * Create a optgroup component which needs to resides within a select tag.
36   *
37   * <!-- END SNIPPET: javadoc -->
38   *
39   * <p/>
40   *
41   * <!-- START SNIPPET: notice -->
42   *
43   * This component is to be used within a  Select component.
44   *
45   * <!-- END SNIPPET: notice -->
46   *
47   * <p/>
48   *
49   * <pre>
50   * <!-- START SNIPPET: example -->
51   *
52   * &lt;s:select label="My Selection"
53   *            name="mySelection"
54   *            value="%{'POPEYE'}"
55   *            list="%{#{'SUPERMAN':'Superman', 'SPIDERMAN':'spiderman'}}"&gt;
56   *    &lt;s:optgroup label="Adult"
57   *                 list="%{#{'SOUTH_PARK':'South Park'}}" /&gt;
58   *    &lt;s:optgroup label="Japanese"
59   *                 list="%{#{'POKEMON':'pokemon','DIGIMON':'digimon','SAILORMOON':'Sailormoon'}}" /&gt;
60   * &lt;/s:select&gt;
61   *
62   * <!-- END SNIPPET: example -->
63   * </pre>
64   *
65   * @s.tag name="optgroup" tld-body-content="JSP" tld-tag-class="org.apache.struts2.views.jsp.ui.OptGroupTag"
66   * description="Renders a Select Tag's OptGroup Tag"
67   */
68  public class OptGroup extends Component {
69  
70  	public static final String INTERNAL_LIST_UI_BEAN_LIST_PARAMETER_KEY = "optGroupInternalListUiBeanList";
71  
72  	private static Log _log = LogFactory.getLog(OptGroup.class);
73  
74  	protected HttpServletRequest req;
75  	protected HttpServletResponse res;
76  
77  	protected ListUIBean internalUiBean;
78  
79  	public OptGroup(ValueStack stack, HttpServletRequest req, HttpServletResponse res) {
80  		super(stack);
81  		this.req = req;
82  		this.res = res;
83  		internalUiBean = new ListUIBean(stack, req, res) {
84  			protected String getDefaultTemplate() {
85  				return "empty";
86  			}
87  		};
88  	}
89  
90  	public boolean end(Writer writer, String body) {
91  		Select select = (Select) findAncestor(Select.class);
92  		if (select == null) {
93  			_log.error("incorrect use of OptGroup component, this component must be used within a Select component",
94  					new IllegalStateException("incorrect use of OptGroup component, this component must be used within a Select component"));
95  			return false;
96  		}
97  		internalUiBean.start(writer);
98  		internalUiBean.end(writer, body);
99  
100 		List listUiBeans = (List) select.getParameters().get(INTERNAL_LIST_UI_BEAN_LIST_PARAMETER_KEY);
101 		if (listUiBeans == null) {
102 			listUiBeans = new ArrayList();
103 		}
104 		listUiBeans.add(internalUiBean);
105 		select.addParameter(INTERNAL_LIST_UI_BEAN_LIST_PARAMETER_KEY, listUiBeans);
106 
107 		return false;
108 	}
109 
110 	/***
111 	 * Set the label attribute.
112 	 * @s.tagattribute required="false"
113 	 */
114 	public void setLabel(String label) {
115 		internalUiBean.setLabel(label);
116 	}
117 
118 	/***
119 	 * Set the disable attribute.
120 	 * @s.tagattribute required="false"
121 	 */
122 	public void setDisabled(String disabled) {
123 		internalUiBean.setDisabled(disabled);
124 	}
125 
126 	/***
127 	 * Set the list attribute.
128 	 * @s.tagattribute required="false"
129 	 */
130 	public void setList(String list) {
131 		internalUiBean.setList(list);
132 	}
133 
134 	/***
135 	 * Set the listKey attribute.
136 	 * @s.tagattribute required="false"
137 	 */
138 	public void setListKey(String listKey) {
139 		internalUiBean.setListKey(listKey);
140 	}
141 
142 	/***
143 	 * Set the listValue attribute.
144 	 * @s.tagattribute required="false"
145 	 */
146 	public void setListValue(String listValue) {
147 		internalUiBean.setListValue(listValue);
148 	}
149 }