📄 formview.java
字号:
/* * @(#)FormView.java 1.29 06/07/25 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package javax.swing.text.html;import java.net.*;import java.io.*;import java.awt.*;import java.awt.event.*;import java.util.*;import javax.swing.*;import javax.swing.event.*;import javax.swing.text.*;/** * Component decorator that implements the view interface * for form elements, <input>, <textarea>, * and <select>. The model for the component is stored * as an attribute of the the element (using StyleConstants.ModelAttribute), * and is used to build the component of the view. The type * of the model is assumed to of the type that would be set by * <code>HTMLDocument.HTMLReader.FormAction</code>. If there are * multiple views mapped over the document, they will share the * embedded component models. * <p> * The following table shows what components get built * by this view. * <table summary="shows what components get built by this view"> * <tr> * <th>Element Type</th> * <th>Component built</th> * </tr> * <tr> * <td>input, type button</td> * <td>JButton</td> * </tr> * <tr> * <td>input, type checkbox</td> * <td>JCheckBox</td> * </tr> * <tr> * <td>input, type image</td> * <td>JButton</td> * </tr> * <tr> * <td>input, type password</td> * <td>JPasswordField</td> * </tr> * <tr> * <td>input, type radio</td> * <td>JRadioButton</td> * </tr> * <tr> * <td>input, type reset</td> * <td>JButton</td> * </tr> * <tr> * <td>input, type submit</td> * <td>JButton</td> * </tr> * <tr> * <td>input, type text</td> * <td>JTextField</td> * </tr> * <tr> * <td>select, size > 1 or multiple attribute defined</td> * <td>JList in a JScrollPane</td> * </tr> * <tr> * <td>select, size unspecified or 1</td> * <td>JComboBox</td> * </tr> * <tr> * <td>textarea</td> * <td>JTextArea in a JScrollPane</td> * </tr> * <tr> * <td>input, type file</td> * <td>JTextField</td> * </tr> * </table> * * @author Timothy Prinzing * @author Sunita Mani * @version 1.29 07/25/06 */public class FormView extends ComponentView implements ActionListener { /** * If a value attribute is not specified for a FORM input element * of type "submit", then this default string is used. * * @deprecated As of 1.3, value now comes from UIManager property * FormView.submitButtonText */ @Deprecated public static final String SUBMIT = new String("Submit Query"); /** * If a value attribute is not specified for a FORM input element * of type "reset", then this default string is used. * * @deprecated As of 1.3, value comes from UIManager UIManager property * FormView.resetButtonText */ @Deprecated public static final String RESET = new String("Reset"); /** * Document attribute name for storing POST data. JEditorPane.getPostData() * uses the same name, should be kept in sync. */ final static String PostDataProperty = "javax.swing.JEditorPane.postdata"; /** * Used to indicate if the maximum span should be the same as the * preferred span. This is used so that the Component's size doesn't * change if there is extra room on a line. The first bit is used for * the X direction, and the second for the y direction. */ private short maxIsPreferred; /** * Creates a new FormView object. * * @param elem the element to decorate */ public FormView(Element elem) { super(elem); } /** * Create the component. This is basically a * big switch statement based upon the tag type * and html attributes of the associated element. */ protected Component createComponent() { AttributeSet attr = getElement().getAttributes(); HTML.Tag t = (HTML.Tag) attr.getAttribute(StyleConstants.NameAttribute); JComponent c = null; Object model = attr.getAttribute(StyleConstants.ModelAttribute); if (t == HTML.Tag.INPUT) { c = createInputComponent(attr, model); } else if (t == HTML.Tag.SELECT) { if (model instanceof OptionListModel) { JList list = new JList((ListModel) model); int size = HTML.getIntegerAttributeValue(attr, HTML.Attribute.SIZE, 1); list.setVisibleRowCount(size); list.setSelectionModel((ListSelectionModel)model); c = new JScrollPane(list); } else { c = new JComboBox((ComboBoxModel) model); maxIsPreferred = 3; } } else if (t == HTML.Tag.TEXTAREA) { JTextArea area = new JTextArea((Document) model); int rows = HTML.getIntegerAttributeValue(attr, HTML.Attribute.ROWS, 1); area.setRows(rows); int cols = HTML.getIntegerAttributeValue(attr, HTML.Attribute.COLS, 20); maxIsPreferred = 3; area.setColumns(cols); c = new JScrollPane(area, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); } if (c != null) { c.setAlignmentY(1.0f); } return c; } /** * Creates a component for an <INPUT> element based on the * value of the "type" attribute. * * @param set of attributes associated with the <INPUT> element. * @param model the value of the StyleConstants.ModelAttribute * @return the component. */ private JComponent createInputComponent(AttributeSet attr, Object model) { JComponent c = null; String type = (String) attr.getAttribute(HTML.Attribute.TYPE); if (type.equals("submit") || type.equals("reset")) { String value = (String) attr.getAttribute(HTML.Attribute.VALUE); if (value == null) { if (type.equals("submit")) { value = UIManager.getString("FormView.submitButtonText"); } else { value = UIManager.getString("FormView.resetButtonText"); } } JButton button = new JButton(value); if (model != null) { button.setModel((ButtonModel)model); button.addActionListener(this); } c = button; maxIsPreferred = 3; } else if (type.equals("image")) { String srcAtt = (String) attr.getAttribute(HTML.Attribute.SRC); JButton button; try { URL base = ((HTMLDocument)getElement().getDocument()).getBase(); URL srcURL = new URL(base, srcAtt); Icon icon = new ImageIcon(srcURL); button = new JButton(icon); } catch (MalformedURLException e) { button = new JButton(srcAtt); } if (model != null) { button.setModel((ButtonModel)model); button.addMouseListener(new MouseEventListener()); } c = button; maxIsPreferred = 3; } else if (type.equals("checkbox")) { c = new JCheckBox(); if (model != null) { ((JCheckBox)c).setModel((JToggleButton.ToggleButtonModel) model); } maxIsPreferred = 3; } else if (type.equals("radio")) { c = new JRadioButton(); if (model != null) { ((JRadioButton)c).setModel((JToggleButton.ToggleButtonModel)model); } maxIsPreferred = 3; } else if (type.equals("text")) { int size = HTML.getIntegerAttributeValue(attr, HTML.Attribute.SIZE, -1); JTextField field; if (size > 0) { field = new JTextField(); field.setColumns(size); } else { field = new JTextField(); field.setColumns(20); } c = field; if (model != null) { field.setDocument((Document) model); } field.addActionListener(this); maxIsPreferred = 3; } else if (type.equals("password")) { JPasswordField field = new JPasswordField(); c = field; if (model != null) { field.setDocument((Document) model); } int size = HTML.getIntegerAttributeValue(attr, HTML.Attribute.SIZE, -1); field.setColumns((size > 0) ? size : 20); field.addActionListener(this); maxIsPreferred = 3; } else if (type.equals("file")) { JTextField field = new JTextField(); if (model != null) { field.setDocument((Document)model); } int size = HTML.getIntegerAttributeValue(attr, HTML.Attribute.SIZE, -1); field.setColumns((size > 0) ? size : 20); JButton browseButton = new JButton(UIManager.getString ("FormView.browseFileButtonText")); Box box = Box.createHorizontalBox(); box.add(field); box.add(Box.createHorizontalStrut(5)); box.add(browseButton); browseButton.addActionListener(new BrowseFileAction( attr, (Document)model)); c = box; maxIsPreferred = 3; } return c; } /** * Determines the maximum span for this view along an * axis. For certain components, the maximum and preferred span are the * same. For others this will return the value * returned by Component.getMaximumSize along the * axis of interest. * * @param axis may be either View.X_AXIS or View.Y_AXIS * @return the span the view would like to be rendered into >= 0. * Typically the view is told to render into the span
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -