KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > teamkonzept > field > TKFieldSwitchList


1 package com.teamkonzept.field;
2
3 import com.teamkonzept.lib.*;
4 import com.teamkonzept.publishing.markups.*;
5 import com.teamkonzept.web.*;
6 import com.teamkonzept.field.db.*;
7 import com.teamkonzept.international.LanguageManager;
8 import org.w3c.dom.*;
9
10 /**
11  * The field switch list control.
12  *
13  * @author $Author: uli $
14  * @version $Revision: 1.32 $
15  */

16 public class TKFieldSwitchList
17     extends TKBaseField
18 {
19     // $Id: TKFieldSwitchList.java,v 1.32 2002/02/27 11:07:04 uli Exp $
20

21     /**
22      * The class identifier.
23      */

24     public static final String JavaDoc CLASS_ID = "FIELDSWITCHLIST";
25
26     /** die eigentliche Erweiterung */
27     TKEmbededSwitch embededSwitch;
28
29     /**
30      * Creates an empty field switch list control.
31      */

32     public TKFieldSwitchList() {};
33
34     public TKFieldSwitchList( String JavaDoc name, TKVector alternatives, String JavaDoc showName )
35     {
36         initFieldSwitchList( CLASS_ID, name, alternatives, showName );
37     }
38
39     public TKFieldSwitchList( String JavaDoc name, TKVector alternatives )
40     {
41         this( name, alternatives, null );
42     }
43
44     public final void initFieldSwitchList( String JavaDoc type, String JavaDoc name, TKVector alternatives, String JavaDoc showName )
45     {
46         initBaseField( type, name, showName );
47         embededSwitch = new TKEmbededSwitch( alternatives );
48     }
49
50     public void init( String JavaDoc fieldType, Object JavaDoc data )
51         throws
52             TKUnregisteredClassException,
53             ClassNotFoundException JavaDoc,
54             InstantiationException JavaDoc,
55             IllegalAccessException JavaDoc
56     {
57         super.init( fieldType, data );
58
59         TKHashtable switchData = (TKHashtable) data;
60         TKVector switchAlternatives = getListOfFields(
61             (TKFieldSwitchListData) switchData.get( SUB_LIST_KEY )
62         );
63
64         embededSwitch = new TKEmbededSwitch( switchAlternatives );
65     }
66
67     /**
68      * Methode zur Definition alternativer Eingabe-Objekte
69      */

70     public TKFieldGroup getDefGroup(TKFieldSwitch allSwitch, TKFieldSwitchList allSwitchList) {
71
72         // Liste von alternativen Eingabe-Objekten
73
TKBaseField [] switchListArray = {
74             new TKInputField( TKFieldSwitchList.NAME_KEY, TKInputField.SMALL_DEFAULT_SIZE, TKInputField.SMALL_DEFAULT_LENGTH, LanguageManager.getText(LANGUAGE_CONTEXT, "FIELDSWITCH_NAME"), TKInputField.CHECK_STRING),
75             new TKInputField( TKFieldSwitchList.SHOW_NAME_KEY, TKInputField.LARGE_DEFAULT_SIZE, TKInputField.LARGE_DEFAULT_LENGTH, LanguageManager.getText(LANGUAGE_CONTEXT, "FIELDSWITCH_SHOWNAME"), TKInputField.CHECK_STRING),
76             allSwitchList
77         };
78         TKFieldGroup switchListGroup =
79             new TKFieldGroup( TKFieldSwitchList.CLASS_ID, new TKVector( switchListArray ), LanguageManager.getText(LANGUAGE_CONTEXT, TKFieldSwitchList.CLASS_ID) );
80
81         return switchListGroup;
82     }
83
84     //***************************************************************************
85

86     /**
87      * Returns the internal representation of this field.
88      *
89      * @return the internal representation of this field.
90      */

91     public Object JavaDoc toData ()
92     {
93         TKHashtable result = (TKHashtable) super.toData();
94         TKHashtable embededResult = (TKHashtable) embededSwitch.toData();
95         result.put( SUB_LIST_KEY, embededResult.get( SUB_LIST_KEY ) );
96         return result;
97     }
98
99     public void addAlternative (TKBaseField alternative)
100     {
101         embededSwitch.addAlternative(alternative);
102     }
103
104     public void removeAlternative (TKBaseField alternative)
105     {
106         embededSwitch.removeAlternative(alternative);
107     }
108
109     public Object JavaDoc compileData( String JavaDoc prefix, TKHashtable data, TKHashtable context )
110     {
111         prefix += fieldName+".";
112
113         int listEntries = Integer.parseInt( (String JavaDoc)data.get( prefix+"SIZE" ) );
114         String JavaDoc addAlternative = (String JavaDoc) data.get( prefix+"ADDALTERNATIVE" );
115         if( addAlternative == null ) addAlternative = "";
116
117         TKVector result = new TKVector( listEntries );
118         for( int i=0; i< listEntries; i++ ) {
119             embededSwitch.setEmbedName( String.valueOf( i ) );
120             result.addElement(
121                 embededSwitch.compileData( prefix, data, context )
122             );
123         }
124         return new TKFieldSwitchListData( addAlternative, result );
125     }
126
127     public Object JavaDoc compileData( String JavaDoc prefix, TKMarkupNode data, TKHashtable context )
128     {
129         /*
130         TKXmlMarkup markup = data == null ? null : (TKXmlMarkup) data.markup;
131         if (markup == null) { return null;}
132
133         if (!markup.name.equals (getName())) {
134             return null;
135         }
136
137         TKXmlTree tree = (TKXmlTree) data.tree;
138         if (tree == null) { return null;}
139
140         TKVector result = new TKVector();
141         prefix += fieldName+".";
142
143         int currentIndex = 0;
144         for (int i = 0; i < tree.size(); i++) {
145
146             Object obj = tree.getSub(i);
147             if (obj == null) continue;
148
149             TKMarkupNode subNode = null;
150             TKXmlMarkup subMarkup = null;
151
152             if (obj instanceof TKXmlMarkup) { // Atomares Markup
153
154                 subMarkup = (TKXmlMarkup) obj;
155                 subNode = new TKMarkupNode (subMarkup,null);
156             } else {
157                 subNode = (TKMarkupNode) obj;
158                 subMarkup = (TKXmlMarkup) subNode.markup;
159             }
160
161             TKXmlMarkup itemMarkup = null;
162             try {
163                 itemMarkup = new TKXmlMarkup("item_"+currentIndex,false,null,-1);
164
165             } catch (Exception ex) {
166
167             }
168
169             TKXmlTree subTree = new TKXmlTree();
170             subTree.addElement (subNode);
171
172             embededSwitch.setEmbedName( itemMarkup.name );
173
174             TKMarkupNode compileNode = new TKMarkupNode (itemMarkup,subTree);
175             result.addElement(embededSwitch.compileData( prefix, compileNode, context ));
176             currentIndex++;
177         }
178         */

179         return new TKFieldSwitchListData( "", null);
180     }
181
182     public Object JavaDoc getDefault()
183     {
184         return new TKFieldSwitchListData( "", new TKVector() );
185     }
186
187     public Object JavaDoc modify( String JavaDoc action, String JavaDoc fieldPath, Object JavaDoc data, String JavaDoc prefix, StringBuffer JavaDoc destination )
188     {
189         TKFieldSwitchListData switchListData = (TKFieldSwitchListData) data;
190         TKVector dataVector = switchListData.data;
191
192         //TKHttp.out().println( "fieldPath="+fieldPath+", action="+action+", prefix="+prefix+", fieldName="+fieldName+"<br>" );
193
int pLength = prefix.length()+fieldName.length();
194         int fpLength = fieldPath.length();
195         if ( fpLength == pLength ) {
196             // action betrifft Liste
197
if( action.equals("ADD") ) {
198                 dataVector.addElement( embededSwitch.getDefault(switchListData.addAlternative) );
199                 destination.append( prefix+fieldName+"."+(dataVector.size()-1)+"." );
200             }
201         }
202         else if( fieldPath.lastIndexOf('.', fpLength-2) == pLength ) {
203             // action betrifft ein Listenelement
204
String JavaDoc idxStr = fieldPath.substring( pLength+1 );
205             int idx = Integer.parseInt( idxStr );
206             if( action.equals( "DELETE" ) ) {
207                 dataVector.removeElementAt( idx );
208                 destination.append( prefix+fieldName );
209                 if( dataVector.size() > 0 )
210                 {
211                     destination.append( "."+(idx>=dataVector.size()?dataVector.size()-1:idx)+"." );
212                 }
213             }
214             else if( action.equals( "INSERT" ) ) {
215                 TKFieldSwitchData entryData = (TKFieldSwitchData) dataVector.get( idx );
216                 dataVector.insertElementAt( embededSwitch.getDefault(entryData.newAlternative), idx );
217                 destination.append( prefix+fieldName+"."+idxStr+"." );
218             }
219             else if( action.startsWith( "MOVE:" ) ){
220                 int newPos = idx;
221                 int total = dataVector.size()-1;
222                 switch( action.charAt(5) ) {
223                     case '0' :
224                         newPos = 0; break;
225                     case '+' :
226                         newPos = (idx==total?total:idx+1); break;
227                     case '-' :
228                         newPos = (idx==0?0:idx-1); break;
229                     case '$' :
230                         newPos = total; break;
231                     default:
232                         break;
233                 }
234                 if( newPos != idx ) {
235                     Object JavaDoc tmp = dataVector.get( idx );
236                     dataVector.removeElementAt( idx );
237                     if( newPos == total ) {
238                         dataVector.addElement( tmp );
239                     }
240                     else {
241                         dataVector.insertElementAt( tmp, newPos );
242                     }
243                 }
244                 destination.append( prefix+fieldName+"."+newPos+"." );
245             }
246             else {
247                 embededSwitch.setEmbedName( idxStr );
248                 embededSwitch.modify(
249                     action,
250                     fieldPath,
251                     dataVector.get( idx ),
252                     prefix+fieldName+'.',
253                     destination
254                 );
255                 if( !destination.toString().endsWith(".") ) {
256                     destination.append( "." );
257                 }
258             }
259         }
260         else {
261             // action betrifft Sub-ELement
262
int idxEnd = fieldPath.indexOf('.',pLength+1);
263             String JavaDoc idxStr = fieldPath.substring( pLength+1, idxEnd );
264             int idx = Integer.parseInt( idxStr );
265             //TKHttp.out().println( "fieldPath="+fieldPath+", prefix="+prefix+", fieldName="+fieldName+", idxStr="+idxStr+"<br>" );
266
embededSwitch.setEmbedName(idxStr);
267             dataVector.put( idx, embededSwitch.modify(
268                 action, fieldPath, dataVector.get( idx ),
269                 prefix+fieldName+'.', destination
270             ) );
271         }
272         return data;
273     }
274
275     public TKBaseField getTarget(String JavaDoc fieldPath, String JavaDoc prefix)
276     {
277         TKBaseField targetField = null;
278
279         int pLength = prefix.length()+fieldName.length();
280         int fpLength = fieldPath.length();
281
282         // action betrifft Sub-ELement
283
if( (fpLength != pLength) && (fieldPath.lastIndexOf('.', fpLength-2) != pLength ) ) {
284
285             int idxEnd = fieldPath.indexOf('.',pLength+1);
286             String JavaDoc idxStr = fieldPath.substring( pLength+1, idxEnd );
287             //TKHttp.out().println( "fieldPath="+fieldPath+", prefix="+prefix+", fieldName="+fieldName+", idxStr="+idxStr+"<br>" );
288
embededSwitch.setEmbedName(idxStr);
289
290             targetField = embededSwitch.getTarget(fieldPath, prefix+fieldName+'.');
291         }
292         return targetField;
293     }
294
295     public void fillIntoTemplate( TKHTMLTemplate t, Object JavaDoc data, String JavaDoc prefix )
296     {
297         TKFieldSwitchListData switchListData = (TKFieldSwitchListData) data;
298         TKVector dataVector = switchListData.data;
299
300         t.set( "SIZE", String.valueOf( dataVector.size() ) );
301
302         embededSwitch.setEmbedName( fieldName );
303         embededSwitch.fillIntoTemplate(
304             t,
305             new TKFieldSwitchData( switchListData.addAlternative, "", null ),
306             prefix
307         );
308
309         super.fillIntoTemplate( t, data, prefix );
310         t.setListIterator(
311             new TKFieldSwitchListIterator(
312                 dataVector,
313                 embededSwitch,
314                 prefix+fieldName+".",
315                 t.getListIterator(),
316                 "SWITCH_LIST"
317         ) );
318
319     }
320
321     public void fillIntoDOM(Document doc, Element node, Object JavaDoc data) throws DOMException
322     {
323         Element me = doc.createElement(getInternationalName());
324         node.appendChild(me);
325         fillAttributesIntoNode(me, data);
326         TKFieldSwitchListData switchListData = (TKFieldSwitchListData) data;
327         TKVector dataVector = switchListData.data;
328         for (int i=0; i < dataVector.size(); i++)
329         {
330             Object JavaDoc feldData = dataVector.get( i );
331             // template.set( listName+".IDX", String.valueOf(i+1) );
332
embededSwitch.setEmbedName( getName() );
333             embededSwitch.fillIntoDOM( doc, me, feldData);
334         }
335         // NICHT FERTIG !!!!!
336
}
337
338     public void fillAttributesIntoNode(Element node, Object JavaDoc data)
339     {
340         super.fillAttributesIntoNode(node, data);
341         TKFieldSwitchListData switchListData = (TKFieldSwitchListData) data;
342         TKVector dataVector = switchListData.data;
343         node.setAttribute("LENGTH", Integer.toString(dataVector.size()));
344     }
345
346     public void fillIntoPresentation( TKHTMLTemplate t, Object JavaDoc data, String JavaDoc scope )
347     {
348         TKFieldSwitchListData switchListData = (TKFieldSwitchListData) data;
349         TKVector dataVector = switchListData.data;
350
351         t.set( scope+"."+getName(), String.valueOf( dataVector.size() ) );
352         t.setListIterator( new TKSwitchListShowIterator(
353             dataVector,
354             embededSwitch,
355             scope,
356             getName(),
357             t.getListIterator()
358         ) );
359
360     }
361
362     public int insertDataIntoDB( TKContentDBData db, Object JavaDoc data, int contentId, int leftNr )
363     {
364         TKFieldSwitchListData switchListData = (TKFieldSwitchListData) data;
365         TKVector dataVector = (TKVector) switchListData.data;
366         int listSize = dataVector.size();
367
368         TKContentNodeTableData node = insertNewContentNode( db, contentId, leftNr );
369         int newNodeId = node.content_node_id;
370
371         insertNewContentValue( db, contentId, newNodeId, 0, String.valueOf( listSize ) );
372
373         leftNr++;
374         for( int i=0; i<listSize; i++ ) {
375             embededSwitch.setEmbedName(Integer.toString(i));
376             leftNr = embededSwitch.insertDataIntoDB( db, dataVector.get( i ), contentId, leftNr )+1;
377         }
378         node.right_nr = leftNr;
379         return leftNr;
380     }
381
382     public Object JavaDoc getDataFromDB( TKContentDBData db )
383     {
384         TKContentNodeTableData node = getContentNodeFromDB( db );
385         TKContentValueTableData value = getContentNodeValueFromDB( db, node );
386         if (value.value.equals(""))
387             return new TKFieldSwitchListData( "", new TKVector() );
388         int size = Integer.parseInt( value.value );
389
390         if( size == 0 ) return new TKFieldSwitchListData( "", new TKVector() );
391
392         TKVector result = new TKVector( size );
393         for( int i=0; i<size; i++ ) {
394             //TKContentNodeTableData item = getContentNodeFromDB( db );
395
embededSwitch.setEmbedName(Integer.toString(i));
396             result.addElement( embededSwitch.getDataFromDB( db ) );
397         }
398         return new TKFieldSwitchListData( "", result );
399     }
400
401     public void clearId()
402     {
403         if( fieldId == -1 ) return;
404
405         fieldId = -1;
406         embededSwitch.clearId();
407     }
408
409     public int realInsertIntoDB( TKFormDBData db, int formId )
410     {
411         if( super.realInsertIntoDB( db, formId ) == -1 ) return -1;
412         insertNewSubFieldList( db, formId, embededSwitch.ALTERNATIVES_KEY, embededSwitch.alternatives );
413         return fieldId;
414     }
415
416     public void initFromDB( String JavaDoc classId, TKFormDBData db, TKVector otherFields )
417         throws
418             TKUnregisteredClassException,
419             ClassNotFoundException JavaDoc,
420             InstantiationException JavaDoc,
421             IllegalAccessException JavaDoc
422     {
423         super.initFromDB( classId, db, otherFields );
424         TKVector switchAlternatives = getSubFieldList( db, TKEmbededSwitch.ALTERNATIVES_KEY, otherFields );
425         embededSwitch = new TKEmbededSwitch( switchAlternatives );
426     }
427
428     /**
429      * Checks wether this object and the specified object
430      * may be treated as equal.
431      *
432      * @param object the object to checked for equality.
433      * @return <CODE>true</CODE> if this object and the
434      * specified object may be treated as equal, otherwise
435      * <CODE>false</CODE>.
436      */

437     public boolean equals (Object JavaDoc object)
438     {
439         if (! super.equals(object))
440         {
441             return false;
442         }
443
444         TKFieldSwitchList field = (TKFieldSwitchList) object;
445
446         return (this.embededSwitch == null ? field.embededSwitch == null : this.embededSwitch.equals(field.embededSwitch));
447     }
448
449     /**
450      * Returns the hash code for this object.
451      *
452      * @return the hash code for this object.
453      */

454     public int hashCode ()
455     {
456         // Implementation for JTest only ;-(
457
return super.hashCode();
458     }
459
460 }
461
Popular Tags