001: // /////////////////////////////
002: // Makumba, Makumba tag library
003: // Copyright (C) 2000-2003 http://www.makumba.org
004: //
005: // This library is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU Lesser General Public
007: // License as published by the Free Software Foundation; either
008: // version 2.1 of the License, or (at your option) any later version.
009: //
010: // This library is distributed in the hope that it will be useful,
011: // but WITHOUT ANY WARRANTY; without even the implied warranty of
012: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: // Lesser General Public License for more details.
014: //
015: // You should have received a copy of the GNU Lesser General Public
016: // License along with this library; if not, write to the Free Software
017: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: //
019: // -------------
020: // $Id: choiceEditor.java 1888 2007-10-21 23:45:48Z rosso_nero $
021: // $Name$
022: /////////////////////////////////////
023:
024: package org.makumba.forms.html;
025:
026: import java.util.ArrayList;
027: import java.util.Dictionary;
028: import java.util.Enumeration;
029: import java.util.Iterator;
030: import java.util.List;
031: import java.util.Vector;
032:
033: import org.makumba.HtmlChoiceWriter;
034: import org.makumba.Pointer;
035: import org.makumba.commons.formatters.RecordFormatter;
036:
037: public abstract class choiceEditor extends FieldEditor {
038:
039: static String[] _params = { "default", "empty", "type", "size",
040: "labelSeparator", "elementSeparator", "nullOption",
041: "selectMultiple" };
042:
043: static String[][] _paramValues = { null, null,
044: { "hidden", "radio", "checkbox", "tickbox" }, null, null,
045: null, null, { "true", "false" } };
046:
047: protected String nullOption = null;
048:
049: public String[] getAcceptedParams() {
050: return _params;
051: }
052:
053: public String[][] getAcceptedValue() {
054: return _paramValues;
055: }
056:
057: /** Get the available options. */
058: public abstract Object getOptions(RecordFormatter rf,
059: int fieldIndex, Dictionary formatParams);
060:
061: /** Gets the number of available options. */
062: public abstract int getOptionsLength(RecordFormatter rf,
063: int fieldIndex, Object opts);
064:
065: /** Gets the value of option 'i'. */
066: public abstract Object getOptionValue(RecordFormatter rf,
067: int fieldIndex, Object options, int i);
068:
069: /** Gets the title/label of option 'i'. */
070: public abstract String formatOptionTitle(RecordFormatter rf,
071: int fieldIndex, Object options, int i);
072:
073: /** Formats an option value, in the sequence of options. */
074: public abstract String formatOptionValue(RecordFormatter rf,
075: int fieldIndex, Object opts, int i, Object val);
076:
077: /** Formats an option value. */
078: public abstract String formatOptionValue(RecordFormatter rf,
079: int fieldIndex, Object val);
080:
081: /** Returns blank string, or " multiple " if multiple selections possible. */
082: // FIXME, would be better with "boolean isMultiple()"
083: public abstract String getMultiple(RecordFormatter rf,
084: int fieldIndex);
085:
086: public abstract boolean isMultiple(RecordFormatter rf,
087: int fieldIndex);
088:
089: /** Gets the default size of the select HTML box. */
090: public abstract int getDefaultSize(RecordFormatter rf,
091: int fieldIndex);
092:
093: /** Null values are ignored */
094: public boolean shouldRemoveNullValue(RecordFormatter rf,
095: int fieldIndex) {
096: return true;
097: }
098:
099: // height? orderBy? where?
100: public String format(RecordFormatter rf, int fieldIndex, Object o,
101: Dictionary formatParams) {
102: String type = (String) formatParams.get("type");
103: boolean hidden = "hidden".equals(type);
104: boolean yn_radio = "radio".equals(type);
105: boolean yn_checkbox = "checkbox".equals(type);
106: boolean yn_tickbox = "tickbox".equals(type);
107: boolean selectMultiple = "true".equals(formatParams
108: .get("selectMultiple"));
109: formatParams.remove("selectMultiple");
110:
111: // check whether the enum Editor should have a null value option.
112: // doing this from here seems a bit dirty, but the formatParams are not available in the subclass.
113: if (this instanceof choiceEditor) {
114: ((choiceEditor) this ).setNullOption(formatParams
115: .get("nullOption"));
116: }
117:
118: if (yn_tickbox) {
119: if (isMultiple(rf, fieldIndex))
120: yn_checkbox = true;
121: else
122: yn_radio = true;
123: }
124:
125: Vector value;
126: o = getValueOrDefault(rf, fieldIndex, o, formatParams);
127: if (o instanceof Vector) {
128: value = (Vector) o;
129: } else {
130: value = new Vector(1);
131: if (o != null)
132: value.addElement(o);
133: }
134:
135: // we clean up null values
136: for (Iterator i = value.iterator(); i.hasNext();)
137: if (i.next() == Pointer.Null) {
138: if (shouldRemoveNullValue(rf, fieldIndex))
139: i.remove();
140: }
141: if (!hidden) {
142: HtmlChoiceWriter hcw = new HtmlChoiceWriter(getInputName(
143: rf, fieldIndex, formatParams));
144: int size = getIntParam(rf, fieldIndex, formatParams, "size");
145: if (size == -1)
146: size = getDefaultSize(rf, fieldIndex);
147: hcw.setSize(size);
148: hcw.setMultiple(isMultiple(rf, fieldIndex)
149: || selectMultiple);
150: hcw.setLiteralHtml(getExtraFormatting(rf, fieldIndex,
151: formatParams));
152:
153: Object opt = getOptions(rf, fieldIndex, formatParams);
154: List values = new ArrayList(getOptionsLength(rf,
155: fieldIndex, opt));
156: List labels = new ArrayList(getOptionsLength(rf,
157: fieldIndex, opt));
158: String[] valueFormattedList = new String[value.size()];
159:
160: for (int i = 0; i < getOptionsLength(rf, fieldIndex, opt); i++) {
161: Object val = getOptionValue(rf, fieldIndex, opt, i);
162:
163: values.add(val == null ? null : formatOptionValue(rf,
164: fieldIndex, opt, i, val));
165: labels.add(formatOptionTitle(rf, fieldIndex, opt, i));
166: // System.out.println(formatOptionTitle(opt,
167: // i)+"="+formatOptionValue(opt, i, val));
168: }
169: hcw.setValues(values);
170: hcw.setLabels(labels);
171:
172: try { // set deprecated values if data type supports it
173: Vector dv = rf.dd.getFieldDefinition(fieldIndex)
174: .getDeprecatedValues();
175: // System.out.println("setting deprecated:"+dv);
176: if (dv != null && !dv.isEmpty()) {
177: String[] dvs = new String[dv.size()];
178: for (int i = 0; i < dv.size(); i++) {
179: dvs[i] = (String) dv.elementAt(i).toString();
180: }
181: hcw.setDeprecatedValues(dvs);
182: }
183: } catch (ClassCastException cce) {
184: }
185:
186: for (int i = 0; i < value.size(); i++) {
187: valueFormattedList[i] = formatOptionValue(rf,
188: fieldIndex, value.get(i));
189: }
190: hcw.setSelectedValues(valueFormattedList);
191:
192: if (yn_radio || yn_checkbox) {
193: String sep = (String) formatParams
194: .get("elementSeparator");
195: if (sep != null)
196: hcw.setOptionSeparator(sep);
197: sep = (String) formatParams.get("labelSeparator");
198: if (sep != null)
199: hcw.setTickLabelSeparator(sep);
200:
201: if (yn_radio)
202: return hcw.getRadioSelect();
203: else
204: return hcw.getCheckboxSelect();
205: }
206:
207: return hcw.getSelect();
208:
209: } else { // hidden
210:
211: StringBuffer sb = new StringBuffer();
212: for (Enumeration f = value.elements(); f.hasMoreElements();) {
213: Object val = f.nextElement();
214: sb.append("<input type=\"hidden\" name=\"").append(
215: getInputName(rf, fieldIndex, formatParams))
216: .append("\" value=\"").append(
217: formatOptionValue(rf, fieldIndex, val))
218: .append("\">");
219: }
220: return sb.toString();
221: }
222: }
223:
224: /**
225: * Return value if not null, or finds the default option and returns it as a Vector.
226: */
227: public Object getValueOrDefault(RecordFormatter rf, int fieldIndex,
228: Object o, Dictionary formatParams) {
229: if (o == null
230: || (o instanceof Vector && ((Vector) o).size() == 0)) {
231: String nullReplacer = (String) formatParams.get("default");
232: if (nullReplacer != null) {
233: Vector v = new Vector();
234: v.add(nullReplacer);
235: return v;
236: }
237: }
238: return o;
239: }
240:
241: /** Sets the value of the null option from the mak:input tag. */
242: public void setNullOption(Object nullOption) {
243: if (nullOption instanceof String
244: && ((String) nullOption).trim().length() > 0) {
245: this .nullOption = (String) nullOption;
246: } else { // if we got no / an empty value, we need to empty the fields due to the re-use of the editors
247: this.nullOption = null;
248: }
249: }
250:
251: }
|