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