KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quikj > client > beans > XMLForm


1 /*
2  * XMLForm.java
3  *
4  * Created on May 10, 2002, 1:48 AM
5  */

6
7 /*
8  * XML Specification
9  *
10  *
11  * The form request is sent using the following XML message
12  *
13  * <form name="name" type="request">
14  * <frame attr .....> ! Top-level frame
15  * <panel layoutmgr="gridbag"> ! currently, gridbag is only one supported
16  * <layout attr ..../> ! Optional, only required for child panels
17  * <panel attr ......>
18  * </panel>
19  * ....
20  * ....
21  * <widget attr..........>
22  * <layout attr....../>
23  * <element attr ...> ..... </element>
24  * </widget>
25  * .....
26  * .....
27  * </panel>
28  * </frame>
29  * </form>
30  *
31  * Frame attributes:
32  * <frame background=color
33  * enabled="yes","no"
34  * fontname=fontnamevalue
35  * fontstyle=fontstylevalue
36  * fontsize= a numeric value
37  * foreground=color
38  * name=text string
39  * position="x,y" ! x, y coordinates for upper left corner
40  * resizable="yes","no"
41  * size="w,h" ! w = width, h = height
42  * title=text string
43  * all dialog attributes are optional
44  *
45  * Panel attributes:
46  * <Panel layoutmgr="gridbag" ! only one currently supported
47  * background=color
48  * enabled="yes","no"
49  * fontname=fontnamevalue
50  * fontstyle=fontstylevalue
51  * fontsize= a numeric value
52  * foreground=color
53  * name=text string
54  * visible="yes", "no"
55  * all dialog attributes are optional
56  *
57  * Layout attributes:
58  * <layout anchor=anchor_value
59  * fill=fill_value
60  * gridh=gridheightvalue
61  * gridw=gridwidthvalue
62  * gridx=gridxvalue
63  * gridy=gridyvalue
64  * insets=insetvalue
65  * intpadx=padxvalue
66  * intpady=padyvalue
67  * weightx=weightxvalue
68  * weighty=weightyvalue
69  *
70  *
71  * where anchor_value= "north", "northeast", "northwest", "center",
72  * "east", "south", "southeast", "southwest",
73                         "west"
74  * color = "0-255,0-255,0-255" (ex: "255,124,63")
75  * echocharvalue = single character to use for echoing
76  * fill_value = "none", "both", "horizontal", "vertical"
77  * fontnamevalue = "Dialog", "DialogInput", "Sans Serif", "Serif",
78  * "Monospaced", "Symbol"
79  * fontstylevalue = "Plain", "Bold", "Italic", "Plain | Italic",
80  * "Bold | Italic"
81  * gridheightvalue = a numeric value or "remainder"
82  * gridwidthvalue = a numeric value or "remainder"
83  * gridxvalue = a numeric value or "relative"
84  * gridyvalue = a numeric value or "relative"
85  * insetvalue = colon separated top:bottom:left:right
86  * padxvalue = a numeric value
87  * padyvalue = a numeric value
88  * size_value = "w,h" ! width, height
89  * weightvalue = a numeric value representing percentage
90  * weightvalue = a numeric value representing percentage
91  *
92  *
93  * Widget layout attributes:
94  *
95  * where widget = textfield, textarea, list, checkbox, submitbutton
96  * resetbutton, label, panel, choice, radiobutton
97  * attr = depends on the widget
98  * element = depends on the widget
99  *
100  
101  * Widget Definition
102  * ------------------
103  * <textfield attr ...>
104  * <layout attr .../>
105  * <value>initial value of the text field</value> <-- optional
106  * </textfield>
107  *
108  * Textfield attributes:
109  * <textfield background=color
110  * columns= a numeric value representing column width
111  * echochar=echocharvalue
112  * editable= "yes", "no"
113  * enabled="yes","no"
114  * fontname=fontnamevalue
115  * fontstyle=fontstylevalue
116  * fontsize= a numeric value
117  * foreground=color
118  * name=text string
119  * visible="yes", "no"
120  * all textfield attributes are optional
121  *
122  ***********************************************************************
123  * <textarea attr ...>
124  * <layout attr .../>
125  * <value>initial value of the text area</value> <-- optional
126  * </textarea>
127  *
128  * Textarea attributes:
129  * <textarea background=color
130  * columns= a numeric value representing column width
131  * editable= "yes", "no"
132  * enabled="yes","no"
133  * fontname=fontnamevalue
134  * fontstyle=fontstylevalue
135  * fontsize= a numeric value
136  * foreground=color
137  * name=text string
138  * rows= a numeric value representing number of rows
139  * scrollbar="None", "Vertical", "Horizontal", "Both"
140  * visible="yes", "no"
141  * all textarea attributes are optional
142  *
143  ***********************************************************************
144  * <list attr ...>
145  * <layout attr .../>
146  * <element selected="yes"!"no">the name of the element</element>
147  * .....
148  * </list>
149  *
150  * selected = "yes", "no" is optional for the element tag
151  *
152  * List attributes:
153  * <list background=color
154  * enabled="yes","no"
155  * fontname=fontnamevalue
156  * fontstyle=fontstylevalue
157  * fontsize= a numeric value
158  * foreground=color
159  * multiplemode="yes", "no"
160  * name=text string
161  * visible="yes", "no"
162  * all list attributes are optional
163  *
164  ***********************************************************************
165  * <choice attr ...>
166  * <layout attr .../>
167  * <element selected="yes"!"no">the name of the element</element>
168  * .....
169  * </choice>
170  *
171  * selected = "yes", "no" is optional for the element tag
172  *
173  * Choice attributes:
174  * <choice background=color
175  * enabled="yes","no"
176  * fontname=fontnamevalue
177  * fontstyle=fontstylevalue
178  * fontsize= a numeric value
179  * foreground=color
180  * name=text string
181  * all choice attributes are optional
182  *
183  ***********************************************************************
184  * <checkbox attr ... >
185  * <layout attr .../>
186  * </checkbox>
187  *
188  * Checkbox attributes:
189  * <checkbox background=color
190  * radiogroup=text string
191  * enabled="yes","no"
192  * fontname=fontnamevalue
193  * fontstyle=fontstylevalue
194  * fontsize= a numeric value
195  * foreground=color
196  * label=text string
197  * name=text string
198  * selected="yes", "no"
199  * visible="yes", "no"
200  * all checkbox attributes are optional
201  *
202  ************************************************************************
203  * <label attr ...>text label
204  * <layout attr .../>
205  * </label>
206  *
207  * Label attributes:
208  * <label alignment="left", "right", "center"
209  * background=color
210  * enabled="yes","no"
211  * fontname=fontnamevalue
212  * fontstyle=fontstylevalue
213  * fontsize= a numeric value
214  * foreground=color
215  * name=text string
216  * visible="yes", "no"
217  * all label attributes are optional
218  *
219  ************************************************************************
220  * <submitbutton attr ... >
221  * <layout attr .../>
222  * </submit>
223  *
224  * Submit Button attributes:
225  * <submitbutton background=color
226  * enabled="yes","no"
227  * fontname=fontnamevalue
228  * fontstyle=fontstylevalue
229  * fontsize= a numeric value
230  * foreground=color
231  * label=text string
232  * name=text string
233  * visible="yes", "no"
234  * all submitbutton attributes are optional
235  *
236  ************************************************************************
237  * <resetbutton attr ... >
238  * <layout attr .../>
239  * </resetbutton>
240  *
241  * Reset Button attributes:
242  * <resetbutton background=color
243  * enabled="yes","no"
244  * fontname=fontnamevalue
245  * fontstyle=fontstylevalue
246  * fontsize= a numeric value
247  * foreground=color
248  * label=text string
249  * name=text string
250  * visible="yes", "no"
251  * all submitbutton attributes are optional
252  ************************************************************************
253  * <radiobuttongroup name="group name" />
254  *
255  * name is the only attribute for radiobutton and it is required.
256  *
257  *************************************************************************
258  *
259  * When the user, clicks on the "submit button", the form response is sent
260  * as follows:
261  *
262  * <form name="name" type="response">
263  * <name value="value"/>
264  * ....
265  * ....
266  * </form>
267  *
268  * where name = name of the widget
269  * value = entered value. In case of a textfield/textarea, it is the
270  * entered text. For checkbox, it is either yes or no. For list
271  * it is either the name of the selected item or "" if no item
272  * is selected.
273  */

