OSDN Git Service

Imported GNU Classpath 0.90
[pf3gnuchains/gcc-fork.git] / libjava / classpath / javax / swing / text / html / FormView.java
1 /* FormView.java -- A view for a variety of HTML form elements
2    Copyright (C) 2006 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
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.
10
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.
15
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.
20
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.
25
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. */
37
38
39 package javax.swing.text.html;
40
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;
47
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;
58
59 /**
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.
63  *
64  * Namely, this view creates the following components:
65  * <table>
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> 
80  * </table>
81  *
82  * @author Roman Kennke (kennke@aicas.com)
83  */
84 public class FormView
85   extends ComponentView
86   implements ActionListener
87 {
88
89   protected class MouseEventListener
90     extends MouseAdapter
91   {
92     /**
93      * Creates a new <code>MouseEventListener</code>.
94      */
95     protected MouseEventListener()
96     {
97       // Nothing to do here.
98     }
99
100     public void mouseReleased(MouseEvent ev)
101     {
102       String data = getImageData(ev.getPoint());
103       imageSubmit(data);
104     }
105   }
106
107   /**
108    * If the value attribute of an <code>&lt;input type=&quot;submit&quot;&gt>
109    * tag is not specified, then this string is used.
110    * 
111    * @deprecated As of JDK1.3 the value is fetched from the UIManager property
112    *             <code>FormView.submitButtonText</code>.
113    */
114   public static final String SUBMIT =
115     UIManager.getString("FormView.submitButtonText");
116
117   /**
118    * If the value attribute of an <code>&lt;input type=&quot;reset&quot;&gt>
119    * tag is not specified, then this string is used.
120    * 
121    * @deprecated As of JDK1.3 the value is fetched from the UIManager property
122    *             <code>FormView.resetButtonText</code>.
123    */
124   public static final String RESET =
125     UIManager.getString("FormView.resetButtonText");
126
127   /**
128    * Creates a new <code>FormView</code>.
129    *
130    * @param el the element that is displayed by this view.
131    */
132   public FormView(Element el)
133   {
134     super(el);
135   }
136
137   /**
138    * Creates the correct AWT component for rendering the form element.
139    */
140   protected Component createComponent()
141   {
142     Component comp = null;
143     Element el = getElement();
144     Object tag = el.getAttributes().getAttribute(StyleConstants.NameAttribute);
145     if (tag.equals(HTML.Tag.INPUT))
146       {
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"))
161           {
162             if (value == null || value.equals(""))
163               value = RESET;
164             comp = new JButton(value);
165           }
166         else if (type.equals("submit"))
167           {
168             if (value == null || value.equals(""))
169               value = SUBMIT;
170             comp = new JButton(value);
171           }
172         else if (type.equals("text"))
173           comp = new JTextField(value);
174         
175       }
176     // FIXME: Implement the remaining components.
177     return comp;
178   }
179
180   /**
181    * Determines the maximum span for this view on the specified axis.
182    *
183    * @param axis the axis along which to determine the span
184    *
185    * @return the maximum span for this view on the specified axis
186    *
187    * @throws IllegalArgumentException if the axis is invalid
188    */
189   public float getMaximumSpan(int axis)
190   {
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.
194     float span;
195     if (axis == X_AXIS)
196       span = getComponent().getMaximumSize().width;
197     else if (axis == Y_AXIS)
198       span = getComponent().getMaximumSize().height;
199     else
200       throw new IllegalArgumentException("Invalid axis parameter");
201     return span;
202   }
203
204   /**
205    * Processes an action from the Swing component.
206    *
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
212    * is submitted.
213    *
214    * @param ev the action event
215    */
216   public void actionPerformed(ActionEvent ev)
217   {
218     Element el = getElement();
219     Object tag = el.getAttributes().getAttribute(StyleConstants.NameAttribute);
220     if (tag.equals(HTML.Tag.INPUT))
221       {
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?
226       }
227     // FIXME: Implement the remaining actions.
228   }
229
230   /**
231    * Submits the form data. A separate thread is created to do the
232    * transmission.
233    *
234    * @param data the form data
235    */
236   protected void submitData(String data)
237   {
238     // FIXME: Implement this.
239   }
240
241   /**
242    * Submits the form data in response to a click on a
243    * <code>&lt;input type=&quot;image&quot;&gt;</code> element.
244    *
245    * @param imageData the mouse click coordinates
246    */
247   protected void imageSubmit(String imageData)
248   {
249     // FIXME: Implement this.
250   }
251
252   /**
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.
257    *
258    * @param p the coordinates of the mouseclick
259    */
260   String getImageData(Point p)
261   {
262     String name = (String) getElement().getAttributes()
263                                             .getAttribute(HTML.Attribute.NAME);
264     String data;
265     if (name == null || name.equals(""))
266       {
267         data = "x=" + p.x + "&y=" + p.y;
268       }
269     else
270       {
271         data = name + ".x=" + p.x + "&" + name + ".y=" + p.y; 
272       }
273     return data;
274   }
275 }