org.jhotdraw.text
Class ColorFormatter

java.lang.Object
  extended by javax.swing.JFormattedTextField.AbstractFormatter
      extended by javax.swing.text.DefaultFormatter
          extended by org.jhotdraw.text.ColorFormatter
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable

public class ColorFormatter
extends javax.swing.text.DefaultFormatter

ColorFormatter is used to format colors into a textual representation which can be edited in an entry field.

The following formats are supported:

By default, the formatter formats Color objects with Format.RGB_INTEGER.

Version:
$Id: ColorFormatter.java 604 2010-01-09 12:00:29Z rawcoder $
Author:
Werner Randelshofer
See Also:
Serialized Form

Nested Class Summary
static class ColorFormatter.Format
          Specifies the formats supported by ColorFormatter.
 
Field Summary
private  boolean allowsNullValue
          Specifies whether the formatter allows null values.
private  boolean isAdaptive
          Specifies whether the formatter should adaptively change its output format depending on the last input format used by the user.
private  ColorFormatter.Format lastUsedInputFormat
          Specifies the last used input format.
private  ColorFormatter.Format outputFormat
          Specifies the preferred output format.
private  java.util.prefs.Preferences prefs
          Preferences used for storing the last used input format.
private static java.util.regex.Pattern rgbHexPattern
          This regular expression is used for parsing the RGB_HEX format.
private static java.util.regex.Pattern rgbIntegerPattern
          This regular expression is used for parsing the RGB_HEX format.
 
Constructor Summary
ColorFormatter()
           
ColorFormatter(ColorFormatter.Format outputFormat, boolean allowsNullValue, boolean isAdaptive)
           
 
Method Summary
static javax.swing.JFormattedTextField.AbstractFormatterFactory createFormatterFactory()
          Convenience method for creating a formatter factory with a ColorFormatter.
static javax.swing.JFormattedTextField.AbstractFormatterFactory createFormatterFactory(ColorFormatter.Format outputFormat, boolean allowsNullValue, boolean isAdaptive)
          Convenience method for creating a formatter factory with a 8@code ColorFormatter}.
 boolean getAllowsNullValue()
          Returns true, if null value is allowed.
 ColorFormatter.Format getLastUsedInputFormat()
          Gets the last used input format.
 ColorFormatter.Format getOutputFormat()
          Gets the output format.
 boolean isAdaptive()
          Returns true, if the color formatter is adaptive.
 void setAdaptive(boolean newValue)
          Sets whether the color formatter adaptively selects its output format depending on the last input format used by the user.
 void setAllowsNullValue(boolean newValue)
          Sets whether a null value is allowed.
private  void setLastUsedInputFormat(ColorFormatter.Format newValue)
           
 void setOutputFormat(ColorFormatter.Format newValue)
          Sets the output format.
 java.lang.Object stringToValue(java.lang.String str)
           
 java.lang.String valueToString(java.lang.Object value)
           
 
Methods inherited from class javax.swing.text.DefaultFormatter
clone, getAllowsInvalid, getCommitsOnValidEdit, getDocumentFilter, getNavigationFilter, getOverwriteMode, getValueClass, install, setAllowsInvalid, setCommitsOnValidEdit, setOverwriteMode, setValueClass
 
Methods inherited from class javax.swing.JFormattedTextField.AbstractFormatter
getActions, getFormattedTextField, invalidEdit, setEditValid, uninstall
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

outputFormat

private ColorFormatter.Format outputFormat
Specifies the preferred output format.


lastUsedInputFormat

private ColorFormatter.Format lastUsedInputFormat
Specifies the last used input format.


rgbHexPattern

private static final java.util.regex.Pattern rgbHexPattern
This regular expression is used for parsing the RGB_HEX format.


rgbIntegerPattern

private static final java.util.regex.Pattern rgbIntegerPattern
This regular expression is used for parsing the RGB_HEX format.


allowsNullValue

private boolean allowsNullValue
Specifies whether the formatter allows null values.


isAdaptive

private boolean isAdaptive
Specifies whether the formatter should adaptively change its output format depending on the last input format used by the user.


prefs

private java.util.prefs.Preferences prefs
Preferences used for storing the last used input format.

Constructor Detail

ColorFormatter

public ColorFormatter()

ColorFormatter

public ColorFormatter(ColorFormatter.Format outputFormat,
                      boolean allowsNullValue,
                      boolean isAdaptive)
Method Detail

setOutputFormat

public void setOutputFormat(ColorFormatter.Format newValue)
Sets the output format.

Parameters:
newValue -

getOutputFormat

public ColorFormatter.Format getOutputFormat()
Gets the output format.


getLastUsedInputFormat

public ColorFormatter.Format getLastUsedInputFormat()
Gets the last used input format.


setAllowsNullValue

public void setAllowsNullValue(boolean newValue)
Sets whether a null value is allowed.

Parameters:
newValue -

getAllowsNullValue

public boolean getAllowsNullValue()
Returns true, if null value is allowed.


setAdaptive

public void setAdaptive(boolean newValue)
Sets whether the color formatter adaptively selects its output format depending on the last input format used by the user.

Parameters:
newValue -

isAdaptive

public boolean isAdaptive()
Returns true, if the color formatter is adaptive.


setLastUsedInputFormat

private void setLastUsedInputFormat(ColorFormatter.Format newValue)

stringToValue

public java.lang.Object stringToValue(java.lang.String str)
                               throws java.text.ParseException
Overrides:
stringToValue in class javax.swing.text.DefaultFormatter
Throws:
java.text.ParseException

valueToString

public java.lang.String valueToString(java.lang.Object value)
                               throws java.text.ParseException
Overrides:
valueToString in class javax.swing.text.DefaultFormatter
Throws:
java.text.ParseException

createFormatterFactory

public static javax.swing.JFormattedTextField.AbstractFormatterFactory createFormatterFactory()
Convenience method for creating a formatter factory with a ColorFormatter. Uses the RGB_INTEGER format, allows null values and is adaptive.


createFormatterFactory

public static javax.swing.JFormattedTextField.AbstractFormatterFactory createFormatterFactory(ColorFormatter.Format outputFormat,
                                                                                              boolean allowsNullValue,
                                                                                              boolean isAdaptive)
Convenience method for creating a formatter factory with a 8@code ColorFormatter}.