274
275
276
277 package com.quikj.client.beans;
278
279 import java.awt.*;
280 import net.n3.nanoxml.*;
281 import java.util.*;
282
283 /**
284  *
285  * @author Jack Martin
286  *
287  * XMLForm is the class that drives display of a user defined
288  * form. Helper classes are used to assist in parsing of the
289  * XML file.
290  *
291  */

292
293 public class XMLForm extends java.awt.Frame JavaDoc
294 {
295     private XMLFormListenerInterface listener = null;
296     private ParentFrameInterface parent;
297     
298     private Object JavaDoc parm = null;
299     
300     private static final String JavaDoc[] nodes =
301     { "form", "frame", "panel", "textfield",
302       "textarea", "list", "checkbox", "submitbutton",
303       "resetbutton", "label", "choice", "layout",
304       "element", "value", "radiobuttongroup" };
305       public static final int NODEINDEX_FORM = 0,
306       NODEINDEX_FRAME = NODEINDEX_FORM + 1,
307       NODEINDEX_PANEL = NODEINDEX_FRAME + 1,
308       NODEINDEX_TEXTFIELD = NODEINDEX_PANEL + 1,
309       NODEINDEX_TEXTAREA = NODEINDEX_TEXTFIELD + 1,
310       NODEINDEX_LIST = NODEINDEX_TEXTAREA + 1,
311       NODEINDEX_CHECKBOX = NODEINDEX_LIST + 1,
312       NODEINDEX_SUBMITBUTTON = NODEINDEX_CHECKBOX + 1,
313       NODEINDEX_RESETBUTTON = NODEINDEX_SUBMITBUTTON + 1,
314       NODEINDEX_LABEL = NODEINDEX_RESETBUTTON + 1,
315       NODEINDEX_CHOICE = NODEINDEX_LABEL + 1,
316       NODEINDEX_LAYOUT = NODEINDEX_CHOICE + 1,
317       NODEINDEX_ELEMENT = NODEINDEX_LAYOUT + 1,
318       NODEINDEX_VALUE = NODEINDEX_ELEMENT + 1,
319       NODEINDEX_RADIOBUTTONGROUP = NODEINDEX_VALUE + 1,
320       NODEINDEXES = nodes.length;
321       
322       private Vector valueField, elementField;
323       private int elementSelected, frameLocationX, frameLocationY, frameSizeW, frameSizeH;
324       
325       private Stack XMLconstraints;
326       private boolean layoutFound;
327       
328       private Hashtable rbgTable;
329       private XMLArrayList awtItems;
330       public String JavaDoc formName;
331       
332       private Locale locale = Locale.US;
333       
334       /** Creates a new instance of XMLForm */
335       public XMLForm(IXMLElement node,
336       XMLFormListenerInterface listener,
337       Object JavaDoc parm,
338       Locale locale,
339       ParentFrameInterface parent)
340       throws XMLFormException
341       {
342           super();
343           
344           this.parent = parent;
345           if (parent != null)
346           {
347               parent.addToFrameList(this);
348           }
349           
350           if (locale != null)
351           {
352             this.locale = locale;
353           }
354           
355           this.listener = listener;
356           this.parm = parm;
357           layoutFound = false;
358           valueField = new Vector();
359           elementField = new Vector();
360           elementSelected = 0;
361           rbgTable = new Hashtable();
362           awtItems = new XMLArrayList();
363           frameLocationX = 200;
364           frameLocationY = 200;
365           frameSizeW = 300;
366           frameSizeH = 400;
367           beginForm(node);
368       }
369       
370       public XMLForm(IXMLElement node,
371       XMLFormListenerInterface listener,
372       Locale locale)
373       throws XMLFormException
374       {
375           this(node, listener, null, locale, null);
376       }
377             
378       /*****************************************************************
379        * beginForm
380        *
381        * This method is the main driver for the XML form parsing
382        * machine. It setsup the initial form, ensures the first element
383        * is <form> and calls the method that will drive parsing of the
384        * data. Once the entire XML form has been parse this method will
385        * display the form.
386        *
387        * Inputs: IXMLElement - XML data to parse
388        * Returns: None
389        *****************************************************************/

390       void beginForm(IXMLElement node)
391       throws XMLFormException
392       {
393           
394           String JavaDoc elementName;
395           elementName = node.getName();
396           
397           setLayout(new GridBagLayout());
398           setBackground(Color.white);
399           setTitle("XML Form");
400           addWindowListener(new java.awt.event.WindowAdapter JavaDoc()
401           {
402               public void windowClosing(java.awt.event.WindowEvent JavaDoc evt)
403               {
404                   closeDialog(evt);
405               }
406           });
407           
408           if ( elementName.equals("form") )
409               handleForm(node, this);
410           else
411               throw new XMLFormException("First element found in XML Form is not <form>");
412           
413           show();
414           toFront();
415           
416       }
417       
418       
419       
420       /*****************************************************************
421        * handleForm
422        *
423        * This method handles the <form ...> element which should be the first
424        * line in the XML form string. It will setup the framwork for the
425        * display. The format is:
426        * <form name="form name" type="request">
427        *
428        * Since it is the first element, all other elements are considered
429        * children and therefore this method is the main driver of the
430        * parsing.
431        *
432        * Note: The parsing methods must be recursive in order to
433        * handle grandchildren within children, etc ...
434        *
435        * Inputs: IXMLElement, Container
436        * Returns: None
437        *****************************************************************/

438       void handleForm(IXMLElement node, Container parent)
439       throws XMLFormException
440       {
441           XMLconstraints = new Stack();
442           Vector enumChildren = node.getChildren();
443           IXMLElement childNode;
444           int childCount, childIndex;
445           XMLAttributes formAttrs = new XMLAttributes();
446           
447           // process the atrributes from the <form ...> definition
448
formAttrs.parseAttributes(node, NODEINDEX_FORM);
449           
450           if ( formAttrs.nameFound == true )
451               formName = formAttrs.attrName;
452           else
453               formName = "";
454           
455           // the type attribute should be = request
456
if ( formAttrs.typeFound == true )
457           {
458               if ( formAttrs.attrType.equals("request") )
459               {
460                   // process all the children
461
childCount = node.getChildrenCount();
462                   childIndex = 0;
463                   while ( childCount > 0 )
464                   {
465                       childNode = (IXMLElement)enumChildren.elementAt(childIndex);
466                       processChild(childNode.getName(), childNode, parent);
467                       childIndex++;
468                       childCount--;
469                   }
470               }
471               else
472                   throw new XMLFormException("Form type is not \"request\": " + formAttrs.attrType);
473           }
474           else
475               throw new XMLFormException("Form type not found.");
476           
477           return;
478       }
479       
480       
481       /*****************************************************************
482        * processChild
483        *
484        * This method is the main processor for the children nodes.
485        * It will validate the child name against the table of allowable
486        * nodes and call the appropriate method to process the data associated
487        * with the node. This method must be recursive as children within
488        * children are possible.
489        *
490        * Inputs: String, IXMLElement, Container
491        * where String is the child name
492        * Returns: None
493        *****************************************************************/

494       void processChild(String JavaDoc childName, IXMLElement childNode, Container parent)
495       throws XMLFormException
496       {
497           int childIndex = -1;
498           
499    /* a null childName indicates PCDATA only.
500     * An example that results in a null childname is:
501     * <sample attr1="a">
502     * <child1 attr1="b"/>sample pcdata
503     * </sample>
504     *
505     * In this sample the number of children is 2. Child number
506     * 1 is named "child1". Child number 2 name is null but it
507     * contains the pcdata string "sample pcdata".
508     */

509           
510           if ( childName == null )
511               valueField.addElement(childNode.getContent());
512           else
513           {
514               // validate the childName against the allowable node values
515
for ( int i=0; i<nodes.length; i++ )
516               {
517                   if ( nodes[i].equals(childName) )
518                   {
519                       childIndex = i;
520                       break;
521                   }
522               }
523               switch ( childIndex )
524               {
525                   case NODEINDEX_FRAME:
526                       handleFrame(childNode, parent);
527                       break;
528                   case NODEINDEX_PANEL:
529                       handlePanel(childNode, parent);
530                       break;
531                   case NODEINDEX_TEXTFIELD:
532                       handleTextfield(childNode, parent);
533                       break;
534                   case NODEINDEX_TEXTAREA:
535                       handleTextarea(childNode, parent);
536                       break;
537                   case NODEINDEX_LIST:
538                       handleList(childNode, parent);
539                       break;
540                   case NODEINDEX_CHECKBOX:
541                       handleCheckbox(childNode, parent);
542                       break;
543                   case NODEINDEX_SUBMITBUTTON:
544                       handleSubmitbutton(childNode, parent);
545                       break;
546                   case NODEINDEX_RESETBUTTON:
547                       handleResetbutton(childNode, parent);
548                       break;
549                   case NODEINDEX_CHOICE:
550                       handleChoice(childNode, parent);
551                       break;
552                   case NODEINDEX_LABEL:
553                       handleLabel(childNode, parent);
554                       break;
555                   case NODEINDEX_LAYOUT:
556                       handleLayout(childNode, parent);
557                       break;
558                   case NODEINDEX_ELEMENT:
559                       handleElement(childNode, parent);
560                       break;
561                   case NODEINDEX_VALUE:
562                       handleValue(childNode, parent);
563                       break;
564                   case NODEINDEX_RADIOBUTTONGROUP:
565                       handleRadiobuttongroup(childNode, parent);
566                       break;
567                   default:
568                       throw new XMLFormException("Unknown child found: " + childName);
569               }
570           }
571       }
572       
573       
574       /*****************************************************************
575        * handleFrame
576        *
577        * This method handles the Frame element. The format is:
578        * <frame attr1="data" attr2="data" ...>
579        * <child ...>
580        * <child ...>
581        * </frame>
582        *
583        * The allowable attributes for the frame element are:
584        * background, enabled, fontname, fontstyle, fontsize,
585        * foreground, name, position, resizable, size, and title.
586        *
587        * The frame element can have the following children:
588        * panel, textfield, textarea, list, checkbox, submitbutton
589        * resetbutton, label, choice, layout, radiobutton
590        *
591        * Inputs: IXMLElement, Container
592        * Returns: None
593        *****************************************************************/

594       void handleFrame(IXMLElement node, Container parent)
595       throws XMLFormException
596       {
597           Vector enumChildren = node.getChildren();
598           IXMLElement childNode;
599           int attrIndex, childCount, childIndex;
600           XMLAttributes frameAttrs = new XMLAttributes();
601           
602           // parse and validate the attr elements within the <frame> tag
603
frameAttrs.parseAttributes(node, NODEINDEX_FRAME);
604           
605           // set the fields based on the attributes defined in the
606
// <frame> tag. Defaults are used for some fields if
607
// their value was not specified in an attribute.
608

609           setBackground(new Color(frameAttrs.attrBackgroundR,
610           frameAttrs.attrBackgroundG,
611           frameAttrs.attrBackgroundB));
612           setForeground(new Color(frameAttrs.attrForegroundR,
613           frameAttrs.attrForegroundG,
614           frameAttrs.attrForegroundB));
615           setEnabled(frameAttrs.attrEnabled);
616           setFont(new Font(frameAttrs.attrFontname,
617           frameAttrs.attrFontstyle,
618           frameAttrs.attrFontsize));
619           setResizable(frameAttrs.attrResizable);
620           
621           if ( frameAttrs.nameFound == true )
622               setName(frameAttrs.attrName);
623           if ( frameAttrs.titleFound == true )
624               setTitle(frameAttrs.attrTitle);
625           
626           setLocation(frameAttrs.attrPositionX, frameAttrs.attrPositionY);
627           frameLocationX = frameAttrs.attrPositionX;
628           frameLocationY = frameAttrs.attrPositionY;
629           
630           setSize(frameAttrs.attrSizeW, frameAttrs.attrSizeH);
631           frameSizeW = frameAttrs.attrSizeW;
632           frameSizeH = frameAttrs.attrSizeH;
633           
634           GridBagConstraints dummy = parseChildren(parent, node);
635           
636           return;
637       }
638       
639       
640       
641       
642       /*****************************************************************
643        * handlePanel
644        *
645        * This method handles the Panel element. The format is:
646        * <panel attr1="data" attr2="data" ...>
647        * <child ...>
648        * <child ...>
649        * </panel>
650        *
651        * The allowable attributes for the panel element are:
652        * background, enabled, fontname, fontstyle, fontsize,
653        * foreground, layoutmgr, name, and visible.
654        *
655        * The panel element can have the following children:
656        * layout, textfield, textarea, list, checkbox, submitbutton,
657        * resetbutton, label, choice, radiobutton, panel
658        *
659        * Inputs: IXMLElement, Container
660        * Returns: None
661        *****************************************************************/

662       void handlePanel(IXMLElement node, Container parent)
663       throws XMLFormException
664       {
665           Panel p = new Panel( new GridBagLayout());
666           GridBagConstraints pConstraints;
667           XMLAttributes panelAttrs = new XMLAttributes();
668           
669           // parse and validate the attr elements within the <panel> tag.
670
panelAttrs.parseAttributes(node, NODEINDEX_PANEL);
671           
672           p.setBackground(new Color(panelAttrs.attrBackgroundR,
673           panelAttrs.attrBackgroundG,
674           panelAttrs.attrBackgroundB));
675           p.setForeground(new Color(panelAttrs.attrForegroundR,
676           panelAttrs.attrForegroundG,
677           panelAttrs.attrForegroundB));
678           p.setEnabled(panelAttrs.attrEnabled);
679           p.setFont(new Font(panelAttrs.attrFontname,
680           panelAttrs.attrFontstyle,
681           panelAttrs.attrFontsize));
682           p.setVisible(panelAttrs.attrVisible);
683           
684           if ( panelAttrs.nameFound == true )
685               p.setName(panelAttrs.attrName);
686           
687           pConstraints = parseChildren(p, node);
688           parent.add(p, pConstraints);
689           
690       }
691       
692       
693       
694       /*****************************************************************
695        * handleTextfield
696        *
697        * This method handles the Textfield element. The format is:
698        * <textfield attr1="data" attr2="data" ...>
699        * <layout attr .../>
700        * <value>text string</value>
701        * </textfield>
702        *
703        * The allowable attributes for the textfield element are:
704        * background, caretposition, columns, echochar,
705        * editable, enabled, fontname, fontstyle, fontsize,
706        * foreground, name, selectionstart, selectionend, and visible.
707        *
708        * The textfield element can have the following optional children:
709        * value, layout
710        *
711        * Inputs: IXMLElement, Container
712        * Returns:
713        *****************************************************************/

714       void handleTextfield(IXMLElement node, Container parent)
715       throws XMLFormException
716       {
717           GridBagConstraints tfConstraints;
718           TextField tf = new TextField();
719           XMLAttributes tfAttrs = new XMLAttributes();
720           
721           // parse and validate the attr elements within the <textfield> tag.
722
tfAttrs.parseAttributes(node, NODEINDEX_TEXTFIELD);
723           
724           tf.setBackground(new Color(tfAttrs.attrBackgroundR,
725           tfAttrs.attrBackgroundG,
726           tfAttrs.attrBackgroundB));
727           tf.setForeground(new Color(tfAttrs.attrForegroundR,
728           tfAttrs.attrForegroundG,
729           tfAttrs.attrForegroundB));
730           tf.setEnabled(tfAttrs.attrEnabled);
731           tf.setFont(new Font(tfAttrs.attrFontname,
732           tfAttrs.attrFontstyle,
733           tfAttrs.attrFontsize));
734           tf.setEditable(tfAttrs.attrEditable);
735           tf.setVisible(tfAttrs.attrVisible);
736           tf.setColumns(tfAttrs.attrColumns);
737           
738           if ( tfAttrs.echoFound == true )
739               tf.setEchoChar(tfAttrs.attrEchochar);
740           if ( tfAttrs.nameFound == true )
741               tf.setName(tfAttrs.attrName);
742           
743           tfConstraints = parseChildren(parent, node);
744           
745           if ( valueField.size() > 0 )
746           {
747               tf.setText((String JavaDoc)valueField.elementAt(0));
748               valueField.removeAllElements();
749           }
750           
751           parent.add(tf, tfConstraints);
752           registerItem(tf, tfAttrs.attrRequired);
753       }
754       
755       
756       /*****************************************************************
757        * handleTextarea
758        * This method handles the Textarea element. The format is:
759        * <textarea attr1="data" attr2="data" ...>
760        * <layout attr .../>
761        * <value>text string</value>
762        * </textfield>
763        *
764        * The allowable attributes for the textarea element are:
765        * background, caretposition, columns, editable,
766        * enabled, fontname, fontstyle, fontsize, foreground,
767        * name, rows, scrollbar, selectionstart, selectionend,
768        * and visible.
769        *
770        * The textarea element can have the following optional children:
771        * value, layout
772        *
773        * Inputs: IXMLElement, Container
774        * Returns: None
775        *****************************************************************/

776       void handleTextarea(IXMLElement node, Container parent)
777       throws XMLFormException
778       {
779           GridBagConstraints taConstraints;
780           XMLAttributes taAttrs = new XMLAttributes();
781           
782           // parse and validate the attr elements within the <textarea> tag.
783
taAttrs.parseAttributes(node, NODEINDEX_TEXTAREA);
784           
785           TextArea ta = new TextArea(null, 0, 0, taAttrs.attrScrollbar);
786           ta.setBackground(new Color(taAttrs.attrBackgroundR,
787           taAttrs.attrBackgroundG,
788           taAttrs.attrBackgroundB));
789           ta.setForeground(new Color(taAttrs.attrForegroundR,
790           taAttrs.attrForegroundG,
791           taAttrs.attrForegroundB));
792           ta.setEnabled(taAttrs.attrEnabled);
793           ta.setFont(new Font(taAttrs.attrFontname,
794           taAttrs.attrFontstyle,
795           taAttrs.attrFontsize));
796           ta.setEditable(taAttrs.attrEditable);
797           ta.setVisible(taAttrs.attrVisible);
798           ta.setColumns(taAttrs.attrColumns);
799           
800           if ( taAttrs.attrRows != -1 )
801             ta.setRows(taAttrs.attrRows);
802           else
803             ta.setRows(3);
804           
805           if ( taAttrs.nameFound == true )
806               ta.setName(taAttrs.attrName);
807           
808           // process all the children
809
taConstraints = parseChildren(parent, node);
810           if ( valueField.size() > 0 )
811           {
812               ta.setText((String JavaDoc)valueField.elementAt(0));
813               valueField.removeAllElements();
814           }
815           
816           parent.add(ta, taConstraints);
817           registerItem(ta, taAttrs.attrRequired);
818       }
819       
820       
821       
822       /*****************************************************************
823        * handleList
824        *
825        * This method handles the List element. The format is:
826        * <list attr1="data" attr2="data" ...>
827        * <layout attr .../>
828        * <element>text string</element>
829        * <element>text string</element>
830        * </list>
831        *
832        * The allowable attributes for the list element are:
833        * background, enabled, fontname, fontstyle,
834        * fontsize, foreground, multiplemode, name,
835        * and visible.
836        *
837        * The list element can have the following children (at least one
838        * <element> required):
839        * element, layout
840        *
841        * Inputs: IXMLElement, Container
842        * Returns: None
843        *****************************************************************/

844       void handleList(IXMLElement node, Container parent)
845       throws XMLFormException
846       {
847           int rows = 1;
848           GridBagConstraints lConstraints;
849           XMLAttributes lAttrs = new XMLAttributes();
850           
851           // parse and validate the attr elements within the <list> tag.
852

853           lAttrs.parseAttributes(node, NODEINDEX_LIST);
854           if ( lAttrs.attrRows != -1 )
855               rows = lAttrs.attrRows;
856           
857           java.awt.List JavaDoc l = new java.awt.List JavaDoc(rows);
858           l.setBackground(new Color(lAttrs.attrBackgroundR,
859           lAttrs.attrBackgroundG,
860           lAttrs.attrBackgroundB));
861           l.setForeground(new Color(lAttrs.attrForegroundR,
862           lAttrs.attrForegroundG,
863           lAttrs.attrForegroundB));
864           l.setEnabled(lAttrs.attrEnabled);
865           l.setFont(new Font(lAttrs.attrFontname,
866           lAttrs.attrFontstyle,
867           lAttrs.attrFontsize));
868           l.setVisible(lAttrs.attrVisible);
869           l.setMultipleMode(lAttrs.attrMultiplemode);
870           if ( lAttrs.nameFound == true )
871               l.setName(lAttrs.attrName);
872           
873           // process all the children
874
lConstraints = parseChildren(this, node);
875           
876           for (int i = 0; i < elementField.size(); i++)
877               l.add((String JavaDoc)elementField.elementAt(i));
878           
879           elementField.removeAllElements();
880           l.select(elementSelected);
881           elementSelected = 0;
882           parent.add(l, lConstraints);
883           registerItem(l, false);
884       }
885       
886       
887       
888       /*****************************************************************
889        * handleCheckbox
890        *
891        * This method handles the Checkbox element. The format is:
892        * <checkbox attr1="data" attr2="data" ...>
893        * <layout attr .../>
894        * </checkbox>
895        *
896        * The allowable attributes for the checkbox element are:
897        * background, enabled, fontname, fontstyle, fontsize,
898        * foreground, label, name, selectedobjects, radiogroup,
899        * selected, and visible.
900        *
901        * The checkbox element can have the following optional children:
902        * layout
903        *
904        * Inputs: IXMLElement, Container
905        * Returns: None
906        *****************************************************************/

907       void handleCheckbox(IXMLElement node, Container parent)
908       throws XMLFormException
909       {
910           GridBagConstraints cbConstraints;
911           Checkbox cb = new Checkbox();
912           XMLAttributes cbAttrs = new XMLAttributes();
913           
914           // parse and validate the attr elements within the <checkbox> tag.
915
cbAttrs.parseAttributes(node, NODEINDEX_CHECKBOX);
916           
917           cb.setBackground(new Color(cbAttrs.attrBackgroundR,
918           cbAttrs.attrBackgroundG,
919           cbAttrs.attrBackgroundB));
920           cb.setForeground(new Color(cbAttrs.attrForegroundR,
921           cbAttrs.attrForegroundG,
922           cbAttrs.attrForegroundB));
923           cb.setEnabled(cbAttrs.attrEnabled);
924           cb.setFont(new Font(cbAttrs.attrFontname,
925           cbAttrs.attrFontstyle,
926           cbAttrs.attrFontsize));
927           cb.setVisible(cbAttrs.attrVisible);
928           
929           
930           if ( cbAttrs.nameFound == true )
931               cb.setName(cbAttrs.attrName);
932           if ( cbAttrs.labelFound == true )
933               cb.setLabel(cbAttrs.attrLabel);
934           if ( cbAttrs.radioGroupFound == true )
935           {
936               CheckboxGroup chkboxgrp;
937               chkboxgrp = (CheckboxGroup)rbgTable.get(cbAttrs.attrRadiogroup);
938               cb.setCheckboxGroup(chkboxgrp);
939               if (cbAttrs.attrSelected == true )
940                   chkboxgrp.setSelectedCheckbox(cb);
941           }
942           else
943               cb.setState(cbAttrs.attrSelected);
944           
945           // process all the children
946
cbConstraints = parseChildren(this, node);
947           parent.add(cb, cbConstraints);
948           registerItem(cb, false);
949       }
950       
951       
952       
953       /*****************************************************************
954        * handleSubmitbutton
955        *
956        * This method handles the Submitbutton element. The format is:
957        * <submitbutton attr1="data" attr2="data" ...>
958        * <layout attr .../>
959        * </submitbutton>
960        *
961        * The allowable attributes for the submitbutton element are:
962        * background, enabled, fontname, fontstyle, fontsize,
963        * foreground, label, name, and visible.
964        *
965        * The submitbutton element can have the following optional children:
966        * layout
967        *
968        * Inputs: IXMLElement, Container
969        * Returns: None
970        *****************************************************************/

971       void handleSubmitbutton(IXMLElement node, Container parent)
972       throws XMLFormException
973       {
974           GridBagConstraints sbConstraints;
975           Button sb = new Button();
976           XMLAttributes sbAttrs = new XMLAttributes();
977           
978           // parse and validate the attr elements within the <submit> tag.
979
sbAttrs.parseAttributes(node, NODEINDEX_SUBMITBUTTON);
980           
981           sb.setBackground(new Color(sbAttrs.attrBackgroundR,
982           sbAttrs.attrBackgroundG,
983           sbAttrs.attrBackgroundB));
984           sb.setForeground(new Color(sbAttrs.attrForegroundR,
985           sbAttrs.attrForegroundG,
986           sbAttrs.attrForegroundB));
987           sb.setEnabled(sbAttrs.attrEnabled);
988           sb.setFont(new Font(sbAttrs.attrFontname,
989           sbAttrs.attrFontstyle,
990           sbAttrs.attrFontsize));
991           sb.setVisible(sbAttrs.attrVisible);
992           sb.addActionListener(new java.awt.event.ActionListener JavaDoc()
993           {
994               public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt)
995               {
996                   XMLsubmitAction(evt);
997               }
998           });
999           
1000          if ( sbAttrs.nameFound == true )
1001              sb.setName(sbAttrs.attrName);
1002          if ( sbAttrs.labelFound == true )
1003              sb.setLabel(sbAttrs.attrLabel);
1004          else
1005              sb.setLabel("Submit");
1006          
1007          // process all the children
1008
sbConstraints = parseChildren(this, node);
1009          parent.add(sb, sbConstraints);
1010          
1011      }
1012      
1013      
1014      
1015      /*****************************************************************
1016       * handleResetbutton
1017       *
1018       * This method handles the Resetbutton element. The format is:
1019       * <resetbutton attr1="data" attr2="data" ...>
1020       * <layout attr .../>
1021       * </resetbutton>
1022       *
1023       * The allowable attributes for the resetbutton element are:
1024       * background, enabled, fontname, fontstyle, fontsize,
1025       * foreground, label, name, and visible.
1026       *
1027       * The resetbutton element can have the following optional children:
1028       * layout
1029       *
1030       * Inputs:
1031       * Returns:
1032       *****************************************************************/

