jdk/src/java.desktop/share/classes/javax/swing/text/html/FormView.java
author martin
Thu, 30 Oct 2014 07:31:41 -0700
changeset 28059 e576535359cc
parent 25859 3317bb8137f4
child 28236 610561ed1847
permissions -rw-r--r--
8067377: My hobby: caning, then then canning, the the can-can Summary: Fix ALL the stutters! Reviewed-by: rriggs, mchung, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21278
diff changeset
     2
 * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing.text.html;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.swing.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Component decorator that implements the view interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * for form elements, <input>, <textarea>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * and <select>.  The model for the component is stored
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 25859
diff changeset
    40
 * as an attribute of the element (using StyleConstants.ModelAttribute),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * and is used to build the component of the view.  The type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * of the model is assumed to of the type that would be set by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <code>HTMLDocument.HTMLReader.FormAction</code>.  If there are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * multiple views mapped over the document, they will share the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * embedded component models.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * The following table shows what components get built
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * by this view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <table summary="shows what components get built by this view">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *   <th>Element Type</th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *   <th>Component built</th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *   <td>input, type button</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *   <td>JButton</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *   <td>input, type checkbox</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *   <td>JCheckBox</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *   <td>input, type image</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *   <td>JButton</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *   <td>input, type password</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *   <td>JPasswordField</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *   <td>input, type radio</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *   <td>JRadioButton</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *   <td>input, type reset</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *   <td>JButton</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *   <td>input, type submit</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *   <td>JButton</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *   <td>input, type text</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *   <td>JTextField</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *   <td>select, size &gt; 1 or multiple attribute defined</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *   <td>JList in a JScrollPane</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *   <td>select, size unspecified or 1</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *   <td>JComboBox</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *   <td>textarea</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *   <td>JTextArea in a JScrollPane</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *   <td>input, type file</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *   <td>JTextField</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * </table>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * @author Timothy Prinzing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * @author Sunita Mani
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
public class FormView extends ComponentView implements ActionListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * If a value attribute is not specified for a FORM input element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * of type "submit", then this default string is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @deprecated As of 1.3, value now comes from UIManager property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *             FormView.submitButtonText
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public static final String SUBMIT = new String("Submit Query");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * If a value attribute is not specified for a FORM input element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * of type "reset", then this default string is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @deprecated As of 1.3, value comes from UIManager UIManager property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *             FormView.resetButtonText
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public static final String RESET = new String("Reset");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * Document attribute name for storing POST data. JEditorPane.getPostData()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * uses the same name, should be kept in sync.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    final static String PostDataProperty = "javax.swing.JEditorPane.postdata";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * Used to indicate if the maximum span should be the same as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * preferred span. This is used so that the Component's size doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * change if there is extra room on a line. The first bit is used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * the X direction, and the second for the y direction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    private short maxIsPreferred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * Creates a new FormView object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @param elem the element to decorate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public FormView(Element elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        super(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * Create the component.  This is basically a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * big switch statement based upon the tag type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * and html attributes of the associated element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    protected Component createComponent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        AttributeSet attr = getElement().getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        HTML.Tag t = (HTML.Tag)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            attr.getAttribute(StyleConstants.NameAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        JComponent c = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        Object model = attr.getAttribute(StyleConstants.ModelAttribute);
15977
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   162
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   163
        // Remove listeners previously registered in shared model
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   164
        // when a new UI component is replaced.  See bug 7189299.
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   165
        removeStaleListenerForModel(model);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        if (t == HTML.Tag.INPUT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            c = createInputComponent(attr, model);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        } else if (t == HTML.Tag.SELECT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            if (model instanceof OptionListModel) {
25193
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 24269
diff changeset
   171
                @SuppressWarnings("unchecked")
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 24269
diff changeset
   172
                JList<?> list = new JList<>((ListModel) model);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                int size = HTML.getIntegerAttributeValue(attr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                                                         HTML.Attribute.SIZE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                                                         1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                list.setVisibleRowCount(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                list.setSelectionModel((ListSelectionModel)model);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                c = new JScrollPane(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            } else {
25193
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 24269
diff changeset
   180
                @SuppressWarnings("unchecked")
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 24269
diff changeset
   181
                JComboBox<?> tmp = new JComboBox<>((ComboBoxModel) model);
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 24269
diff changeset
   182
                c = tmp;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                maxIsPreferred = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        } else if (t == HTML.Tag.TEXTAREA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            JTextArea area = new JTextArea((Document) model);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            int rows = HTML.getIntegerAttributeValue(attr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                                                     HTML.Attribute.ROWS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                                                     1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            area.setRows(rows);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            int cols = HTML.getIntegerAttributeValue(attr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                                                     HTML.Attribute.COLS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                                                     20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            maxIsPreferred = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            area.setColumns(cols);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            c = new JScrollPane(area,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                                JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            c.setAlignmentY(1.0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * Creates a component for an &lt;INPUT&gt; element based on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * value of the "type" attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @param set of attributes associated with the &lt;INPUT&gt; element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @param model the value of the StyleConstants.ModelAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * @return the component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    private JComponent createInputComponent(AttributeSet attr, Object model) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        JComponent c = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        String type = (String) attr.getAttribute(HTML.Attribute.TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        if (type.equals("submit") || type.equals("reset")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            String value = (String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                attr.getAttribute(HTML.Attribute.VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                if (type.equals("submit")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                    value = UIManager.getString("FormView.submitButtonText");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                    value = UIManager.getString("FormView.resetButtonText");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            JButton button = new JButton(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            if (model != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                button.setModel((ButtonModel)model);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                button.addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            c = button;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            maxIsPreferred = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        } else if (type.equals("image")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            String srcAtt = (String) attr.getAttribute(HTML.Attribute.SRC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            JButton button;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                URL base = ((HTMLDocument)getElement().getDocument()).getBase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                URL srcURL = new URL(base, srcAtt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                Icon icon = new ImageIcon(srcURL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                button  = new JButton(icon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            } catch (MalformedURLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                button = new JButton(srcAtt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            if (model != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                button.setModel((ButtonModel)model);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                button.addMouseListener(new MouseEventListener());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            c = button;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            maxIsPreferred = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        } else if (type.equals("checkbox")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            c = new JCheckBox();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            if (model != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                ((JCheckBox)c).setModel((JToggleButton.ToggleButtonModel) model);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            maxIsPreferred = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        } else if (type.equals("radio")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            c = new JRadioButton();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            if (model != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                ((JRadioButton)c).setModel((JToggleButton.ToggleButtonModel)model);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            maxIsPreferred = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        } else if (type.equals("text")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            int size = HTML.getIntegerAttributeValue(attr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                                                     HTML.Attribute.SIZE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                                                     -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            JTextField field;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            if (size > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                field = new JTextField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                field.setColumns(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                field = new JTextField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                field.setColumns(20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            c = field;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            if (model != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                field.setDocument((Document) model);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            field.addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            maxIsPreferred = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        } else if (type.equals("password")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            JPasswordField field = new JPasswordField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            c = field;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            if (model != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                field.setDocument((Document) model);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            int size = HTML.getIntegerAttributeValue(attr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                                                     HTML.Attribute.SIZE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                                                     -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            field.setColumns((size > 0) ? size : 20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            field.addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            maxIsPreferred = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        } else if (type.equals("file")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            JTextField field = new JTextField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            if (model != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                field.setDocument((Document)model);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            int size = HTML.getIntegerAttributeValue(attr, HTML.Attribute.SIZE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                                                     -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            field.setColumns((size > 0) ? size : 20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            JButton browseButton = new JButton(UIManager.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                                           ("FormView.browseFileButtonText"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            Box box = Box.createHorizontalBox();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            box.add(field);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            box.add(Box.createHorizontalStrut(5));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            box.add(browseButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            browseButton.addActionListener(new BrowseFileAction(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                                           attr, (Document)model));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            c = box;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            maxIsPreferred = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
15977
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   319
    private void removeStaleListenerForModel(Object model) {
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   320
        if (model instanceof DefaultButtonModel) {
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   321
            // case of JButton whose model is DefaultButtonModel
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   322
            // Need to remove stale ActionListener, ChangeListener and
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   323
            // ItemListener that are instance of AbstractButton$Handler.
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   324
            DefaultButtonModel buttonModel = (DefaultButtonModel) model;
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   325
            String listenerClass = "javax.swing.AbstractButton$Handler";
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   326
            for (ActionListener listener : buttonModel.getActionListeners()) {
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   327
                if (listenerClass.equals(listener.getClass().getName())) {
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   328
                    buttonModel.removeActionListener(listener);
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   329
                }
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   330
            }
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   331
            for (ChangeListener listener : buttonModel.getChangeListeners()) {
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   332
                if (listenerClass.equals(listener.getClass().getName())) {
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   333
                    buttonModel.removeChangeListener(listener);
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   334
                }
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   335
            }
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   336
            for (ItemListener listener : buttonModel.getItemListeners()) {
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   337
                if (listenerClass.equals(listener.getClass().getName())) {
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   338
                    buttonModel.removeItemListener(listener);
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   339
                }
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   340
            }
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   341
        } else if (model instanceof AbstractListModel) {
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   342
            // case of JComboBox and JList
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   343
            // For JList, the stale ListDataListener is instance
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   344
            // BasicListUI$Handler.
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   345
            // For JComboBox, there are 2 stale ListDataListeners, which are
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   346
            // BasicListUI$Handler and BasicComboBoxUI$Handler.
25193
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 24269
diff changeset
   347
            @SuppressWarnings("unchecked")
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 24269
diff changeset
   348
            AbstractListModel<?> listModel = (AbstractListModel) model;
15977
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   349
            String listenerClass1 =
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   350
                    "javax.swing.plaf.basic.BasicListUI$Handler";
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   351
            String listenerClass2 =
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   352
                    "javax.swing.plaf.basic.BasicComboBoxUI$Handler";
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   353
            for (ListDataListener listener : listModel.getListDataListeners()) {
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   354
                if (listenerClass1.equals(listener.getClass().getName())
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   355
                        || listenerClass2.equals(listener.getClass().getName()))
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   356
                {
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   357
                    listModel.removeListDataListener(listener);
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   358
                }
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   359
            }
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   360
        } else if (model instanceof AbstractDocument) {
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   361
            // case of JPasswordField, JTextField and JTextArea
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   362
            // All have 2 stale DocumentListeners.
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   363
            String listenerClass1 =
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   364
                    "javax.swing.plaf.basic.BasicTextUI$UpdateHandler";
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   365
            String listenerClass2 =
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   366
                    "javax.swing.text.DefaultCaret$Handler";
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   367
            AbstractDocument docModel = (AbstractDocument) model;
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   368
            for (DocumentListener listener : docModel.getDocumentListeners()) {
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   369
                if (listenerClass1.equals(listener.getClass().getName())
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   370
                        || listenerClass2.equals(listener.getClass().getName()))
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   371
                {
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   372
                    docModel.removeDocumentListener(listener);
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   373
                }
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   374
            }
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   375
        }
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   376
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * Determines the maximum span for this view along an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * axis. For certain components, the maximum and preferred span are the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * same. For others this will return the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * returned by Component.getMaximumSize along the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * axis of interest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * @param axis may be either View.X_AXIS or View.Y_AXIS
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 15977
diff changeset
   386
     * @return   the span the view would like to be rendered into &gt;= 0.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     *           Typically the view is told to render into the span
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     *           that is returned, although there is no guarantee.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     *           The parent may choose to resize or break the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * @exception IllegalArgumentException for an invalid axis
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    public float getMaximumSpan(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        switch (axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        case View.X_AXIS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            if ((maxIsPreferred & 1) == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                super.getMaximumSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                return getPreferredSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            return super.getMaximumSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        case View.Y_AXIS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            if ((maxIsPreferred & 2) == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                super.getMaximumSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                return getPreferredSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            return super.getMaximumSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        return super.getMaximumSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    /**
15977
225648de9c4d 8008289: DefaultButtonModel instance keeps stale listeners in html FormView
dingxmin
parents: 10100
diff changeset
   414
     * Responsible for processing the ActionEvent.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * If the element associated with the FormView,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * has a type of "submit", "reset", "text" or "password"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * then the action is processed.  In the case of a "submit"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * the form is submitted.  In the case of a "reset"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * the form is reset to its original state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * In the case of "text" or "password", if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * element is the last one of type "text" or "password",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * the form is submitted.  Otherwise, focus is transferred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * to the next component in the form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * @param evt the ActionEvent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    public void actionPerformed(ActionEvent evt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        Element element = getElement();
7014
eb4fcf73ee99 6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents: 5506
diff changeset
   429
        StringBuilder dataBuffer = new StringBuilder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        HTMLDocument doc = (HTMLDocument)getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        AttributeSet attr = element.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        String type = (String) attr.getAttribute(HTML.Attribute.TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        if (type.equals("submit")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            getFormData(dataBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            submitData(dataBuffer.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        } else if (type.equals("reset")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            resetForm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        } else if (type.equals("text") || type.equals("password")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            if (isLastTextOrPasswordField()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                getFormData(dataBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                submitData(dataBuffer.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                getComponent().transferFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * This method is responsible for submitting the form data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * A thread is forked to undertake the submission.
24269
325ea76ea0e9 8042105: Fix some more doclint issues in javax.swing.text.html classes
yan
parents: 23010
diff changeset
   454
     *
325ea76ea0e9 8042105: Fix some more doclint issues in javax.swing.text.html classes
yan
parents: 23010
diff changeset
   455
     * @param data data to submit
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    protected void submitData(String data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        Element form = getFormElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        AttributeSet attrs = form.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        HTMLDocument doc = (HTMLDocument) form.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        URL base = doc.getBase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        String target = (String) attrs.getAttribute(HTML.Attribute.TARGET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        if (target == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            target = "_self";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        String method = (String) attrs.getAttribute(HTML.Attribute.METHOD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        if (method == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            method = "GET";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        method = method.toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        boolean isPostMethod = method.equals("post");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        if (isPostMethod) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            storePostData(doc, target, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        String action = (String) attrs.getAttribute(HTML.Attribute.ACTION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        URL actionURL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            actionURL = (action == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                ? new URL(base.getProtocol(), base.getHost(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                                        base.getPort(), base.getFile())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                : new URL(base, action);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            if (!isPostMethod) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                String query = data.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                actionURL = new URL(actionURL + "?" + query);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        } catch (MalformedURLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            actionURL = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        final JEditorPane c = (JEditorPane) getContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        HTMLEditorKit kit = (HTMLEditorKit) c.getEditorKit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        FormSubmitEvent formEvent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        if (!kit.isAutoFormSubmission() || doc.isFrameDocument()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            FormSubmitEvent.MethodType methodType = isPostMethod
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                    ? FormSubmitEvent.MethodType.POST
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                    : FormSubmitEvent.MethodType.GET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            formEvent = new FormSubmitEvent(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                    FormView.this, HyperlinkEvent.EventType.ACTIVATED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                    actionURL, form, target, methodType, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        // setPage() may take significant time so schedule it to run later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        final FormSubmitEvent fse = formEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        final URL url = actionURL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        SwingUtilities.invokeLater(new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                if (fse != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                    c.fireHyperlinkUpdate(fse);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                        c.setPage(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                    } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                        UIManager.getLookAndFeel().provideErrorFeedback(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    private void storePostData(HTMLDocument doc, String target, String data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        /* POST data is stored into the document property named by constant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
         * PostDataProperty from where it is later retrieved by method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
         * JEditorPane.getPostData().  If the current document is in a frame,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
         * the data is initially put into the toplevel (frameset) document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
         * property (named <PostDataProperty>.<Target frame name>).  It is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
         * responsibility of FrameView which updates the target frame
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
         * to move data from the frameset document property into the frame
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
         * document property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        Document propDoc = doc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        String propName = PostDataProperty;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        if (doc.isFrameDocument()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            // find the top-most JEditorPane holding the frameset view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            FrameView.FrameEditorPane p =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                    (FrameView.FrameEditorPane) getContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            FrameView v = p.getFrameView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            JEditorPane c = v.getOutermostJEditorPane();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                propDoc = c.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                propName += ("." + target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        propDoc.putProperty(propName, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * MouseEventListener class to handle form submissions when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * an input with type equal to image is clicked on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * A MouseListener is necessary since along with the image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * data the coordinates associated with the mouse click
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * need to be submitted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    protected class MouseEventListener extends MouseAdapter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        public void mouseReleased(MouseEvent evt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            String imageData = getImageData(evt.getPoint());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            imageSubmit(imageData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * This method is called to submit a form in response
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * to a click on an image -- an &lt;INPUT&gt; form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * element of type "image".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * @param imageData the mouse click coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    protected void imageSubmit(String imageData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
7014
eb4fcf73ee99 6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents: 5506
diff changeset
   577
        StringBuilder dataBuffer = new StringBuilder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        Element elem = getElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        HTMLDocument hdoc = (HTMLDocument)elem.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        getFormData(dataBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        if (dataBuffer.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            dataBuffer.append('&');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        dataBuffer.append(imageData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        submitData(dataBuffer.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * Extracts the value of the name attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * associated with the input element of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * image.  If name is defined it is encoded using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * the URLEncoder.encode() method and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * image data is returned in the following format:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     *      name + ".x" +"="+ x +"&"+ name +".y"+"="+ y
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     *      "x="+ x +"&y="+ y
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * @param point associated with the mouse click.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * @return the image data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    private String getImageData(Point point) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        String mouseCoords = point.x + ":" + point.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        int sep = mouseCoords.indexOf(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        String x = mouseCoords.substring(0, sep);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        String y = mouseCoords.substring(++sep);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        String name = (String) getElement().getAttributes().getAttribute(HTML.Attribute.NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        String data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        if (name == null || name.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            data = "x="+ x +"&y="+ y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            name = URLEncoder.encode(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            data = name + ".x" +"="+ x +"&"+ name +".y"+"="+ y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        return data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * The following methods provide functionality required to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * iterate over a the elements of the form and in the case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * of a form submission, extract the data from each model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * that is associated with each form element, and in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * case of reset, reinitialize the each model to its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * initial state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * Returns the Element representing the <code>FORM</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    private Element getFormElement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        Element elem = getElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        while (elem != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            if (elem.getAttributes().getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                (StyleConstants.NameAttribute) == HTML.Tag.FORM) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                return elem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            elem = elem.getParentElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * Iterates over the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * element hierarchy, extracting data from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * models associated with the relevant form elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * "Relevant" means the form elements that are part
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * of the same form whose element triggered the submit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * action.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * @param buffer        the buffer that contains that data to submit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * @param targetElement the element that triggered the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     *                      form submission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     */
7014
eb4fcf73ee99 6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents: 5506
diff changeset
   658
    private void getFormData(StringBuilder buffer) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        Element formE = getFormElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        if (formE != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            ElementIterator it = new ElementIterator(formE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            Element next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            while ((next = it.next()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                if (isControl(next)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                    String type = (String)next.getAttributes().getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                                       (HTML.Attribute.TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                    if (type != null && type.equals("submit") &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                        next != getElement()) {
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20158
diff changeset
   671
                        // do nothing - this submit is not the trigger
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                    } else if (type == null || !type.equals("image")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                        // images only result in data if they triggered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                        // the submit and they require that the mouse click
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                        // coords be appended to the data.  Hence its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                        // processing is handled by the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                        loadElementDataIntoBuffer(next, buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * Loads the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * associated with the element into the buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * The format in which data is appended depends
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * on the type of the form element.  Essentially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * data is loaded in name/value pairs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     */
7014
eb4fcf73ee99 6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents: 5506
diff changeset
   692
    private void loadElementDataIntoBuffer(Element elem, StringBuilder buffer) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        AttributeSet attr = elem.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        String name = (String)attr.getAttribute(HTML.Attribute.NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        String value = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        HTML.Tag tag = (HTML.Tag)elem.getAttributes().getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                                  (StyleConstants.NameAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        if (tag == HTML.Tag.INPUT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            value = getInputElementData(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        } else if (tag ==  HTML.Tag.TEXTAREA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            value = getTextAreaData(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        } else if (tag == HTML.Tag.SELECT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            loadSelectData(attr, buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        if (name != null && value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            appendBuffer(buffer, name, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * Returns the data associated with an &lt;INPUT&gt; form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * element.  The value of "type" attributes is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * used to determine the type of the model associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * with the element and then the relevant data is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * extracted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    private String getInputElementData(AttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        Object model = attr.getAttribute(StyleConstants.ModelAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        String type = (String) attr.getAttribute(HTML.Attribute.TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        String value = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        if (type.equals("text") || type.equals("password")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            Document doc = (Document)model;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                value = doc.getText(0, doc.getLength());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                value = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        } else if (type.equals("submit") || type.equals("hidden")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            value = (String) attr.getAttribute(HTML.Attribute.VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                value = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        } else if (type.equals("radio") || type.equals("checkbox")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            ButtonModel m = (ButtonModel)model;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            if (m.isSelected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                value = (String) attr.getAttribute(HTML.Attribute.VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                    value = "on";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        } else if (type.equals("file")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            Document doc = (Document)model;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            String path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                path = doc.getText(0, doc.getLength());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                path = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            if (path != null && path.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                value = path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * Returns the data associated with the &lt;TEXTAREA&gt; form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     * element.  This is done by getting the text stored in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * Document model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
    private String getTextAreaData(AttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        Document doc = (Document)attr.getAttribute(StyleConstants.ModelAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            return doc.getText(0, doc.getLength());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * Loads the buffer with the data associated with the Select
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * form element.  Basically, only items that are selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * and have their name attribute set are added to the buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     */
7014
eb4fcf73ee99 6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents: 5506
diff changeset
   786
    private void loadSelectData(AttributeSet attr, StringBuilder buffer) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        String name = (String)attr.getAttribute(HTML.Attribute.NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        Object m = attr.getAttribute(StyleConstants.ModelAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        if (m instanceof OptionListModel) {
25193
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 24269
diff changeset
   794
            @SuppressWarnings("unchecked")
10100
c525c5fbb86c 7031941: Use generificated JComboBox and JList in core libraries
rupashka
parents: 7668
diff changeset
   795
            OptionListModel<Option> model = (OptionListModel<Option>) m;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            for (int i = 0; i < model.getSize(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                if (model.isSelectedIndex(i)) {
10100
c525c5fbb86c 7031941: Use generificated JComboBox and JList in core libraries
rupashka
parents: 7668
diff changeset
   799
                    Option option = model.getElementAt(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                    appendBuffer(buffer, name, option.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        } else if (m instanceof ComboBoxModel) {
25193
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 24269
diff changeset
   804
            @SuppressWarnings("unchecked")
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 24269
diff changeset
   805
            ComboBoxModel<?> model = (ComboBoxModel)m;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            Option option = (Option)model.getSelectedItem();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
            if (option != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                appendBuffer(buffer, name, option.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     * Appends name / value pairs into the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     * buffer.  Both names and values are encoded using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * URLEncoder.encode() method before being added to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     * buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     */
7014
eb4fcf73ee99 6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents: 5506
diff changeset
   819
    private void appendBuffer(StringBuilder buffer, String name, String value) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        if (buffer.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
            buffer.append('&');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        String encodedName = URLEncoder.encode(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        buffer.append(encodedName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        buffer.append('=');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        String encodedValue = URLEncoder.encode(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        buffer.append(encodedValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     * Returns true if the Element <code>elem</code> represents a control.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
    private boolean isControl(Element elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        return elem.isLeaf();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * Iterates over the element hierarchy to determine if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     * the element parameter, which is assumed to be an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     * &lt;INPUT&gt; element of type password or text, is the last
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * one of either kind, in the form to which it belongs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
    boolean isLastTextOrPasswordField() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        Element parent = getFormElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        Element elem = getElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
            ElementIterator it = new ElementIterator(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            Element next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
            boolean found = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
            while ((next = it.next()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                if (next == elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                    found = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                else if (found && isControl(next)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                    AttributeSet elemAttr = next.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                    if (HTMLDocument.matchNameAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                                     (elemAttr, HTML.Tag.INPUT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                        String type = (String)elemAttr.getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                                                  (HTML.Attribute.TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
                        if ("text".equals(type) || "password".equals(type)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * Resets the form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * to its initial state by reinitializing the models
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     * associated with each form element to their initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     * values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * param elem the element that triggered the reset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
    void resetForm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        Element parent = getFormElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
            ElementIterator it = new ElementIterator(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
            Element next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
            while((next = it.next()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
                if (isControl(next)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                    AttributeSet elemAttr = next.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                    Object m = elemAttr.getAttribute(StyleConstants.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                                                     ModelAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                    if (m instanceof TextAreaDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                        TextAreaDocument doc = (TextAreaDocument)m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                        doc.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                    } else if (m instanceof PlainDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                            PlainDocument doc =  (PlainDocument)m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                            doc.remove(0, doc.getLength());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                            if (HTMLDocument.matchNameAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                                             (elemAttr, HTML.Tag.INPUT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                                String value = (String)elemAttr.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                                           getAttribute(HTML.Attribute.VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
                                if (value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                                    doc.insertString(0, value, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                        } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                    } else if (m instanceof OptionListModel) {
25193
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 24269
diff changeset
   912
                        @SuppressWarnings("unchecked")
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 24269
diff changeset
   913
                        OptionListModel<?> model = (OptionListModel) m;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
                        int size = model.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
                        for (int i = 0; i < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
                            model.removeIndexInterval(i, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
                        BitSet selectionRange = model.getInitialSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
                        for (int i = 0; i < selectionRange.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
                            if (selectionRange.get(i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
                                model.addSelectionInterval(i, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
                    } else if (m instanceof OptionComboBoxModel) {
25193
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 24269
diff changeset
   925
                        @SuppressWarnings("unchecked")
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 24269
diff changeset
   926
                        OptionComboBoxModel<?> model = (OptionComboBoxModel) m;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                        Option option = model.getInitialSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                        if (option != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                            model.setSelectedItem(option);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
                    } else if (m instanceof JToggleButton.ToggleButtonModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                        boolean checked = ((String)elemAttr.getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
                                           (HTML.Attribute.CHECKED) != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                        JToggleButton.ToggleButtonModel model =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                                        (JToggleButton.ToggleButtonModel)m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
                        model.setSelected(checked);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * BrowseFileAction is used for input type == file. When the user
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     * clicks the button a JFileChooser is brought up allowing the user
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     * to select a file in the file system. The resulting path to the selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     * file is set in the text field (actually an instance of Document).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
    private class BrowseFileAction implements ActionListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        private AttributeSet attrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        private Document model;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        BrowseFileAction(AttributeSet attrs, Document model) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
            this.attrs = attrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
            this.model = model;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        public void actionPerformed(ActionEvent ae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
            // PENDING: When mime support is added to JFileChooser use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
            // accept value of attrs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
            JFileChooser fc = new JFileChooser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
            fc.setMultiSelectionEnabled(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
            if (fc.showOpenDialog(getContainer()) ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                  JFileChooser.APPROVE_OPTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                File selected = fc.getSelectedFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
                if (selected != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
                        if (model.getLength() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                            model.remove(0, model.getLength());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                        model.insertString(0, selected.getPath(), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
                    } catch (BadLocationException ble) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
}