jformattedtextfield.java
来自「linux下建立JAVA虚拟机的源码KAFFE」· Java 代码 · 共 649 行 · 第 1/2 页
JAVA
649 行
/* JFormattedTextField.java -- Copyright (C) 2003, 2004 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package javax.swing;import java.awt.event.FocusEvent;import java.io.Serializable;import java.text.DateFormat;import java.text.Format;import java.text.NumberFormat;import java.text.ParseException;import java.util.Date;import javax.swing.text.AbstractDocument;import javax.swing.text.DateFormatter;import javax.swing.text.DefaultFormatter;import javax.swing.text.DefaultFormatterFactory;import javax.swing.text.Document;import javax.swing.text.DocumentFilter;import javax.swing.text.InternationalFormatter;import javax.swing.text.NavigationFilter;import javax.swing.text.NumberFormatter;/** * A text field that makes use of a formatter to display and edit a specific * type of data. The value that is displayed can be an arbitrary object. The * formatter is responsible for displaying the value in a textual form and * it may allow editing of the value. * * Formatters are usually obtained using an instance of * {@link AbstractFormatterFactory}. This factory is responsible for providing * an instance of {@link AbstractFormatter} that is able to handle the * formatting of the value of the JFormattedTextField. * * @author Michael Koch * @author Anthony Balkissoon abalkiss at redhat dot com * * @since 1.4 */public class JFormattedTextField extends JTextField{ private static final long serialVersionUID = 5464657870110180632L; /** * An abstract base implementation for a formatter that can be used by * a JTextField. A formatter can display a specific type of object and * may provide a way to edit this value. */ public abstract static class AbstractFormatter implements Serializable { private static final long serialVersionUID = -5193212041738979680L; private JFormattedTextField textField; public AbstractFormatter () { //Do nothing here. } /** * Clones the AbstractFormatter and removes the association to any * particular JFormattedTextField. * * @return a clone of this formatter with no association to any particular * JFormattedTextField * @throws CloneNotSupportedException if the Object's class doesn't support * the {@link Cloneable} interface */ protected Object clone () throws CloneNotSupportedException { // Clone this formatter. AbstractFormatter newFormatter = (AbstractFormatter)super.clone(); // And remove the association to the JFormattedTextField. newFormatter.textField = null; return newFormatter; } /** * Returns a custom set of Actions that this formatter supports. Should * be subclassed by formatters that have a custom set of Actions. * * @return <code>null</code>. Should be subclassed by formatters that want * to install custom Actions on the JFormattedTextField. */ protected Action[] getActions () { return null; } /** * Gets the DocumentFilter for this formatter. Should be subclassed * by formatters wishing to install a filter that oversees Document * mutations. * * @return <code>null</code>. Should be subclassed by formatters * that want to restrict Document mutations. */ protected DocumentFilter getDocumentFilter () { // Subclasses should override this if they want to install a // DocumentFilter. return null; } /** * Returns the JFormattedTextField on which this formatter is * currently installed. * * @return the JFormattedTextField on which this formatter is currently * installed */ protected JFormattedTextField getFormattedTextField () { return textField; } /** * Gets the NavigationFilter for this formatter. Should be subclassed * by formatters (such as {@link DefaultFormatter}) that wish to * restrict where the cursor can be placed within the text field. * * @return <code>null</code>. Subclassed by formatters that want to restrict * cursor location within the JFormattedTextField. */ protected NavigationFilter getNavigationFilter () { // This should be subclassed if the formatter wants to install // a NavigationFilter on the JFormattedTextField. return null; } /** * Installs this formatter on the specified JFormattedTextField. This * converts the current value to a displayable String and displays it, * and installs formatter specific Actions from <code>getActions</code>. * It also installs a DocumentFilter and NavigationFilter on the * JFormattedTextField. * <p> * If there is a <code>ParseException</code> this sets the text to an * empty String and marks the text field in an invalid state. * * @param textField the JFormattedTextField on which to install this * formatter */ public void install(JFormattedTextField textField) { // Uninstall the current textfield. if (this.textField != null) uninstall(); this.textField = textField; // Install some state on the text field, including display text, // DocumentFilter, NavigationFilter, and formatter specific Actions. if (textField != null) { try { // Set the text of the field. textField.setText(valueToString(textField.getValue())); Document doc = textField.getDocument(); // Set the DocumentFilter for the field's Document. if (doc instanceof AbstractDocument) ((AbstractDocument)doc).setDocumentFilter(getDocumentFilter()); // Set the NavigationFilter. textField.setNavigationFilter(getNavigationFilter()); // Set the Formatter Actions // FIXME: Have to add the actions from getActions() } catch (ParseException pe) { // Set the text to an empty String and mark the field as invalid. textField.setText(""); setEditValid(false); } } } /** * Clears the state installed on the JFormattedTextField by the formatter. * This resets the DocumentFilter, NavigationFilter, and any additional * Actions (returned by <code>getActions()</code>). */ public void uninstall () { // Set the DocumentFilter for the field's Document. Document doc = textField.getDocument(); if (doc instanceof AbstractDocument) ((AbstractDocument)doc).setDocumentFilter(null); textField.setNavigationFilter(null); // FIXME: Have to remove the Actions from getActions() this.textField = null; } /** * Invoke this method when invalid values are entered. This forwards the * call to the JFormattedTextField. */ protected void invalidEdit () { textField.invalidEdit(); } /** * This method updates the <code>editValid</code> property of * JFormattedTextField. * * @param valid the new state for the <code>editValid</code> property */ protected void setEditValid (boolean valid) { textField.editValid = valid; } /** * Parses <code>text</code> to return a corresponding Object. * * @param text the String to parse * @return an Object that <code>text</code> represented * @throws ParseException if there is an error in the conversion */ public abstract Object stringToValue (String text) throws ParseException; /** * Returns a String to be displayed, based on the Object * <code>value</code>. * * @param value the Object from which to generate a String * @return a String to be displayed * @throws ParseException if there is an error in the conversion */ public abstract String valueToString (Object value) throws ParseException; } /** * Delivers instances of an {@link AbstractFormatter} for * a specific value type for a JFormattedTextField. */ public abstract static class AbstractFormatterFactory { public AbstractFormatterFactory () { // Do nothing here. } public abstract AbstractFormatter getFormatter (JFormattedTextField tf); } /** The possible focusLostBehavior options **/ public static final int COMMIT = 0; public static final int COMMIT_OR_REVERT = 1; public static final int REVERT = 2; public static final int PERSIST = 3; /** The most recent valid and committed value **/ private Object value; /** The behaviour for when this text field loses focus **/ private int focusLostBehavior = COMMIT_OR_REVERT; /** The formatter factory currently being used **/ private AbstractFormatterFactory formatterFactory; /** The formatter currently being used **/ private AbstractFormatter formatter; // Package-private to avoid an accessor method. boolean editValid = true; /** * Creates a JFormattedTextField with no formatter factory. * <code>setValue</code> or <code>setFormatterFactory</code> will * properly configure this text field to edit a particular type * of value. */ public JFormattedTextField () { this((AbstractFormatterFactory) null, null); } /** * Creates a JFormattedTextField that can handle the specified Format. * An appopriate AbstractFormatter and AbstractFormatterFactory will
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?