API Overview API Index Package Overview Direct link to this page
JDK 1.6
  javax.swing.text.html. FormView View Javadoc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915

/*
 * @(#)FormView.java	1.29 06/07/25
 *
 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package javax.swing.text.html;

import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;

/**
 * Component decorator that implements the view interface 
 * for form elements, <input>, <textarea>,
 * and <select>.  The model for the component is stored 
 * as an attribute of the the element (using StyleConstants.ModelAttribute), 
 * and is used to build the component of the view.  The type
 * of the model is assumed to of the type that would be set by
 * <code>HTMLDocument.HTMLReader.FormAction</code>.  If there are
 * multiple views mapped over the document, they will share the 
 * embedded component models.
 * <p>
 * The following table shows what components get built
 * by this view.
 * <table summary="shows what components get built by this view">
 * <tr>
 *   <th>Element Type</th>
 *   <th>Component built</th>
 * </tr>
 * <tr>
 *   <td>input, type button</td>
 *   <td>JButton</td>
 * </tr>
 * <tr>
 *   <td>input, type checkbox</td>
 *   <td>JCheckBox</td>
 * </tr>
 * <tr>
 *   <td>input, type image</td>
 *   <td>JButton</td>
 * </tr>
 * <tr>
 *   <td>input, type password</td>
 *   <td>JPasswordField</td>
 * </tr>
 * <tr>
 *   <td>input, type radio</td>
 *   <td>JRadioButton</td>
 * </tr>
 * <tr>
 *   <td>input, type reset</td>
 *   <td>JButton</td>
 * </tr>
 * <tr>
 *   <td>input, type submit</td>
 *   <td>JButton</td>
 * </tr>
 * <tr>
 *   <td>input, type text</td>
 *   <td>JTextField</td>
 * </tr>
 * <tr>
 *   <td>select, size &gt; 1 or multiple attribute defined</td>
 *   <td>JList in a JScrollPane</td>
 * </tr>
 * <tr>
 *   <td>select, size unspecified or 1</td>
 *   <td>JComboBox</td>
 * </tr>
 * <tr>
 *   <td>textarea</td>
 *   <td>JTextArea in a JScrollPane</td>
 * </tr>
 * <tr>
 *   <td>input, type file</td>
 *   <td>JTextField</td>
 * </tr>
 * </table>
 *
 * @author Timothy Prinzing
 * @author Sunita Mani
 * @version 1.29 07/25/06
 */
public class FormView extends ComponentView implements ActionListener {

    /**
     * If a value attribute is not specified for a FORM input element
     * of type "submit", then this default string is used.
     *
     * @deprecated As of 1.3, value now comes from UIManager property
     *             FormView.submitButtonText
     */
@Deprecated
    public static final String SUBMIT = new String("Submit Query");
    /**
     * If a value attribute is not specified for a FORM input element
     * of type "reset", then this default string is used.
     *
     * @deprecated As of 1.3, value comes from UIManager UIManager property
     *             FormView.resetButtonText
     */
@Deprecated
    public static final String RESET = new String("Reset");

    /**
     * Document attribute name for storing POST data. JEditorPane.getPostData()
     * uses the same name, should be kept in sync.
     */
    final static String PostDataProperty = "javax.swing.JEditorPane.postdata";

    /**
     * Used to indicate if the maximum span should be the same as the
     * preferred span. This is used so that the Component's size doesn't
     * change if there is extra room on a line. The first bit is used for
     * the X direction, and the second for the y direction.
     */
    private short maxIsPreferred;

    /**
     * Creates a new FormView object.
     *
     * @param elem the element to decorate
     */
    public FormView(Element elem) {
	super(elem);
    }

