Debian Codegraph @

src/com/jidesoft/converter/RgbColorConverter.java

raw
1 /*
2 * @(#) RgbColorConverter.java
3 *
4 * Copyright 2002 - 2003 JIDE Software. All rights reserved.
5 */
6 package com.jidesoft.converter;
7
8 import java.awt.*;
9 import java.util.StringTokenizer;
10
11 /**
12 * If alpha value is not included, converts Color to/from "XXX, XXX, XXX" format. For example "0, 0, 0" is Color(0, 0,
13 * 0) and "255, 0, 255" is Color(255, 0, 255).
14 * <p/>
15 * If alpha value is included, converts Color to/from "XXX, XXX, XXX, XXX" format. For example "0, 0, 0, 255" is
16 * Color(0, 0, 0, 255) and "255, 0, 255, 100" is Color(255, 0, 255, 100).
17 */
18 public class RgbColorConverter extends ColorConverter {
19
20 private boolean _alphaIncluded = false;
21
22 /**
23 * Creates a RgbColorConverter. This is the default constructor and will not include alpha value.
24 */
25 public RgbColorConverter() {
26 }
27
28 /**
29 * Creates a RgbColorConverter. With this constructor, you can create a converter with alpha value included.
30 *
31 * @param alphaIncluded the flag if alpha value will be included in this converter
32 */
33 public RgbColorConverter(boolean alphaIncluded) {
34 _alphaIncluded = alphaIncluded;
35 }
36
37 /**
38 * Get the flag if this converter should consider alpha value.
39 * <p/>
40 * If you use default constructor, the default value of this flag is false.
41 * <p/>
42 *
43 * @return true if this converter should consider alpha value.
44 *
45 * @see RgbColorConverter
46 */
47 public boolean isAlphaIncluded() {
48 return _alphaIncluded;
49 }
50
51 /**
52 * Set the flag if this converter should consider alpha value.
53 * <p/>
54 *
55 * @param alphaIncluded the flag if this converter should consider alpha value.
56 * @see #isAlphaIncluded()
57 */
58 public void setAlphaIncluded(boolean alphaIncluded) {
59 _alphaIncluded = alphaIncluded;
60 }
61
62 public String toString(Object object, ConverterContext context) {
63 if (object instanceof Color) {
64 Color color = (Color) object;
65 StringBuffer colorText = new StringBuffer();
66 colorText.append(color.getRed()).append(", ");
67 colorText.append(color.getGreen()).append(", ");
68 colorText.append(color.getBlue());
69 if (isAlphaIncluded()) {
70 colorText.append(", ").append(color.getAlpha());
71 }
72 return new String(colorText);
73 }
74 else {
75 return "";
76 }
77 }
78
79 public boolean supportToString(Object object, ConverterContext context) {
80 return true;
81 }
82
83 public boolean supportFromString(String string, ConverterContext context) {
84 return true;
85 }
86
87 public Object fromString(String string, ConverterContext context) {
88 if (string == null || string.trim().length() == 0) {
89 return null;
90 }
91 StringTokenizer token = new StringTokenizer(string, ",; ");
92 int r = 0, g = 0, b = 0, a = 255;
93 if (token.hasMoreTokens()) {
94 String s = token.nextToken();
95 try {
96 r = Integer.parseInt(s, 10) % 256;
97 if (r < 0) {
98 r += 256;
99 }
100 }
101 catch (NumberFormatException e) {
102 return null;
103 }
104 }
105 if (token.hasMoreTokens()) {
106 String s = token.nextToken();
107 try {
108 g = Integer.parseInt(s, 10) % 256;
109 if (g < 0) {
110 g += 256;
111 }
112 }
113 catch (NumberFormatException e) {
114 return null;
115 }
116 }
117 if (token.hasMoreTokens()) {
118 String s = token.nextToken();
119 try {
120 b = Integer.parseInt(s, 10) % 256;
121 if (b < 0) {
122 b += 256;
123 }
124 }
125 catch (NumberFormatException e) {
126 return null;
127 }
128 }
129 if (isAlphaIncluded() && token.hasMoreTokens()) {
130 String s = token.nextToken();
131 try {
132 a = Integer.parseInt(s, 10) % 256;
133 if (a < 0) {
134 a += 256;
135 }
136 }
137 catch (NumberFormatException e) {
138 return null;
139 }
140 }
141
142 return new Color(r, g, b, a);
143 }
144 }