Author: tchemit Date: 2012-08-06 14:06:18 +0200 (Mon, 06 Aug 2012) New Revision: 2387 Url: http://nuiton.org/repositories/revision/nuiton-utils/2387 Log: refs #2229: Make a colorConverter (add javadoc + fixe exceptions) Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/converter/ColorConverter.java Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/converter/ColorConverter.java =================================================================== --- trunk/nuiton-utils/src/main/java/org/nuiton/util/converter/ColorConverter.java 2012-07-31 22:27:06 UTC (rev 2386) +++ trunk/nuiton-utils/src/main/java/org/nuiton/util/converter/ColorConverter.java 2012-08-06 12:06:18 UTC (rev 2387) @@ -1,17 +1,25 @@ package org.nuiton.util.converter; +import org.apache.commons.beanutils.ConversionException; import org.apache.commons.beanutils.Converter; import java.awt.Color; import java.util.Scanner; +/** + * Converter of {@link Color}. + * + * @author mallon <mallon@codelutin.com> + * @author tchemit <chemit@codelutin.com> + * @since 2.5.3 + */ public class ColorConverter implements Converter { @Override public Object convert(Class aClass, Object value) { if (!isEnabled(aClass)) { - throw new IllegalArgumentException("unsupported type: " + aClass); + throw new ConversionException("unsupported type: " + aClass); } String valueToString = (String) value; @@ -20,19 +28,23 @@ * Two formatting cases : * - 'java.awt.Color[r=255,g=51,b=51]', for example * - hexa, like '#000000' - * */ + */ + Color result; try { if (valueToString.length() == 7 && valueToString.charAt(0) == '#') { - Color color = new Color(Integer.parseInt(valueToString.substring(1), 16)); - return color; + result= new Color(Integer.parseInt(valueToString.substring(1), 16)); } else { Scanner sc = new Scanner(valueToString); sc.useDelimiter("\\D+"); - Color color = new Color(sc.nextInt(), sc.nextInt(), sc.nextInt()); - return color; + result = new Color(sc.nextInt(), sc.nextInt(), sc.nextInt()); } - } catch (Exception ex) { - throw new IllegalArgumentException("colors must be of the form #xxxxxx ('#' followed by six hexadecimal digits), or the name of a constant field in java.awt.Color (found: '" + valueToString + "')"); + return result; + } catch (Exception e) { + throw new ConversionException( + "colors must be of the form #xxxxxx ('#' followed by " + + "six hexadecimal digits), or the name of a constant " + + "field in java.awt.Color (found: '" + valueToString + "')", + e); } }