KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quikj > application > web > talk > client > XMLFormUser


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

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

25 public class XMLFormUser implements XMLFormListenerInterface
26 {
27     
28     private static String JavaDoc [] awtClasses =
29     { "Checkbox", "Choice", "List",
30       "TextArea", "TextField" };
31       
32       private static final int AWTINDEX_CHECKBOX = 0,
33       AWTINDEX_CHOICE = AWTINDEX_CHECKBOX + 1,
34       AWTINDEX_LIST = AWTINDEX_CHOICE + 1,
35       AWTINDEX_TEXTAREA = AWTINDEX_LIST + 1,
36       AWTINDEX_TEXTFIELD = AWTINDEX_TEXTAREA + 1,
37       AWTINDEX_BUTTON = AWTINDEX_TEXTFIELD + 1;
38       
39       private StringBuffer JavaDoc awtString = new StringBuffer JavaDoc();
40       private String JavaDoc formString;
41       private boolean submitSuccess;
42       private String JavaDoc failString;
43       private TalkSession session;
44       private CallInfo cinfo;
45       private CallPartyElement clngPartyInfo;
46       private ServerCommunications com;
47       
48       private Locale locale = Locale.US;
49       
50       /** Creates a new instance of XMLFormUser */
51       public XMLFormUser(TalkSession s,
52       CallInfo c,
53       CallPartyElement clng,
54       ServerCommunications c2,
55       Locale locale)
56       {
57           super();
58           
59           if (locale != null)
60           {
61               this.locale = locale;
62           }
63           
64           session = s;
65           cinfo = c;
66           clngPartyInfo = clng;
67           com = c2;
68           failString = "";
69           submitSuccess = true;
70       }
71       
72       public boolean submitSuccessful()
73       {
74           return submitSuccess;
75       }
76       
77       public String JavaDoc getMsgString()
78       {
79           return failString;
80       }
81       
82       public void clearMsgString()
83       {
84           failString = "";
85       }
86       
87       /*****************************************************************
88        * resetActionPerformed
89        *
90        * This method handles the processing required when a reset button
91        * is clicked. This method drives the re-setting of all user inputable
92        * fields.
93        *
94        * Inputs: ActionEvent, ArrayList
95        * Returns: None
96        *****************************************************************/

97       public void resetActionPerformed(java.awt.event.ActionEvent JavaDoc evt,
98       XMLArrayList awtItems)
99       {
100           int i, awtType;
101           Object JavaDoc obj;
102           
103           for ( i=0; i<awtItems.size(); i++ )
104           {
105               // for each object in the awtItems array we need to
106
// get it from the array and convert the object to an
107
// integer value and so the appropriate handler can be
108
// called.
109
obj = getItem(i, awtItems);
110               awtType = convertObject(obj);
111               switch ( awtType )
112               {
113                   case AWTINDEX_CHECKBOX:
114                       clearCheckbox((Checkbox)obj);
115                       break;
116                   case AWTINDEX_CHOICE:
117                       clearChoice((Choice)obj);
118                       break;
119                   case AWTINDEX_LIST:
120                       clearList((java.awt.List JavaDoc)obj);
121                       break;
122                   case AWTINDEX_TEXTAREA:
123                       clearTextarea((TextArea)obj);
124                       break;
125                   case AWTINDEX_TEXTFIELD:
126                       clearTextfield((TextField)obj);
127                       break;
128                   default:
129                       break;
130               }
131           }
132       }
133       
134       
135       
136       
137       /*****************************************************************
138        * clearCheckbox
139        *
140        * This method clears a Checkbox field such that no items are
141        * selected. In the process of handling a Checkbox, it must
142        * determine if the Checkbox is a member of a CheckboxGroup
143        * to ensure no items in the group are selected.
144        *
145        * Inputs: Checkbox
146        * Returns: None
147        *****************************************************************/

148       void clearCheckbox(Checkbox cb)
149       {
150           cb.setState(false);
151           CheckboxGroup cbg;
152           cbg = cb.getCheckboxGroup();
153           if ( cbg != null )
154               cbg.setSelectedCheckbox(null);
155       }
156       
157       
158       /*****************************************************************
159        * clearChoice
160        *
161        * This method clears a Choice field.
162        *
163        * Inputs: Choice
164        * Returns: None
165        *****************************************************************/

166       void clearChoice(Choice c)
167       {
168           c.select(0);
169       }
170       
171       /*****************************************************************
172        * clearList
173        *
174        * This method clears a List field. It ensures all members of the
175        * list are de-selected.
176        *
177        * Inputs: List
178        * Returns: None
179        *****************************************************************/

180       void clearList(java.awt.List JavaDoc l)
181       {
182           int [] indexes = l.getSelectedIndexes();
183           for ( int i=0; i<indexes.length; i++ )
184               l.deselect(indexes[i]);
185       }
186       
187       
188       /*****************************************************************
189        * clearTextarea
190        *
191        * This method clears a TextArea field.
192        *
193        * Inputs: TextArea
194        * Returns: None
195        *****************************************************************/

196       void clearTextarea(TextArea ta)
197       {
198           ta.setText("");
199       }
200       
201       /*****************************************************************
202        * clearTextfield
203        *
204        * This method clears a TextField field.
205        *
206        * Inputs: TextField
207        * Returns: None
208        *****************************************************************/

209       void clearTextfield(TextField tf)
210       {
211           tf.setText("");
212       }
213       
214       
215       
216       /*****************************************************************
217        * submitActionPerformed
218        *
219        * This method handles the processing required when a submit button
220        * is clicked. This method drives the collection of all user inputable
221        * fields and the generation of the XML string that is sent to the
222        * host for processing.
223        *
224        * Inputs: ActionEvent
225        * Returns: None
226        *****************************************************************/

227       public void submitActionPerformed(java.awt.event.ActionEvent JavaDoc evt,
228       XMLArrayList awtItems,
229       String JavaDoc formName)
230       {
231           int i, awtType;
232           Object JavaDoc obj;
233           boolean required;
234           
235           submitSuccess = true;
236           awtString = new StringBuffer JavaDoc();
237           awtString.append("<form");
238           if ( formName != "" )
239           {
240               awtString.append(" name=\""
241               + TalkMessageParser.encodeXMLString(formName) + "\"");
242           }
243           
244           awtString.append(" type=\"response\">\n");
245           
246           for ( i=0; i < awtItems.size(); i++ )
247           {
248               obj = getItem(i, awtItems);
249               required = getRequired(i, awtItems);
250               awtType = convertObject(obj);
251               switch ( awtType )
252               {
253                   case AWTINDEX_CHECKBOX:
254                       getCheckboxInput((Checkbox)obj);
255                       break;
256                   case AWTINDEX_CHOICE:
257                       getChoiceInput((Choice)obj);
258                       break;
259                   case AWTINDEX_LIST:
260                       getListInput((java.awt.List JavaDoc)obj);
261                       break;
262                   case AWTINDEX_TEXTAREA:
263                       getTextareaInput((TextArea)obj, required);
264                       break;
265                   case AWTINDEX_TEXTFIELD:
266                       getTextfieldInput((TextField)obj, required);
267                       break;
268                   default:
269                       break;
270               }
271           }
272           awtString.append("</form>");
273           
274           if (submitSuccess == true )
275           {
276               XMLFormResponseElement userResponse = new XMLFormResponseElement();
277               userResponse.setXmlForm(awtString.toString());
278               formString = userResponse.format();
279               if (cinfo != null)
280               {
281                   if (cinfo.isConnected() == true)
282                   {
283                       String JavaDoc from_name = clngPartyInfo.getUserName();
284                       if (clngPartyInfo.getFullName() != null)
285                       {
286                           if (clngPartyInfo.getFullName().length() > 0)
287                           {
288                               from_name = clngPartyInfo.getFullName();
289                           }
290                       }
291                       
292                       RTPMessage message = new RTPMessage();
293                       message.setSessionId(cinfo.getSessionId());
294                       message.setFrom(from_name);
295                       MediaElements elements = new MediaElements();
296                       elements.addMediaElement(userResponse);
297                       message.setMediaElements(elements);
298                       com.sendRequestMessage("text/xml", message.format());
299                       
300                       session.appendToConversation(from_name + ": ", Color.red, Font.BOLD);
301                       session.appendToConversation("Form data sent.\n", null, -1);
302                   }
303               }
304           }
305           
306           return;
307           
308       }
309       
310       void getCheckboxInput(Checkbox cb)
311       {
312           String JavaDoc s;
313           CheckboxGroup cbg;
314           
315           awtString.append("<checkbox");
316           s = cb.getName();
317           if ( s.length() != 0 )
318           {
319               awtString.append(" name=\""
320               + TalkMessageParser.encodeXMLString(s) + "\"");
321           }
322           awtString.append(">");
323           
324           if (cb.getState() == true )
325           {
326               awtString.append("yes");
327           }
328           else
329           {
330               awtString.append("no");
331           }
332           
333           awtString.append("</checkbox>\n");
334       }
335       
336       
337       void getChoiceInput(Choice c)
338       {
339           String JavaDoc s;
340           
341           if ( c.getSelectedIndex() != -1 )
342           {
343               awtString.append("<choice");
344               s = c.getName();
345               if ( s.length() != 0 )
346               {
347                   awtString.append(" name=\""
348                   + TalkMessageParser.encodeXMLString(s) + "\"");
349               }
350               
351               awtString.append(">"
352               + c.getSelectedItem() + "</choice>\n");
353           }
354       }
355       
356       void getListInput(java.awt.List JavaDoc l)
357       {
358           String JavaDoc [] listItems = l.getSelectedItems();
359           String JavaDoc s;
360           
361           if ( listItems.length > 0 )
362           {
363               for (int i = 0; i < listItems.length; i++ )
364               {
365                   awtString.append("<list");
366                   s = l.getName();
367                   if ( s.length() != 0 )
368                   {
369                       awtString.append(" name=\""
370                       + TalkMessageParser.encodeXMLString(s) + "\"");
371                   }
372                   awtString.append(">"
373                   + TalkMessageParser.encodeXMLString(listItems[i])
374                   + "</list>\n");
375               }
376           }
377       }
378             
379       void getTextareaInput(TextArea ta, boolean required)
380       {
381           String JavaDoc s, name;
382           awtString.append("<textarea");
383           name = ta.getName();
384           if ( name.length() != 0 )
385           {
386               awtString.append(" name=\""
387               + TalkMessageParser.encodeXMLString(name) + "\"");
388           }
389           awtString.append(">");
390           
391           s = ta.getText();
392           awtString.append(TalkMessageParser.encodeXMLString(s) + "</textarea>\n");
393           
394           if ( (s.length() == 0) & (required == true) )
395           {
396               submitSuccess = false;
397               
398               String JavaDoc newline = "";
399               if (failString.length() > 0)
400               {
401                   newline = "\n";
402               }
403               
404               failString = failString
405               + name + ": "
406               + java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.client.language",
407                 locale).getString("cannot_be_empty")
408               + newline;
409           }
410       }
411       
412       void getTextfieldInput(TextField tf, boolean required)
413       {
414           String JavaDoc s, name;
415           awtString.append("<textfield");
416           name = tf.getName();
417           if ( name.length() != 0 )
418           {
419               awtString.append(" name=\""
420               + TalkMessageParser.encodeXMLString(name) + "\"");
421           }
422           
423           awtString.append(">");
424           s = tf.getText();
425           awtString.append(TalkMessageParser.encodeXMLString(s)
426           + "</textfield>\n");
427           
428           if ( (s.length() == 0) & (required == true) )
429           {
430               submitSuccess = false;
431               String JavaDoc newline = "";
432               if (failString.length() > 0)
433               {
434                   newline = "\n";
435               }
436               failString = failString
437               + name + ": "
438               + java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.client.language",
439                 locale).getString("cannot_be_empty")
440               + newline;
441           }
442       }
443       
444       /*****************************************************************
445        * getItem
446        *
447        * This method will get the awt element that was saved. This is
448        * necessary when a "clear" button is entered and all input fields
449        * on the form need to be cleared. It is also necessary when a
450        * "submit" button is entered and all input needs to be gathered
451        * and sent to the host for processing.
452        *
453        * Inputs: int (index into items array), XMLArrayList
454        * Returns: Object (which really is one of the java.awt elements)
455        *****************************************************************/

