KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quikj > application > utilities > form > XMLFormUser


1 /*
2  * XMLFormUser.java
3  *
4  * Created on June 3, 2002, 9:41 AM
5  */

6
7 package com.quikj.application.utilities.form;
8 import com.quikj.client.beans.*;
9 import java.util.*;
10 import java.awt.*;
11
12 /**
13  *
14  * @author Jack Martin
15  *
16  * XMLFormUser is the class that is used on the "user-client"
17  * to handle user input and collection of data via a submit
18  * button. This class provides the action listners to clear
19  * data and to collect data. The submit action collects the
20  * data and formats the XML string to be passed to the server.
21  *
22  */

23 public class XMLFormUser implements XMLFormListenerInterface
24 {
25     
26     private static String JavaDoc [] awtClasses =
27     { "Checkbox", "Choice", "List",
28       "TextArea", "TextField" };
29       
30       private static final int AWTINDEX_CHECKBOX = 0,
31       AWTINDEX_CHOICE = AWTINDEX_CHECKBOX + 1,
32       AWTINDEX_LIST = AWTINDEX_CHOICE + 1,
33       AWTINDEX_TEXTAREA = AWTINDEX_LIST + 1,
34       AWTINDEX_TEXTFIELD = AWTINDEX_TEXTAREA + 1,
35       AWTINDEX_BUTTON = AWTINDEX_TEXTFIELD + 1;
36       
37       private String JavaDoc awtString, formString;
38       private boolean submitSuccess;
39       private String JavaDoc failString;
40       
41       /** Creates a new instance of XMLFormUser */
42       public XMLFormUser()
43       {
44           super();
45           
46           awtString = "";
47           failString = "";
48           submitSuccess = true;
49       }
50       
51       public boolean submitSuccessful()
52       {
53           return submitSuccess;
54       }
55       
56       public String JavaDoc getMsgString()
57       {
58           return failString;
59       }
60       
61       public void clearMsgString()
62       {
63           failString = "";
64       }
65       
66       /*****************************************************************
67        * resetActionPerformed
68        *
69        * This method handles the processing required when a reset button
70        * is clicked. This method drives the re-setting of all user inputable
71        * fields.
72        *
73        * Inputs: ActionEvent, ArrayList
74        * Returns: None
75        *****************************************************************/

76       public void resetActionPerformed(java.awt.event.ActionEvent JavaDoc evt,
77       XMLArrayList awtItems)
78       {
79           int i, awtType;
80           Object JavaDoc obj;
81           
82           for ( i=0; i<awtItems.size(); i++ )
83           {
84               // for each object in the awtItems array we need to
85
// get it from the array and convert the object to an
86
// integer value and so the appropriate handler can be
87
// called.
88
obj = getItem(i, awtItems);
89               awtType = convertObject(obj);
90               switch ( awtType )
91               {
92                   case AWTINDEX_CHECKBOX:
93                       clearCheckbox((Checkbox)obj);
94                       break;
95                   case AWTINDEX_CHOICE:
96                       clearChoice((Choice)obj);
97                       break;
98                   case AWTINDEX_LIST:
99                       clearList((java.awt.List JavaDoc)obj);
100                       break;
101                   case AWTINDEX_TEXTAREA:
102                       clearTextarea((TextArea)obj);
103                       break;
104                   case AWTINDEX_TEXTFIELD:
105                       clearTextfield((TextField)obj);
106                       break;
107                   default:
108                       break;
109               }
110           }
111       }
112       
113       
114       
115       
116       /*****************************************************************
117        * clearCheckbox
118        *
119        * This method clears a Checkbox field such that no items are
120        * selected. In the process of handling a Checkbox, it must
121        * determine if the Checkbox is a member of a CheckboxGroup
122        * to ensure no items in the group are selected.
123        *
124        * Inputs: Checkbox
125        * Returns: None
126        *****************************************************************/

127       void clearCheckbox(Checkbox cb)
128       {
129           cb.setState(false);
130           CheckboxGroup cbg;
131           cbg = cb.getCheckboxGroup();
132           if ( cbg != null )
133               cbg.setSelectedCheckbox(null);
134       }
135       
136       
137       /*****************************************************************
138        * clearChoice
139        *
140        * This method clears a Choice field.
141        *
142        * Inputs: Choice
143        * Returns: None
144        *****************************************************************/

145       void clearChoice(Choice c)
146       {
147           c.select(0);
148       }
149       
150       /*****************************************************************
151        * clearList
152        *
153        * This method clears a List field. It ensures all members of the
154        * list are de-selected.
155        *
156        * Inputs: List
157        * Returns: None
158        *****************************************************************/

159       void clearList(java.awt.List JavaDoc l)
160       {
161           int [] indexes = l.getSelectedIndexes();
162           for ( int i=0; i<indexes.length; i++ )
163               l.deselect(indexes[i]);
164       }
165       
166       
167       /*****************************************************************
168        * clearTextarea
169        *
170        * This method clears a TextArea field.
171        *
172        * Inputs: TextArea
173        * Returns: None
174        *****************************************************************/

175       void clearTextarea(TextArea ta)
176       {
177           ta.setText("");
178       }
179       
180       /*****************************************************************
181        * clearTextfield
182        *
183        * This method clears a TextField field.
184        *
185        * Inputs: TextField
186        * Returns: None
187        *****************************************************************/

188       void clearTextfield(TextField tf)
189       {
190           tf.setText("");
191       }
192       
193       
194       
195       /*****************************************************************
196        * submitActionPerformed
197        *
198        * This method handles the processing required when a submit button
199        * is clicked. This method drives the collection of all user inputable
200        * fields and the generation of the XML string that is sent to the
201        * host for processing.
202        *
203        * Inputs: ActionEvent
204        * Returns: None
205        *****************************************************************/

206       public void submitActionPerformed(java.awt.event.ActionEvent JavaDoc evt,
207       XMLArrayList awtItems,
208       String JavaDoc formName)
209       {
210           int i, awtType;
211           Object JavaDoc obj;
212           boolean required;
213           
214           submitSuccess = true;
215           awtString = "<form";
216           if ( formName != "" )
217               awtString = awtString + " name=\"" + formName + "\"";
218           
219           awtString += " type=\"response\">\n";
220           
221           for ( i=0; i<awtItems.size(); i++ )
222           {
223               obj = getItem(i, awtItems);
224               required = getRequired(i, awtItems);
225               awtType = convertObject(obj);
226               switch ( awtType )
227               {
228                   case AWTINDEX_CHECKBOX:
229                       getCheckboxInput((Checkbox)obj);
230                       break;
231                   case AWTINDEX_CHOICE:
232                       getChoiceInput((Choice)obj);
233                       break;
234                   case AWTINDEX_LIST:
235                       getListInput((java.awt.List JavaDoc)obj);
236                       break;
237                   case AWTINDEX_TEXTAREA:
238                       getTextareaInput((TextArea)obj, required);
239                       break;
240                   case AWTINDEX_TEXTFIELD:
241                       getTextfieldInput((TextField)obj, required);
242                       break;
243                   default:
244                       break;
245               }
246           }
247           awtString += "</form>";
248           
249           if (submitSuccess == true )
250           {
251               Frame frame = new Frame("Result");
252               new TextAreaDialog(frame, "XML Response", "Response", "Dismiss",
253               awtString);
254               System.exit(0);
255           }
256           
257           return;
258       }
259       
260       
261       void getCheckboxInput(Checkbox cb)
262       {
263           String JavaDoc s;
264           CheckboxGroup cbg;
265           
266           if ( cb.getState() == true )
267           {
268               awtString += "<checkbox";
269               s = cb.getName();
270               if ( s.length() != 0 )
271                   awtString = awtString + " name=\"" + s + "\"";
272               
273               awtString += " selected=\"yes\"/>\n";
274           }
275       }
276       
277       
278       void getChoiceInput(Choice c)
279       {
280           String JavaDoc s;
281           
282           if ( c.getSelectedIndex() != -1 )
283           {
284               awtString += "<choice";
285               s = c.getName();
286               if ( s.length() != 0 )
287                   awtString = awtString + " name=\"" + s + "\"";
288               
289               awtString += ">";
290               awtString += "\n <element selected=\"yes\">";
291               awtString = awtString + c.getSelectedItem() + "</element>";
292               awtString += "\n</choice>\n";
293           }
294       }
295       
296       
297       void getListInput(java.awt.List JavaDoc l)
298       {
299           String JavaDoc [] listItems = l.getSelectedItems();
300           String JavaDoc s;
301           
302           if ( listItems.length > 0 )
303           {
304               awtString += "<list";
305               s = l.getName();
306               if ( s.length() != 0 )
307                   awtString = awtString + " name=\"" + s + "\"";
308               awtString += ">";
309               
310               for ( int i=0; i<listItems.length; i++ )
311               {
312                   awtString += "\n <element selected=\"yes\">";
313                   awtString = awtString + listItems[i] + "</element>";
314               }
315               awtString += "\n</list>\n";
316           }
317       }
318       
319       
320       void getTextareaInput(TextArea ta, boolean required)
321       {
322           String JavaDoc s, name;
323           awtString += "<textarea";
324           name = ta.getName();
325           if ( name.length() != 0 )
326               awtString = awtString + " name=\"" + name + "\"";
327           
328           awtString += ">";
329           s = ta.getText();
330           awtString = awtString + s + "\n</textarea>\n";
331           
332           if ( (s.length() == 0) & (required == true) )
333           {
334               submitSuccess = false;
335               String JavaDoc newline = "";
336               if (failString.length() > 0)
337               {
338                   newline = "\n";
339               }
340               failString = failString + "Text area " + name + " cannot be blank" + newline;
341           }
342           
343       }
344       
345       
346       void getTextfieldInput(TextField tf, boolean required)
347       {
348           String JavaDoc s, name;
349           awtString += "<textfield";
350           name = tf.getName();
351           if ( name.length() != 0 )
352               awtString = awtString + " name=\"" + name + "\"";
353           
354           awtString += ">";
355           s = tf.getText();
356           awtString = awtString + s + "\n</textfield>\n";
357           
358           if ( (s.length() == 0) & (required == true) )
359           {
360               submitSuccess = false;
361               
362               String JavaDoc newline = "";
363               if (failString.length() > 0)
364               {
365                   newline = "\n";
366               }
367               failString = failString + "Text field " + name + " cannot be blank" + newline;
368           }
369       }
370       
371       /*****************************************************************
372        * getItem
373        *
374        * This method will get the awt element that was saved. This is
375        * necessary when a "clear" button is entered and all input fields
376        * on the form need to be cleared. It is also necessary when a
377        * "submit" button is entered and all input needs to be gathered
378        * and sent to the host for processing.
379        *
380        * Inputs: int (index into items array), XMLArrayList
381        * Returns: Object (which really is one of the java.awt elements)
382        *****************************************************************/

383       Object JavaDoc getItem( int index, XMLArrayList awtItems)
384       {
385           return(awtItems.get(index));
386       }
387       
388       /*****************************************************************
389        * getRequired
390        *
391        * This method will get the required flag for awt element that was saved.
392        * This is used to determine if input in a field is required or not.
393        *
394        * Inputs: int (index into items array), XMLArrayList
395        * Returns: Object (which really is one of the java.awt elements)
396        *****************************************************************/

397       boolean getRequired( int index, XMLArrayList awtItems)
398       {
399           return(awtItems.isRequired(index));
400       }
401       
402       
403       /*****************************************************************
404        * convertObject
405        *
406        * This method convert an awt object into an integer value such that
407        * processing of that awt element can be done. This method is
408        * necessary as the awt object is placed into an array if user input
409        * is allowed for that object type (text field for example). When
410        * getting the object from the array it is necessary to know what type
411        * of awt object we are handling.
412        *
413        * Inputs: Object (which really is one of the java.awt elements)
414        * Returns: int (index into awtClass that identifies the type of awt
415        * element found).
416        *****************************************************************/

417       int convertObject(Object JavaDoc obj)
418       {
419           int returnValue = -1, count;
420           StringTokenizer awtType;
421           String JavaDoc objString, classString;
422           
423           objString = obj.getClass().toString();
424           awtType = new StringTokenizer(objString, ". ");
425           count = awtType.countTokens();
426           for ( int i=0; i<count-1; i++)
427               awtType.nextToken();
428           classString = awtType.nextToken();
429           
430           for ( int i=0; i<awtClasses.length; i++ )
431           {
432               if ( classString.equals(awtClasses[i]) )
433               {
434                   returnValue = i;
435                   break;
436               }
437           }
438           
439           return (returnValue);
440       }
441       
442       
443 }
444
Popular Tags