001: /*
002: * $Id: OptGroup.java 501746 2007-01-31 06:22:43Z mrdon $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.components;
022:
023: import java.io.Writer;
024: import java.util.ArrayList;
025: import java.util.List;
026:
027: import javax.servlet.http.HttpServletRequest;
028: import javax.servlet.http.HttpServletResponse;
029:
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032: import org.apache.struts2.views.annotations.StrutsTag;
033: import org.apache.struts2.views.annotations.StrutsTagAttribute;
034:
035: import com.opensymphony.xwork2.inject.Container;
036: import com.opensymphony.xwork2.inject.Inject;
037: import com.opensymphony.xwork2.util.ValueStack;
038:
039: /**
040: * <!-- START SNIPPET: javadoc -->
041: *
042: * Create a optgroup component which needs to resides within a select tag.
043: *
044: * <!-- END SNIPPET: javadoc -->
045: *
046: * <p/>
047: *
048: * <!-- START SNIPPET: notice -->
049: *
050: * This component is to be used within a Select component.
051: *
052: * <!-- END SNIPPET: notice -->
053: *
054: * <p/>
055: *
056: * <pre>
057: * <!-- START SNIPPET: example -->
058: *
059: * <s:select label="My Selection"
060: * name="mySelection"
061: * value="%{'POPEYE'}"
062: * list="%{#{'SUPERMAN':'Superman', 'SPIDERMAN':'spiderman'}}">
063: * <s:optgroup label="Adult"
064: * list="%{#{'SOUTH_PARK':'South Park'}}" />
065: * <s:optgroup label="Japanese"
066: * list="%{#{'POKEMON':'pokemon','DIGIMON':'digimon','SAILORMOON':'Sailormoon'}}" />
067: * </s:select>
068: *
069: * <!-- END SNIPPET: example -->
070: * </pre>
071: *
072: */
073: @StrutsTag(name="optgroup",tldTagClass="org.apache.struts2.views.jsp.ui.OptGroupTag",description="Renders a Select Tag's OptGroup Tag")
074: public class OptGroup extends Component {
075:
076: public static final String INTERNAL_LIST_UI_BEAN_LIST_PARAMETER_KEY = "optGroupInternalListUiBeanList";
077:
078: private static Log _log = LogFactory.getLog(OptGroup.class);
079:
080: protected HttpServletRequest req;
081: protected HttpServletResponse res;
082:
083: protected ListUIBean internalUiBean;
084:
085: public OptGroup(ValueStack stack, HttpServletRequest req,
086: HttpServletResponse res) {
087: super (stack);
088: this .req = req;
089: this .res = res;
090: internalUiBean = new ListUIBean(stack, req, res) {
091: protected String getDefaultTemplate() {
092: return "empty";
093: }
094: };
095: }
096:
097: @Inject
098: public void setContainer(Container container) {
099: container.inject(internalUiBean);
100: }
101:
102: public boolean end(Writer writer, String body) {
103: Select select = (Select) findAncestor(Select.class);
104: if (select == null) {
105: _log
106: .error(
107: "incorrect use of OptGroup component, this component must be used within a Select component",
108: new IllegalStateException(
109: "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(
116: INTERNAL_LIST_UI_BEAN_LIST_PARAMETER_KEY);
117: if (listUiBeans == null) {
118: listUiBeans = new ArrayList();
119: }
120: listUiBeans.add(internalUiBean);
121: select.addParameter(INTERNAL_LIST_UI_BEAN_LIST_PARAMETER_KEY,
122: listUiBeans);
123:
124: return false;
125: }
126:
127: @StrutsTagAttribute(description="Set the label attribute")
128: public void setLabel(String label) {
129: internalUiBean.setLabel(label);
130: }
131:
132: @StrutsTagAttribute(description="Set the disable attribute.")
133: public void setDisabled(String disabled) {
134: internalUiBean.setDisabled(disabled);
135: }
136:
137: @StrutsTagAttribute(description="Set the list attribute.")
138: public void setList(String list) {
139: internalUiBean.setList(list);
140: }
141:
142: @StrutsTagAttribute(description="Set the listKey attribute.")
143: public void setListKey(String listKey) {
144: internalUiBean.setListKey(listKey);
145: }
146:
147: @StrutsTagAttribute(description="Set the listValue attribute.")
148: public void setListValue(String listValue) {
149: internalUiBean.setListValue(listValue);
150: }
151: }
|