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: import org.w3c.dom.*;
009:
010: /**
011: * The field switch list control.
012: *
013: * @author $Author: uli $
014: * @version $Revision: 1.32 $
015: */
016: public class TKFieldSwitchList extends TKBaseField {
017: // $Id: TKFieldSwitchList.java,v 1.32 2002/02/27 11:07:04 uli Exp $
018:
019: /**
020: * The class identifier.
021: */
022: public static final String CLASS_ID = "FIELDSWITCHLIST";
023:
024: /** die eigentliche Erweiterung */
025: TKEmbededSwitch embededSwitch;
026:
027: /**
028: * Creates an empty field switch list control.
029: */
030: public TKFieldSwitchList() {
031: };
032:
033: public TKFieldSwitchList(String name, TKVector alternatives,
034: String showName) {
035: initFieldSwitchList(CLASS_ID, name, alternatives, showName);
036: }
037:
038: public TKFieldSwitchList(String name, TKVector alternatives) {
039: this (name, alternatives, null);
040: }
041:
042: public final void initFieldSwitchList(String type, String name,
043: TKVector alternatives, String showName) {
044: initBaseField(type, name, showName);
045: embededSwitch = new TKEmbededSwitch(alternatives);
046: }
047:
048: public void init(String fieldType, Object data)
049: throws TKUnregisteredClassException,
050: ClassNotFoundException, InstantiationException,
051: IllegalAccessException {
052: super .init(fieldType, data);
053:
054: TKHashtable switchData = (TKHashtable) data;
055: TKVector switchAlternatives = getListOfFields((TKFieldSwitchListData) switchData
056: .get(SUB_LIST_KEY));
057:
058: embededSwitch = new TKEmbededSwitch(switchAlternatives);
059: }
060:
061: /**
062: * Methode zur Definition alternativer Eingabe-Objekte
063: */
064: public TKFieldGroup getDefGroup(TKFieldSwitch allSwitch,
065: TKFieldSwitchList allSwitchList) {
066:
067: // Liste von alternativen Eingabe-Objekten
068: TKBaseField[] switchListArray = {
069: new TKInputField(TKFieldSwitchList.NAME_KEY,
070: TKInputField.SMALL_DEFAULT_SIZE,
071: TKInputField.SMALL_DEFAULT_LENGTH,
072: LanguageManager.getText(LANGUAGE_CONTEXT,
073: "FIELDSWITCH_NAME"),
074: TKInputField.CHECK_STRING),
075: new TKInputField(TKFieldSwitchList.SHOW_NAME_KEY,
076: TKInputField.LARGE_DEFAULT_SIZE,
077: TKInputField.LARGE_DEFAULT_LENGTH,
078: LanguageManager.getText(LANGUAGE_CONTEXT,
079: "FIELDSWITCH_SHOWNAME"),
080: TKInputField.CHECK_STRING), allSwitchList };
081: TKFieldGroup switchListGroup = new TKFieldGroup(
082: TKFieldSwitchList.CLASS_ID, new TKVector(
083: switchListArray), LanguageManager.getText(
084: LANGUAGE_CONTEXT, TKFieldSwitchList.CLASS_ID));
085:
086: return switchListGroup;
087: }
088:
089: //***************************************************************************
090:
091: /**
092: * Returns the internal representation of this field.
093: *
094: * @return the internal representation of this field.
095: */
096: public Object toData() {
097: TKHashtable result = (TKHashtable) super .toData();
098: TKHashtable embededResult = (TKHashtable) embededSwitch
099: .toData();
100: result.put(SUB_LIST_KEY, embededResult.get(SUB_LIST_KEY));
101: return result;
102: }
103:
104: public void addAlternative(TKBaseField alternative) {
105: embededSwitch.addAlternative(alternative);
106: }
107:
108: public void removeAlternative(TKBaseField alternative) {
109: embededSwitch.removeAlternative(alternative);
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: String addAlternative = (String) data.get(prefix
119: + "ADDALTERNATIVE");
120: if (addAlternative == null)
121: addAlternative = "";
122:
123: TKVector result = new TKVector(listEntries);
124: for (int i = 0; i < listEntries; i++) {
125: embededSwitch.setEmbedName(String.valueOf(i));
126: result.addElement(embededSwitch.compileData(prefix, data,
127: context));
128: }
129: return new TKFieldSwitchListData(addAlternative, result);
130: }
131:
132: public Object compileData(String prefix, TKMarkupNode data,
133: TKHashtable context) {
134: /*
135: TKXmlMarkup markup = data == null ? null : (TKXmlMarkup) data.markup;
136: if (markup == null) { return null;}
137:
138: if (!markup.name.equals (getName())) {
139: return null;
140: }
141:
142: TKXmlTree tree = (TKXmlTree) data.tree;
143: if (tree == null) { return null;}
144:
145: TKVector result = new TKVector();
146: prefix += fieldName+".";
147:
148: int currentIndex = 0;
149: for (int i = 0; i < tree.size(); i++) {
150:
151: Object obj = tree.getSub(i);
152: if (obj == null) 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:
166: TKXmlMarkup itemMarkup = null;
167: try {
168: itemMarkup = new TKXmlMarkup("item_"+currentIndex,false,null,-1);
169:
170: } catch (Exception ex) {
171:
172: }
173:
174: TKXmlTree subTree = new TKXmlTree();
175: subTree.addElement (subNode);
176:
177: embededSwitch.setEmbedName( itemMarkup.name );
178:
179: TKMarkupNode compileNode = new TKMarkupNode (itemMarkup,subTree);
180: result.addElement(embededSwitch.compileData( prefix, compileNode, context ));
181: currentIndex++;
182: }
183: */
184: return new TKFieldSwitchListData("", null);
185: }
186:
187: public Object getDefault() {
188: return new TKFieldSwitchListData("", new TKVector());
189: }
190:
191: public Object modify(String action, String fieldPath, Object data,
192: String prefix, StringBuffer destination) {
193: TKFieldSwitchListData switchListData = (TKFieldSwitchListData) data;
194: TKVector dataVector = switchListData.data;
195:
196: //TKHttp.out().println( "fieldPath="+fieldPath+", action="+action+", prefix="+prefix+", fieldName="+fieldName+"<br>" );
197: int pLength = prefix.length() + fieldName.length();
198: int fpLength = fieldPath.length();
199: if (fpLength == pLength) {
200: // action betrifft Liste
201: if (action.equals("ADD")) {
202: dataVector.addElement(embededSwitch
203: .getDefault(switchListData.addAlternative));
204: destination.append(prefix + fieldName + "."
205: + (dataVector.size() - 1) + ".");
206: }
207: } else if (fieldPath.lastIndexOf('.', fpLength - 2) == pLength) {
208: // action betrifft ein Listenelement
209: String idxStr = fieldPath.substring(pLength + 1);
210: int idx = Integer.parseInt(idxStr);
211: if (action.equals("DELETE")) {
212: dataVector.removeElementAt(idx);
213: destination.append(prefix + fieldName);
214: if (dataVector.size() > 0) {
215: destination.append("."
216: + (idx >= dataVector.size() ? dataVector
217: .size() - 1 : idx) + ".");
218: }
219: } else if (action.equals("INSERT")) {
220: TKFieldSwitchData entryData = (TKFieldSwitchData) dataVector
221: .get(idx);
222: dataVector.insertElementAt(embededSwitch
223: .getDefault(entryData.newAlternative), idx);
224: destination.append(prefix + fieldName + "." + idxStr
225: + ".");
226: } else if (action.startsWith("MOVE:")) {
227: int newPos = idx;
228: int total = dataVector.size() - 1;
229: switch (action.charAt(5)) {
230: case '0':
231: newPos = 0;
232: break;
233: case '+':
234: newPos = (idx == total ? total : idx + 1);
235: break;
236: case '-':
237: newPos = (idx == 0 ? 0 : idx - 1);
238: break;
239: case '$':
240: newPos = total;
241: break;
242: default:
243: break;
244: }
245: if (newPos != idx) {
246: Object tmp = dataVector.get(idx);
247: dataVector.removeElementAt(idx);
248: if (newPos == total) {
249: dataVector.addElement(tmp);
250: } else {
251: dataVector.insertElementAt(tmp, newPos);
252: }
253: }
254: destination.append(prefix + fieldName + "." + newPos
255: + ".");
256: } else {
257: embededSwitch.setEmbedName(idxStr);
258: embededSwitch.modify(action, fieldPath, dataVector
259: .get(idx), prefix + fieldName + '.',
260: destination);
261: if (!destination.toString().endsWith(".")) {
262: destination.append(".");
263: }
264: }
265: } else {
266: // action betrifft Sub-ELement
267: int idxEnd = fieldPath.indexOf('.', pLength + 1);
268: String idxStr = fieldPath.substring(pLength + 1, idxEnd);
269: int idx = Integer.parseInt(idxStr);
270: //TKHttp.out().println( "fieldPath="+fieldPath+", prefix="+prefix+", fieldName="+fieldName+", idxStr="+idxStr+"<br>" );
271: embededSwitch.setEmbedName(idxStr);
272: dataVector.put(idx, embededSwitch.modify(action, fieldPath,
273: dataVector.get(idx), prefix + fieldName + '.',
274: destination));
275: }
276: return data;
277: }
278:
279: public TKBaseField getTarget(String fieldPath, String prefix) {
280: TKBaseField targetField = null;
281:
282: int pLength = prefix.length() + fieldName.length();
283: int fpLength = fieldPath.length();
284:
285: // action betrifft Sub-ELement
286: if ((fpLength != pLength)
287: && (fieldPath.lastIndexOf('.', fpLength - 2) != pLength)) {
288:
289: int idxEnd = fieldPath.indexOf('.', pLength + 1);
290: String idxStr = fieldPath.substring(pLength + 1, idxEnd);
291: //TKHttp.out().println( "fieldPath="+fieldPath+", prefix="+prefix+", fieldName="+fieldName+", idxStr="+idxStr+"<br>" );
292: embededSwitch.setEmbedName(idxStr);
293:
294: targetField = embededSwitch.getTarget(fieldPath, prefix
295: + fieldName + '.');
296: }
297: return targetField;
298: }
299:
300: public void fillIntoTemplate(TKHTMLTemplate t, Object data,
301: String prefix) {
302: TKFieldSwitchListData switchListData = (TKFieldSwitchListData) data;
303: TKVector dataVector = switchListData.data;
304:
305: t.set("SIZE", String.valueOf(dataVector.size()));
306:
307: embededSwitch.setEmbedName(fieldName);
308: embededSwitch.fillIntoTemplate(t, new TKFieldSwitchData(
309: switchListData.addAlternative, "", null), prefix);
310:
311: super .fillIntoTemplate(t, data, prefix);
312: t.setListIterator(new TKFieldSwitchListIterator(dataVector,
313: embededSwitch, prefix + fieldName + ".", t
314: .getListIterator(), "SWITCH_LIST"));
315:
316: }
317:
318: public void fillIntoDOM(Document doc, Element node, Object data)
319: throws DOMException {
320: Element me = doc.createElement(getInternationalName());
321: node.appendChild(me);
322: fillAttributesIntoNode(me, data);
323: TKFieldSwitchListData switchListData = (TKFieldSwitchListData) data;
324: TKVector dataVector = switchListData.data;
325: for (int i = 0; i < dataVector.size(); i++) {
326: Object feldData = dataVector.get(i);
327: // template.set( listName+".IDX", String.valueOf(i+1) );
328: embededSwitch.setEmbedName(getName());
329: embededSwitch.fillIntoDOM(doc, me, feldData);
330: }
331: // NICHT FERTIG !!!!!
332: }
333:
334: public void fillAttributesIntoNode(Element node, Object data) {
335: super .fillAttributesIntoNode(node, data);
336: TKFieldSwitchListData switchListData = (TKFieldSwitchListData) data;
337: TKVector dataVector = switchListData.data;
338: node
339: .setAttribute("LENGTH", Integer.toString(dataVector
340: .size()));
341: }
342:
343: public void fillIntoPresentation(TKHTMLTemplate t, Object data,
344: String scope) {
345: TKFieldSwitchListData switchListData = (TKFieldSwitchListData) data;
346: TKVector dataVector = switchListData.data;
347:
348: t.set(scope + "." + getName(), String
349: .valueOf(dataVector.size()));
350: t.setListIterator(new TKSwitchListShowIterator(dataVector,
351: embededSwitch, scope, getName(), t.getListIterator()));
352:
353: }
354:
355: public int insertDataIntoDB(TKContentDBData db, Object data,
356: int contentId, int leftNr) {
357: TKFieldSwitchListData switchListData = (TKFieldSwitchListData) data;
358: TKVector dataVector = (TKVector) switchListData.data;
359: int listSize = dataVector.size();
360:
361: TKContentNodeTableData node = insertNewContentNode(db,
362: contentId, leftNr);
363: int newNodeId = node.content_node_id;
364:
365: insertNewContentValue(db, contentId, newNodeId, 0, String
366: .valueOf(listSize));
367:
368: leftNr++;
369: for (int i = 0; i < listSize; i++) {
370: embededSwitch.setEmbedName(Integer.toString(i));
371: leftNr = embededSwitch.insertDataIntoDB(db, dataVector
372: .get(i), contentId, leftNr) + 1;
373: }
374: node.right_nr = leftNr;
375: return leftNr;
376: }
377:
378: public Object getDataFromDB(TKContentDBData db) {
379: TKContentNodeTableData node = getContentNodeFromDB(db);
380: TKContentValueTableData value = getContentNodeValueFromDB(db,
381: node);
382: if (value.value.equals(""))
383: return new TKFieldSwitchListData("", new TKVector());
384: int size = Integer.parseInt(value.value);
385:
386: if (size == 0)
387: return new TKFieldSwitchListData("", new TKVector());
388:
389: TKVector result = new TKVector(size);
390: for (int i = 0; i < size; i++) {
391: //TKContentNodeTableData item = getContentNodeFromDB( db );
392: embededSwitch.setEmbedName(Integer.toString(i));
393: result.addElement(embededSwitch.getDataFromDB(db));
394: }
395: return new TKFieldSwitchListData("", result);
396: }
397:
398: public void clearId() {
399: if (fieldId == -1)
400: return;
401:
402: fieldId = -1;
403: embededSwitch.clearId();
404: }
405:
406: public int realInsertIntoDB(TKFormDBData db, int formId) {
407: if (super .realInsertIntoDB(db, formId) == -1)
408: return -1;
409: insertNewSubFieldList(db, formId,
410: embededSwitch.ALTERNATIVES_KEY,
411: embededSwitch.alternatives);
412: return fieldId;
413: }
414:
415: public void initFromDB(String classId, TKFormDBData db,
416: TKVector otherFields) throws TKUnregisteredClassException,
417: ClassNotFoundException, InstantiationException,
418: IllegalAccessException {
419: super .initFromDB(classId, db, otherFields);
420: TKVector switchAlternatives = getSubFieldList(db,
421: TKEmbededSwitch.ALTERNATIVES_KEY, otherFields);
422: embededSwitch = new TKEmbededSwitch(switchAlternatives);
423: }
424:
425: /**
426: * Checks wether this object and the specified object
427: * may be treated as equal.
428: *
429: * @param object the object to checked for equality.
430: * @return <CODE>true</CODE> if this object and the
431: * specified object may be treated as equal, otherwise
432: * <CODE>false</CODE>.
433: */
434: public boolean equals(Object object) {
435: if (!super .equals(object)) {
436: return false;
437: }
438:
439: TKFieldSwitchList field = (TKFieldSwitchList) object;
440:
441: return (this .embededSwitch == null ? field.embededSwitch == null
442: : this .embededSwitch.equals(field.embededSwitch));
443: }
444:
445: /**
446: * Returns the hash code for this object.
447: *
448: * @return the hash code for this object.
449: */
450: public int hashCode() {
451: // Implementation for JTest only ;-(
452: return super.hashCode();
453: }
454:
455: }
|