1033      void handleResetbutton(IXMLElement node, Container parent)
1034      throws XMLFormException
1035      {
1036          GridBagConstraints rbConstraints;
1037          Button rb = new Button();
1038          XMLAttributes rbAttrs = new XMLAttributes();
1039          
1040          // parse and validate the attr elements within the <resetbutton> tag.
1041
rbAttrs.parseAttributes(node, NODEINDEX_RESETBUTTON);
1042          
1043          rb.setBackground(new Color(rbAttrs.attrBackgroundR,
1044          rbAttrs.attrBackgroundG,
1045          rbAttrs.attrBackgroundB));
1046          rb.setForeground(new Color(rbAttrs.attrForegroundR,
1047          rbAttrs.attrForegroundG,
1048          rbAttrs.attrForegroundB));
1049          rb.setEnabled(rbAttrs.attrEnabled);
1050          rb.setFont(new Font(rbAttrs.attrFontname,
1051          rbAttrs.attrFontstyle,
1052          rbAttrs.attrFontsize));
1053          rb.setVisible(rbAttrs.attrVisible);
1054          rb.addActionListener(new java.awt.event.ActionListener JavaDoc()
1055          {
1056              public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt)
1057              {
1058                  listener.resetActionPerformed(evt, awtItems); }
1059          });
1060          
1061          if ( rbAttrs.nameFound == true )
1062              rb.setName(rbAttrs.attrName);
1063          if ( rbAttrs.labelFound == true )
1064              rb.setLabel(rbAttrs.attrLabel);
1065          else
1066              rb.setLabel("Reset");
1067          
1068          // process all the children
1069
rbConstraints = parseChildren(this, node);
1070          parent.add(rb, rbConstraints);
1071          
1072      }
1073      
1074      
1075      
1076      /*****************************************************************
1077       * handleChoice
1078       *
1079       * This method handles the choice element. The format is:
1080       * <choice attr1="data" attr2="data" ...>
1081       * <layout attr .../>
1082       * <element>text string</element>
1083       * <element>text string</element>
1084       * </choice>
1085       *
1086       * The allowable attributes for the choice element are:
1087       * background, enabled, fontname, fontstyle, fontsize,
1088       * foreground, and name.
1089       *
1090       * The choice element can have the following children (at least one
1091       * <element> required):
1092       * element, layout
1093       *
1094       * Inputs: IXMLElement, Container
1095       * Returns: None
1096       *****************************************************************/

