Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / javax / swing / text / html / FormView.java
blobb85c6943404951328ba2aecb9d95d0358cd8d769
1 /* FormView.java -- A view for a variety of HTML form elements
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.swing.text.html;
41 import java.awt.Component;
42 import java.awt.event.ActionEvent;
43 import java.awt.event.ActionListener;
45 import javax.swing.JButton;
46 import javax.swing.JCheckBox;
47 import javax.swing.JPasswordField;
48 import javax.swing.JRadioButton;
49 import javax.swing.JTextField;
50 import javax.swing.UIManager;
51 import javax.swing.text.AttributeSet;
52 import javax.swing.text.ComponentView;
53 import javax.swing.text.Element;
54 import javax.swing.text.StyleConstants;
56 /**
57 * A View that renders HTML form elements like buttons and input fields.
58 * This is implemented as a {@link ComponentView} that creates different Swing
59 * component depending on the type and setting of the different form elements.
61 * Namely, this view creates the following components:
62 * <table>
63 * <tr><th>Element type</th><th>Swing component</th></tr>
64 * <tr><td>input, button</td><td>JButton</td></tr>
65 * <tr><td>input, checkbox</td><td>JButton</td></tr>
66 * <tr><td>input, image</td><td>JButton</td></tr>
67 * <tr><td>input, password</td><td>JButton</td></tr>
68 * <tr><td>input, radio</td><td>JButton</td></tr>
69 * <tr><td>input, reset</td><td>JButton</td></tr>
70 * <tr><td>input, submit</td><td>JButton</td></tr>
71 * <tr><td>input, text</td><td>JButton</td></tr>
72 * <tr><td>select, size > 1 or with multiple attribute</td>
73 * <td>JList in JScrollPane</td></tr>
74 * <tr><td>select, size unspecified or == 1</td><td>JComboBox</td></tr>
75 * <tr><td>textarea, text</td><td>JTextArea in JScrollPane</td></tr>
76 * <tr><td>input, file</td><td>JTextField</td></tr>
77 * </table>
79 * @author Roman Kennke (kennke@aicas.com)
81 public class FormView
82 extends ComponentView
83 implements ActionListener
86 /**
87 * If the value attribute of an <code>&lt;input type=&quot;submit&quot;&gt>
88 * tag is not specified, then this string is used.
90 * @deprecated As of JDK1.3 the value is fetched from the UIManager property
91 * <code>FormView.submitButtonText</code>.
93 public static final String SUBMIT =
94 UIManager.getString("FormView.submitButtonText");
96 /**
97 * If the value attribute of an <code>&lt;input type=&quot;reset&quot;&gt>
98 * tag is not specified, then this string is used.
100 * @deprecated As of JDK1.3 the value is fetched from the UIManager property
101 * <code>FormView.resetButtonText</code>.
103 public static final String RESET =
104 UIManager.getString("FormView.resetButtonText");
107 * Creates a new <code>FormView</code>.
109 * @param el the element that is displayed by this view.
111 public FormView(Element el)
113 super(el);
117 * Creates the correct AWT component for rendering the form element.
119 protected Component createComponent()
121 Component comp = null;
122 Element el = getElement();
123 Object tag = el.getAttributes().getAttribute(StyleConstants.NameAttribute);
124 if (tag.equals(HTML.Tag.INPUT))
126 AttributeSet atts = el.getAttributes();
127 String type = (String) atts.getAttribute(HTML.Attribute.TYPE);
128 String value = (String) atts.getAttribute(HTML.Attribute.VALUE);
129 if (type.equals("button"))
130 comp = new JButton(value);
131 else if (type.equals("checkbox"))
132 comp = new JCheckBox(value);
133 else if (type.equals("image"))
134 comp = new JButton(value); // FIXME: Find out how to fetch the image.
135 else if (type.equals("password"))
136 comp = new JPasswordField(value);
137 else if (type.equals("radio"))
138 comp = new JRadioButton(value);
139 else if (type.equals("reset"))
141 if (value == null || value.equals(""))
142 value = RESET;
143 comp = new JButton(value);
145 else if (type.equals("submit"))
147 if (value == null || value.equals(""))
148 value = SUBMIT;
149 comp = new JButton(value);
151 else if (type.equals("text"))
152 comp = new JTextField(value);
155 // FIXME: Implement the remaining components.
156 return comp;
160 * Determines the maximum span for this view on the specified axis.
162 * @param axis the axis along which to determine the span
164 * @return the maximum span for this view on the specified axis
166 * @throws IllegalArgumentException if the axis is invalid
168 public float getMaximumSpan(int axis)
170 // FIXME: The specs say that for some components the maximum span == the
171 // preferred span of the component. This should be figured out and
172 // implemented accordingly.
173 float span;
174 if (axis == X_AXIS)
175 span = getComponent().getMaximumSize().width;
176 else if (axis == Y_AXIS)
177 span = getComponent().getMaximumSize().height;
178 else
179 throw new IllegalArgumentException("Invalid axis parameter");
180 return span;
184 * Processes an action from the Swing component.
186 * If the action comes from a submit button, the form is submitted by calling
187 * {@link #submitData}. In the case of a reset button, the form is reset to
188 * the original state. If the action comes from a password or text field,
189 * then the input focus is transferred to the next input element in the form,
190 * unless this text/password field is the last one, in which case the form
191 * is submitted.
193 * @param ev the action event
195 public void actionPerformed(ActionEvent ev)
197 Element el = getElement();
198 Object tag = el.getAttributes().getAttribute(StyleConstants.NameAttribute);
199 if (tag.equals(HTML.Tag.INPUT))
201 AttributeSet atts = el.getAttributes();
202 String type = (String) atts.getAttribute(HTML.Attribute.TYPE);
203 if (type.equals("submit"))
204 submitData(""); // FIXME: How to fetch the actual form data?
206 // FIXME: Implement the remaining actions.
210 * Submits the form data. A separate thread is created to do the
211 * transmission.
213 * @param data the form data
215 protected void submitData(String data)
217 // FIXME: Implement this.
221 * Submits the form data in response to a click on a
222 * <code>&lt;input type=&quot;image&quot;&gt;</code> element.
224 * @param imageData the mouse click coordinates
226 protected void imageSubmit(String imageData)
228 // FIXME: Implement this.