%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.commons.jelly.tags.swing.converters.ColorConverter |
|
|
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.swing.converters; |
|
17 | ||
18 | import java.awt.Color; |
|
19 | import java.awt.SystemColor; |
|
20 | import java.lang.reflect.Field; |
|
21 | import java.lang.reflect.Modifier; |
|
22 | ||
23 | import org.apache.commons.beanutils.Converter; |
|
24 | ||
25 | /** |
|
26 | * A Converter that turns Strings in the form "#uuuuuu" (as RGB triple) |
|
27 | * or the name of one of the {@link Color}-constants of the class |
|
28 | * {@link Color} or {@link SystemColor}. |
|
29 | * <p> |
|
30 | * TODO: provide support of ARGB colors as well.<br> |
|
31 | * Future: provide support for color-spaces, indexed colors... |
|
32 | * (in particular theme-based colors) |
|
33 | * |
|
34 | * @author <a href="mailto:paul@activemath.org">Paul Libbrecht</a> |
|
35 | * @version $Revision: 155420 $ |
|
36 | */ |
|
37 | 4 | public class ColorConverter implements Converter { |
38 | ||
39 | 4 | private static String usageText = |
40 | "A color is encoded as a java.awt.Color name or a #xxxxxx triple of hex-bytes."; |
|
41 | ||
42 | public Object convert(Class type, Object value) { |
|
43 | 0 | if (value != null) { |
44 | 0 | String s = value.toString(); |
45 | 0 | if (s.length() <= 1) { |
46 | 0 | throw new IllegalArgumentException(usageText); |
47 | } |
|
48 | 0 | if (s.charAt(0) == '#') { |
49 | 0 | if (s.length() != 7) { |
50 | 0 | throw new IllegalArgumentException(usageText); |
51 | } |
|
52 | 0 | int colorValue = 0; |
53 | try { |
|
54 | 0 | colorValue = Integer.parseInt(s.substring(1), 16); |
55 | 0 | return new Color(colorValue); |
56 | } |
|
57 | 0 | catch (NumberFormatException ex) { |
58 | 0 | throw new IllegalArgumentException( |
59 | "Can't parse \"" |
|
60 | + s |
|
61 | + "\" as an hexadecimal number: " |
|
62 | + ex); |
|
63 | } |
|
64 | } |
|
65 | else { |
|
66 | // a color name |
|
67 | try { |
|
68 | // could it be this is already somewhere: get the value of a static final by string |
|
69 | 0 | Field f = SystemColor.class.getField(s); |
70 | 0 | if (f == null |
71 | || !Modifier.isStatic(f.getModifiers()) |
|
72 | || !Modifier.isFinal(f.getModifiers()) |
|
73 | || !Modifier.isPublic(f.getModifiers()) |
|
74 | || !Color.class.isAssignableFrom(f.getType())) { |
|
75 | ||
76 | 0 | throw new IllegalArgumentException(usageText); |
77 | } |
|
78 | 0 | return (Color) f.get(SystemColor.class); |
79 | } |
|
80 | 0 | catch (Exception ex) { |
81 | 0 | throw new IllegalArgumentException( |
82 | "Can't parse \"" + s + "\" as a color-name: " + ex); |
|
83 | } |
|
84 | } |
|
85 | } |
|
86 | 0 | return null; |
87 | } |
|
88 | ||
89 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |