001: /*
002: * (C) Copyright 2004 Nabh Information Systems, Inc.
003: *
004: * All copyright notices regarding Nabh's products MUST remain
005: * intact in the scripts and in the outputted HTML.
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021: package com.nabhinc.portlet.mvcportlet.taglib;
022:
023: import java.io.IOException;
024:
025: import javax.portlet.PortletException;
026: import javax.portlet.RenderRequest;
027: import javax.servlet.jsp.JspException;
028: import javax.servlet.jsp.JspWriter;
029:
030: import com.nabhinc.portlet.mvcportlet.core.Constants;
031: import com.nabhinc.portlet.mvcportlet.core.ControllerPortletConfig;
032: import com.nabhinc.portlet.mvcportlet.core.Form;
033: import com.nabhinc.portlet.mvcportlet.core.FormField;
034: import com.nabhinc.portlet.mvcportlet.core.FormFieldPopulator;
035: import com.nabhinc.portlet.mvcportlet.core.ValueAndLabel;
036:
037: /**
038: * "select" tag that populates itself based on a populator
039: * configured in portlet-config.xml file.
040: * Added
041: *
042: * @author Padmanabh Dabke
043: * (c) 2004 Nabh Information Systems, Inc. All Rights Reserved.
044: */
045: public class SelectTag extends BaseTag {
046: /**
047: *
048: */
049: private static final long serialVersionUID = 1L;
050:
051: /**
052: * Should multiple selections be allowed. Any non-null value will
053: * trigger rendering this.
054: */
055: private boolean multiple = false;
056:
057: /**
058: * How many available options should be displayed when this element
059: * is rendered?
060: */
061: private String size = null;
062:
063: /**
064: * Selection value if it is not supplied by the form parameter.
065: */
066: private String selection = null;
067:
068: /**
069: * Flag indicating insertion of an empty option in the beginning.
070: */
071: private boolean insertEmptyOption = true;
072:
073: public void release() {
074: super .release();
075: this .multiple = false;
076: this .size = null;
077: this .selection = null;
078: this .insertEmptyOption = true;
079: }
080:
081: public boolean getMultiple() {
082: return (this .multiple);
083: }
084:
085: public void setMultiple(boolean flag) {
086: this .multiple = flag;
087: }
088:
089: public String getSize() {
090: return (this .size);
091: }
092:
093: public void setSize(String size) {
094: this .size = size;
095: }
096:
097: public String getSelection() {
098: return (this .selection);
099: }
100:
101: public void setSelection(String sel) {
102: this .selection = sel;
103: }
104:
105: public boolean getInsertEmptyOption() {
106: return insertEmptyOption;
107: }
108:
109: public void setInsertEmptyOption(boolean flag) {
110: this .insertEmptyOption = flag;
111: }
112:
113: public int doStartTag() throws JspException {
114: JspWriter out = pageContext.getOut();
115: Form form = (Form) pageContext
116: .getAttribute(Constants.FORM_ATTRIB);
117: if (form == null)
118: throw new JspException(
119: "Failed to find a form within MVCPortlet select element.");
120: FormField field = form.getField(getName());
121: if (field == null)
122: throw new JspException("Failed to find a field named "
123: + getName() + "within MVCPortlet select element.");
124: FormFieldPopulator pop = field.getPopulator();
125: if (pop == null)
126: throw new JspException("Form field " + field.getName()
127: + " must have a populator.");
128: // if multiple is not specified, then check if it is defined in the FormField.
129: if (!this .multiple) {
130: if (field.isMultiValued())
131: this .multiple = true;
132: }
133: renderSelectStartElement(out);
134: writeBody(out, pop);
135:
136: return (EVAL_BODY_INCLUDE);
137: }
138:
139: private void renderSelectStartElement(JspWriter out)
140: throws JspException {
141: try {
142: out.print("<select name=\"");
143: out.print(getName());
144: out.print("\"");
145: if (btAccesskey != null) {
146: out.print(" accesskey=\"");
147: out.print(btAccesskey);
148: out.print("\"");
149: }
150: if (multiple) {
151: out.print(" multiple=\"multiple\"");
152: }
153: if (size != null) {
154: out.print(" size=\"");
155: out.print(size);
156: out.print("\"");
157: }
158: if (tabindex != null) {
159: out.print(" tabindex=\"");
160: out.print(tabindex);
161: out.print("\"");
162: }
163: out.print(prepareEventHandlers());
164: out.print(prepareStyles());
165: out.print(">");
166: } catch (IOException ioe) {
167: throw new JspException("IO exception", ioe);
168: }
169:
170: }
171:
172: /**
173: * Calculate the match values we will actually be using.
174: * @throws JspException
175: */
176: private void writeBody(JspWriter out, FormFieldPopulator pop)
177: throws JspException {
178: try {
179: RenderRequest request = (RenderRequest) pageContext
180: .getRequest().getAttribute("javax.portlet.request");
181: Object optionsObj = pop.getValue(request);
182: if (optionsObj == null)
183: return;
184: String[] selection = null;
185: if (this .multiple) {
186: selection = getParamValues(request);
187: } else {
188: String paramVal = getParamValue(request);
189: if (paramVal != null) {
190: selection = new String[] { paramVal };
191: }
192: }
193: // if (selection == null) selection = pop.getDefaultSelection(request);
194: if (selection == null) {
195: if (this .selection == null) {
196: selection = pop.getDefaultValues(request);
197: if (selection == null)
198: selection = new String[0];
199: } else {
200: selection = new String[] { this .selection };
201: }
202: }
203: if (insertEmptyOption && (!multiple))
204: out.print("<option></option>");
205:
206: ControllerPortletConfig cpConfig = (ControllerPortletConfig) request
207: .getAttribute(Constants.PORTLET_CONFIG_ATTRIB);
208:
209: if (optionsObj.getClass().getComponentType().equals(
210: String.class)) {
211: // We have an array of strings that serves as values as well
212: // as labels
213: String[] options = (String[]) optionsObj;
214: for (int i = 0; i < options.length; i++) {
215: out.print("<option value=\"");
216: out.print(options[i]);
217: out.print("\" ");
218: for (int j = 0; j < selection.length; j++) {
219: if (selection[j].equals(options[i])) {
220: out.print("selected=\"selected\"");
221: break;
222: }
223: }
224: out.print(">");
225: out.print(cpConfig.getLocalizedMessage(options[i],
226: request));
227: out.print("</option>");
228: }
229: } else {
230: // We have an array of ValueAndLabel objects
231: ValueAndLabel[] options = (ValueAndLabel[]) optionsObj;
232: for (int i = 0; i < options.length; i++) {
233: out.print("<option value=\"");
234: out.print(options[i].getValue());
235: out.print("\" ");
236: for (int j = 0; j < selection.length; j++) {
237: if (selection[j].equals(options[i].getValue())) {
238: out.print("selected=\"selected\"");
239: break;
240: }
241: }
242: out.print(">");
243: out.print(cpConfig.getLocalizedMessage(options[i]
244: .getLabel(), request));
245: out.print("</option>");
246: }
247:
248: }
249: } catch (IOException ioe) {
250: error("IOException in SelectTag", ioe);
251: throw new JspException("IO exception.", ioe);
252: } catch (PortletException pe) {
253: error("PortletException in SelectTag", pe);
254: throw new JspException("Portlet exception", pe);
255: }
256: }
257:
258: /**
259: * Render the end of select.
260: *
261: * @exception JspException if a JSP exception has occurred
262: */
263: public int doEndTag() throws JspException {
264: try {
265: JspWriter out = pageContext.getOut();
266: out.print("</select>");
267: return (EVAL_PAGE);
268: } catch (IOException ioe) {
269: throw new JspException("IO exception.", ioe);
270: }
271: }
272:
273: }
|