1097      void handleChoice(IXMLElement node, Container parent)
1098      throws XMLFormException
1099      {
1100          GridBagConstraints cConstraints;
1101          Choice c = new Choice();
1102          XMLAttributes cAttrs = new XMLAttributes();
1103          
1104          // parse and validate the attr elements within the <choice> tag.
1105
cAttrs.parseAttributes(node, NODEINDEX_CHOICE);
1106          
1107          c.setBackground(new Color(cAttrs.attrBackgroundR,
1108          cAttrs.attrBackgroundG,
1109          cAttrs.attrBackgroundB));
1110          c.setForeground(new Color(cAttrs.attrForegroundR,
1111          cAttrs.attrForegroundG,
1112          cAttrs.attrForegroundB));
1113          c.setEnabled(cAttrs.attrEnabled);
1114          c.setFont(new Font(cAttrs.attrFontname,
1115          cAttrs.attrFontstyle,
1116          cAttrs.attrFontsize));
1117          if ( cAttrs.nameFound == true)
1118              c.setName(cAttrs.attrName);
1119          
1120          // process all the children
1121
cConstraints = parseChildren(this, node);
1122          
1123          for (int i = 0; i < elementField.size(); i++)
1124              c.add((String JavaDoc)elementField.elementAt(i));
1125          
1126          elementField.removeAllElements();
1127          c.select(elementSelected);
1128          elementSelected = 0;
1129          parent.add(c, cConstraints);
1130          registerItem(c, false);
1131          
1132      }
1133      
1134      
1135      
1136      /*****************************************************************
1137       * handleLabel
1138       *
1139       * This method handles the label element. The format is:
1140       * <label attr1="data" attr2="data" ...>
1141       * <layout attr .../>
1142       * text string
1143       * </label>
1144       *
1145       * The allowable attributes for the label element are:
1146       * alignment, background, enabled, fontname, fontstyle, fontsize,
1147       * foreground, name, and visible.
1148       *
1149       * The label element must have a text string and can have the following
1150       * optional children:
1151       * layout
1152       *
1153       * Inputs: IXMLElement, Container
1154       * Returns: None
1155       *****************************************************************/

