< prev index next >

src/java.desktop/share/classes/javax/swing/text/DefaultFormatter.java

Print this page

        

*** 32,60 **** import java.text.ParseException; import javax.swing.*; import javax.swing.text.*; /** ! * <code>DefaultFormatter</code> formats arbitrary objects. Formatting is done ! * by invoking the <code>toString</code> method. In order to convert the * value back to a String, your class must provide a constructor that * takes a String argument. If no single argument constructor that takes a * String is found, the returned value will be the String passed into ! * <code>stringToValue</code>. * <p> ! * Instances of <code>DefaultFormatter</code> can not be used in multiple ! * instances of <code>JFormattedTextField</code>. To obtain a copy of ! * an already configured <code>DefaultFormatter</code>, use the ! * <code>clone</code> method. * <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&trade; ! * has been added to the <code>java.beans</code> package. * Please see {@link java.beans.XMLEncoder}. * * @see javax.swing.JFormattedTextField.AbstractFormatter * * @since 1.4 --- 32,60 ---- import java.text.ParseException; import javax.swing.*; import javax.swing.text.*; /** ! * {@code DefaultFormatter} formats arbitrary objects. Formatting is done ! * by invoking the {@code toString} method. In order to convert the * value back to a String, your class must provide a constructor that * takes a String argument. If no single argument constructor that takes a * String is found, the returned value will be the String passed into ! * {@code stringToValue}. * <p> ! * Instances of {@code DefaultFormatter} can not be used in multiple ! * instances of {@code JFormattedTextField}. To obtain a copy of ! * an already configured {@code DefaultFormatter}, use the ! * {@code clone} method. * <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&trade; ! * has been added to the {@code java.beans} package. * Please see {@link java.beans.XMLEncoder}. * * @see javax.swing.JFormattedTextField.AbstractFormatter * * @since 1.4
*** 91,123 **** overwriteMode = true; allowsInvalid = true; } /** ! * 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>. * <p> * Subclasses will typically only need to override this if they * wish to install additional listeners on the ! * <code>JFormattedTextField</code>. * <p> ! * 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 * in an invalid state. * <p> * 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 * state changes. * * @param ftf JFormattedTextField to format for, may be null indicating * uninstall from current JFormattedTextField. --- 91,123 ---- overwriteMode = true; allowsInvalid = true; } /** ! * Installs the {@code DefaultFormatter} onto a particular ! * {@code JFormattedTextField}. ! * This will invoke {@code valueToString} to convert the ! * current value from the {@code JFormattedTextField} to ! * a String. This will then install the {@code Action}s from ! * {@code getActions}, the {@code DocumentFilter} ! * returned from {@code getDocumentFilter} and the ! * {@code NavigationFilter} returned from ! * {@code getNavigationFilter} onto the ! * {@code JFormattedTextField}. * <p> * Subclasses will typically only need to override this if they * wish to install additional listeners on the ! * {@code JFormattedTextField}. * <p> ! * If there is a {@code ParseException} in converting the * current value to a String, this will set the text to an empty ! * String, and mark the {@code JFormattedTextField} as being * in an invalid state. * <p> * While this is a public method, this is typically only useful ! * for subclassers of {@code JFormattedTextField}. ! * {@code JFormattedTextField} will invoke this method at * the appropriate times when the value changes, or its internal * state changes. * * @param ftf JFormattedTextField to format for, may be null indicating * uninstall from current JFormattedTextField.
*** 127,165 **** positionCursorAtInitialLocation(); } /** * Sets when edits are published back to the ! * <code>JFormattedTextField</code>. If true, <code>commitEdit</code> * is invoked after every valid edit (any time the text is edited). On ! * the other hand, if this is false than the <code>DefaultFormatter</code> ! * does not publish edits back to the <code>JFormattedTextField</code>. ! * As such, the only time the value of the <code>JFormattedTextField</code> ! * will change is when <code>commitEdit</code> is invoked on ! * <code>JFormattedTextField</code>, typically when enter is pressed ! * or focus leaves the <code>JFormattedTextField</code>. * * @param commit Used to indicate when edits are committed back to the * JTextComponent */ public void setCommitsOnValidEdit(boolean commit) { commitOnEdit = commit; } /** * Returns when edits are published back to the ! * <code>JFormattedTextField</code>. * * @return true if edits are committed after every valid edit */ public boolean getCommitsOnValidEdit() { return commitOnEdit; } /** * Configures the behavior when inserting characters. If ! * <code>overwriteMode</code> is true (the default), new characters * overwrite existing characters in the model. * * @param overwriteMode Indicates if overwrite or overstrike mode is used */ public void setOverwriteMode(boolean overwriteMode) { --- 127,165 ---- positionCursorAtInitialLocation(); } /** * Sets when edits are published back to the ! * {@code JFormattedTextField}. If true, {@code commitEdit} * is invoked after every valid edit (any time the text is edited). On ! * the other hand, if this is false than the {@code DefaultFormatter} ! * does not publish edits back to the {@code JFormattedTextField}. ! * As such, the only time the value of the {@code JFormattedTextField} ! * will change is when {@code commitEdit} is invoked on ! * {@code JFormattedTextField}, typically when enter is pressed ! * or focus leaves the {@code JFormattedTextField}. * * @param commit Used to indicate when edits are committed back to the * JTextComponent */ public void setCommitsOnValidEdit(boolean commit) { commitOnEdit = commit; } /** * Returns when edits are published back to the ! * {@code JFormattedTextField}. * * @return true if edits are committed after every valid edit */ public boolean getCommitsOnValidEdit() { return commitOnEdit; } /** * Configures the behavior when inserting characters. If ! * {@code overwriteMode} is true (the default), new characters * overwrite existing characters in the model. * * @param overwriteMode Indicates if overwrite or overstrike mode is used */ public void setOverwriteMode(boolean overwriteMode) {
*** 175,186 **** return overwriteMode; } /** * Sets whether or not the value being edited is allowed to be invalid ! * for a length of time (that is, <code>stringToValue</code> throws ! * a <code>ParseException</code>). * It is often convenient to allow the user to temporarily input an * invalid value. * * @param allowsInvalid Used to indicate if the edited value must always * be valid --- 175,186 ---- return overwriteMode; } /** * Sets whether or not the value being edited is allowed to be invalid ! * for a length of time (that is, {@code stringToValue} throws ! * a {@code ParseException}). * It is often convenient to allow the user to temporarily input an * invalid value. * * @param allowsInvalid Used to indicate if the edited value must always * be valid
*** 220,236 **** return valueClass; } /** * Converts the passed in String into an instance of ! * <code>getValueClass</code> by way of the constructor that ! * takes a String argument. If <code>getValueClass</code> * returns null, the Class of the current value in the ! * <code>JFormattedTextField</code> will be used. If this is null, a * String will be returned. If the constructor throws an exception, a ! * <code>ParseException</code> will be thrown. If there is no single ! * argument String constructor, <code>string</code> will be returned. * * @throws ParseException if there is an error in the conversion * @param string String to convert * @return Object representation of text */ --- 220,236 ---- return valueClass; } /** * Converts the passed in String into an instance of ! * {@code getValueClass} by way of the constructor that ! * takes a String argument. If {@code getValueClass} * returns null, the Class of the current value in the ! * {@code JFormattedTextField} will be used. If this is null, a * String will be returned. If the constructor throws an exception, a ! * {@code ParseException} will be thrown. If there is no single ! * argument String constructor, {@code string} will be returned. * * @throws ParseException if there is an error in the conversion * @param string String to convert * @return Object representation of text */
*** 269,279 **** return string; } /** * Converts the passed in Object into a String by way of the ! * <code>toString</code> method. * * @throws ParseException if there is an error in the conversion * @param value Value to convert * @return String representation of value */ --- 269,279 ---- return string; } /** * Converts the passed in Object into a String by way of the ! * {@code toString} method. * * @throws ParseException if there is an error in the conversion * @param value Value to convert * @return String representation of value */
*** 283,294 **** } return value.toString(); } /** ! * Returns the <code>DocumentFilter</code> used to restrict the characters ! * that can be input into the <code>JFormattedTextField</code>. * * @return DocumentFilter to restrict edits */ protected DocumentFilter getDocumentFilter() { if (documentFilter == null) { --- 283,294 ---- } return value.toString(); } /** ! * Returns the {@code DocumentFilter} used to restrict the characters ! * that can be input into the {@code JFormattedTextField}. * * @return DocumentFilter to restrict edits */ protected DocumentFilter getDocumentFilter() { if (documentFilter == null) {
*** 296,306 **** } return documentFilter; } /** ! * Returns the <code>NavigationFilter</code> used to restrict where the * cursor can be placed. * * @return NavigationFilter to restrict navigation */ protected NavigationFilter getNavigationFilter() { --- 296,306 ---- } return documentFilter; } /** ! * Returns the {@code NavigationFilter} used to restrict where the * cursor can be placed. * * @return NavigationFilter to restrict navigation */ protected NavigationFilter getNavigationFilter() {
*** 335,372 **** } } /** * Returns the initial location to position the cursor at. This forwards ! * the call to <code>getNextNavigatableChar</code>. */ int getInitialVisualPosition() { return getNextNavigatableChar(0, 1); } /** * Subclasses should override this if they want cursor navigation * to skip certain characters. A return value of false indicates ! * the character at <code>offset</code> should be skipped when * navigating throught the field. */ boolean isNavigatable(int offset) { return true; } /** ! * Returns true if the text in <code>text</code> can be inserted. This * does not mean the text will ultimately be inserted, it is used if * text can trivially reject certain characters. */ boolean isLegalInsertText(String text) { return true; } /** * Returns the next editable character starting at offset incrementing ! * the offset by <code>direction</code>. */ private int getNextNavigatableChar(int offset, int direction) { int max = getFormattedTextField().getDocument().getLength(); while (offset >= 0 && offset < max) { --- 335,372 ---- } } /** * Returns the initial location to position the cursor at. This forwards ! * the call to {@code getNextNavigatableChar}. */ int getInitialVisualPosition() { return getNextNavigatableChar(0, 1); } /** * Subclasses should override this if they want cursor navigation * to skip certain characters. A return value of false indicates ! * the character at {@code offset} should be skipped when * navigating throught the field. */ boolean isNavigatable(int offset) { return true; } /** ! * Returns true if the text in {@code text} can be inserted. This * does not mean the text will ultimately be inserted, it is used if * text can trivially reject certain characters. */ boolean isLegalInsertText(String text) { return true; } /** * Returns the next editable character starting at offset incrementing ! * the offset by {@code direction}. */ private int getNextNavigatableChar(int offset, int direction) { int max = getFormattedTextField().getDocument().getLength(); while (offset >= 0 && offset < max) {
*** 378,389 **** return offset; } /** * A convenience methods to return the result of deleting ! * <code>deleteLength</code> characters at <code>offset</code> ! * and inserting <code>replaceString</code> at <code>offset</code> * in the current text field. */ String getReplaceString(int offset, int deleteLength, String replaceString) { String string = getFormattedTextField().getText(); --- 378,389 ---- return offset; } /** * A convenience methods to return the result of deleting ! * {@code deleteLength} characters at {@code offset} ! * and inserting {@code replaceString} at {@code offset} * in the current text field. */ String getReplaceString(int offset, int deleteLength, String replaceString) { String string = getFormattedTextField().getText();
*** 398,410 **** } return result; } /* ! * Returns true if the operation described by <code>rh</code> will ! * result in a legal edit. This may set the <code>value</code> ! * field of <code>rh</code>. */ boolean isValidEdit(ReplaceHolder rh) { if (!getAllowsInvalid()) { String newString = getReplaceString(rh.offset, rh.length, rh.text); --- 398,410 ---- } return result; } /* ! * Returns true if the operation described by {@code rh} will ! * result in a legal edit. This may set the {@code value} ! * field of {@code rh}. */ boolean isValidEdit(ReplaceHolder rh) { if (!getAllowsInvalid()) { String newString = getReplaceString(rh.offset, rh.length, rh.text);
*** 418,428 **** } return true; } /** ! * Invokes <code>commitEdit</code> on the JFormattedTextField. */ void commitEdit() throws ParseException { JFormattedTextField ftf = getFormattedTextField(); if (ftf != null) { --- 418,428 ---- } return true; } /** ! * Invokes {@code commitEdit} on the JFormattedTextField. */ void commitEdit() throws ParseException { JFormattedTextField ftf = getFormattedTextField(); if (ftf != null) {
*** 430,449 **** } } /** * Pushes the value to the JFormattedTextField if the current value ! * is valid and invokes <code>setEditValid</code> based on the * validity of the value. */ void updateValue() { updateValue(null); } /** ! * Pushes the <code>value</code> to the editor if we are to ! * commit on edits. If <code>value</code> is null, the current value * will be obtained from the text component. */ void updateValue(Object value) { try { if (value == null) { --- 430,449 ---- } } /** * Pushes the value to the JFormattedTextField if the current value ! * is valid and invokes {@code setEditValid} based on the * validity of the value. */ void updateValue() { updateValue(null); } /** ! * Pushes the {@code value} to the editor if we are to ! * commit on edits. If {@code value} is null, the current value * will be obtained from the text component. */ void updateValue(Object value) { try { if (value == null) {
*** 461,472 **** } } /** * Returns the next cursor position from offset by incrementing ! * <code>direction</code>. This uses ! * <code>getNextNavigatableChar</code> * as well as constraining the location to the max position. */ int getNextCursorPosition(int offset, int direction) { int newOffset = getNextNavigatableChar(offset, direction); int max = getFormattedTextField().getDocument().getLength(); --- 461,472 ---- } } /** * Returns the next cursor position from offset by incrementing ! * {@code direction}. This uses ! * {@code getNextNavigatableChar} * as well as constraining the location to the max position. */ int getNextCursorPosition(int offset, int direction) { int newOffset = getNextNavigatableChar(offset, direction); int max = getFormattedTextField().getDocument().getLength();
*** 537,569 **** } return value; } /** ! * Returns true if the edit described by <code>rh</code> will result * in a legal value. */ boolean canReplace(ReplaceHolder rh) { return isValidEdit(rh); } /** ! * DocumentFilter method, funnels into <code>replace</code>. */ void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException { ReplaceHolder rh = getReplaceHolder(fb, offset, length, text, attrs); replace(rh); } /** ! * If the edit described by <code>rh</code> is legal, this will * return true, commit the edit (if necessary) and update the cursor ! * position. This forwards to <code>canReplace</code> and ! * <code>isLegalInsertText</code> as necessary to determine if * the edit is in fact legal. * <p> * All of the DocumentFilter methods funnel into here, you should * generally only have to override this. */ --- 537,569 ---- } return value; } /** ! * Returns true if the edit described by {@code rh} will result * in a legal value. */ boolean canReplace(ReplaceHolder rh) { return isValidEdit(rh); } /** ! * DocumentFilter method, funnels into {@code replace}. */ void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException { ReplaceHolder rh = getReplaceHolder(fb, offset, length, text, attrs); replace(rh); } /** ! * If the edit described by {@code rh} is legal, this will * return true, commit the edit (if necessary) and update the cursor ! * position. This forwards to {@code canReplace} and ! * {@code isLegalInsertText} as necessary to determine if * the edit is in fact legal. * <p> * All of the DocumentFilter methods funnel into here, you should * generally only have to override this. */
< prev index next >