/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* <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 nubers, eg <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
* <li>Pressing +/- (- is determined from the
* <code>DecimalFormatSymbols</code> associated with the
* <code>DecimalFormat</code>) in the exponent field will
* </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
* 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<sup><font size="-2">TM</font></sup>
* has been added to the <code>java.beans</code> package.
* Please see {@link java.beans.XMLEncoder}.
*
* @since 1.4
*/
/** The special characters from the Format instance. */
/**
* Creates a <code>NumberFormatter</code> with the a default
* <code>NumberFormat</code> instance obtained from
* <code>NumberFormat.getNumberInstance()</code>.
*/
public NumberFormatter() {
this(NumberFormat.getNumberInstance());
}
/**
* Creates a NumberFormatter with the specified Format instance.
*
* @param format Format used to dictate legal values
*/
super(format);
setAllowsInvalid(true);
setCommitsOnValidEdit(false);
setOverwriteMode(false);
}
/**
* 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
*/
}
else {
specialChars = "";
}
}
/**
* Invokes <code>parseObject</code> on <code>f</code>, returning
* its value.
*/
if (f == null) {
return text;
}
}
/**
* 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>.
*/
if (valueClass == Integer.class) {
}
else if (valueClass == Long.class) {
}
else if (valueClass == Float.class) {
}
else if (valueClass == Double.class) {
}
else if (valueClass == Byte.class) {
}
else if (valueClass == Short.class) {
}
}
return value;
}
/**
* Returns the character that is used to toggle to positive values.
*/
private char getPositiveSign() {
return '+';
}
/**
* Returns the character that is used to toggle to negative values.
*/
private char getMinusSign() {
return dfs.getMinusSign();
}
return '-';
}
/**
* Returns the character that is used to toggle to negative values.
*/
private char getDecimalSeparator() {
return dfs.getDecimalSeparator();
}
return '.';
}
/**
* Returns the DecimalFormatSymbols from the Format instance.
*/
if (f instanceof DecimalFormat) {
return ((DecimalFormat)f).getDecimalFormatSymbols();
}
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.
*/
if (getAllowsInvalid()) {
return true;
}
return false;
}
}
return true;
}
/**
* Subclassed to treat the decimal separator, grouping separator,
* exponent symbol, percent, permille, currency and sign as literals.
*/
return false;
}
size--;
size--;
}
}
size--;
}
size--;
}
size--;
}
size--;
}
size--;
}
return size == 0;
}
return true;
}
/**
* Subclassed to make the decimal separator navigatable, as well
* as making the character between the integer field and the next
* field navigatable.
*/
if (!super.isNavigatable(index)) {
// Don't skip the decimal, it causes wierd behavior
}
return true;
}
/**
* Returns the first <code>NumberFormat.Field</code> starting
* <code>index</code> incrementing by <code>direction</code>.
*/
if (isValidMask()) {
}
}
}
}
}
}
return null;
}
/**
* is inserted.
*/
return;
}
}
/**
* 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.
*/
try {
}
else {
// exponent
}
return true;
}
} catch (ParseException pe) {
invalidEdit();
}
}
return false;
}
/**
* Invoked to toggle the sign. For this to work the value class
* must have a single arg constructor that takes a String.
*/
// toString isn't localized, so that using +/- should work
// correctly.
if (positive) {
}
}
else {
}
}
}
if (valueClass == null) {
}
try {
}
}
}
}
return null;
}
/**
* Invoked to toggle the sign of the exponent (for scientific
* numbers).
*/
int replaceLength = 0;
if (loc >= 0) {
replaceLength = 1;
}
if (aChar == getPositiveSign()) {
}
else {
}
return stringToValue(string);
}
}