JHotDraw 7.5.1

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
Direct Known Subclasses:
ColorToolTipTextFormatter

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 is adaptive, meaning that the format depends on the ColorSpace of the current Color value.

FIXME - This class does too much work. It should be split up into individual classes for each of the supported formats.

Version:
$Id: ColorFormatter.java 660 2010-07-08 20:52:06Z rawcoder $
Author:
Werner Randelshofer
See Also:
Serialized Form

Nested Class Summary
static class ColorFormatter.Format
          Specifies the formats supported by ColorFormatter.
 
Field Summary
protected  boolean allowsNullValue
          Specifies whether the formatter allows null values.
protected static java.util.regex.Pattern grayPercentagePattern
          This regular expression is used for parsing the GRAY_PERCENTAGE format.
protected static java.util.regex.Pattern hsbPercentagePattern
          This regular expression is used for parsing the HSB_PERCENTAGE format.
protected  boolean isAdaptive
          Specifies whether the formatter should adaptively change its output format depending on the last input format used by the user.
protected  ColorFormatter.Format lastUsedInputFormat
          Specifies the last used input format.
protected  java.text.DecimalFormat numberFormat
           
protected  ColorFormatter.Format outputFormat
          Specifies the preferred output format.
protected  java.util.prefs.Preferences prefs
          Preferences used for storing the last used input format.
protected static java.util.regex.Pattern rgbHexPattern
          This regular expression is used for parsing the RGB_HEX format.
protected static java.util.regex.Pattern rgbIntegerPattern
          This regular expression is used for parsing the RGB_INTEGER format.
protected static java.util.regex.Pattern rgbIntegerShortPattern
          This regular expression is used for parsing the RGB_INTEGER format.
protected static java.util.regex.Pattern rgbPercentagePattern
          This regular expression is used for parsing the RGB_PERCENTAGE 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.
 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

protected ColorFormatter.Format outputFormat
Specifies the preferred output format.


lastUsedInputFormat

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


rgbHexPattern

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


rgbIntegerShortPattern

protected static final java.util.regex.Pattern rgbIntegerShortPattern
This regular expression is used for parsing the RGB_INTEGER format.


rgbIntegerPattern

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


rgbPercentagePattern

protected static final java.util.regex.Pattern rgbPercentagePattern
This regular expression is used for parsing the RGB_PERCENTAGE format.


hsbPercentagePattern

protected static final java.util.regex.Pattern hsbPercentagePattern
This regular expression is used for parsing the HSB_PERCENTAGE format. This format is recognized when the degree sign is present.


grayPercentagePattern

protected static final java.util.regex.Pattern grayPercentagePattern
This regular expression is used for parsing the GRAY_PERCENTAGE format. This format is recognized when the degree sign is present.


allowsNullValue

protected boolean allowsNullValue
Specifies whether the formatter allows null values.


isAdaptive

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


prefs

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


numberFormat

protected java.text.DecimalFormat numberFormat
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.


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_SHORT 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}.


Copyright 1996-2010 (c) by the authors and contributors of the JHotDraw project.
Some rights reserved.