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