1156      void handleLabel(IXMLElement node, Container parent)
1157      throws XMLFormException
1158      {
1159          Label fl = new Label();
1160          GridBagConstraints flConstraints;
1161          XMLAttributes flAttrs = new XMLAttributes();
1162          
1163          // parse and validate the attr elements within the <label> tag.
1164

1165          flAttrs.parseAttributes(node, NODEINDEX_LABEL);
1166          
1167          fl.setBackground(new Color(flAttrs.attrBackgroundR,
1168          flAttrs.attrBackgroundG,
1169          flAttrs.attrBackgroundB));
1170          fl.setForeground(new Color(flAttrs.attrForegroundR,
1171          flAttrs.attrForegroundG,
1172          flAttrs.attrForegroundB));
1173          fl.setEnabled(flAttrs.attrEnabled);
1174          fl.setFont(new Font(flAttrs.attrFontname,
1175          flAttrs.attrFontstyle,
1176          flAttrs.attrFontsize));
1177          fl.setVisible(flAttrs.attrVisible);
1178          
1179          if ( flAttrs.nameFound == true )
1180              fl.setName(flAttrs.attrName);
1181          if ( flAttrs.attrAlignment != -1 )
1182              fl.setAlignment(flAttrs.attrAlignment);
1183          
1184          // process all the children
1185
flConstraints = parseChildren(this, node);
1186          if ( valueField.size() > 0 )
1187          {
1188              fl.setText((String JavaDoc)valueField.elementAt(0));
1189              valueField.removeAllElements();
1190          }
1191          parent.add(fl, flConstraints);
1192      }
1193      
1194      
1195      
1196      /*****************************************************************
1197       * handleLayout
1198       *
1199       * This method handles the layout element. The format is:
1200       * <layout attr .../>
1201       * In order to handle children within children, all of which can
1202       * have the <layout> tag, the parsed data is pushed onto a
1203       * stack. The calling method can then pop the elements from
1204       * the stack when appropriate.
1205       *
1206       * The allowable attributes for the label element are:
1207       * anchor, fill, gridh, gridw, gridx, gridy, insets, intpadx,
1208       * intpady, weightx, weighty
1209       *
1210       * The layout element does not have children.
1211       *
1212       * Inputs: IXMLElement, Container
1213       * Returns: None directly but does push GridBagConstraints onto a
1214       * stack.
1215       *****************************************************************/