    /**
     * Create the component.  This is basically a
     * big switch statement based upon the tag type
     * and html attributes of the associated element.
     */
    protected Component createComponent() {
	AttributeSet attr = getElement().getAttributes();
	HTML.Tag t = (HTML.Tag) 
	    attr.getAttribute(StyleConstants.NameAttribute);
	JComponent c = null;
	Object model = attr.getAttribute(StyleConstants.ModelAttribute);
	if (t == HTML.Tag.INPUT) {
	    c = createInputComponent(attr, model);
	} else if (t == HTML.Tag.SELECT) {

	    if (model instanceof OptionListModel) {

		JList list = new JList((ListModel) model);
		int size = HTML.getIntegerAttributeValue(attr,
							 HTML.Attribute.SIZE,
							 1);
		list.setVisibleRowCount(size);
		list.setSelectionModel((ListSelectionModel)model);
		c = new JScrollPane(list);
	    } else {
		c = new JComboBox((ComboBoxModel) model);
                maxIsPreferred = 3;
	    }
	} else if (t == HTML.Tag.TEXTAREA) {
	    JTextArea area = new JTextArea((Document) model);
	    int rows = HTML.getIntegerAttributeValue(attr,
						     HTML.Attribute.ROWS,
						     1);
	    area.setRows(rows);
	    int cols = HTML.getIntegerAttributeValue(attr,
						     HTML.Attribute.COLS,
						     20);
            maxIsPreferred = 3;
	    area.setColumns(cols);
	    c = new JScrollPane(area, 
				JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
				JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
	}

	if (c != null) {
	    c.setAlignmentY(1.0f);
	}
	return c;
    }


    /**
     * Creates a component for an &lt;INPUT&gt; element based on the
     * value of the "type" attribute.
     *
     * @param set of attributes associated with the &lt;INPUT&gt; element.
     * @param model the value of the StyleConstants.ModelAttribute
     * @return the component.
     */
    private JComponent createInputComponent(AttributeSet attr, Object model) {
	JComponent c = null;
	String type = (String) attr.getAttribute(HTML.Attribute.TYPE);

	if (type.equals("submit") || type.equals("reset")) {
	    String value = (String) 
		attr.getAttribute(HTML.Attribute.VALUE);
	    if (value == null) {
		if (type.equals("submit")) {
		    value = UIManager.getString("FormView.submitButtonText");
		} else {
		    value = UIManager.getString("FormView.resetButtonText");
		}
	    }
	    JButton button = new JButton(value);
	    if (model != null) {
		button.setModel((ButtonModel)model);
		button.addActionListener(this);
	    }
	    c = button;
            maxIsPreferred = 3;
	} else if (type.equals("image")) {
	    String srcAtt = (String) attr.getAttribute(HTML.Attribute.SRC);
	    JButton button;
	    try {
		URL base = ((HTMLDocument)getElement().getDocument()).getBase();
		URL srcURL = new URL(base, srcAtt);
		Icon icon = new ImageIcon(srcURL);
		button  = new JButton(icon);
	    } catch (MalformedURLException e) {
		button = new JButton(srcAtt);
	    }
	    if (model != null) {
		button.setModel((ButtonModel)model);
		button.addMouseListener(new MouseEventListener());
	    }
	    c = button;
            maxIsPreferred = 3;
	} else if (type.equals("checkbox")) {
	    c = new JCheckBox();
	    if (model != null) {
		((JCheckBox)c).setModel((JToggleButton.ToggleButtonModel) model);
	    }
            maxIsPreferred = 3;
	} else if (type.equals("radio")) {
	    c = new JRadioButton();
	    if (model != null) {
		((JRadioButton)c).setModel((JToggleButton.ToggleButtonModel)model);
	    }
            maxIsPreferred = 3;
	} else if (type.equals("text")) {
	    int size = HTML.getIntegerAttributeValue(attr,
						     HTML.Attribute.SIZE,
						     -1);
	    JTextField field;
	    if (size > 0) {
		field = new JTextField();
		field.setColumns(size);
	    }
	    else {
		field = new JTextField();
		field.setColumns(20);
	    }
	    c = field;
	    if (model != null) {
		field.setDocument((Document) model);
	    }
	    field.addActionListener(this);
            maxIsPreferred = 3;
	} else if (type.equals("password")) {
	    JPasswordField field = new JPasswordField();
	    c = field;
	    if (model != null) {
		field.setDocument((Document) model);
	    }
	    int size = HTML.getIntegerAttributeValue(attr,
						     HTML.Attribute.SIZE,
						     -1);
            field.setColumns((size > 0) ? size : 20);
	    field.addActionListener(this);
            maxIsPreferred = 3;
	} else if (type.equals("file")) {
            JTextField field = new JTextField();
	    if (model != null) {
		field.setDocument((Document)model);
	    }
	    int size = HTML.getIntegerAttributeValue(attr, HTML.Attribute.SIZE,
						     -1);
            field.setColumns((size > 0) ? size : 20);
            JButton browseButton = new JButton(UIManager.getString
                                           ("FormView.browseFileButtonText"));
            Box box = Box.createHorizontalBox();
            box.add(field);
            box.add(Box.createHorizontalStrut(5));
            box.add(browseButton);
            browseButton.addActionListener(new BrowseFileAction(
                                           attr, (Document)model));
            c = box;
            maxIsPreferred = 3;
        }
	return c;
    }


    /**
     * Determines the maximum span for this view along an
     * axis. For certain components, the maximum and preferred span are the
     * same. For others this will return the value
     * returned by Component.getMaximumSize along the
     * axis of interest.
     *
     * @param axis may be either View.X_AXIS or View.Y_AXIS
     * @return   the span the view would like to be rendered into >= 0.
     *           Typically the view is told to render into the span
     *           that is returned, although there is no guarantee.  
     *           The parent may choose to resize or break the view.
     * @exception IllegalArgumentException for an invalid axis
     */
    public float getMaximumSpan(int axis) {
        switch (axis) {
        case View.X_AXIS:
            if ((maxIsPreferred & 1) == 1) {
                super.getMaximumSpan(axis);
                return getPreferredSpan(axis);
            }
            return super.getMaximumSpan(axis);
        case View.Y_AXIS:
            if ((maxIsPreferred & 2) == 2) {
                super.getMaximumSpan(axis);
                return getPreferredSpan(axis);
            }
            return super.getMaximumSpan(axis);
        default:
            break;
        }
        return super.getMaximumSpan(axis);
    }


    /**
     * Responsible for processeing the ActionEvent.
     * If the element associated with the FormView,
     * has a type of "submit", "reset", "text" or "password" 
     * then the action is processed.  In the case of a "submit" 
     * the form is submitted.  In the case of a "reset"
     * the form is reset to its original state.
     * In the case of "text" or "password", if the 
     * element is the last one of type "text" or "password",
     * the form is submitted.  Otherwise, focus is transferred
     * to the next component in the form.
     *
     * @param evt the ActionEvent.
     */
    public void actionPerformed(ActionEvent evt) {
	Element element = getElement();
	StringBuffer dataBuffer = new StringBuffer();
	HTMLDocument doc = (HTMLDocument)getDocument();
	AttributeSet attr = element.getAttributes();
	
	String type = (String) attr.getAttribute(HTML.Attribute.TYPE);

	if (type.equals("submit")) { 
	    getFormData(dataBuffer);
	    submitData(dataBuffer.toString());
	} else if (type.equals("reset")) {
	    resetForm();
	} else if (type.equals("text") || type.equals("password")) {
	    if (isLastTextOrPasswordField()) {
		getFormData(dataBuffer);
		submitData(dataBuffer.toString());
	    } else {
		getComponent().transferFocus();
	    }
	}
    }


    /**
     * This method is responsible for submitting the form data.
     * A thread is forked to undertake the submission.
     */
    protected void submitData(String data) {
        Element form = getFormElement();
        AttributeSet attrs = form.getAttributes();
        HTMLDocument doc = (HTMLDocument) form.getDocument();
        URL base = doc.getBase();

        String target = (String) attrs.getAttribute(HTML.Attribute.TARGET);
        if (target == null) {
            target = "_self";
        }
        
        String method = (String) attrs.getAttribute(HTML.Attribute.METHOD);
        if (method == null) {
            method = "GET";
        }
        method = method.toLowerCase();
        boolean isPostMethod = method.equals("post");
        if (isPostMethod) {
            storePostData(doc, target, data);
        }
        
        String action = (String) attrs.getAttribute(HTML.Attribute.ACTION);
        URL actionURL;
        try { 
            actionURL = (action == null)
                ? new URL(base.getProtocol(), base.getHost(),
                                        base.getPort(), base.getFile())
                : new URL(base, action);
            if (!isPostMethod) {
                String query = data.toString();
                actionURL = new URL(actionURL + "?" + query);
            }
        } catch (MalformedURLException e) {
            actionURL = null;
        }
        final JEditorPane c = (JEditorPane) getContainer();
        HTMLEditorKit kit = (HTMLEditorKit) c.getEditorKit();
        
        FormSubmitEvent formEvent = null;
        if (!kit.isAutoFormSubmission() || doc.isFrameDocument()) {
            FormSubmitEvent.MethodType methodType = isPostMethod
                    ? FormSubmitEvent.MethodType.POST
                    : FormSubmitEvent.MethodType.GET;
            formEvent = new FormSubmitEvent(
                    FormView.this, HyperlinkEvent.EventType.ACTIVATED,
                    actionURL, form, target, methodType, data);
            
        }
        // setPage() may take significant time so schedule it to run later.
        final FormSubmitEvent fse = formEvent;
        final URL url = actionURL;
        SwingUtilities.invokeLater(new Runnable() {            
            public void run() {
                if (fse != null) {
                    c.fireHyperlinkUpdate(fse);
                } else {
                    try {
                        c.setPage(url);
                    } catch (IOException e) {
                        UIManager.getLookAndFeel().provideErrorFeedback(c);
                    }
                }
            }
        });
    }
        
    private void storePostData(HTMLDocument doc, String target, String data) {
        
        /* POST data is stored into the document property named by constant
         * PostDataProperty from where it is later retrieved by method
         * JEditorPane.getPostData().  If the current document is in a frame,
         * the data is initially put into the toplevel (frameset) document
         * property (named <PostDataProperty>.<Target frame name>).  It is the
         * responsibility of FrameView which updates the target frame
         * to move data from the frameset document property into the frame
         * document property.
         */
        
        Document propDoc = doc;
        String propName = PostDataProperty;
        
        if (doc.isFrameDocument()) {
            // find the top-most JEditorPane holding the frameset view.
            FrameView.FrameEditorPane p =
                    (FrameView.FrameEditorPane) getContainer();
            FrameView v = p.getFrameView();
            JEditorPane c = v.getOutermostJEditorPane();
            if (c != null) {
                propDoc = c.getDocument();
                propName += ("." + target);
            }
        }
        
        propDoc.putProperty(propName, data);
    }

    /**
     * MouseEventListener class to handle form submissions when
     * an input with type equal to image is clicked on.
     * A MouseListener is necessary since along with the image
     * data the coordinates associated with the mouse click
     * need to be submitted.
     */
    protected class MouseEventListener extends MouseAdapter {

	public void mouseReleased(MouseEvent evt) {
	    String imageData = getImageData(evt.getPoint());
	    imageSubmit(imageData);
	}
    }

    /**
     * This method is called to submit a form in response
     * to a click on an image -- an &lt;INPUT&gt; form
     * element of type "image".
     *
     * @param imageData the mouse click coordinates.
     */
    protected void imageSubmit(String imageData) {

	StringBuffer dataBuffer = new StringBuffer();
	Element elem = getElement();
	HTMLDocument hdoc = (HTMLDocument)elem.getDocument();
	getFormData(dataBuffer);
	if (dataBuffer.length() > 0) {
	    dataBuffer.append('&');
	}
	dataBuffer.append(imageData);
	submitData(dataBuffer.toString());
	return;
    }

    /**
     * Extracts the value of the name attribute
     * associated with the input element of type
     * image.  If name is defined it is encoded using
     * the URLEncoder.encode() method and the 
     * image data is returned in the following format:
     *	    name + ".x" +"="+ x +"&"+ name +".y"+"="+ y
     * otherwise,
     * 	    "x="+ x +"&y="+ y
     * 
     * @param point associated with the mouse click.
     * @return the image data.
     */
    private String getImageData(Point point) {
	    
	String mouseCoords = point.x + ":" + point.y;
	int sep = mouseCoords.indexOf(':');
	String x = mouseCoords.substring(0, sep);
	String y = mouseCoords.substring(++sep);
	String name = (String) getElement().getAttributes().getAttribute(HTML.Attribute.NAME);
	
	String data;
	if (name == null || name.equals("")) {
	    data = "x="+ x +"&y="+ y;
	} else {
	    name = URLEncoder.encode(name);
	    data = name + ".x" +"="+ x +"&"+ name +".y"+"="+ y;
	}
	return data;
    }


    /**
     * The following methods provide functionality required to
     * iterate over a the elements of the form and in the case
     * of a form submission, extract the data from each model
     * that is associated with each form element, and in the
     * case of reset, reinitialize the each model to its
     * initial state.
     */


    /**
     * Returns the Element representing the <code>FORM</code>.
     */
    private Element getFormElement() {
        Element elem = getElement();
        while (elem != null) {
            if (elem.getAttributes().getAttribute
                (StyleConstants.NameAttribute) == HTML.Tag.FORM) {
                return elem;
            }
            elem = elem.getParentElement();
        }
        return null;
    }

    /**
     * Iterates over the 
     * element hierarchy, extracting data from the 
     * models associated with the relevant form elements.
     * "Relevant" means the form elements that are part
     * of the same form whose element triggered the submit
     * action.
     *
     * @param buffer        the buffer that contains that data to submit
     * @param targetElement the element that triggered the 
     *                      form submission
     */
    void getFormData(StringBuffer buffer) {
        Element formE = getFormElement();
        if (formE != null) {
            ElementIterator it = new ElementIterator(formE);
            Element next;

            while ((next = it.next()) != null) {
                if (isControl(next)) {
                    String type = (String)next.getAttributes().getAttribute
                                       (HTML.Attribute.TYPE);

                    if (type != null && type.equals("submit") && 
                        next != getElement()) {
                        // do nothing - this submit isnt the trigger
                    } else if (type == null || !type.equals("image")) {
                        // images only result in data if they triggered
                        // the submit and they require that the mouse click
                        // coords be appended to the data.  Hence its
                        // processing is handled by the view.
                        loadElementDataIntoBuffer(next, buffer);
                    }
                }
            }
        }
    }

    /**
     * Loads the data
     * associated with the element into the buffer.
     * The format in which data is appended depends
     * on the type of the form element.  Essentially
     * data is loaded in name/value pairs.
     * 
     */
    private void loadElementDataIntoBuffer(Element elem, StringBuffer buffer) {

	AttributeSet attr = elem.getAttributes();
	String name = (String)attr.getAttribute(HTML.Attribute.NAME);
	if (name == null) {
	    return;
 	}
	String value = null;
	HTML.Tag tag = (HTML.Tag)elem.getAttributes().getAttribute
                                  (StyleConstants.NameAttribute);

	if (tag == HTML.Tag.INPUT) {
	    value = getInputElementData(attr);
	} else if (tag ==  HTML.Tag.TEXTAREA) {
	    value = getTextAreaData(attr);
	} else if (tag == HTML.Tag.SELECT) {
	    loadSelectData(attr, buffer);
	}
	
	if (name != null && value != null) {
	    appendBuffer(buffer, name, value);
	}
    }


    /**
     * Returns the data associated with an &lt;INPUT&gt; form
     * element.  The value of "type" attributes is
     * used to determine the type of the model associated
     * with the element and then the relevant data is
     * extracted.
     */
    private String getInputElementData(AttributeSet attr) {
	
	Object model = attr.getAttribute(StyleConstants.ModelAttribute);
	String type = (String) attr.getAttribute(HTML.Attribute.TYPE);
	String value = null;
	
	if (type.equals("text") || type.equals("password")) {
	    Document doc = (Document)model;
	    try {
		value = doc.getText(0, doc.getLength());
	    } catch (BadLocationException e) {
		value = null;
	    }
	} else if (type.equals("submit") || type.equals("hidden")) {
	    value = (String) attr.getAttribute(HTML.Attribute.VALUE);
	    if (value == null) {
		value = "";
	    }
	} else if (type.equals("radio") || type.equals("checkbox")) {
	    ButtonModel m = (ButtonModel)model;
	    if (m.isSelected()) {
		value = (String) attr.getAttribute(HTML.Attribute.VALUE);
		if (value == null) {
		    value = "on";
		}
	    }
	} else if (type.equals("file")) {
	    Document doc = (Document)model;
            String path;

	    try {
		path = doc.getText(0, doc.getLength());
	    } catch (BadLocationException e) {
		path = null;
	    }
            if (path != null && path.length() > 0) {
                value = path;
/*

                try {
                    Reader reader = new BufferedReader(new FileReader(path));
                    StringBuffer buffer = new StringBuffer();
                    char[] cBuff = new char[1024];
                    int read;

                    try {
                        while ((read = reader.read(cBuff)) != -1) {
                            buffer.append(cBuff, 0, read);
                        }
                    } catch (IOException ioe) {
                        buffer = null;
                    }
                    try {
                        reader.close();
                    } catch (IOException ioe) {}
                    if (buffer != null) {
                        value = buffer.toString();
                    }
                } catch (IOException ioe) {}
*/
            }
        }
	return value;
    }

    /**
     * Returns the data associated with the &lt;TEXTAREA&gt; form
     * element.  This is done by getting the text stored in the
     * Document model.
     */
    private String getTextAreaData(AttributeSet attr) {
	Document doc = (Document)attr.getAttribute(StyleConstants.ModelAttribute);
	try {
	    return doc.getText(0, doc.getLength());
	} catch (BadLocationException e) {
	    return null;
	}
    }


    /**
     * Loads the buffer with the data associated with the Select
     * form element.  Basically, only items that are selected
     * and have their name attribute set are added to the buffer.
     */
    private void loadSelectData(AttributeSet attr, StringBuffer buffer) {

	String name = (String)attr.getAttribute(HTML.Attribute.NAME);
	if (name == null) {
	    return;
	}
	Object m = attr.getAttribute(StyleConstants.ModelAttribute);
	if (m instanceof OptionListModel) {
	    OptionListModel model = (OptionListModel)m;
	    
	    for (int i = 0; i < model.getSize(); i++) {
		if (model.isSelectedIndex(i)) {
		    Option option = (Option) model.getElementAt(i);
		    appendBuffer(buffer, name, option.getValue());
		}
	    }
	} else if (m instanceof ComboBoxModel) {
	    ComboBoxModel model = (ComboBoxModel)m;
	    Option option = (Option)model.getSelectedItem();
	    if (option != null) {
		appendBuffer(buffer, name, option.getValue());
	    }
	}
    }

    /**
     * Appends name / value pairs into the 
     * buffer.  Both names and values are encoded using the 
     * URLEncoder.encode() method before being added to the
     * buffer.
     */
    private void appendBuffer(StringBuffer buffer, String name, String value) {
	if (buffer.length() > 0) {
	    buffer.append('&');
	}
	String encodedName = URLEncoder.encode(name);
	buffer.append(encodedName);
	buffer.append('=');
	String encodedValue = URLEncoder.encode(value);
	buffer.append(encodedValue);
    }

    /**
     * Returns true if the Element <code>elem</code> represents a control.
     */
    private boolean isControl(Element elem) {
        return elem.isLeaf();
    }

    /**
     * Iterates over the element hierarchy to determine if
     * the element parameter, which is assumed to be an
     * &lt;INPUT&gt; element of type password or text, is the last
     * one of either kind, in the form to which it belongs.
     */
    boolean isLastTextOrPasswordField() {
        Element parent = getFormElement();
        Element elem = getElement();

        if (parent != null) {
            ElementIterator it = new ElementIterator(parent);
            Element next;
            boolean found = false;

            while ((next = it.next()) != null) {
                if (next == elem) {
                    found = true;
                }
                else if (found && isControl(next)) {
                    AttributeSet elemAttr = next.getAttributes();

                    if (HTMLDocument.matchNameAttribute
                                     (elemAttr, HTML.Tag.INPUT)) {
                        String type = (String)elemAttr.getAttribute
                                                  (HTML.Attribute.TYPE);

                        if ("text".equals(type) || "password".equals(type)) {
                            return false;
                        }
                    }
                }
            }
        }
        return true;
    }

    /**
     * Resets the form
     * to its initial state by reinitializing the models
     * associated with each form element to their initial
     * values.
     *
     * param elem the element that triggered the reset
     */
    void resetForm() {
        Element parent = getFormElement();

        if (parent != null) {
            ElementIterator it = new ElementIterator(parent);
            Element next;

            while((next = it.next()) != null) {
                if (isControl(next)) {
                    AttributeSet elemAttr = next.getAttributes();
                    Object m = elemAttr.getAttribute(StyleConstants.
                                                     ModelAttribute);
                    if (m instanceof TextAreaDocument) {
                        TextAreaDocument doc = (TextAreaDocument)m;
                        doc.reset();
                    } else if (m instanceof PlainDocument) {
                        try {
                            PlainDocument doc =  (PlainDocument)m;
                            doc.remove(0, doc.getLength());
                            if (HTMLDocument.matchNameAttribute
                                             (elemAttr, HTML.Tag.INPUT)) {
                                String value = (String)elemAttr.
                                           getAttribute(HTML.Attribute.VALUE);
                                if (value != null) {
                                    doc.insertString(0, value, null);
                                }
                            }
                        } catch (BadLocationException e) {
                        }
                    } else if (m instanceof OptionListModel) {
                        OptionListModel model = (OptionListModel) m;
                        int size = model.getSize();
                        for (int i = 0; i < size; i++) {
                            model.removeIndexInterval(i, i);
                        }
                        BitSet selectionRange = model.getInitialSelection();
                        for (int i = 0; i < selectionRange.size(); i++) {
                            if (selectionRange.get(i)) {
                                model.addSelectionInterval(i, i);
                            }
                        }
                    } else if (m instanceof OptionComboBoxModel) {
                        OptionComboBoxModel model = (OptionComboBoxModel) m;
                        Option option = model.getInitialSelection();
                        if (option != null) {
                            model.setSelectedItem(option);
                        }
                    } else if (m instanceof JToggleButton.ToggleButtonModel) {
                        boolean checked = ((String)elemAttr.getAttribute
                                           (HTML.Attribute.CHECKED) != null);
                        JToggleButton.ToggleButtonModel model =
                                        (JToggleButton.ToggleButtonModel)m;
                        model.setSelected(checked);
                    }
                }
            }
        }
    }


    /**
     * BrowseFileAction is used for input type == file. When the user
     * clicks the button a JFileChooser is brought up allowing the user
     * to select a file in the file system. The resulting path to the selected
     * file is set in the text field (actually an instance of Document).
     */
    private class BrowseFileAction implements ActionListener {
        private AttributeSet attrs;
        private Document model;

        BrowseFileAction(AttributeSet attrs, Document model) {
            this.attrs = attrs;
            this.model = model;
        }

        public void actionPerformed(ActionEvent ae) {
            // PENDING: When mime support is added to JFileChooser use the
            // accept value of attrs.
            JFileChooser fc = new JFileChooser();
            fc.setMultiSelectionEnabled(false);
            if (fc.showOpenDialog(getContainer()) ==
                  JFileChooser.APPROVE_OPTION) {
                File selected = fc.getSelectedFile();

                if (selected != null) {
                    try {
                        if (model.getLength() > 0) {
                            model.remove(0, model.getLength());
                        }
                        model.insertString(0, selected.getPath(), null);
                    } catch (BadLocationException ble) {}
                }
            }
        }
    }
}

Generated By: JavaOnTracks Doclet 0.1.4     ©Thibaut Colar