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: charEditor.java 1688 2007-09-25 12:54:46Z manuel_gay $
021: // $Name$
022: /////////////////////////////////////
023:
024: package org.makumba.forms.html;
025:
026: import java.util.Dictionary;
027:
028: import org.makumba.HtmlUtils;
029: import org.makumba.commons.formatters.FieldFormatter;
030: import org.makumba.commons.formatters.InvalidValueException;
031: import org.makumba.commons.formatters.RecordFormatter;
032:
033: public class charEditor extends FieldEditor {
034:
035: private static final class SingletonHolder {
036: static final FieldEditor singleton = new charEditor();
037: }
038:
039: /** Don't use this, use getInstance() */
040: protected charEditor() {
041: }
042:
043: public static FieldFormatter getInstance() {
044: return SingletonHolder.singleton;
045: }
046:
047: // public static final charEditor singleton = new charEditor();
048:
049: static String[] _params = { "default", "empty", "type", "size",
050: "maxlength" };
051:
052: static String[][] _paramValues = { null, null,
053: { "text", "password" }, null, null };
054:
055: public String[] getAcceptedParams() {
056: return _params;
057: }
058:
059: public String[][] getAcceptedValue() {
060: return _paramValues;
061: }
062:
063: public int getWidth(RecordFormatter rf, int fieldIndex) {
064: return rf.dd.getFieldDefinition(fieldIndex).getWidth();
065: }
066:
067: public String getParams(RecordFormatter rf, int fieldIndex,
068: Dictionary formatParams) {
069: String ret = getIntParamString(rf, fieldIndex, formatParams,
070: "size");
071: int n = getIntParam(rf, fieldIndex, formatParams, "maxlength");
072: if (n > getWidth(rf, fieldIndex))
073: throw new InvalidValueException(rf.expr[fieldIndex],
074: "invalid too big for maxlength " + n);
075: if (n == -1)
076: n = getWidth(rf, fieldIndex);
077: ret += "maxlength=\"" + n + "\" ";
078: return ret;
079: }
080:
081: /** Formats the input-field in case of null object */
082: public String formatNull(RecordFormatter rf, int fieldIndex,
083: Dictionary formatParams) {
084: return formatNotNull(rf, fieldIndex, null, formatParams);
085: }
086:
087: /** Formats the input-field in case of not-null object */
088: public String formatNotNull(RecordFormatter rf, int fieldIndex,
089: Object o, Dictionary formatParams) {
090: String test = getParams(rf, fieldIndex, formatParams);
091: return "<input name=\""
092: + getInputName(rf, fieldIndex, formatParams)
093: + "\" type=\""
094: + getInputType(rf, fieldIndex, formatParams)
095: + "\" value=\""
096: + formatValue(rf, fieldIndex, o, formatParams) + "\" "
097: + test
098: + getExtraFormatting(rf, fieldIndex, formatParams)
099: + ">";
100: }
101:
102: /** Formats the value to appear in an input statement. */
103: public String formatValue(RecordFormatter rf, int fieldIndex,
104: Object o, Dictionary formatParams) {
105: String s = (o == null) ? null : HtmlUtils.string2html(o
106: .toString());
107: return resetValueFormat(rf, fieldIndex, s, formatParams);
108: }
109:
110: /*
111: * Formats the value to appear in hidden input statement: don't overload
112: * default behaviour set in FieldEditor.
113: */
114: // public String formatHiddenValue(Object o, Dictionary formatParams) {}
115: // public String getLiteral(Object o, Dictionary formatParams)
116: // { }
117: public String getInputType(RecordFormatter rf, int fieldIndex,
118: Dictionary formatParams) {
119: String s = (String) formatParams.get("type");
120: if (s == null)
121: s = "text";
122: return s;
123: }
124: }
|