001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.components;
006:
007: import java.io.Writer;
008: import java.util.ArrayList;
009: import java.util.Iterator;
010: import java.util.List;
011:
012: import javax.servlet.http.HttpServletRequest;
013: import javax.servlet.http.HttpServletResponse;
014:
015: import org.apache.commons.logging.Log;
016: import org.apache.commons.logging.LogFactory;
017:
018: import com.opensymphony.xwork.util.OgnlValueStack;
019:
020: /**
021: * <!-- START SNIPPET: javadoc -->
022: *
023: * Create a optgroup component which needs to resides within a select tag.
024: *
025: * <!-- END SNIPPET: javadoc -->
026: *
027: * <p/>
028: *
029: * <!-- START SNIPPET: notice -->
030: *
031: * This component is to be used within a Select component.
032: *
033: * <!-- END SNIPPET: notice -->
034: *
035: * <p/>
036: *
037: * <pre>
038: * <!-- START SNIPPET: example -->
039: *
040: * <ww:select label="My Selection"
041: * name="mySelection"
042: * value="%{'POPEYE'}"
043: * list="%{#{'SUPERMAN':'Superman', 'SPIDERMAN':'spiderman'}}">
044: * <ww:optgroup label="Adult"
045: * list="%{#{'SOUTH_PARK':'South Park'}}" />
046: * <ww:optgroup label="Japanese"
047: * list="%{#{'POKEMON':'pokemon','DIGIMON':'digimon','SAILORMOON':'Sailormoon'}}" />
048: * </ww:select>
049: *
050: * <!-- END SNIPPET: example -->
051: * </pre>
052: *
053: * @author tm_jee
054: * @version $Date: 2006-07-07 19:56:48 +0200 (Fri, 07 Jul 2006) $ $Id: OptGroup.java 2635 2006-07-07 17:56:48Z tmjee $
055: * @ww.tag name="optgroup" tld-body-content="JSP" tld-tag-class="com.opensymphony.webwork.views.jsp.ui.OptGroupTag"
056: * description="Renders a Select Tag's OptGroup Tag"
057: */
058: public class OptGroup extends Component {
059:
060: public static final String INTERNAL_LIST_UI_BEAN_LIST_PARAMETER_KEY = "optGroupInternalListUiBeanList";
061:
062: private static Log _log = LogFactory.getLog(OptGroup.class);
063:
064: protected HttpServletRequest req;
065: protected HttpServletResponse res;
066:
067: protected ListUIBean internalUiBean;
068:
069: public OptGroup(OgnlValueStack stack, HttpServletRequest req,
070: HttpServletResponse res) {
071: super (stack);
072: this .req = req;
073: this .res = res;
074: internalUiBean = new ListUIBean(stack, req, res) {
075: protected String getDefaultTemplate() {
076: return "empty";
077: }
078: };
079: }
080:
081: public boolean end(Writer writer, String body) {
082: Select select = (Select) findAncestor(Select.class);
083: if (select == null) {
084: _log
085: .error(
086: "incorrect use of OptGroup component, this component must be used within a Select component",
087: new IllegalStateException(
088: "incorrect use of OptGroup component, this component must be used within a Select component"));
089: return false;
090: }
091: internalUiBean.start(writer);
092: internalUiBean.end(writer, body);
093:
094: List listUiBeans = (List) select.getParameters().get(
095: INTERNAL_LIST_UI_BEAN_LIST_PARAMETER_KEY);
096: if (listUiBeans == null) {
097: listUiBeans = new ArrayList();
098: }
099: listUiBeans.add(internalUiBean);
100: select.addParameter(INTERNAL_LIST_UI_BEAN_LIST_PARAMETER_KEY,
101: listUiBeans);
102:
103: return false;
104: }
105:
106: /**
107: * Set the label attribute.
108: * @ww.tagattribute required="false"
109: */
110: public void setLabel(String label) {
111: internalUiBean.setLabel(label);
112: }
113:
114: /**
115: * Set the disable attribute.
116: * @ww.tagattribute required="false"
117: */
118: public void setDisabled(String disabled) {
119: internalUiBean.setDisabled(disabled);
120: }
121:
122: /**
123: * Set the list attribute.
124: * @ww.tagattribute required="false"
125: */
126: public void setList(String list) {
127: internalUiBean.setList(list);
128: }
129:
130: /**
131: * Set the listKey attribute.
132: * @ww.tagattribute required="false"
133: */
134: public void setListKey(String listKey) {
135: internalUiBean.setListKey(listKey);
136: }
137:
138: /**
139: * Set the listValue attribute.
140: * @ww.tagattribute required="false"
141: */
142: public void setListValue(String listValue) {
143: internalUiBean.setListValue(listValue);
144: }
145: }
|