001: package com.teamkonzept.field;
002:
003: import java.util.Enumeration;
004: import com.teamkonzept.lib.*;
005: import com.teamkonzept.web.*;
006: import com.teamkonzept.field.db.*;
007: import com.teamkonzept.international.LanguageManager;
008:
009: /**
010: * The option field control.
011: *
012: * @author $Author: uli $
013: * @version $Revision: 1.19 $
014: */
015: public abstract class TKOptionField extends TKAtomField {
016: // $Id: TKOptionField.java,v 1.19 2002/02/27 11:07:04 uli Exp $
017:
018: public static final String MULTIPLE_KEY = "MULTIPLE";
019: public static final String OPTIONS_KEY = "OPTIONS";
020:
021: protected boolean multiple = false;
022:
023: protected TKVector optionList = null;
024:
025: public TKOptionField() {
026: };
027:
028: protected TKOptionField(String classId, String name,
029: String showName, TKVector optionList, boolean multiple) {
030: initOptionField(classId, name, showName, optionList, multiple);
031: }
032:
033: public final void initOptionField(String type, String name,
034: String showName, TKVector optionList, boolean multiple) {
035: initAtomField(type, name, showName);
036: this .optionList = optionList;
037: this .multiple = multiple;
038: }
039:
040: public void init(String fieldClass, Object data)
041: throws TKUnregisteredClassException,
042: ClassNotFoundException, InstantiationException,
043: IllegalAccessException {
044: TKHashtable checkData = (TKHashtable) data;
045: super .init(fieldClass, data);
046:
047: this .multiple = ((String) checkData.get(MULTIPLE_KEY))
048: .equals("YES");
049: optionList = getListOfOptions((TKVector) checkData
050: .get(OPTIONS_KEY));
051: }
052:
053: /**
054: * Methode zur Definition eines Optionseldes
055: */
056: public TKFieldGroup getDefGroup(TKFieldSwitch allSwitch,
057: TKFieldSwitchList allSwitchList) {
058:
059: TKVector multipleOptions = new TKVector(2);
060: multipleOptions
061: .addElement(new TKOptionFieldEntry(LanguageManager
062: .getText(LanguageManager.GENERAL, "YES"), "YES"));
063: multipleOptions.addElement(new TKOptionFieldEntry(
064: LanguageManager.getText(LanguageManager.GENERAL, "NO"),
065: "NO"));
066:
067: TKBaseField[] optionArray = {
068: new TKInputField("NAME",
069: TKInputField.SMALL_DEFAULT_SIZE,
070: TKInputField.SMALL_DEFAULT_LENGTH,
071: LanguageManager.getText(LANGUAGE_CONTEXT,
072: "OPTIONFIELD_NAME"),
073: TKInputField.CHECK_STRING),
074: new TKInputField("SHOWNAME",
075: TKInputField.LARGE_DEFAULT_SIZE,
076: TKInputField.LARGE_DEFAULT_LENGTH,
077: LanguageManager.getText(LANGUAGE_CONTEXT,
078: "OPTIONFIELD_SHOWNAME"),
079: TKInputField.CHECK_STRING) };
080:
081: TKFieldGroup optionGroup = new TKFieldGroup("OPTION",
082: new TKVector(optionArray), LanguageManager.getText(
083: LANGUAGE_CONTEXT, "OPTION"));
084:
085: return optionGroup;
086: }
087:
088: /**
089: * Returns the internal representation of this field.
090: *
091: * @return the internal representation of this field.
092: */
093: public Object toData() {
094: TKHashtable result = (TKHashtable) super .toData();
095: result.put(MULTIPLE_KEY, (multiple ? "YES" : "NO"));
096: result.put(OPTIONS_KEY, getDataOfOptions(optionList));
097: return result;
098: }
099:
100: public void fillIntoTemplate(TKHTMLTemplate t, Object value,
101: String prefix) {
102: super .fillIntoTemplate(t, value, prefix);
103:
104: t.setListIterator(new TKOptionFieldIterator(optionList,
105: fieldName, t.getListIterator(), "OPTION_LIST"));
106: t.set(prefix + fieldName, value);
107:
108: if (multiple)
109: t.set(prefix + fieldName + ".MULTIPLE", Boolean.TRUE);
110: }
111:
112: public int realInsertIntoDB(TKFormDBData db, int formId) {
113: if (super .realInsertIntoDB(db, formId) == -1)
114: return -1;
115:
116: insertNewFieldAttribute(db, formId, MULTIPLE_KEY, 0, String
117: .valueOf(multiple));
118: int osize = optionList.size();
119: insertNewFieldAttribute(db, formId, OPTIONS_KEY, 0, String
120: .valueOf(osize));
121: for (int i = 0; i < osize; i++) {
122: TKOptionFieldEntry entry = (TKOptionFieldEntry) optionList
123: .get(i);
124: insertNewFieldAttribute(db, formId,
125: TKOptionFieldEntry.OPTION_KEY, i, entry.option);
126: insertNewFieldAttribute(db, formId,
127: TKOptionFieldEntry.VALUE_KEY, i, entry.value);
128: }
129: return fieldId;
130: }
131:
132: public void initFromDB(String classId, TKFormDBData db,
133: TKVector otherFields) throws TKUnregisteredClassException,
134: ClassNotFoundException, InstantiationException,
135: IllegalAccessException {
136: super .initFromDB(classId, db, otherFields);
137:
138: multiple = (new Boolean(getFieldAttribute(db, MULTIPLE_KEY, 0)))
139: .booleanValue();
140: int size = Integer.parseInt(getFieldAttribute(db, OPTIONS_KEY,
141: 0));
142: if (size == 0) {
143: optionList = new TKVector();
144: } else {
145: optionList = new TKVector(size);
146: for (int i = 0; i < size; i++) {
147: String option = getFieldAttribute(db,
148: TKOptionFieldEntry.OPTION_KEY, i);
149: String value = getFieldAttribute(db,
150: TKOptionFieldEntry.VALUE_KEY, i);
151: optionList.addElement(new TKOptionFieldEntry(option,
152: value));
153: }
154: }
155: }
156:
157: public static final TKVector getListOfOptions(TKVector options) {
158: TKVector optionList = new TKVector(options.size());
159: Enumeration e = options.elements();
160: while (e.hasMoreElements()) {
161: TKHashtable option = (TKHashtable) e.nextElement();
162: optionList.addElement(new TKOptionFieldEntry(
163: (String) option.get(SHOW_NAME_KEY), (String) option
164: .get(NAME_KEY)));
165: }
166: return optionList;
167: }
168:
169: public static final TKVector getDataOfOptions(TKVector optionList) {
170: TKVector options = new TKVector(optionList.size());
171: Enumeration e = optionList.elements();
172: while (e.hasMoreElements()) {
173: TKOptionFieldEntry entry = (TKOptionFieldEntry) e
174: .nextElement();
175: TKHashtable option = new TKHashtable();
176: option.put(SHOW_NAME_KEY, entry.option);
177: option.put(NAME_KEY, entry.value);
178: options.addElement(option);
179: }
180: return options;
181: }
182:
183: /**
184: * Checks wether this object and the specified object
185: * may be treated as equal.
186: *
187: * @param object the object to checked for equality.
188: * @return <CODE>true</CODE> if this object and the
189: * specified object may be treated as equal, otherwise
190: * <CODE>false</CODE>.
191: */
192: public boolean equals(Object object) {
193: if (!super .equals(object)) {
194: return false;
195: }
196:
197: TKOptionField field = (TKOptionField) object;
198:
199: return (this .multiple == field.multiple)
200: && (this .optionList == null ? field.optionList == null
201: : this .optionList.equals(field.optionList));
202: }
203:
204: /**
205: * Returns the hash code for this object.
206: *
207: * @return the hash code for this object.
208: */
209: public int hashCode() {
210: // Implementation for JTest only ;-(
211: return super.hashCode();
212: }
213:
214: }
|