InternationalFormatter.java revision 2362
4628N/A * Copyright (c) 2000, 2008, Oracle and/or its affiliates. All rights reserved. 1121N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1121N/A * This code is free software; you can redistribute it and/or modify it 1121N/A * under the terms of the GNU General Public License version 2 only, as 1121N/A * published by the Free Software Foundation. Oracle designates this 1121N/A * particular file as subject to the "Classpath" exception as provided 1121N/A * by Oracle in the LICENSE file that accompanied this code. 1121N/A * This code is distributed in the hope that it will be useful, but WITHOUT 1121N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1121N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1121N/A * version 2 for more details (a copy is included in the LICENSE file that 1121N/A * You should have received a copy of the GNU General Public License version 1121N/A * 2 along with this work; if not, write to the Free Software Foundation, 2362N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1121N/A * or visit www.oracle.com if you need additional information or have any 4628N/A * <code>InternationalFormatter</code> extends <code>DefaultFormatter</code>, 4628N/A * using an instance of <code>java.text.Format</code> to handle the 1121N/A * conversion to a String, and the conversion from a String. 1121N/A * If <code>getAllowsInvalid()</code> is false, this will ask the 1121N/A * <code>Format</code> to format the current text on every edit. 1121N/A * You can specify a minimum and maximum value by way of the 1121N/A * <code>setMinimum</code> and <code>setMaximum</code> methods. In order 1121N/A * for this to work the values returned from <code>stringToValue</code> must be 1121N/A * comparable to the min/max values by way of the <code>Comparable</code> 1121N/A * Be careful how you configure the <code>Format</code> and the 1121N/A * <code>InternationalFormatter</code>, as it is possible to create a 1121N/A * situation where certain values can not be input. Consider the date 4628N/A * format 'M/d/yy', an <code>InternationalFormatter</code> that is always 4628N/A * valid (<code>setAllowsInvalid(false)</code>), is in overwrite mode 4628N/A * (<code>setOverwriteMode(true)</code>) and the date 7/1/99. In this 4628N/A * case the user will not be able to enter a two digit month or day of 4628N/A * If <code>InternationalFormatter</code> is configured to only allow valid 4628N/A * values (<code>setAllowsInvalid(false)</code>), every valid edit will result 4628N/A * in the text of the <code>JFormattedTextField</code> being completely reset 4628N/A * from the <code>Format</code>. 1121N/A * The cursor position will also be adjusted as literal characters are 1121N/A * <code>InternationalFormatter</code>'s behavior of 1121N/A * <code>stringToValue</code> is slightly different than that of 1121N/A * <code>DefaultTextFormatter</code>, it does the following: 1121N/A * <li><code>parseObject</code> is invoked on the <code>Format</code> 1121N/A * specified by <code>setFormat</code> 1121N/A * <li>If a Class has been set for the values (<code>setValueClass</code>), 1121N/A * supers implementation is invoked to convert the value returned 1121N/A * from <code>parseObject</code> to the appropriate class. 1121N/A * <li>If a <code>ParseException</code> has not been thrown, and the value 1121N/A * is outside the min/max a <code>ParseException</code> is thrown. 1121N/A * <li>The value is returned. 1121N/A * <code>InternationalFormatter</code> implements <code>stringToValue</code> 1121N/A * in this manner so that you can specify an alternate Class than 1121N/A * <code>Format</code> may return. 1121N/A * <strong>Warning:</strong> 1121N/A * Serialized objects of this class will not be compatible with 1121N/A * future Swing releases. The current serialization support is 1121N/A * appropriate for short term storage or RMI between applications running 1121N/A * the same version of Swing. As of 1.4, support for long term storage 1121N/A * of all JavaBeans<sup><font size="-2">TM</font></sup> 1121N/A * has been added to the <code>java.beans</code> package. 1121N/A * @see java.lang.Comparable 1121N/A * Used by <code>getFields</code>. 1121N/A * Object used to handle the conversion. * Can be used to impose a maximum value. * Can be used to impose a minimum value. * <code>InternationalFormatter</code>'s behavior is dicatated by a * <code>AttributedCharacterIterator</code> that is obtained from * the <code>Format</code>. On every edit, assuming * allows invalid is false, the <code>Format</code> instance is invoked * with <code>formatToCharacterIterator</code>. A <code>BitSet</code> is * also kept upto date with the non-literal characters, that is * for every index in the <code>AttributedCharacterIterator</code> an * entry in the bit set is updated based on the return value from * <code>isLiteral(Map)</code>. <code>isLiteral(int)</code> then uses * this cached information. * If allowsInvalid is false, every edit results in resetting the complete * text of the JTextComponent. * InternationalFormatterFilter can also provide two actions suitable for * incrementing and decrementing. To enable this a subclass must * override <code>getSupportsIncrement</code> to return true, and * override <code>adjustValue</code> to handle the changing of the * value. If you want to support changing the value outside of * the valid FieldPositions, you will need to override * <code>canIncrement</code>. * A bit is set for every index identified in the * AttributedCharacterIterator that is not considered decoration. * This should only be used if validMask is true. * Used to iterate over characters. * True if the Format was able to convert the value to a String and * Current value being displayed. * If true, DocumentFilter methods are unconditionally allowed, * and no checking is done on their values. This is used when * Creates an <code>InternationalFormatter</code> with no * <code>Format</code> specified. * Creates an <code>InternationalFormatter</code> with the specified * <code>Format</code> instance. * @param format Format instance used for converting from/to Strings * Sets the format that dictates the legal values that can be edited * @param format <code>Format</code> instance used for converting * Returns the format that dictates the legal values that can be edited * @return Format instance used for converting from/to Strings * Sets the minimum permissible value. If the <code>valueClass</code> has * not been specified, and <code>minimum</code> is non null, the * <code>valueClass</code> will be set to that of the class of * @param minimum Minimum legal value that can be input * Returns the minimum permissible value. * @return Minimum legal value that can be input * Sets the maximum permissible value. If the <code>valueClass</code> has * not been specified, and <code>max</code> is non null, the * <code>valueClass</code> will be set to that of the class of * @param max Maximum legal value that can be input * Returns the maximum permissible value. * @return Maximum legal value that can be input * Installs the <code>DefaultFormatter</code> onto a particular * <code>JFormattedTextField</code>. * This will invoke <code>valueToString</code> to convert the * current value from the <code>JFormattedTextField</code> to * a String. This will then install the <code>Action</code>s from * <code>getActions</code>, the <code>DocumentFilter</code> * returned from <code>getDocumentFilter</code> and the * <code>NavigationFilter</code> returned from * <code>getNavigationFilter</code> onto the * <code>JFormattedTextField</code>. * Subclasses will typically only need to override this if they * wish to install additional listeners on the * <code>JFormattedTextField</code>. * If there is a <code>ParseException</code> in converting the * current value to a String, this will set the text to an empty * String, and mark the <code>JFormattedTextField</code> as being * While this is a public method, this is typically only useful * for subclassers of <code>JFormattedTextField</code>. * <code>JFormattedTextField</code> will invoke this method at * the appropriate times when the value changes, or its internal * @param ftf JFormattedTextField to format for, may be null indicating * uninstall from current JFormattedTextField. // invoked again as the mask should now be valid. * Returns a String representation of the Object <code>value</code>. * This invokes <code>format</code> on the current <code>Format</code>. * @throws ParseException if there is an error in the conversion * @param value Value to convert * @return String representation of value * Returns the <code>Object</code> representation of the * <code>String</code> <code>text</code>. * @param text <code>String</code> to convert * @return <code>Object</code> representation of text * @throws ParseException if there is an error in the conversion // Convert to the value class if the Value returned from the // Format does not match. * Returns the <code>Format.Field</code> constants associated with * the text at <code>offset</code>. If <code>offset</code> is not * a valid location into the current text, this will return an * @param offset offset into text to be examined * @return Format.Field constants associated with the text at the // This will work if the currently edited value is valid. * Creates a copy of the DefaultFormatter. * @return copy of the DefaultFormatter * If <code>getSupportsIncrement</code> returns true, this returns * Invokes <code>parseObject</code> on <code>f</code>, returning * Returns true if <code>value</code> is between the min/max. * @param wantsCCE If false, and a ClassCastException is thrown in * comparing the values, the exception is consumed and * Returns a Set of the attribute identifiers at <code>index</code>. * Returns the start of the first run that contains the attribute * <code>id</code>. This will return <code>-1</code> if the attribute * Returns the <code>AttributedCharacterIterator</code> used to * Updates the AttributedCharacterIterator and bitset, if necessary. * Updates the AttributedCharacterIterator by invoking * <code>formatToCharacterIterator</code> on the <code>Format</code>. * <code>updateMask(AttributedCharacterIterator)</code> * is then invoked to update the internal bitmask. * Returns the number of literal characters before <code>index</code>. * Returns true if the character at index is a literal, that is * Returns the literal character at index. * Returns true if the character at offset is navigatable too. This * is implemented in terms of <code>isLiteral</code>, subclasses * may wish to provide different behavior. * Overriden to update the mask after invoking supers implementation. * Overriden to unconditionally allow the replace if * ignoreDocumentMutate is true. * Returns the index of the next non-literal character starting at * index. If index is not a literal, it will be returned. * @param direction Amount to increment looking for non-literal * Overriden in an attempt to honor the literals. * not allow invalid values and are in overwrite mode, this does the * following for each character in the replacement range: * <li>If the character is a literal, add it to the string to replace * with. If there is text to insert and it doesn't match the * literal, then insert the literal in the the middle of the insert * text. This allows you to either paste in literals or not and * <li>If there is no text to insert, replace it with ' '. * If not in overwrite mode, and there is text to insert it is * inserted at the next non literal index going forward. If there * is only text to remove, it is removed from the next non literal // Backspace, adjust to actually delete next non-literal. // Nothing to replace it with, assume ' ' // insert (or insert and remove) * When in !allowsInvalid mode the text is reset on every edit, thus * supers implementation will position the cursor at the wrong position. * As such, this invokes supers implementation and then invokes * <code>repositionCursor</code> to correctly reset the cursor. * Repositions the cursor. <code>startLiteralCount</code> gives * the number of literals to the start of the deleted range, end * gives the ending location to adjust from, direction gives * the direction relative to <code>end</code> to position the * Returns the character from the mask that has been buffered * Returns true if the current mask is valid. * Returns true if <code>attributes</code> is null or empty. * Updates the interal bitset from <code>iterator</code>. This will * set <code>validMask</code> to true if <code>iterator</code> is // Update the literal mask * Returns true if <code>field</code> is non-null. * Subclasses that wish to allow incrementing to happen outside of * the known fields will need to override this. * Selects the fields identified by <code>attributes</code>. * Returns the field that will be adjusted by adjustValue. * Returns the number of occurences of <code>f</code> before * the location <code>start</code> in the current * <code>AttributedCharacterIterator</code>. * Subclasses supporting incrementing must override this to handle * the actual incrementing. <code>value</code> is the current value, * <code>attributes</code> gives the field the cursor is in (may be * null depending upon <code>canIncrement</code>) and * <code>direction</code> is the amount to increment by. * Returns false, indicating InternationalFormatter does not allow * incrementing of the value. Subclasses that wish to support * return true. Subclasses should also override * <code>adjustValue</code>. * Resets the value of the JFormattedTextField to be * Subclassed to update the internal representation of the mask after * the default read operation has completed. * Overriden to return an instance of <code>ExtendedReplaceHolder</code>. * As InternationalFormatter replaces the complete text on every edit, * ExtendedReplaceHolder keeps track of the offset and length passed /** Offset of the insert/remove. This may differ from offset in * that if !allowsInvalid the text is replaced on every edit. */ /** Length of the text. This may differ from text.length in * that if !allowsInvalid the text is replaced on every edit. */ * Resets the region to delete to be the complete document and * the text from invoking valueToString on the current value. // Need to reset the complete string as Format's result can // be completely different. // Should never happen, otherwise canReplace would have * IncrementAction is used to increment the value by a certain amount. * It calls into <code>adjustValue</code> to handle the actual * incrementing of the value. // This will work if the currently edited value is valid.