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 org.w3c.dom.*;
008: import com.teamkonzept.international.LanguageManager;
009:
010: /**
011: * The field list control.
012: *
013: * @author $Author: uli $
014: * @version $Revision: 1.30.4.1 $
015: */
016: public class TKFieldList extends TKBaseField {
017:
018: /**
019: * The class identifier.
020: */
021: public static final String CLASS_ID = "FIELDLIST";
022: public static final String LIST_ENTRY_KEY = "LISTENTRY";
023: public static final String ENTRY_DESCRIPTION_KEY = "ENTRY_DESCRIPTION";
024:
025: TKBaseField listEntry;
026:
027: /**
028: * Creates an empty field list control.
029: */
030: public TKFieldList() {
031: // NOP
032: }
033:
034: /**
035: * Creates a field list control with the specified properties.
036: *
037: * @param name the field name.
038: * @param listEntry the list entry.
039: */
040: public TKFieldList(String name, TKBaseField listEntry) {
041: this (name, listEntry, null);
042: }
043:
044: /**
045: * Creates a field list control with the specified properties.
046: *
047: * @param name the field name.
048: * @param listEntry the list entry.
049: * @param showName the field description.
050: */
051: public TKFieldList(String name, TKBaseField listEntry,
052: String showName) {
053: initFieldList(name, listEntry, showName);
054: }
055:
056: public final void initFieldList(String name, TKBaseField listEntry,
057: String showName) {
058: initBaseField(CLASS_ID, name, showName);
059: this .listEntry = listEntry;
060: }
061:
062: public void init(String fieldClass, Object data)
063: throws TKUnregisteredClassException,
064: ClassNotFoundException, InstantiationException,
065: IllegalAccessException {
066: super .init(fieldClass, data);
067:
068: TKHashtable listData = (TKHashtable) data;
069: TKFieldSwitchData subType = (TKFieldSwitchData) listData
070: .get(SUB_TYPE_KEY);
071:
072: if (subType.alternative.length() == 0)
073: throw new InstantiationException("no listtype for "
074: + fieldType + " " + fieldName);
075:
076: this .listEntry = (TKBaseField) TKFieldRegistry.registry.get(
077: subType.alternative, subType.data);
078: }
079:
080: /**
081: * Methode zur Definition einer Feldliste
082: * Listen von gleichartigen Eingabe-Objekten
083: */
084: public TKFieldGroup getDefGroup(TKFieldSwitch allSwitch,
085: TKFieldSwitchList allSwitchList) {
086: TKBaseField[] listArray = {
087: new TKInputField(TKFieldList.NAME_KEY,
088: TKInputField.SMALL_DEFAULT_SIZE,
089: TKInputField.SMALL_DEFAULT_LENGTH,
090: LanguageManager.getText(LANGUAGE_CONTEXT,
091: "FIELDLIST_NAME"),
092: TKInputField.CHECK_STRING),
093: new TKInputField(TKFieldList.SHOW_NAME_KEY,
094: TKInputField.LARGE_DEFAULT_SIZE,
095: TKInputField.LARGE_DEFAULT_LENGTH,
096: LanguageManager.getText(LANGUAGE_CONTEXT,
097: "FIELDLIST_SHOWNAME"),
098: TKInputField.CHECK_STRING), allSwitch };
099: TKFieldGroup listGroup = new TKFieldGroup(TKFieldList.CLASS_ID,
100: new TKVector(listArray), LanguageManager.getText(
101: LANGUAGE_CONTEXT, TKFieldList.CLASS_ID));
102:
103: return listGroup;
104: }
105:
106: public Object toData() {
107: TKHashtable result = (TKHashtable) super .toData();
108: result.put(SUB_TYPE_KEY, getDataOfAlternative(listEntry));
109: return result;
110: }
111:
112: public Object compileData(String prefix, TKHashtable data,
113: TKHashtable context) {
114: prefix += fieldName + ".";
115:
116: int listEntries = Integer.parseInt((String) data.get(prefix
117: + "SIZE"));
118: TKVector result = new TKVector(listEntries);
119: for (int i = 0; i < listEntries; i++) {
120: result.addElement(listEntry.compileData(prefix + i + ".",
121: data, context));
122: }
123: return result;
124: }
125:
126: public Object compileData(String prefix, TKMarkupNode data,
127: TKHashtable context) {
128:
129: TKXmlMarkup markup = data == null ? null
130: : (TKXmlMarkup) data.markup;
131: if (markup == null) {
132: return null;
133: }
134:
135: if (!markup.name.equals(getName())) {
136: return null;
137: }
138:
139: TKXmlTree tree = (TKXmlTree) data.tree;
140: if (tree == null) {
141: return null;
142: }
143:
144: TKVector result = new TKVector();
145: prefix += fieldName + ".";
146:
147: int currentIndex = 0;
148: for (int i = 0; i < tree.size(); i++) {
149:
150: Object obj = tree.getSub(i);
151: if (obj == null)
152: continue;
153:
154: TKMarkupNode subNode = null;
155: TKXmlMarkup subMarkup = null;
156:
157: if (obj instanceof TKXmlMarkup) { // Atomares Markup
158:
159: subMarkup = (TKXmlMarkup) obj;
160: subNode = new TKMarkupNode(subMarkup, null);
161: } else {
162: subNode = (TKMarkupNode) obj;
163: subMarkup = (TKXmlMarkup) subNode.markup;
164: }
165: result.addElement(listEntry.compileData(prefix + i + ".",
166: subNode, context));
167: currentIndex++;
168: }
169:
170: return result;
171: }
172:
173: public void fillIntoTemplate(TKHTMLTemplate t, Object data,
174: String prefix) {
175: TKVector dataVector = (TKVector) data;
176: super .fillIntoTemplate(t, data, prefix);
177: t.set("SIZE", String.valueOf(dataVector.size()));
178: t.setListIterator(new TKFieldListIterator(dataVector,
179: listEntry, prefix + fieldName + ".", t
180: .getListIterator(), "FIELDLIST"));
181:
182: t.set(ENTRY_DESCRIPTION_KEY, this .listEntry.getShowName());
183: }
184:
185: public void fillIntoDOM(Document doc, Element node, Object data)
186: throws DOMException {
187: Element me = doc.createElement(getInternationalName());
188: node.appendChild(me);
189: fillAttributesIntoNode(me, data);
190: TKVector dataVector = (TKVector) data;
191: for (int i = 0; i < dataVector.size(); i++) {
192: Object fieldData = dataVector.get(i);
193: listEntry.fillIntoDOM(doc, me, fieldData);
194: //template.set("LISTPOS", String.valueOf(i+1));
195: }
196: }
197:
198: public void fillIntoPresentation(TKHTMLTemplate t, Object data,
199: String scope) {
200: TKVector dataVector = (TKVector) data;
201: t.set(scope + "." + getName(), String
202: .valueOf(dataVector.size()));
203: t.setListIterator(new TKListShowIterator(dataVector, listEntry,
204: scope, getName(), t.getListIterator()));
205:
206: }
207:
208: public Object getDefault() {
209: return new TKVector();
210: }
211:
212: public Object modify(String action, String fieldPath, Object data,
213: String prefix, StringBuffer destination) {
214: TKVector dataVector = (TKVector) data;
215: int pLength = prefix.length() + fieldName.length();
216: int fpLength = fieldPath.length();
217: if (fpLength == pLength) {
218: // action betrifft Liste
219: if (action.equals("ADD")) {
220: dataVector.addElement(listEntry.getDefault());
221: destination.append(prefix + fieldName + "."
222: + (dataVector.size() - 1) + ".");
223: }
224: } else if (fieldPath.lastIndexOf('.', fpLength - 2) == pLength) {
225: // action betrifft ein Listenelement
226: String idxStr = fieldPath.substring(pLength + 1,
227: fpLength - 1);
228: int idx = Integer.parseInt(idxStr);
229: if (action.equals("DELETE")) {
230: dataVector.removeElementAt(idx);
231: destination.append(prefix + fieldName);
232: if (dataVector.size() > 0)
233: destination.append("."
234: + (idx >= dataVector.size() ? dataVector
235: .size() - 1 : idx) + ".");
236: }
237: if (action.equals("INSERT")) {
238: dataVector.insertElementAt(listEntry.getDefault(), idx);
239: destination.append(prefix + fieldName + "." + idxStr
240: + ".");
241: } else if (action.startsWith("MOVE:")) {
242: int newPos = idx;
243: int total = dataVector.size() - 1;
244: switch (action.charAt(5)) {
245: case '0':
246: newPos = 0;
247: break;
248: case '+':
249: newPos = (idx == total ? total : idx + 1);
250: break;
251: case '-':
252: newPos = (idx == 0 ? 0 : idx - 1);
253: break;
254: case '$':
255: newPos = total;
256: break;
257: default:
258: break;
259: }
260: if (newPos != idx) {
261: Object tmp = dataVector.get(idx);
262: dataVector.removeElementAt(idx);
263: if (newPos == total) {
264: dataVector.addElement(tmp);
265: } else {
266: dataVector.insertElementAt(tmp, newPos);
267: }
268: }
269: destination.append(prefix + fieldName + "." + newPos
270: + ".");
271: }
272: } else {
273: // action betrifft Sub-ELement
274: int idxEnd = fieldPath.indexOf('.', pLength + 1);
275: String idxStr = fieldPath.substring(pLength + 1, idxEnd);
276: int idx = Integer.parseInt(idxStr);
277: dataVector.put(idx, listEntry.modify(action, fieldPath,
278: dataVector.get(idx), prefix + fieldName + '.' + idx
279: + '.', destination));
280: }
281: return data;
282: }
283:
284: public TKBaseField getTarget(String fieldPath, String prefix) {
285:
286: TKBaseField targetField = null;
287: int pLength = prefix.length() + fieldName.length();
288: int fpLength = fieldPath.length();
289:
290: // action betrifft Sub-ELement
291: if ((fpLength != pLength)
292: && (fieldPath.lastIndexOf('.', fpLength - 2) != pLength)) {
293: int idxEnd = fieldPath.indexOf('.', pLength + 1);
294: String idxStr = fieldPath.substring(pLength + 1, idxEnd);
295: int idx = Integer.parseInt(idxStr);
296:
297: targetField = listEntry.getTarget(fieldPath, prefix
298: + fieldName + '.' + idx + '.');
299: }
300:
301: return targetField;
302: }
303:
304: public int insertDataIntoDB(TKContentDBData db, Object data,
305: int contentId, int leftNr) {
306: TKVector dataVector = (TKVector) data;
307: int listSize = dataVector.size();
308:
309: TKContentNodeTableData node = insertNewContentNode(db,
310: contentId, leftNr);
311: int newNodeId = node.content_node_id;
312:
313: insertNewContentValue(db, contentId, newNodeId, 0, String
314: .valueOf(listSize));
315:
316: leftNr++;
317: for (int i = 0; i < listSize; i++) {
318: leftNr = listEntry.insertDataIntoDB(db, dataVector.get(i),
319: contentId, leftNr) + 1;
320: }
321: node.right_nr = leftNr;
322: return leftNr;
323: }
324:
325: public Object getDataFromDB(TKContentDBData db) {
326: TKContentNodeTableData node = getContentNodeFromDB(db);
327: TKContentValueTableData value = getContentNodeValueFromDB(db,
328: node);
329: int size = Integer.parseInt(value.value);
330:
331: if (size == 0)
332: return new TKVector();
333:
334: TKVector result = new TKVector(size);
335: for (int i = 0; i < size; i++) {
336: result.addElement(listEntry.getDataFromDB(db));
337: }
338: return result;
339: }
340:
341: public void clearId() {
342: if (fieldId == -1)
343: return;
344:
345: fieldId = -1;
346: listEntry.clearId();
347: }
348:
349: public int realInsertIntoDB(TKFormDBData db, int formId) {
350: if (super .realInsertIntoDB(db, formId) == -1)
351: return -1;
352:
353: TKSubFieldTableData subFieldDB = insertNewSubField(db, formId,
354: LIST_ENTRY_KEY, 0);
355: listEntry.realInsertIntoDB(db, formId);
356: subFieldDB.sub_field_id = listEntry.fieldId;
357:
358: return fieldId;
359: }
360:
361: public void initFromDB(String classId, TKFormDBData db,
362: TKVector otherFields) throws TKUnregisteredClassException,
363: ClassNotFoundException, InstantiationException,
364: IllegalAccessException {
365: super .initFromDB(classId, db, otherFields);
366: listEntry = getSubField(db, LIST_ENTRY_KEY, 0, otherFields);
367: }
368:
369: /**
370: * Checks wether this object and the specified object
371: * may be treated as equal.
372: *
373: * @param object the object to checked for equality.
374: * @return <CODE>true</CODE> if this object and the
375: * specified object may be treated as equal, otherwise
376: * <CODE>false</CODE>.
377: */
378: public boolean equals(Object object) {
379: if (!super .equals(object)) {
380: return false;
381: }
382:
383: TKFieldList field = (TKFieldList) object;
384:
385: return (this .listEntry == null ? field.listEntry == null
386: : this .listEntry.equals(field.listEntry));
387: }
388:
389: /**
390: * Returns the hash code for this object.
391: *
392: * @return the hash code for this object.
393: */
394: public int hashCode() {
395: // Implementation for JTest only ;-(
396: return super.hashCode();
397: }
398:
399: }
|