View Javadoc

1   /*
2    * Copyright 2002,2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.jelly.tags.swt.converters;
17  
18  import java.util.StringTokenizer;
19  
20  import org.apache.commons.beanutils.Converter;
21  import org.eclipse.swt.graphics.RGB;
22  
23  /***
24   * A Converter that converts Strings in the form "#uuuuuu" or "x,y,z" into a RGB object
25   *
26   * @author <a href="mailto:ckl@dacelo.nl">Christiaan ten Klooster</a>
27   * @version $Revision: 155420 $
28   */
29  public class ColorConverter implements Converter {
30  
31      private static final ColorConverter instance = new ColorConverter();
32  
33      private static String usageText =
34          "Color value should be in the form of '#xxxxxx' or 'x,y,z'";
35  
36      public static ColorConverter getInstance() {
37          return instance;
38      }
39  
40      /***
41       * Parsers a String in the form "x, y, z" into an SWT RGB class
42       * @param value
43       * @return RGB
44       */
45      protected RGB parseRGB(String value) {
46          StringTokenizer items = new StringTokenizer(value, ",");
47          int red = 0;
48          int green = 0;
49          int blue = 0;
50          if (items.hasMoreTokens()) {
51              red = parseNumber(items.nextToken());
52          }
53          if (items.hasMoreTokens()) {
54              green = parseNumber(items.nextToken());
55          }
56          if (items.hasMoreTokens()) {
57              blue = parseNumber(items.nextToken());
58          }
59          return new RGB(red, green, blue);
60      }
61  
62      /***
63       * Parsers a String in the form "#xxxxxx" into an SWT RGB class
64       * @param value
65       * @return RGB
66       */
67      protected RGB parseHtml(String value) {
68          if (value.length() != 7) {
69              throw new IllegalArgumentException(usageText);
70          }
71          int colorValue = 0;
72          try {
73              colorValue = Integer.parseInt(value.substring(1), 16);
74              java.awt.Color swingColor = new java.awt.Color(colorValue);
75              return new RGB(
76                  swingColor.getRed(),
77                  swingColor.getGreen(),
78                  swingColor.getBlue());
79          } catch (NumberFormatException ex) {
80              throw new IllegalArgumentException(
81                  value + "is not a valid Html color\n " + ex);
82          }
83      }
84  
85      /***
86       * Parse a String
87       */
88      public RGB parse(String value) {
89          if (value.length() <= 1) {
90              throw new IllegalArgumentException(usageText);
91          }
92  
93          if (value.charAt(0) == '#') {
94              return parseHtml(value);
95          } else if (value.indexOf(',') != -1) {
96              return parseRGB(value);
97          } else {
98              throw new IllegalArgumentException(usageText);
99          }
100     }
101 
102     // Converter interface
103     //-------------------------------------------------------------------------
104     public Object convert(Class type, Object value) {
105         Object answer = null;
106         if (value != null) {
107             String text = value.toString();
108             answer = parse(text);
109         }
110 
111         System.out.println("Converting value: " + value + " into: " + answer);
112 
113         return answer;
114     }
115 
116     protected int parseNumber(String text) {
117         text = text.trim();
118         return Integer.parseInt(text.trim());
119     }
120 }