1216      void handleLayout(IXMLElement node, Container parent)
1217      throws XMLFormException
1218      {
1219          
1220          XMLFormLayout layoutConstraints = new XMLFormLayout();
1221          
1222          try
1223          {
1224              layoutConstraints.parseLayout(node);
1225              layoutFound = true;
1226              XMLconstraints.push(layoutConstraints);
1227          }
1228          catch (XMLFormException exc)
1229          {
1230              throw new XMLFormException("Error parsing Layout element.");
1231          }
1232          
1233      }
1234      
1235      
1236      /*****************************************************************
1237       * handleRadiobuttongroup
1238       *
1239       * This method handles the radiobutton element. The format is:
1240       * <radiobuttongroup name="group name"/>
1241       *
1242       * The radiogroup element has no children.
1243       *
1244       * Inputs: IXMLElement, Container
1245       * Returns: None
1246       *****************************************************************/

1247      void handleRadiobuttongroup(IXMLElement node, Container parent)
1248      throws XMLFormException
1249      {
1250          XMLAttributes rbgrpAttrs = new XMLAttributes();
1251          
1252          // make sure there are no children
1253
if ( node.getChildrenCount() == 0 )
1254          {
1255              rbgrpAttrs.parseAttributes(node, NODEINDEX_RADIOBUTTONGROUP);
1256              if ( rbgrpAttrs.nameFound == true)
1257              {
1258                  CheckboxGroup rbgrp = new CheckboxGroup();
1259                  rbgTable.put(rbgrpAttrs.attrName, rbgrp);
1260              }
1261              
1262          }
1263          else
1264              throw new XMLFormException("Children found in Radiogroup element.");
1265          
1266      }
1267      
1268      
1269      /*****************************************************************
1270       * handleElement
1271       *
1272       * This method handles the element element. The format is:
1273       * <element selected="yes"!"no">data</element>
1274       * There are no attributes for the <element> element.
1275       *
1276       * In order to handle multiple <element>'s within a tag, a
1277       * stack has been implemented. All <element>'s within a
1278       * parent are stacked for use by the parent processing method.
1279       *
1280       * The <element> element does not have children but does contain
1281       * PCDATA.
1282       *
1283       * Inputs: IXMLElement, Container
1284       * Returns: None
1285       *****************************************************************/