456       Object JavaDoc getItem( int index, XMLArrayList awtItems)
457       {
458           return(awtItems.get(index));
459       }
460       
461       /*****************************************************************
462        * getRequired
463        *
464        * This method will get the required flag for awt element that was saved.
465        * This is used to determine if input in a field is required or not.
466        *
467        * Inputs: int (index into items array), XMLArrayList
468        * Returns: Object (which really is one of the java.awt elements)
469        *****************************************************************/

470       boolean getRequired( int index, XMLArrayList awtItems)
471       {
472           return(awtItems.isRequired(index));
473       }
474       
475       
476       /*****************************************************************
477        * convertObject
478        *
479        * This method convert an awt object into an integer value such that
480        * processing of that awt element can be done. This method is
481        * necessary as the awt object is placed into an array if user input
482        * is allowed for that object type (text field for example). When
483        * getting the object from the array it is necessary to know what type
484        * of awt object we are handling.
485        *
486        * Inputs: Object (which really is one of the java.awt elements)
487        * Returns: int (index into awtClass that identifies the type of awt
488        * element found).
489        *****************************************************************/

490       int convertObject(Object JavaDoc obj)
491       {
492           int returnValue = -1, count;
493           StringTokenizer awtType;
494           String JavaDoc objString, classString;
495           
496           objString = obj.getClass().toString();
497           awtType = new StringTokenizer(objString, ". ");
498           count = awtType.countTokens();
499           for ( int i=0; i<count-1; i++)
500               awtType.nextToken();
501           classString = awtType.nextToken();
502           
503           for ( int i=0; i<awtClasses.length; i++ )
504           {
505               if ( classString.equals(awtClasses[i]) )
506               {
507                   returnValue = i;
508                   break;
509               }
510           }
511           
512           return (returnValue);
513       }
514       
515       
516 }
517
Popular Tags