jdk/src/share/classes/javax/swing/text/html/FormView.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 7014 eb4fcf73ee99
child 10100 c525c5fbb86c
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
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);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if (t == HTML.Tag.INPUT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            c = createInputComponent(attr, model);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        } else if (t == HTML.Tag.SELECT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            if (model instanceof OptionListModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                JList list = new JList((ListModel) model);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                int size = HTML.getIntegerAttributeValue(attr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                                                         HTML.Attribute.SIZE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                                                         1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                list.setVisibleRowCount(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                list.setSelectionModel((ListSelectionModel)model);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                c = new JScrollPane(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                c = new JComboBox((ComboBoxModel) model);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                maxIsPreferred = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        } else if (t == HTML.Tag.TEXTAREA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            JTextArea area = new JTextArea((Document) model);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            int rows = HTML.getIntegerAttributeValue(attr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                                                     HTML.Attribute.ROWS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                                                     1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            area.setRows(rows);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            int cols = HTML.getIntegerAttributeValue(attr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                                                     HTML.Attribute.COLS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                                                     20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            maxIsPreferred = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            area.setColumns(cols);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            c = new JScrollPane(area,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                                JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            c.setAlignmentY(1.0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * Creates a component for an &lt;INPUT&gt; element based on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * value of the "type" attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * @param set of attributes associated with the &lt;INPUT&gt; element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @param model the value of the StyleConstants.ModelAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @return the component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    private JComponent createInputComponent(AttributeSet attr, Object model) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        JComponent c = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        String type = (String) attr.getAttribute(HTML.Attribute.TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        if (type.equals("submit") || type.equals("reset")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            String value = (String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                attr.getAttribute(HTML.Attribute.VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                if (type.equals("submit")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                    value = UIManager.getString("FormView.submitButtonText");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                    value = UIManager.getString("FormView.resetButtonText");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            JButton button = new JButton(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            if (model != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                button.setModel((ButtonModel)model);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                button.addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            c = button;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            maxIsPreferred = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        } else if (type.equals("image")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            String srcAtt = (String) attr.getAttribute(HTML.Attribute.SRC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            JButton button;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                URL base = ((HTMLDocument)getElement().getDocument()).getBase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                URL srcURL = new URL(base, srcAtt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                Icon icon = new ImageIcon(srcURL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                button  = new JButton(icon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            } catch (MalformedURLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                button = new JButton(srcAtt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            if (model != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                button.setModel((ButtonModel)model);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                button.addMouseListener(new MouseEventListener());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            c = button;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            maxIsPreferred = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        } else if (type.equals("checkbox")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            c = new JCheckBox();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            if (model != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                ((JCheckBox)c).setModel((JToggleButton.ToggleButtonModel) model);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            maxIsPreferred = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        } else if (type.equals("radio")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            c = new JRadioButton();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            if (model != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                ((JRadioButton)c).setModel((JToggleButton.ToggleButtonModel)model);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            maxIsPreferred = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        } else if (type.equals("text")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            int size = HTML.getIntegerAttributeValue(attr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                                                     HTML.Attribute.SIZE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                                                     -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            JTextField field;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            if (size > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                field = new JTextField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                field.setColumns(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                field = new JTextField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                field.setColumns(20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            c = field;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            if (model != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                field.setDocument((Document) model);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            field.addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            maxIsPreferred = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        } else if (type.equals("password")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            JPasswordField field = new JPasswordField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            c = field;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            if (model != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                field.setDocument((Document) model);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            int size = HTML.getIntegerAttributeValue(attr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                                                     HTML.Attribute.SIZE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                                                     -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            field.setColumns((size > 0) ? size : 20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            field.addActionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            maxIsPreferred = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        } else if (type.equals("file")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            JTextField field = new JTextField();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            if (model != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                field.setDocument((Document)model);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            int size = HTML.getIntegerAttributeValue(attr, HTML.Attribute.SIZE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                                                     -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            field.setColumns((size > 0) ? size : 20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            JButton browseButton = new JButton(UIManager.getString
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                                           ("FormView.browseFileButtonText"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            Box box = Box.createHorizontalBox();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            box.add(field);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            box.add(Box.createHorizontalStrut(5));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            box.add(browseButton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            browseButton.addActionListener(new BrowseFileAction(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                                           attr, (Document)model));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            c = box;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            maxIsPreferred = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * Determines the maximum span for this view along an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * axis. For certain components, the maximum and preferred span are the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * same. For others this will return the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * returned by Component.getMaximumSize along the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * axis of interest.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * @param axis may be either View.X_AXIS or View.Y_AXIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * @return   the span the view would like to be rendered into >= 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     *           Typically the view is told to render into the span
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     *           that is returned, although there is no guarantee.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     *           The parent may choose to resize or break the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * @exception IllegalArgumentException for an invalid axis
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    public float getMaximumSpan(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        switch (axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        case View.X_AXIS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            if ((maxIsPreferred & 1) == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                super.getMaximumSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                return getPreferredSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            return super.getMaximumSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        case View.Y_AXIS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            if ((maxIsPreferred & 2) == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                super.getMaximumSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                return getPreferredSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            return super.getMaximumSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        return super.getMaximumSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * Responsible for processeing the ActionEvent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * If the element associated with the FormView,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * has a type of "submit", "reset", "text" or "password"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * then the action is processed.  In the case of a "submit"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * the form is submitted.  In the case of a "reset"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * the form is reset to its original state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * In the case of "text" or "password", if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * element is the last one of type "text" or "password",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * the form is submitted.  Otherwise, focus is transferred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * to the next component in the form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * @param evt the ActionEvent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    public void actionPerformed(ActionEvent evt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        Element element = getElement();
7014
eb4fcf73ee99 6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents: 5506
diff changeset
   365
        StringBuilder dataBuffer = new StringBuilder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        HTMLDocument doc = (HTMLDocument)getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        AttributeSet attr = element.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        String type = (String) attr.getAttribute(HTML.Attribute.TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        if (type.equals("submit")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            getFormData(dataBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            submitData(dataBuffer.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        } else if (type.equals("reset")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            resetForm();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        } else if (type.equals("text") || type.equals("password")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            if (isLastTextOrPasswordField()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                getFormData(dataBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                submitData(dataBuffer.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                getComponent().transferFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * This method is responsible for submitting the form data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * A thread is forked to undertake the submission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    protected void submitData(String data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        Element form = getFormElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        AttributeSet attrs = form.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        HTMLDocument doc = (HTMLDocument) form.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        URL base = doc.getBase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        String target = (String) attrs.getAttribute(HTML.Attribute.TARGET);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        if (target == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            target = "_self";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        String method = (String) attrs.getAttribute(HTML.Attribute.METHOD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        if (method == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            method = "GET";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        method = method.toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        boolean isPostMethod = method.equals("post");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        if (isPostMethod) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            storePostData(doc, target, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        String action = (String) attrs.getAttribute(HTML.Attribute.ACTION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        URL actionURL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            actionURL = (action == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                ? new URL(base.getProtocol(), base.getHost(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                                        base.getPort(), base.getFile())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                : new URL(base, action);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            if (!isPostMethod) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                String query = data.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                actionURL = new URL(actionURL + "?" + query);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        } catch (MalformedURLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            actionURL = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        final JEditorPane c = (JEditorPane) getContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        HTMLEditorKit kit = (HTMLEditorKit) c.getEditorKit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        FormSubmitEvent formEvent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        if (!kit.isAutoFormSubmission() || doc.isFrameDocument()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            FormSubmitEvent.MethodType methodType = isPostMethod
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                    ? FormSubmitEvent.MethodType.POST
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                    : FormSubmitEvent.MethodType.GET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            formEvent = new FormSubmitEvent(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                    FormView.this, HyperlinkEvent.EventType.ACTIVATED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                    actionURL, form, target, methodType, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        // setPage() may take significant time so schedule it to run later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        final FormSubmitEvent fse = formEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        final URL url = actionURL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        SwingUtilities.invokeLater(new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                if (fse != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                    c.fireHyperlinkUpdate(fse);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                        c.setPage(url);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                    } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                        UIManager.getLookAndFeel().provideErrorFeedback(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    private void storePostData(HTMLDocument doc, String target, String data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        /* POST data is stored into the document property named by constant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
         * PostDataProperty from where it is later retrieved by method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
         * JEditorPane.getPostData().  If the current document is in a frame,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
         * the data is initially put into the toplevel (frameset) document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
         * property (named <PostDataProperty>.<Target frame name>).  It is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
         * responsibility of FrameView which updates the target frame
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
         * to move data from the frameset document property into the frame
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
         * document property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        Document propDoc = doc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        String propName = PostDataProperty;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        if (doc.isFrameDocument()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            // find the top-most JEditorPane holding the frameset view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            FrameView.FrameEditorPane p =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                    (FrameView.FrameEditorPane) getContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            FrameView v = p.getFrameView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            JEditorPane c = v.getOutermostJEditorPane();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                propDoc = c.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                propName += ("." + target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        propDoc.putProperty(propName, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * MouseEventListener class to handle form submissions when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * an input with type equal to image is clicked on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * A MouseListener is necessary since along with the image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * data the coordinates associated with the mouse click
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * need to be submitted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    protected class MouseEventListener extends MouseAdapter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        public void mouseReleased(MouseEvent evt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            String imageData = getImageData(evt.getPoint());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            imageSubmit(imageData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * This method is called to submit a form in response
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * to a click on an image -- an &lt;INPUT&gt; form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * element of type "image".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * @param imageData the mouse click coordinates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    protected void imageSubmit(String imageData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
7014
eb4fcf73ee99 6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents: 5506
diff changeset
   511
        StringBuilder dataBuffer = new StringBuilder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        Element elem = getElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        HTMLDocument hdoc = (HTMLDocument)elem.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        getFormData(dataBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        if (dataBuffer.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            dataBuffer.append('&');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        dataBuffer.append(imageData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        submitData(dataBuffer.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * Extracts the value of the name attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * associated with the input element of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * image.  If name is defined it is encoded using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * the URLEncoder.encode() method and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * image data is returned in the following format:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     *      name + ".x" +"="+ x +"&"+ name +".y"+"="+ y
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     *      "x="+ x +"&y="+ y
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * @param point associated with the mouse click.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * @return the image data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    private String getImageData(Point point) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        String mouseCoords = point.x + ":" + point.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        int sep = mouseCoords.indexOf(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        String x = mouseCoords.substring(0, sep);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        String y = mouseCoords.substring(++sep);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        String name = (String) getElement().getAttributes().getAttribute(HTML.Attribute.NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        String data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        if (name == null || name.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            data = "x="+ x +"&y="+ y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            name = URLEncoder.encode(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            data = name + ".x" +"="+ x +"&"+ name +".y"+"="+ y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        return data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * The following methods provide functionality required to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * iterate over a the elements of the form and in the case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * of a form submission, extract the data from each model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * that is associated with each form element, and in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * case of reset, reinitialize the each model to its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * initial state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * Returns the Element representing the <code>FORM</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    private Element getFormElement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        Element elem = getElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        while (elem != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            if (elem.getAttributes().getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                (StyleConstants.NameAttribute) == HTML.Tag.FORM) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                return elem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            elem = elem.getParentElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * Iterates over the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * element hierarchy, extracting data from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * models associated with the relevant form elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * "Relevant" means the form elements that are part
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * of the same form whose element triggered the submit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * action.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * @param buffer        the buffer that contains that data to submit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * @param targetElement the element that triggered the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     *                      form submission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     */
7014
eb4fcf73ee99 6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents: 5506
diff changeset
   592
    private void getFormData(StringBuilder buffer) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        Element formE = getFormElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        if (formE != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            ElementIterator it = new ElementIterator(formE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            Element next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            while ((next = it.next()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                if (isControl(next)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                    String type = (String)next.getAttributes().getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                                       (HTML.Attribute.TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                    if (type != null && type.equals("submit") &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                        next != getElement()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                        // do nothing - this submit isnt the trigger
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                    } else if (type == null || !type.equals("image")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                        // images only result in data if they triggered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                        // the submit and they require that the mouse click
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                        // coords be appended to the data.  Hence its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                        // processing is handled by the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                        loadElementDataIntoBuffer(next, buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                    }
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * Loads the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * associated with the element into the buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * The format in which data is appended depends
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * on the type of the form element.  Essentially
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * data is loaded in name/value pairs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     */
7014
eb4fcf73ee99 6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents: 5506
diff changeset
   626
    private void loadElementDataIntoBuffer(Element elem, StringBuilder buffer) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        AttributeSet attr = elem.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        String name = (String)attr.getAttribute(HTML.Attribute.NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        String value = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        HTML.Tag tag = (HTML.Tag)elem.getAttributes().getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                                  (StyleConstants.NameAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        if (tag == HTML.Tag.INPUT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            value = getInputElementData(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        } else if (tag ==  HTML.Tag.TEXTAREA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            value = getTextAreaData(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        } else if (tag == HTML.Tag.SELECT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            loadSelectData(attr, buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        if (name != null && value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            appendBuffer(buffer, name, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * Returns the data associated with an &lt;INPUT&gt; form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * element.  The value of "type" attributes is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * used to determine the type of the model associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * with the element and then the relevant data is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * extracted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    private String getInputElementData(AttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        Object model = attr.getAttribute(StyleConstants.ModelAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        String type = (String) attr.getAttribute(HTML.Attribute.TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        String value = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        if (type.equals("text") || type.equals("password")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            Document doc = (Document)model;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                value = doc.getText(0, doc.getLength());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                value = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        } else if (type.equals("submit") || type.equals("hidden")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            value = (String) attr.getAttribute(HTML.Attribute.VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                value = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        } else if (type.equals("radio") || type.equals("checkbox")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            ButtonModel m = (ButtonModel)model;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            if (m.isSelected()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                value = (String) attr.getAttribute(HTML.Attribute.VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                    value = "on";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        } else if (type.equals("file")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            Document doc = (Document)model;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            String path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                path = doc.getText(0, doc.getLength());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                path = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
            if (path != null && path.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                value = path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     * Returns the data associated with the &lt;TEXTAREA&gt; form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * element.  This is done by getting the text stored in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * Document model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    private String getTextAreaData(AttributeSet attr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        Document doc = (Document)attr.getAttribute(StyleConstants.ModelAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            return doc.getText(0, doc.getLength());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * Loads the buffer with the data associated with the Select
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * form element.  Basically, only items that are selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * and have their name attribute set are added to the buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     */
7014
eb4fcf73ee99 6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents: 5506
diff changeset
   720
    private void loadSelectData(AttributeSet attr, StringBuilder buffer) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        String name = (String)attr.getAttribute(HTML.Attribute.NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        Object m = attr.getAttribute(StyleConstants.ModelAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        if (m instanceof OptionListModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            OptionListModel model = (OptionListModel)m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            for (int i = 0; i < model.getSize(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                if (model.isSelectedIndex(i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                    Option option = (Option) model.getElementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                    appendBuffer(buffer, name, option.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        } else if (m instanceof ComboBoxModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            ComboBoxModel model = (ComboBoxModel)m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            Option option = (Option)model.getSelectedItem();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            if (option != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                appendBuffer(buffer, name, option.getValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     * Appends name / value pairs into the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     * buffer.  Both names and values are encoded using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * URLEncoder.encode() method before being added to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     * buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     */
7014
eb4fcf73ee99 6432566: Replace usage of StringBuffer with StringBuilder in Swing
rupashka
parents: 5506
diff changeset
   751
    private void appendBuffer(StringBuilder buffer, String name, String value) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        if (buffer.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            buffer.append('&');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        String encodedName = URLEncoder.encode(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        buffer.append(encodedName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        buffer.append('=');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        String encodedValue = URLEncoder.encode(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        buffer.append(encodedValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * Returns true if the Element <code>elem</code> represents a control.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
    private boolean isControl(Element elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        return elem.isLeaf();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * Iterates over the element hierarchy to determine if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     * the element parameter, which is assumed to be an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * &lt;INPUT&gt; element of type password or text, is the last
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     * one of either kind, in the form to which it belongs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
    boolean isLastTextOrPasswordField() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        Element parent = getFormElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        Element elem = getElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            ElementIterator it = new ElementIterator(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            Element next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            boolean found = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            while ((next = it.next()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                if (next == elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                    found = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                else if (found && isControl(next)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                    AttributeSet elemAttr = next.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                    if (HTMLDocument.matchNameAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                                     (elemAttr, HTML.Tag.INPUT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                        String type = (String)elemAttr.getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                                                  (HTML.Attribute.TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                        if ("text".equals(type) || "password".equals(type)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        return true;
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
     * Resets the form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * to its initial state by reinitializing the models
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * associated with each form element to their initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     * param elem the element that triggered the reset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    void resetForm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        Element parent = getFormElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
            ElementIterator it = new ElementIterator(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            Element next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
            while((next = it.next()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                if (isControl(next)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                    AttributeSet elemAttr = next.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                    Object m = elemAttr.getAttribute(StyleConstants.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                                                     ModelAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                    if (m instanceof TextAreaDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                        TextAreaDocument doc = (TextAreaDocument)m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                        doc.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                    } else if (m instanceof PlainDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                            PlainDocument doc =  (PlainDocument)m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                            doc.remove(0, doc.getLength());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                            if (HTMLDocument.matchNameAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                                             (elemAttr, HTML.Tag.INPUT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                                String value = (String)elemAttr.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                                           getAttribute(HTML.Attribute.VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                                if (value != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                                    doc.insertString(0, value, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                        } catch (BadLocationException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                    } else if (m instanceof OptionListModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                        OptionListModel model = (OptionListModel) m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                        int size = model.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                        for (int i = 0; i < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                            model.removeIndexInterval(i, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                        BitSet selectionRange = model.getInitialSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                        for (int i = 0; i < selectionRange.size(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                            if (selectionRange.get(i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                                model.addSelectionInterval(i, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                    } else if (m instanceof OptionComboBoxModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                        OptionComboBoxModel model = (OptionComboBoxModel) m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                        Option option = model.getInitialSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                        if (option != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                            model.setSelectedItem(option);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                    } else if (m instanceof JToggleButton.ToggleButtonModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                        boolean checked = ((String)elemAttr.getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
                                           (HTML.Attribute.CHECKED) != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
                        JToggleButton.ToggleButtonModel model =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                                        (JToggleButton.ToggleButtonModel)m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                        model.setSelected(checked);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * BrowseFileAction is used for input type == file. When the user
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * clicks the button a JFileChooser is brought up allowing the user
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     * to select a file in the file system. The resulting path to the selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     * file is set in the text field (actually an instance of Document).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
    private class BrowseFileAction implements ActionListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        private AttributeSet attrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        private Document model;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        BrowseFileAction(AttributeSet attrs, Document model) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
            this.attrs = attrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
            this.model = model;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        public void actionPerformed(ActionEvent ae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            // PENDING: When mime support is added to JFileChooser use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
            // accept value of attrs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
            JFileChooser fc = new JFileChooser();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
            fc.setMultiSelectionEnabled(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
            if (fc.showOpenDialog(getContainer()) ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                  JFileChooser.APPROVE_OPTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                File selected = fc.getSelectedFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                if (selected != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                        if (model.getLength() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                            model.remove(0, model.getLength());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                        model.insertString(0, selected.getPath(), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                    } catch (BadLocationException ble) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
}