package com.mindprod.jcolourchooser;
import javax.swing.text.DefaultFormatter;
import java.text.ParseException;
/**
* hex NumberFormatter for a JSpinner.
*
* @author Roedy Green, Canadian Mind Products
* @version 1.0 2007-08-15 Created with IntelliJ IDEA.
* @since 2007-08-15
*/
public class HexNumberFormatter extends DefaultFormatter
{
private final int width;
/**
* hex NumberFormatter for a JSpinner.
* <p/>
* constructor
* <p/>
*
* @param width how many chars wide is the field.
*/
public HexNumberFormatter( int width )
{
if ( width > 7 )
{
throw new IllegalArgumentException( "HexFormat width > 7" );
}
this.width = width;
}
/**
* 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
* thows an exception, a <code>ParseException</code> will be thrown. If there is no single argument String
* constructor, <code>string</code> will be returned.
*
* @param string String to convert
*
* @return Object representation of text, namely Integer
* @throws java.text.ParseException if there is an error in the conversion
*/
public Object stringToValue( String string ) throws ParseException
{
try
{
if ( string.length() > width )
{
throw new ParseException( "Max " + width + " digits allowed.",
0 );
}
return Integer.valueOf( string, 16 );
}
catch ( NumberFormatException nfe )
{
throw new ParseException( string, 0 );
}
}
/**
* Converts the passed in Object into a String by way of the <code>toString</code> method.
*
* @param value Value to convert, Integer
*
* @return String representation of value, padded with left zeroes to width.
* @throws ParseException if there is an error in the conversion
*/
public String valueToString( Object value ) throws ParseException
{
final int asInt = ( Integer ) value;
final String hex = Integer.toHexString( asInt );
final int lz = width - hex.length();
if ( lz <= 0 )
{
return hex;
}
else
{
return "00000000".substring( 0, lz ) + hex;
}
}
}