001: /*
002: WikiForms - a WikiPage FORM handler for JSPWiki.
003:
004: Copyright (C) 2003 BaseN.
005:
006: JSPWiki Copyright (C) 2002 Janne Jalkanen (Janne.Jalkanen@iki.fi)
007:
008: This program is free software; you can redistribute it and/or modify
009: it under the terms of the GNU Lesser General Public License as published
010: by the Free Software Foundation; either version 2.1 of the License, or
011: (at your option) any later version.
012:
013: This program is distributed in the hope that it will be useful,
014: but WITHOUT ANY WARRANTY; without even the implied warranty of
015: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: GNU Lesser General Public License for more details.
017:
018: You should have received a copy of the GNU Lesser General Public License
019: along with this program; if not, write to the Free Software
020: */
021: package com.ecyrd.jspwiki.forms;
022:
023: import com.ecyrd.jspwiki.*;
024: import com.ecyrd.jspwiki.plugin.PluginException;
025: import com.ecyrd.jspwiki.plugin.WikiPlugin;
026:
027: import java.util.*;
028:
029: import org.apache.ecs.ConcreteElement;
030: import org.apache.ecs.xhtml.option;
031: import org.apache.ecs.xhtml.select;
032:
033: /**
034: * @author ebu
035: */
036: public class FormSelect extends FormElement {
037: public String execute(WikiContext ctx, Map params)
038: throws PluginException {
039: // Don't render if no error and error-only-rendering is on.
040: FormInfo info = getFormInfo(ctx);
041:
042: ResourceBundle rb = ctx
043: .getBundle(WikiPlugin.CORE_PLUGINS_RESOURCEBUNDLE);
044: Map previousValues = null;
045:
046: if (info != null) {
047: if (info.hide()) {
048: return "<p>" + rb.getString("forminput.noneedtoshow")
049: + "</p>";
050: }
051: previousValues = info.getSubmission();
052: }
053:
054: if (previousValues == null) {
055: previousValues = new HashMap();
056: }
057:
058: ConcreteElement field = null;
059:
060: field = buildSelect(params, previousValues, rb);
061:
062: // We should look for extra params, e.g. width, ..., here.
063: if (field != null)
064: return field.toString(ctx.getEngine().getContentEncoding());
065:
066: return "";
067: }
068:
069: /**
070: * Builds a Select element.
071: */
072: private select buildSelect(Map pluginParams, Map ctxValues,
073: ResourceBundle rb) throws PluginException {
074: String inputName = (String) pluginParams.get(PARAM_INPUTNAME);
075: if (inputName == null) {
076: throw new PluginException(rb
077: .getString("formselect.namemissing"));
078: }
079:
080: String inputValue = (String) pluginParams.get(PARAM_VALUE);
081: String previousValue = (String) ctxValues.get(inputName);
082: //
083: // We provide several ways to override the separator, in case
084: // some input application the default value.
085: //
086: String optionSeparator = (String) pluginParams.get("separator");
087: if (optionSeparator == null)
088: optionSeparator = (String) ctxValues.get("separator."
089: + inputName);
090: if (optionSeparator == null)
091: optionSeparator = (String) ctxValues
092: .get("select.separator");
093: if (optionSeparator == null)
094: optionSeparator = ";";
095:
096: String optionSelector = (String) pluginParams.get("selector");
097: if (optionSelector == null)
098: optionSelector = (String) ctxValues.get("selector."
099: + inputName);
100: if (optionSelector == null)
101: optionSelector = (String) ctxValues.get("select.selector");
102: if (optionSelector == null)
103: optionSelector = "*";
104: if (optionSelector.equals(optionSeparator))
105: optionSelector = null;
106: if (inputValue == null)
107: inputValue = "";
108:
109: // If values from the context contain the separator, we assume
110: // that the plugin or something else has given us a better
111: // list to display.
112: boolean contextValueOverride = false;
113: if (previousValue != null) {
114: if (previousValue.indexOf(optionSeparator) != -1) {
115: inputValue = previousValue;
116: previousValue = null;
117: } else {
118: // If a context value exists, but it's not a list,
119: // it'll just override any existing selector
120: // indications.
121: contextValueOverride = true;
122: }
123: }
124:
125: String[] options = inputValue.split(optionSeparator);
126: if (options == null)
127: options = new String[0];
128: int previouslySelected = -1;
129:
130: option[] optionElements = new option[options.length];
131:
132: //
133: // Figure out which one of the options to select: prefer the one
134: // that was previously selected, otherwise try to find the one
135: // with the "select" marker.
136: //
137: for (int i = 0; i < options.length; i++) {
138: int indicated = -1;
139: options[i] = options[i].trim();
140:
141: if (options[i].startsWith(optionSelector)) {
142: options[i] = options[i].substring(optionSelector
143: .length());
144: indicated = i;
145: }
146: if (previouslySelected == -1) {
147: if (!contextValueOverride && indicated > 0) {
148: previouslySelected = indicated;
149: } else if (previousValue != null
150: && options[i].equals(previousValue)) {
151: previouslySelected = i;
152: }
153: }
154:
155: optionElements[i] = new option(options[i]);
156: optionElements[i].addElement(options[i]);
157: }
158:
159: if (previouslySelected > -1)
160: optionElements[previouslySelected].setSelected(true);
161: select field = new select(HANDLERPARAM_PREFIX + inputName,
162: optionElements);
163:
164: return (field);
165: }
166: }
|