1286      void handleElement(IXMLElement node, Container parent)
1287      throws XMLFormException
1288      {
1289          XMLAttributes eAttrs = new XMLAttributes();
1290          
1291          // process the atrributes from the <form ...> definition
1292
eAttrs.parseAttributes(node, NODEINDEX_ELEMENT);
1293          if ( node.getChildrenCount() == 0 )
1294          {
1295              elementField.addElement(node.getContent());
1296              if (eAttrs.attrSelected == true)
1297                  elementSelected = elementField.size() - 1;
1298          }
1299          else
1300              throw new XMLFormException("Children found in <element>");
1301          
1302      }
1303      
1304      
1305      /*****************************************************************
1306       * handleValue
1307       *
1308       * This method handles the value element. The format is:
1309       * <value>data</value>
1310       *
1311       * There are no attributes for the value element.
1312       *
1313       * In order to handle multiple <value>'s within a tag, a
1314       * stack has been implemented. All <value>'s within a
1315       * parent are stacked for use by the parent processing method.
1316       *
1317       * The value element does not have children but does contain
1318       * PCDATA.
1319       *
1320       * Inputs: IXMLElement, Container
1321       * Returns: None
1322       *****************************************************************/

1323      void handleValue(IXMLElement node, Container parent)
1324      throws XMLFormException
1325      {
1326          if ( node.getAttributeCount() == 0)
1327              if ( node.getChildrenCount() == 0 )
1328                  valueField.addElement(node.getContent());
1329              else
1330                  throw new XMLFormException("Children found in <value>");
1331          else
1332              throw new XMLFormException("Attributes found in <value>");
1333      }
1334      
1335      
1336      /*****************************************************************
1337       * parseChildren
1338       *
1339       * This method is used to drive the parsing of the children elements.
1340       *
1341       * Inputs: Container, IXMLElement
1342       * Returns: GridBagConstraints
1343       *****************************************************************/

