1 package com.teamkonzept.field; 2 3 import java.util.Enumeration ; 4 import com.teamkonzept.lib.*; 5 import com.teamkonzept.web.*; 6 import com.teamkonzept.field.db.*; 7 import com.teamkonzept.international.LanguageManager; 8 9 15 public abstract class TKOptionField 16 extends TKAtomField 17 { 18 20 public static final String MULTIPLE_KEY = "MULTIPLE"; 21 public static final String OPTIONS_KEY = "OPTIONS"; 22 23 protected boolean multiple = false; 24 25 protected TKVector optionList = null; 26 27 public TKOptionField() {}; 28 protected TKOptionField( 29 String classId, 30 String name, 31 String showName, 32 TKVector optionList, 33 boolean multiple 34 ) 35 { 36 initOptionField( classId, name, showName, optionList, multiple ); 37 } 38 39 public final void initOptionField ( 40 String type, 41 String name, 42 String showName, 43 TKVector optionList, 44 boolean multiple 45 ) 46 { 47 initAtomField( type, name, showName ); 48 this.optionList = optionList; 49 this.multiple = multiple; 50 } 51 52 public void init( String fieldClass, Object data ) 53 throws 54 TKUnregisteredClassException, 55 ClassNotFoundException , 56 InstantiationException , 57 IllegalAccessException  58 { 59 TKHashtable checkData = (TKHashtable) data; 60 super.init( fieldClass, data ); 61 62 this.multiple = ((String )checkData.get(MULTIPLE_KEY)).equals("YES"); 63 optionList = getListOfOptions( (TKVector) checkData.get(OPTIONS_KEY) ); 64 } 65 66 69 public TKFieldGroup getDefGroup(TKFieldSwitch allSwitch, TKFieldSwitchList allSwitchList) { 70 71 TKVector multipleOptions = new TKVector(2); 72 multipleOptions.addElement( new TKOptionFieldEntry( LanguageManager.getText(LanguageManager.GENERAL, "YES"), "YES" ) ); 73 multipleOptions.addElement( new TKOptionFieldEntry( LanguageManager.getText(LanguageManager.GENERAL, "NO"), "NO" ) ); 74 75 TKBaseField [] optionArray = { 76 new TKInputField( "NAME", TKInputField.SMALL_DEFAULT_SIZE, TKInputField.SMALL_DEFAULT_LENGTH, LanguageManager.getText(LANGUAGE_CONTEXT, "OPTIONFIELD_NAME"), TKInputField.CHECK_STRING), 77 new TKInputField( "SHOWNAME", TKInputField.LARGE_DEFAULT_SIZE, TKInputField.LARGE_DEFAULT_LENGTH, LanguageManager.getText(LANGUAGE_CONTEXT, "OPTIONFIELD_SHOWNAME"), TKInputField.CHECK_STRING) 78 }; 79 80 TKFieldGroup optionGroup = 81 new TKFieldGroup( "OPTION", new TKVector( optionArray ), LanguageManager.getText(LANGUAGE_CONTEXT, "OPTION") ); 82 83 return optionGroup; 84 } 85 86 91 public Object toData () 92 { 93 TKHashtable result = (TKHashtable) super.toData(); 94 result.put( MULTIPLE_KEY, ( multiple ? "YES" : "NO" ) ); 95 result.put( OPTIONS_KEY, getDataOfOptions( optionList ) ); 96 return result; 97 } 98 99 public void fillIntoTemplate( TKHTMLTemplate t, Object value, String prefix ) 100 { 101 super.fillIntoTemplate( t, value, prefix ); 102 103 t.setListIterator( 104 new TKOptionFieldIterator( 105 optionList, 106 fieldName, 107 t.getListIterator(), 108 "OPTION_LIST" 109 ) ); 110 t.set( prefix+fieldName, value ); 111 112 if( multiple ) 113 t.set( prefix+fieldName+".MULTIPLE", Boolean.TRUE ); 114 } 115 116 public int realInsertIntoDB( TKFormDBData db, int formId ) 117 { 118 if( super.realInsertIntoDB( db, formId ) == -1 ) return -1; 119 120 insertNewFieldAttribute( db, formId, MULTIPLE_KEY, 0, String.valueOf( multiple ) ); 121 int osize = optionList.size(); 122 insertNewFieldAttribute( db, formId, OPTIONS_KEY, 0, String.valueOf( osize ) ); 123 for( int i=0; i<osize; i++ ) { 124 TKOptionFieldEntry entry = (TKOptionFieldEntry) optionList.get(i); 125 insertNewFieldAttribute( db, formId, TKOptionFieldEntry.OPTION_KEY, i, entry.option ); 126 insertNewFieldAttribute( db, formId, TKOptionFieldEntry.VALUE_KEY, i, entry.value ); 127 } 128 return fieldId; 129 } 130 131 public void initFromDB( String classId, TKFormDBData db, TKVector otherFields ) 132 throws 133 TKUnregisteredClassException, 134 ClassNotFoundException , 135 InstantiationException , 136 IllegalAccessException  137 { 138 super.initFromDB( classId, db, otherFields ); 139 140 multiple = (new Boolean ( getFieldAttribute( db, MULTIPLE_KEY, 0 ) )).booleanValue(); 141 int size = Integer.parseInt( getFieldAttribute( db, OPTIONS_KEY, 0 ) ); 142 if( size == 0 ) { 143 optionList = new TKVector(); 144 } 145 else { 146 optionList = new TKVector( size ); 147 for( int i=0; i<size; i++ ) { 148 String option = getFieldAttribute( db, TKOptionFieldEntry.OPTION_KEY, i ); 149 String value = getFieldAttribute( db, TKOptionFieldEntry.VALUE_KEY, i ); 150 optionList.addElement( new TKOptionFieldEntry( option, value ) ); 151 } 152 } 153 } 154 155 public static final TKVector getListOfOptions( TKVector options ) 156 { 157 TKVector optionList = new TKVector( options.size() ); 158 Enumeration e = options.elements(); 159 while( e.hasMoreElements() ) { 160 TKHashtable option = (TKHashtable) e.nextElement(); 161 optionList.addElement( new TKOptionFieldEntry( 162 (String ) option.get(SHOW_NAME_KEY), 163 (String ) option.get(NAME_KEY) 164 ) ); 165 } 166 return optionList; 167 } 168 169 public static final TKVector getDataOfOptions( TKVector optionList ) 170 { 171 TKVector options = new TKVector( optionList.size() ); 172 Enumeration e = optionList.elements(); 173 while( e.hasMoreElements() ) { 174 TKOptionFieldEntry entry = (TKOptionFieldEntry) e.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 192 public boolean equals (Object object) 193 { 194 if (! super.equals(object)) 195 { 196 return false; 197 } 198 199 TKOptionField field = (TKOptionField) object; 200 201 return (this.multiple == field.multiple) && 202 (this.optionList == null ? field.optionList == null : this.optionList.equals(field.optionList)); 203 } 204 205 210 public int hashCode () 211 { 212 return super.hashCode(); 214 } 215 216 } 217
| Popular Tags
|