01: /*
02: WikiForms - a WikiPage FORM handler for JSPWiki.
03:
04: Copyright (C) 2003 BaseN.
05:
06: JSPWiki Copyright (C) 2002 Janne Jalkanen (Janne.Jalkanen@iki.fi)
07:
08: This program is free software; you can redistribute it and/or modify
09: it under the terms of the GNU Lesser General Public License as published
10: by the Free Software Foundation; either version 2.1 of the License, or
11: (at your option) any later version.
12:
13: This program is distributed in the hope that it will be useful,
14: but WITHOUT ANY WARRANTY; without even the implied warranty of
15: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16: GNU Lesser General Public License for more details.
17:
18: You should have received a copy of the GNU Lesser General Public License
19: along with this program; if not, write to the Free Software
20: */
21: package com.ecyrd.jspwiki.forms;
22:
23: import com.ecyrd.jspwiki.*;
24: import com.ecyrd.jspwiki.plugin.PluginException;
25: import com.ecyrd.jspwiki.plugin.WikiPlugin;
26:
27: import java.util.*;
28:
29: import org.apache.ecs.xhtml.input;
30:
31: /**
32: * Creates a simple input text field.
33: */
34: public class FormInput extends FormElement {
35: public static final String PARAM_TYPE = "type";
36: public static final String PARAM_SIZE = "size";
37:
38: /**
39: * Generates a dynamic form element on the WikiPage.
40: */
41: public String execute(WikiContext ctx, Map params)
42: throws PluginException {
43: String inputName = (String) params.get(PARAM_INPUTNAME);
44: String inputValue = (String) params.get(PARAM_VALUE);
45: String inputType = (String) params.get(PARAM_TYPE);
46: String size = (String) params.get(PARAM_SIZE);
47: ResourceBundle rb = ctx
48: .getBundle(WikiPlugin.CORE_PLUGINS_RESOURCEBUNDLE);
49:
50: if (inputName == null)
51: throw new PluginException(rb
52: .getString("forminput.namemissing"));
53: if (inputValue == null)
54: inputValue = "";
55:
56: // Don't render if no error and error-only-rendering is on.
57: FormInfo info = getFormInfo(ctx);
58: Map previousValues = null;
59: if (info != null) {
60: if (info.hide()) {
61: return ("<p>" + rb.getString("forminput.noneedtoshow") + "</p>");
62: }
63: previousValues = info.getSubmission();
64: }
65:
66: if (previousValues == null) {
67: previousValues = new HashMap();
68: }
69:
70: // In order to isolate posted form elements into their own
71: // map, prefix the variable name here. It will be stripped
72: // when the handler plugin is executed.
73: input field = new input(inputType, HANDLERPARAM_PREFIX
74: + inputName, inputValue);
75:
76: String checked = (String) params.get("checked");
77: field.setChecked(TextUtil.isPositive(checked)
78: || "checked".equalsIgnoreCase(checked));
79:
80: String oldValue = (String) previousValues.get(inputName);
81: if (oldValue != null) {
82: field.setValue(oldValue);
83: }
84:
85: if (size != null)
86: field.setSize(size);
87:
88: return field.toString(ctx.getEngine().getContentEncoding());
89: }
90: }
|