001: package com.teamkonzept.field;
002:
003: import com.teamkonzept.lib.*;
004: import com.teamkonzept.publishing.markups.*;
005: import com.teamkonzept.web.*;
006: import com.teamkonzept.field.db.*;
007: import com.teamkonzept.international.LanguageManager;
008:
009: /**
010: * The input field control.
011: *
012: * @author $Author: uli $
013: * @version $Revision: 1.26 $
014: */
015: public class TKInputField extends TKAtomField {
016: // $Id: TKInputField.java,v 1.26 2002/02/27 11:07:04 uli Exp $
017:
018: /**
019: * The class identifier.
020: */
021: public static final String CLASS_ID = "INPUT";
022: public static final String LENGTH_KEY = "LENGTH";
023: public static final String SIZE_KEY = "SIZE";
024: public static final String INPUT_CHECK_KEY = "INPUT_CHECK";
025: public static final String INPUT_MANDATORY_KEY = "INPUT_MANDATORY";
026:
027: public static final int CHECK_UNDEFINED = -1;
028: public static final int CHECK_INTEGER = 0;
029: public static final int CHECK_OPTIONAL_INTEGER = 1;
030: public static final int CHECK_STRING = 2;
031:
032: public final static int SMALL_DEFAULT_SIZE = 16;
033: public final static int LARGE_DEFAULT_SIZE = 50;
034: public final static int SMALL_DEFAULT_LENGTH = 80;
035: public final static int LARGE_DEFAULT_LENGTH = 254;
036:
037: public static final boolean[] CHECK_MANDATORY = { true, false, true };
038:
039: public static final String[] CHECK_NAMES = { "integer",
040: "optional_integer", "string" };
041:
042: protected int size;
043: protected int length;
044:
045: // Initialize to default: no check at all.
046: protected int check = CHECK_UNDEFINED;
047:
048: public TKInputField() {
049: };
050:
051: public TKInputField(String name, int size) {
052: this (name, size, size, null);
053: }
054:
055: public TKInputField(String name, int size, String showName) {
056: this (name, size, size, showName);
057: }
058:
059: public TKInputField(String name, int size, int length,
060: String showName) {
061: this (name, size, length, showName, CHECK_UNDEFINED);
062: }
063:
064: public TKInputField(String name, int size, int length,
065: String showName, int check) {
066: initInputField(CLASS_ID, name, size, length, showName, check);
067: }
068:
069: public final void initInputField(String type, String name,
070: int size, int length, String showName, int check) {
071: initAtomField(type, name, showName);
072:
073: this .size = size;
074: this .length = length;
075: this .check = check;
076: }
077:
078: public void init(String classId, Object initData)
079: throws TKUnregisteredClassException,
080: ClassNotFoundException, InstantiationException,
081: IllegalAccessException {
082: super .init(classId, initData);
083:
084: String value = null;
085: TKHashtable data = (TKHashtable) initData;
086:
087: this .size = Integer.parseInt((String) data.get(SIZE_KEY));
088:
089: value = (String) data.get(LENGTH_KEY);
090: this .length = value != null && value.length() > 0 ? Integer
091: .parseInt(value) : -1;
092:
093: value = (String) data.get(INPUT_CHECK_KEY);
094: this .check = value != null && value.length() > 0 ? Integer
095: .parseInt(value) : CHECK_UNDEFINED;
096: }
097:
098: /**
099: ueberschrieben, damit der NAME und SHOWNAME ausgegeben wird
100: */
101: public String getInternationalName() {
102: if (fieldName.equals(NAME_KEY)
103: || fieldName.equals(SHOW_NAME_KEY))
104: return fieldName;
105: else
106: return super .getInternationalName();
107: }
108:
109: /**
110: * Methode zur Definition eines Eingabefeldes
111: */
112: public TKFieldGroup getDefGroup(TKFieldSwitch allSwitch,
113: TKFieldSwitchList allSwitchList) {
114: TKBaseField[] inputArray = {
115: new TKInputField(TKInputField.NAME_KEY,
116: SMALL_DEFAULT_SIZE, SMALL_DEFAULT_LENGTH,
117: LanguageManager.getText(LANGUAGE_CONTEXT,
118: "INPUT_NAME"),
119: TKInputField.CHECK_STRING),
120: new TKInputField(TKInputField.SHOW_NAME_KEY,
121: LARGE_DEFAULT_SIZE, LARGE_DEFAULT_LENGTH,
122: LanguageManager.getText(LANGUAGE_CONTEXT,
123: "INPUT_SHOWNAME"),
124: TKInputField.CHECK_STRING),
125: new TKInputField(TKInputField.SIZE_KEY, 3, 3,
126: LanguageManager.getText(LANGUAGE_CONTEXT,
127: "INPUT_SIZE"),
128: TKInputField.CHECK_INTEGER),
129: new TKInputField(TKInputField.LENGTH_KEY, 3, 3,
130: LanguageManager.getText(LANGUAGE_CONTEXT,
131: "INPUT_MAXLENGTH"),
132: TKInputField.CHECK_OPTIONAL_INTEGER) };
133:
134: return new TKFieldGroup(TKInputField.CLASS_ID, new TKVector(
135: inputArray), LanguageManager.getText(LANGUAGE_CONTEXT,
136: TKInputField.CLASS_ID));
137: }
138:
139: public Object toData() {
140: TKHashtable result = (TKHashtable) super .toData();
141:
142: result.put(SIZE_KEY, String.valueOf(size));
143:
144: if (this .length > -1) {
145: result.put(LENGTH_KEY, String.valueOf(this .length));
146: }
147:
148: if (this .check > CHECK_UNDEFINED) {
149: result.put(INPUT_CHECK_KEY, String.valueOf(this .check));
150: }
151:
152: return result;
153: }
154:
155: public void fillIntoTemplate(TKHTMLTemplate t, Object value,
156: String prefix) {
157: super .fillIntoTemplate(t, value, prefix);
158:
159: t.set(SIZE_KEY, String.valueOf(this .size));
160:
161: if (this .length > -1) {
162: t.set(LENGTH_KEY, String.valueOf(this .length));
163: }
164:
165: if (this .check > CHECK_UNDEFINED) {
166: t.set(INPUT_CHECK_KEY, CHECK_NAMES[this .check]);
167:
168: if (CHECK_MANDATORY[this .check]) {
169: t.set(INPUT_MANDATORY_KEY, INPUT_MANDATORY_KEY);
170: }
171: }
172: }
173:
174: public void initFromDB(String classId, TKFormDBData db,
175: TKVector otherFields) throws TKUnregisteredClassException,
176: ClassNotFoundException, InstantiationException,
177: IllegalAccessException {
178: super .initFromDB(classId, db, otherFields);
179:
180: this .size = Integer
181: .parseInt(getFieldAttribute(db, SIZE_KEY, 0));
182: this .length = Integer.parseInt(getFieldAttribute(db,
183: LENGTH_KEY, 0));
184: }
185:
186: public int realInsertIntoDB(TKFormDBData db, int formId) {
187: if (super .realInsertIntoDB(db, formId) == -1)
188: return -1;
189:
190: insertNewFieldAttribute(db, formId, SIZE_KEY, 0, String
191: .valueOf(this .size));
192: insertNewFieldAttribute(db, formId, LENGTH_KEY, 0, String
193: .valueOf(this .length));
194:
195: return fieldId;
196: }
197:
198: public Object compileData(String prefix, TKMarkupNode data,
199: TKHashtable context) {
200: TKXmlMarkup markup = data == null ? null
201: : (TKXmlMarkup) data.markup;
202: if (markup == null) {
203: return null;
204: }
205:
206: if (!markup.name.equals(getName())) {
207: return null;
208: }
209:
210: TKXmlTree tree = (TKXmlTree) data.tree;
211: if (tree == null) {
212: return null;
213: }
214:
215: String val = tree.getSingleText();
216:
217: return val == null ? getDefault() : val.trim();
218: }
219:
220: /**
221: * Checks wether this object and the specified object
222: * may be treated as equal.
223: *
224: * @param object the object to checked for equality.
225: * @return <CODE>true</CODE> if this object and the
226: * specified object may be treated as equal, otherwise
227: * <CODE>false</CODE>.
228: */
229: public boolean equals(Object object) {
230: if (!super .equals(object)) {
231: return false;
232: }
233:
234: TKInputField field = (TKInputField) object;
235:
236: return (this .size == field.size)
237: && (this .length == field.length);
238: }
239:
240: /**
241: * Returns the hash code for this object.
242: *
243: * @return the hash code for this object.
244: */
245: public int hashCode() {
246: // Implementation for JTest only ;-(
247: return super.hashCode();
248: }
249:
250: }
|