KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jsp2_1 > examples > elresolver > ColorImplicitObject


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  *
21  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
22  *
23  * Portions Copyright Apache Software Foundation.
24  */

25 package jsp2_1.examples.elresolver;
26
27 import java.io.BufferedReader JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.InputStream JavaDoc;
30 import java.io.InputStreamReader JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.StringTokenizer JavaDoc;
34
35 /**
36  * Implicit object that ${Color} resolves to.
37  *
38  * @author Mark Roth
39  */

40 public class ColorImplicitObject {
41     
42     /** Set of colors by name */
43     private static HashMap JavaDoc colorNames = null;
44     
45     /**
46      * Returns a color from an HTML-style hex String, e.g. #f0f0f0
47      */

48     public static ColorRGB fromHex(String JavaDoc hex) {
49         return fromColor(java.awt.Color.decode(hex));
50     }
51     
52     /**
53      * Returns a color from a java.awt.Color object.
54      */

55     public static ColorRGB fromColor(java.awt.Color JavaDoc color) {
56         return new ColorRGB(color.getRed(), color.getGreen(), color.getBlue());
57     }
58     
59     /**
60      * Returns a color from a name. Uses the resource rgb.txt to load
61      * color names.
62      */

63     public static ColorRGB fromName(String JavaDoc name) {
64         if(colorNames == null) {
65             loadColorNames();
66         }
67         return (ColorRGB)colorNames.get(name);
68     }
69     
70     public String JavaDoc toString() {
71         return "Color Implicit Object";
72     }
73     
74     /**
75      * Package-scope method to get list of all color names
76      */

77     static Iterator JavaDoc colorNameIterator() {
78         if(colorNames == null) {
79             loadColorNames();
80         }
81         return colorNames.keySet().iterator();
82     }
83     
84     /**
85      * Loads colors from resource rgb.txt and converts them to
86      * instances of ColorRGB.
87      */

88     private synchronized static void loadColorNames() {
89         if(colorNames == null) {
90             colorNames = new HashMap JavaDoc();
91             try {
92                 BufferedReader JavaDoc in = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(
93                     ColorImplicitObject.class.getResourceAsStream(
94                     "/jsp2_1/examples/elresolver/rgb.txt")));
95                 String JavaDoc line;
96                 while((line = in.readLine()) != null) {
97                     if(!line.startsWith("!")) {
98                         String JavaDoc colorText = line.substring(0, 12);
99                         String JavaDoc colorName = line.substring(12).trim();
100                         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(
101                             colorText, " ");
102                         int red = Integer.parseInt(st.nextToken().trim());
103                         int green = Integer.parseInt(st.nextToken().trim());
104                         int blue = Integer.parseInt(st.nextToken().trim());
105                         colorNames.put(colorName, new ColorRGB(red, green,
106                             blue));
107                     }
108                 }
109                 in.close();
110             }
111             catch(IOException JavaDoc e) {
112                 throw new RuntimeException JavaDoc("Could not load rgb.txt", e);
113             }
114         }
115     }
116     
117     /**
118      * Color whose red property has been specified
119      * (e.g. ${Color[100]})
120      */

121     public static class ColorR {
122         private int red;
123         public ColorR(int red) {
124             // Handle low or high values robustly.
125
if(red < 0) red = 0;
126             if(red > 255) red = 255;
127             this.red = red;
128         }
129         public int getRed() {
130             return red;
131         }
132         public void setRed(int r) {
133             red = r;
134         }
135         public String JavaDoc toString() {
136             return "Color(" + red + ", ?, ?)";
137         }
138     }
139     
140     /**
141      * Color whose red and green properties have been specified
142      * (e.g. ${Color[100][150]})
143      */

144     public static class ColorRG
145         extends ColorR
146     {
147         private int green;
148         public ColorRG(int red, int green) {
149             super(red);
150             // Handle low or high values robustly.
151
if(green < 0) green = 0;
152             if(green > 255) green = 255;
153             this.green = green;
154         }
155         public int getGreen() {
156             return green;
157         }
158         public void setGreen(int g) {
159             green = g;
160         }
161         public String JavaDoc toString() {
162             return "Color(" + getRed() + ", " + green + ", ?)";
163         }
164     }
165     
166     public static class ColorRGB
167         extends ColorRG
168     {
169         private int blue;
170         public ColorRGB(int red, int green, int blue) {
171             super(red, green);
172             // Handle low or high values robustly.
173
if(blue < 0) blue = 0;
174             if(blue > 255) blue = 255;
175             this.blue = blue;
176         }
177         public int getBlue() {
178             return blue;
179         }
180         public void setBlue(int b) {
181             blue = blue;
182         }
183         public java.awt.Color JavaDoc getColor() {
184             return new java.awt.Color JavaDoc(getRed(), getGreen(), getBlue());
185         }
186         
187         public ColorRGB getDarker() {
188             java.awt.Color JavaDoc darkerColor = getColor().darker();
189             return fromColor(darkerColor);
190         }
191         
192         public ColorRGB getBrighter() {
193             java.awt.Color JavaDoc brighterColor = getColor().brighter();
194             return fromColor(brighterColor);
195         }
196         
197         public String JavaDoc getHex() {
198             return "#" + toHex(getRed()) + toHex(getGreen()) +
199                 toHex(getBlue());
200         }
201         
202         private String JavaDoc toHex(int i) {
203             String JavaDoc result;
204             if(i < 16) {
205                 result = "0" + Integer.toHexString(i);
206             }
207             else {
208                 result = Integer.toHexString(i);
209             }
210             return result;
211         }
212         
213         public String JavaDoc toString() {
214             return "Color(" + getRed() + ", " + getGreen() + ", " + blue + ")";
215         }
216     }
217 }
218
Popular Tags