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 16 public class TKFieldSwitchList 17 extends TKBaseField 18 { 19 21 24 public static final String CLASS_ID = "FIELDSWITCHLIST"; 25 26 27 TKEmbededSwitch embededSwitch; 28 29 32 public TKFieldSwitchList() {}; 33 34 public TKFieldSwitchList( String name, TKVector alternatives, String showName ) 35 { 36 initFieldSwitchList( CLASS_ID, name, alternatives, showName ); 37 } 38 39 public TKFieldSwitchList( String name, TKVector alternatives ) 40 { 41 this( name, alternatives, null ); 42 } 43 44 public final void initFieldSwitchList( String type, String name, TKVector alternatives, String showName ) 45 { 46 initBaseField( type, name, showName ); 47 embededSwitch = new TKEmbededSwitch( alternatives ); 48 } 49 50 public void init( String fieldType, Object data ) 51 throws 52 TKUnregisteredClassException, 53 ClassNotFoundException , 54 InstantiationException , 55 IllegalAccessException  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 70 public TKFieldGroup getDefGroup(TKFieldSwitch allSwitch, TKFieldSwitchList allSwitchList) { 71 72 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 86 91 public Object 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 compileData( String prefix, TKHashtable data, TKHashtable context ) 110 { 111 prefix += fieldName+"."; 112 113 int listEntries = Integer.parseInt( (String )data.get( prefix+"SIZE" ) ); 114 String addAlternative = (String ) 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 compileData( String prefix, TKMarkupNode data, TKHashtable context ) 128 { 129 179 return new TKFieldSwitchListData( "", null); 180 } 181 182 public Object getDefault() 183 { 184 return new TKFieldSwitchListData( "", new TKVector() ); 185 } 186 187 public Object modify( String action, String fieldPath, Object data, String prefix, StringBuffer destination ) 188 { 189 TKFieldSwitchListData switchListData = (TKFieldSwitchListData) data; 190 TKVector dataVector = switchListData.data; 191 192 int pLength = prefix.length()+fieldName.length(); 194 int fpLength = fieldPath.length(); 195 if ( fpLength == pLength ) { 196 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 String 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 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 int idxEnd = fieldPath.indexOf('.',pLength+1); 263 String idxStr = fieldPath.substring( pLength+1, idxEnd ); 264 int idx = Integer.parseInt( idxStr ); 265 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 fieldPath, String prefix) 276 { 277 TKBaseField targetField = null; 278 279 int pLength = prefix.length()+fieldName.length(); 280 int fpLength = fieldPath.length(); 281 282 if( (fpLength != pLength) && (fieldPath.lastIndexOf('.', fpLength-2) != pLength ) ) { 284 285 int idxEnd = fieldPath.indexOf('.',pLength+1); 286 String idxStr = fieldPath.substring( pLength+1, idxEnd ); 287 embededSwitch.setEmbedName(idxStr); 289 290 targetField = embededSwitch.getTarget(fieldPath, prefix+fieldName+'.'); 291 } 292 return targetField; 293 } 294 295 public void fillIntoTemplate( TKHTMLTemplate t, Object data, String 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 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 feldData = dataVector.get( i ); 331 embededSwitch.setEmbedName( getName() ); 333 embededSwitch.fillIntoDOM( doc, me, feldData); 334 } 335 } 337 338 public void fillAttributesIntoNode(Element node, Object 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 data, String 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 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 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 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 classId, TKFormDBData db, TKVector otherFields ) 417 throws 418 TKUnregisteredClassException, 419 ClassNotFoundException , 420 InstantiationException , 421 IllegalAccessException  422 { 423 super.initFromDB( classId, db, otherFields ); 424 TKVector switchAlternatives = getSubFieldList( db, TKEmbededSwitch.ALTERNATIVES_KEY, otherFields ); 425 embededSwitch = new TKEmbededSwitch( switchAlternatives ); 426 } 427 428 437 public boolean equals (Object 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 454 public int hashCode () 455 { 456 return super.hashCode(); 458 } 459 460 } 461
| Popular Tags
|