KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.teamkonzept.field;
2
3 import java.util.Enumeration JavaDoc;
4 import com.teamkonzept.lib.*;
5 import com.teamkonzept.web.*;
6 import com.teamkonzept.field.db.*;
7 import com.teamkonzept.international.LanguageManager;
8
9 /**
10  * The option field control.
11  *
12  * @author $Author: uli $
13  * @version $Revision: 1.19 $
14  */

15 public abstract class TKOptionField
16     extends TKAtomField
17 {
18     // $Id: TKOptionField.java,v 1.19 2002/02/27 11:07:04 uli Exp $
19

20     public static final String JavaDoc MULTIPLE_KEY = "MULTIPLE";
21     public static final String JavaDoc OPTIONS_KEY = "OPTIONS";
22
23     protected boolean multiple = false;
24
25     protected TKVector optionList = null;
26
27     public TKOptionField() {};
28     protected TKOptionField(
29         String JavaDoc classId,
30         String JavaDoc name,
31         String JavaDoc 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 JavaDoc type,
41         String JavaDoc name,
42         String JavaDoc 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 JavaDoc fieldClass, Object JavaDoc data )
53         throws
54             TKUnregisteredClassException,
55             ClassNotFoundException JavaDoc,
56             InstantiationException JavaDoc,
57             IllegalAccessException JavaDoc
58     {
59         TKHashtable checkData = (TKHashtable) data;
60         super.init( fieldClass, data );
61
62         this.multiple = ((String JavaDoc)checkData.get(MULTIPLE_KEY)).equals("YES");
63         optionList = getListOfOptions( (TKVector) checkData.get(OPTIONS_KEY) );
64     }
65
66     /**
67      * Methode zur Definition eines Optionseldes
68      */

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     /**
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         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 JavaDoc value, String JavaDoc 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 JavaDoc classId, TKFormDBData db, TKVector otherFields )
132         throws
133             TKUnregisteredClassException,
134             ClassNotFoundException JavaDoc,
135             InstantiationException JavaDoc,
136             IllegalAccessException JavaDoc
137     {
138         super.initFromDB( classId, db, otherFields );
139
140         multiple = (new Boolean JavaDoc( 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 JavaDoc option = getFieldAttribute( db, TKOptionFieldEntry.OPTION_KEY, i );
149                 String JavaDoc 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 JavaDoc e = options.elements();
159         while( e.hasMoreElements() ) {
160             TKHashtable option = (TKHashtable) e.nextElement();
161             optionList.addElement( new TKOptionFieldEntry(
162                 (String JavaDoc) option.get(SHOW_NAME_KEY),
163                 (String JavaDoc) 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 JavaDoc 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     /**
184      * Checks wether this object and the specified object
185      * may be treated as equal.
186      *
187      * @param object the object to checked for equality.
188      * @return <CODE>true</CODE> if this object and the
189      * specified object may be treated as equal, otherwise
190      * <CODE>false</CODE>.
191      */

192     public boolean equals (Object JavaDoc 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     /**
206      * Returns the hash code for this object.
207      *
208      * @return the hash code for this object.
209      */

210     public int hashCode ()
211     {
212         // Implementation for JTest only ;-(
213
return super.hashCode();
214     }
215
216 }
217
Popular Tags