1344      GridBagConstraints parseChildren(Container parent, IXMLElement node)
1345      throws XMLFormException
1346      {
1347          int childCount, childIndex;
1348          boolean defaultConstraints = true;
1349          GridBagConstraints itemConstraints;
1350          Vector enumChildren = node.getChildren();
1351          IXMLElement childNode;
1352          
1353          childCount = node.getChildrenCount();
1354          childIndex = 0;
1355          while ( childCount > 0 )
1356          {
1357              childNode = (IXMLElement)enumChildren.elementAt(childIndex);
1358              processChild(childNode.getName(), childNode, parent);
1359              childIndex++;
1360              childCount--;
1361              // if the <layout> element found set the flag so that the
1362
// default constraints won't be used.
1363
if ( layoutFound )
1364              {
1365                  layoutFound = false;
1366                  defaultConstraints = false;
1367              }
1368          }
1369          
1370          if ( defaultConstraints == true )
1371          {
1372              itemConstraints = new GridBagConstraints();
1373              itemConstraints.gridx = 0;
1374              itemConstraints.gridy = 0;
1375              itemConstraints.fill = GridBagConstraints.HORIZONTAL;
1376          }
1377          else
1378          {
1379              XMLFormLayout layoutConstraints;
1380              layoutConstraints = (XMLFormLayout)XMLconstraints.pop();
1381              itemConstraints = layoutConstraints.getXMLGridBagConstraints();
1382          }
1383          
1384          return (itemConstraints);
1385      }
1386      
1387      
1388      /*****************************************************************
1389       * registerItem
1390       *
1391       * This method will save the awt element for future use. This is
1392       * necessary when a "clear" button is entered and all input fields
1393       * on the form need to be cleared. It is also necessary when a
1394       * "submit" button is entered and all input needs to be gathered
1395       * and sent to the host for processing.
1396       *
1397       * Inputs: Object (which really is one of the java.awt elements)
1398       * Returns: None
1399       *****************************************************************/

1400      void registerItem(Object JavaDoc obj, boolean flag)
1401      {
1402          awtItems.add(obj, flag);
1403      }
1404      
1405      public void XMLsubmitAction(java.awt.event.ActionEvent JavaDoc evt)
1406      {
1407          listener.submitActionPerformed(evt, awtItems, formName);
1408          
1409          if ( listener.submitSuccessful() == true )
1410          {
1411              setVisible(false);
1412              
1413              if (parent != null)
1414              {
1415                  parent.removeFromFrameList(XMLForm.this);
1416              }
1417              
1418              dispose();
1419          }
1420          else
1421          {
1422              new InformationDialog(this,
1423              java.util.ResourceBundle.getBundle("com.quikj.client.beans.language",
1424              locale).getString("Invalid_Data"),
1425              listener.getMsgString(),
1426              java.util.ResourceBundle.getBundle("com.quikj.client.beans.language",
1427              locale).getString("_Close_"),
1428              true);
1429              listener.clearMsgString();
1430          }
1431      }
1432      
1433      private void closeDialog(java.awt.event.WindowEvent JavaDoc evt)
1434      {
1435          setVisible(false);
1436          dispose();
1437      }
1438}
1439
1440
Popular Tags