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)
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
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
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.Point;
43 import java.awt.event.ActionEvent;
44 import java.awt.event.ActionListener;
45 import java.awt.event.MouseAdapter;
46 import java.awt.event.MouseEvent;
48 import javax.swing.JButton;
49 import javax.swing.JCheckBox;
50 import javax.swing.JPasswordField;
51 import javax.swing.JRadioButton;
52 import javax.swing.JTextField;
53 import javax.swing.UIManager;
54 import javax.swing.text.AttributeSet;
55 import javax.swing.text.ComponentView;
56 import javax.swing.text.Element;
57 import javax.swing.text.StyleConstants;
60 * A View that renders HTML form elements like buttons and input fields.
61 * This is implemented as a {@link ComponentView} that creates different Swing
62 * component depending on the type and setting of the different form elements.
64 * Namely, this view creates the following components:
66 * <tr><th>Element type</th><th>Swing component</th></tr>
67 * <tr><td>input, button</td><td>JButton</td></tr>
68 * <tr><td>input, checkbox</td><td>JButton</td></tr>
69 * <tr><td>input, image</td><td>JButton</td></tr>
70 * <tr><td>input, password</td><td>JButton</td></tr>
71 * <tr><td>input, radio</td><td>JButton</td></tr>
72 * <tr><td>input, reset</td><td>JButton</td></tr>
73 * <tr><td>input, submit</td><td>JButton</td></tr>
74 * <tr><td>input, text</td><td>JButton</td></tr>
75 * <tr><td>select, size > 1 or with multiple attribute</td>
76 * <td>JList in JScrollPane</td></tr>
77 * <tr><td>select, size unspecified or == 1</td><td>JComboBox</td></tr>
78 * <tr><td>textarea, text</td><td>JTextArea in JScrollPane</td></tr>
79 * <tr><td>input, file</td><td>JTextField</td></tr>
82 * @author Roman Kennke (kennke@aicas.com)
86 implements ActionListener
89 protected class MouseEventListener
93 * Creates a new <code>MouseEventListener</code>.
95 protected MouseEventListener()
97 // Nothing to do here.
100 public void mouseReleased(MouseEvent ev)
102 String data = getImageData(ev.getPoint());
108 * If the value attribute of an <code><input type="submit">>
109 * tag is not specified, then this string is used.
111 * @deprecated As of JDK1.3 the value is fetched from the UIManager property
112 * <code>FormView.submitButtonText</code>.
114 public static final String SUBMIT =
115 UIManager.getString("FormView.submitButtonText");
118 * If the value attribute of an <code><input type="reset">>
119 * tag is not specified, then this string is used.
121 * @deprecated As of JDK1.3 the value is fetched from the UIManager property
122 * <code>FormView.resetButtonText</code>.
124 public static final String RESET =
125 UIManager.getString("FormView.resetButtonText");
128 * Creates a new <code>FormView</code>.
130 * @param el the element that is displayed by this view.
132 public FormView(Element el)
138 * Creates the correct AWT component for rendering the form element.
140 protected Component createComponent()
142 Component comp = null;
143 Element el = getElement();
144 Object tag = el.getAttributes().getAttribute(StyleConstants.NameAttribute);
145 if (tag.equals(HTML.Tag.INPUT))
147 AttributeSet atts = el.getAttributes();
148 String type = (String) atts.getAttribute(HTML.Attribute.TYPE);
149 String value = (String) atts.getAttribute(HTML.Attribute.VALUE);
150 if (type.equals("button"))
151 comp = new JButton(value);
152 else if (type.equals("checkbox"))
153 comp = new JCheckBox(value);
154 else if (type.equals("image"))
155 comp = new JButton(value); // FIXME: Find out how to fetch the image.
156 else if (type.equals("password"))
157 comp = new JPasswordField(value);
158 else if (type.equals("radio"))
159 comp = new JRadioButton(value);
160 else if (type.equals("reset"))
162 if (value == null || value.equals(""))
164 comp = new JButton(value);
166 else if (type.equals("submit"))
168 if (value == null || value.equals(""))
170 comp = new JButton(value);
172 else if (type.equals("text"))
173 comp = new JTextField(value);
176 // FIXME: Implement the remaining components.
181 * Determines the maximum span for this view on the specified axis.
183 * @param axis the axis along which to determine the span
185 * @return the maximum span for this view on the specified axis
187 * @throws IllegalArgumentException if the axis is invalid
189 public float getMaximumSpan(int axis)
191 // FIXME: The specs say that for some components the maximum span == the
192 // preferred span of the component. This should be figured out and
193 // implemented accordingly.
196 span = getComponent().getMaximumSize().width;
197 else if (axis == Y_AXIS)
198 span = getComponent().getMaximumSize().height;
200 throw new IllegalArgumentException("Invalid axis parameter");
205 * Processes an action from the Swing component.
207 * If the action comes from a submit button, the form is submitted by calling
208 * {@link #submitData}. In the case of a reset button, the form is reset to
209 * the original state. If the action comes from a password or text field,
210 * then the input focus is transferred to the next input element in the form,
211 * unless this text/password field is the last one, in which case the form
214 * @param ev the action event
216 public void actionPerformed(ActionEvent ev)
218 Element el = getElement();
219 Object tag = el.getAttributes().getAttribute(StyleConstants.NameAttribute);
220 if (tag.equals(HTML.Tag.INPUT))
222 AttributeSet atts = el.getAttributes();
223 String type = (String) atts.getAttribute(HTML.Attribute.TYPE);
224 if (type.equals("submit"))
225 submitData(""); // FIXME: How to fetch the actual form data?
227 // FIXME: Implement the remaining actions.
231 * Submits the form data. A separate thread is created to do the
234 * @param data the form data
236 protected void submitData(String data)
238 // FIXME: Implement this.
242 * Submits the form data in response to a click on a
243 * <code><input type="image"></code> element.
245 * @param imageData the mouse click coordinates
247 protected void imageSubmit(String imageData)
249 // FIXME: Implement this.
253 * Determines the image data that should be submitted in response to a
254 * mouse click on a image. This is either 'x=<p.x>&y=<p.y>' if the name
255 * attribute of the element is null or '' or
256 * <name>.x=<p.x>&<name>.y=<p.y>' when the name attribute is not empty.
258 * @param p the coordinates of the mouseclick
260 String getImageData(Point p)
262 String name = (String) getElement().getAttributes()
263 .getAttribute(HTML.Attribute.NAME);
265 if (name == null || name.equals(""))
267 data = "x=" + p.x + "&y=" + p.y;
271 data = name + ".x=" + p.x + "&" + name + ".y=" + p.y;