< prev index next >
src/java.desktop/share/classes/javax/swing/text/NumberFormatter.java
Print this page
*** 29,107 ****
import java.util.*;
import sun.reflect.misc.ReflectUtil;
import sun.swing.SwingUtilities2;
/**
! * <code>NumberFormatter</code> subclasses <code>InternationalFormatter</code>
* adding special behavior for numbers. Among the specializations are
! * (these are only used if the <code>NumberFormatter</code> does not display
! * invalid numbers, for example, <code>setAllowsInvalid(false)</code>):
* <ul>
* <li>Pressing +/- (- is determined from the
! * <code>DecimalFormatSymbols</code> associated with the
! * <code>DecimalFormat</code>) in any field but the exponent
* field will attempt to change the sign of the number to
* positive/negative.
* <li>Pressing +/- (- is determined from the
! * <code>DecimalFormatSymbols</code> associated with the
! * <code>DecimalFormat</code>) in the exponent field will
* attempt to change the sign of the exponent to positive/negative.
* </ul>
* <p>
* If you are displaying scientific numbers, you may wish to turn on
! * overwrite mode, <code>setOverwriteMode(true)</code>. For example:
* <pre>
* DecimalFormat decimalFormat = new DecimalFormat("0.000E0");
* NumberFormatter textFormatter = new NumberFormatter(decimalFormat);
* textFormatter.setOverwriteMode(true);
* textFormatter.setAllowsInvalid(false);
* </pre>
* <p>
* If you are going to allow the user to enter decimal
* values, you should either force the DecimalFormat to contain at least
! * one decimal (<code>#.0###</code>), or allow the value to be invalid
! * <code>setAllowsInvalid(true)</code>. Otherwise users may not be able to
* input decimal values.
* <p>
! * <code>NumberFormatter</code> provides slightly different behavior to
! * <code>stringToValue</code> than that of its superclass. If you have
* specified a Class for values, {@link #setValueClass}, that is one of
! * of <code>Integer</code>, <code>Long</code>, <code>Float</code>,
! * <code>Double</code>, <code>Byte</code> or <code>Short</code> and
! * the Format's <code>parseObject</code> returns an instance of
! * <code>Number</code>, the corresponding instance of the value class
* will be created using the constructor appropriate for the primitive
* type the value class represents. For example:
! * <code>setValueClass(Integer.class)</code> will cause the resulting
* value to be created via
! * <code>new Integer(((Number)formatter.parseObject(string)).intValue())</code>.
* This is typically useful if you
! * wish to set a min/max value as the various <code>Number</code>
* implementations are generally not comparable to each other. This is also
! * useful if for some reason you need a specific <code>Number</code>
* implementation for your values.
* <p>
* <strong>Warning:</strong>
* Serialized objects of this class will not be compatible with
* future Swing releases. The current serialization support is
* appropriate for short term storage or RMI between applications running
* the same version of Swing. As of 1.4, support for long term storage
* of all JavaBeans™
! * has been added to the <code>java.beans</code> package.
* Please see {@link java.beans.XMLEncoder}.
*
* @since 1.4
*/
@SuppressWarnings("serial") // Same-version serialization only
public class NumberFormatter extends InternationalFormatter {
/** The special characters from the Format instance. */
private String specialChars;
/**
! * Creates a <code>NumberFormatter</code> with the a default
! * <code>NumberFormat</code> instance obtained from
! * <code>NumberFormat.getNumberInstance()</code>.
*/
public NumberFormatter() {
this(NumberFormat.getNumberInstance());
}
--- 29,107 ----
import java.util.*;
import sun.reflect.misc.ReflectUtil;
import sun.swing.SwingUtilities2;
/**
! * {@code NumberFormatter} subclasses {@code InternationalFormatter}
* adding special behavior for numbers. Among the specializations are
! * (these are only used if the {@code NumberFormatter} does not display
! * invalid numbers, for example, {@code setAllowsInvalid(false)}):
* <ul>
* <li>Pressing +/- (- is determined from the
! * {@code DecimalFormatSymbols} associated with the
! * {@code DecimalFormat}) in any field but the exponent
* field will attempt to change the sign of the number to
* positive/negative.
* <li>Pressing +/- (- is determined from the
! * {@code DecimalFormatSymbols} associated with the
! * {@code DecimalFormat}) in the exponent field will
* attempt to change the sign of the exponent to positive/negative.
* </ul>
* <p>
* If you are displaying scientific numbers, you may wish to turn on
! * overwrite mode, {@code setOverwriteMode(true)}. For example:
* <pre>
* DecimalFormat decimalFormat = new DecimalFormat("0.000E0");
* NumberFormatter textFormatter = new NumberFormatter(decimalFormat);
* textFormatter.setOverwriteMode(true);
* textFormatter.setAllowsInvalid(false);
* </pre>
* <p>
* If you are going to allow the user to enter decimal
* values, you should either force the DecimalFormat to contain at least
! * one decimal ({@code #.0###}), or allow the value to be invalid
! * {@code setAllowsInvalid(true)}. Otherwise users may not be able to
* input decimal values.
* <p>
! * {@code NumberFormatter} provides slightly different behavior to
! * {@code stringToValue} than that of its superclass. If you have
* specified a Class for values, {@link #setValueClass}, that is one of
! * of {@code Integer}, {@code Long}, {@code Float},
! * {@code Double}, {@code Byte} or {@code Short} and
! * the Format's {@code parseObject} returns an instance of
! * {@code Number}, the corresponding instance of the value class
* will be created using the constructor appropriate for the primitive
* type the value class represents. For example:
! * {@code setValueClass(Integer.class)} will cause the resulting
* value to be created via
! * {@code new Integer(((Number)formatter.parseObject(string)).intValue())}.
* This is typically useful if you
! * wish to set a min/max value as the various {@code Number}
* implementations are generally not comparable to each other. This is also
! * useful if for some reason you need a specific {@code Number}
* implementation for your values.
* <p>
* <strong>Warning:</strong>
* Serialized objects of this class will not be compatible with
* future Swing releases. The current serialization support is
* appropriate for short term storage or RMI between applications running
* the same version of Swing. As of 1.4, support for long term storage
* of all JavaBeans™
! * has been added to the {@code java.beans} package.
* Please see {@link java.beans.XMLEncoder}.
*
* @since 1.4
*/
@SuppressWarnings("serial") // Same-version serialization only
public class NumberFormatter extends InternationalFormatter {
/** The special characters from the Format instance. */
private String specialChars;
/**
! * Creates a {@code NumberFormatter} with the a default
! * {@code NumberFormat} instance obtained from
! * {@code NumberFormat.getNumberInstance()}.
*/
public NumberFormatter() {
this(NumberFormat.getNumberInstance());
}
*** 122,132 ****
* Sets the format that dictates the legal values that can be edited
* and displayed.
* <p>
* If you have used the nullary constructor the value of this property
* will be determined for the current locale by way of the
! * <code>NumberFormat.getNumberInstance()</code> method.
*
* @param format NumberFormat instance used to dictate legal values
*/
public void setFormat(Format format) {
super.setFormat(format);
--- 122,132 ----
* Sets the format that dictates the legal values that can be edited
* and displayed.
* <p>
* If you have used the nullary constructor the value of this property
* will be determined for the current locale by way of the
! * {@code NumberFormat.getNumberInstance()} method.
*
* @param format NumberFormat instance used to dictate legal values
*/
public void setFormat(Format format) {
super.setFormat(format);
*** 152,162 ****
specialChars = "";
}
}
/**
! * Invokes <code>parseObject</code> on <code>f</code>, returning
* its value.
*/
Object stringToValue(String text, Format f) throws ParseException {
if (f == null) {
return text;
--- 152,162 ----
specialChars = "";
}
}
/**
! * Invokes {@code parseObject} on {@code f}, returning
* its value.
*/
Object stringToValue(String text, Format f) throws ParseException {
if (f == null) {
return text;
*** 166,179 ****
return convertValueToValueClass(value, getValueClass());
}
/**
* Converts the passed in value to the passed in class. This only
! * works if <code>valueClass</code> is one of <code>Integer</code>,
! * <code>Long</code>, <code>Float</code>, <code>Double</code>,
! * <code>Byte</code> or <code>Short</code> and <code>value</code>
! * is an instanceof <code>Number</code>.
*/
private Object convertValueToValueClass(Object value,
Class<?> valueClass) {
if (valueClass != null && (value instanceof Number)) {
Number numberValue = (Number)value;
--- 166,179 ----
return convertValueToValueClass(value, getValueClass());
}
/**
* Converts the passed in value to the passed in class. This only
! * works if {@code valueClass} is one of {@code Integer},
! * {@code Long}, {@code Float}, {@code Double},
! * {@code Byte} or {@code Short} and {@code value}
! * is an instanceof {@code Number}.
*/
private Object convertValueToValueClass(Object value,
Class<?> valueClass) {
if (valueClass != null && (value instanceof Number)) {
Number numberValue = (Number)value;
*** 241,253 ****
}
return null;
}
/**
! * Subclassed to return false if <code>text</code> contains in an invalid
* character to insert, that is, it is not a digit
! * (<code>Character.isDigit()</code>) and
* not one of the characters defined by the DecimalFormatSymbols.
*/
boolean isLegalInsertText(String text) {
if (getAllowsInvalid()) {
return true;
--- 241,253 ----
}
return null;
}
/**
! * Subclassed to return false if {@code text} contains in an invalid
* character to insert, that is, it is not a digit
! * ({@code Character.isDigit()}) and
* not one of the characters defined by the DecimalFormatSymbols.
*/
boolean isLegalInsertText(String text) {
if (getAllowsInvalid()) {
return true;
*** 312,323 ****
}
return true;
}
/**
! * Returns the first <code>NumberFormat.Field</code> starting
! * <code>index</code> incrementing by <code>direction</code>.
*/
private NumberFormat.Field getFieldFrom(int index, int direction) {
if (isValidMask()) {
int max = getFormattedTextField().getDocument().getLength();
AttributedCharacterIterator iterator = getIterator();
--- 312,323 ----
}
return true;
}
/**
! * Returns the first {@code NumberFormat.Field} starting
! * {@code index} incrementing by {@code direction}.
*/
private NumberFormat.Field getFieldFrom(int index, int direction) {
if (isValidMask()) {
int max = getFormattedTextField().getDocument().getLength();
AttributedCharacterIterator iterator = getIterator();
*** 357,367 ****
super.replace(fb, offset, length, string, attr);
}
/**
* Will change the sign of the integer or exponent field if
! * <code>aChar</code> is the positive or minus sign. Returns
* true if a sign change was attempted.
*/
private boolean toggleSignIfNecessary(DocumentFilter.FilterBypass fb,
int offset, char aChar) throws
BadLocationException {
--- 357,367 ----
super.replace(fb, offset, length, string, attr);
}
/**
* Will change the sign of the integer or exponent field if
! * {@code aChar} is the positive or minus sign. Returns
* true if a sign change was attempted.
*/
private boolean toggleSignIfNecessary(DocumentFilter.FilterBypass fb,
int offset, char aChar) throws
BadLocationException {
< prev index next >