KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > css > engine > value > AbstractColorManager


1 /*
2
3    Copyright 2002-2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.css.engine.value;
19
20 import org.apache.batik.css.engine.CSSEngine;
21 import org.apache.batik.css.engine.CSSStylableElement;
22 import org.apache.batik.css.engine.StyleMap;
23 import org.apache.batik.util.CSSConstants;
24 import org.w3c.css.sac.LexicalUnit;
25 import org.w3c.dom.DOMException JavaDoc;
26 import org.w3c.dom.css.CSSPrimitiveValue;
27
28 /**
29  * This class provides a manager for the property with support for
30  * CSS color values.
31  *
32  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
33  * @version $Id: AbstractColorManager.java,v 1.6 2004/12/03 12:20:15 deweese Exp $
34  */

35 public abstract class AbstractColorManager extends IdentifierManager {
36     
37     /**
38      * The identifier values.
39      */

40     protected final static StringMap values = new StringMap();
41     static {
42         values.put(CSSConstants.CSS_AQUA_VALUE,
43                    ValueConstants.AQUA_VALUE);
44         values.put(CSSConstants.CSS_BLACK_VALUE,
45                    ValueConstants.BLACK_VALUE);
46         values.put(CSSConstants.CSS_BLUE_VALUE,
47                    ValueConstants.BLUE_VALUE);
48         values.put(CSSConstants.CSS_FUCHSIA_VALUE,
49                    ValueConstants.FUCHSIA_VALUE);
50         values.put(CSSConstants.CSS_GRAY_VALUE,
51                    ValueConstants.GRAY_VALUE);
52         values.put(CSSConstants.CSS_GREEN_VALUE,
53                    ValueConstants.GREEN_VALUE);
54         values.put(CSSConstants.CSS_LIME_VALUE,
55                    ValueConstants.LIME_VALUE);
56         values.put(CSSConstants.CSS_MAROON_VALUE,
57                    ValueConstants.MAROON_VALUE);
58         values.put(CSSConstants.CSS_NAVY_VALUE,
59                    ValueConstants.NAVY_VALUE);
60         values.put(CSSConstants.CSS_OLIVE_VALUE,
61                    ValueConstants.OLIVE_VALUE);
62         values.put(CSSConstants.CSS_PURPLE_VALUE,
63                    ValueConstants.PURPLE_VALUE);
64         values.put(CSSConstants.CSS_RED_VALUE,
65                    ValueConstants.RED_VALUE);
66         values.put(CSSConstants.CSS_SILVER_VALUE,
67                    ValueConstants.SILVER_VALUE);
68         values.put(CSSConstants.CSS_TEAL_VALUE,
69                    ValueConstants.TEAL_VALUE);
70         values.put(CSSConstants.CSS_WHITE_VALUE,
71                    ValueConstants.WHITE_VALUE);
72         values.put(CSSConstants.CSS_YELLOW_VALUE,
73                    ValueConstants.YELLOW_VALUE);
74
75         values.put(CSSConstants.CSS_ACTIVEBORDER_VALUE,
76                    ValueConstants.ACTIVEBORDER_VALUE);
77         values.put(CSSConstants.CSS_ACTIVECAPTION_VALUE,
78                    ValueConstants.ACTIVECAPTION_VALUE);
79         values.put(CSSConstants.CSS_APPWORKSPACE_VALUE,
80                    ValueConstants.APPWORKSPACE_VALUE);
81         values.put(CSSConstants.CSS_BACKGROUND_VALUE,
82                    ValueConstants.BACKGROUND_VALUE);
83         values.put(CSSConstants.CSS_BUTTONFACE_VALUE,
84                    ValueConstants.BUTTONFACE_VALUE);
85         values.put(CSSConstants.CSS_BUTTONHIGHLIGHT_VALUE,
86                    ValueConstants.BUTTONHIGHLIGHT_VALUE);
87         values.put(CSSConstants.CSS_BUTTONSHADOW_VALUE,
88                    ValueConstants.BUTTONSHADOW_VALUE);
89         values.put(CSSConstants.CSS_BUTTONTEXT_VALUE,
90                    ValueConstants.BUTTONTEXT_VALUE);
91         values.put(CSSConstants.CSS_CAPTIONTEXT_VALUE,
92                    ValueConstants.CAPTIONTEXT_VALUE);
93         values.put(CSSConstants.CSS_GRAYTEXT_VALUE,
94                    ValueConstants.GRAYTEXT_VALUE);
95         values.put(CSSConstants.CSS_HIGHLIGHT_VALUE,
96                    ValueConstants.HIGHLIGHT_VALUE);
97         values.put(CSSConstants.CSS_HIGHLIGHTTEXT_VALUE,
98                    ValueConstants.HIGHLIGHTTEXT_VALUE);
99         values.put(CSSConstants.CSS_INACTIVEBORDER_VALUE,
100                    ValueConstants.INACTIVEBORDER_VALUE);
101         values.put(CSSConstants.CSS_INACTIVECAPTION_VALUE,
102                    ValueConstants.INACTIVECAPTION_VALUE);
103         values.put(CSSConstants.CSS_INACTIVECAPTIONTEXT_VALUE,
104                    ValueConstants.INACTIVECAPTIONTEXT_VALUE);
105         values.put(CSSConstants.CSS_INFOBACKGROUND_VALUE,
106                    ValueConstants.INFOBACKGROUND_VALUE);
107         values.put(CSSConstants.CSS_INFOTEXT_VALUE,
108                    ValueConstants.INFOTEXT_VALUE);
109         values.put(CSSConstants.CSS_MENU_VALUE,
110                    ValueConstants.MENU_VALUE);
111         values.put(CSSConstants.CSS_MENUTEXT_VALUE,
112                    ValueConstants.MENUTEXT_VALUE);
113         values.put(CSSConstants.CSS_SCROLLBAR_VALUE,
114                    ValueConstants.SCROLLBAR_VALUE);
115         values.put(CSSConstants.CSS_THREEDDARKSHADOW_VALUE,
116                    ValueConstants.THREEDDARKSHADOW_VALUE);
117         values.put(CSSConstants.CSS_THREEDFACE_VALUE,
118                    ValueConstants.THREEDFACE_VALUE);
119         values.put(CSSConstants.CSS_THREEDHIGHLIGHT_VALUE,
120                    ValueConstants.THREEDHIGHLIGHT_VALUE);
121         values.put(CSSConstants.CSS_THREEDLIGHTSHADOW_VALUE,
122                    ValueConstants.THREEDLIGHTSHADOW_VALUE);
123         values.put(CSSConstants.CSS_THREEDSHADOW_VALUE,
124                    ValueConstants.THREEDSHADOW_VALUE);
125         values.put(CSSConstants.CSS_WINDOW_VALUE,
126                    ValueConstants.WINDOW_VALUE);
127         values.put(CSSConstants.CSS_WINDOWFRAME_VALUE,
128                    ValueConstants.WINDOWFRAME_VALUE);
129         values.put(CSSConstants.CSS_WINDOWTEXT_VALUE,
130                    ValueConstants.WINDOWTEXT_VALUE);
131     }
132
133     /**
134      * The computed identifier values.
135      */

136     protected final static StringMap computedValues = new StringMap();
137     static {
138         computedValues.put(CSSConstants.CSS_BLACK_VALUE,
139                            ValueConstants.BLACK_RGB_VALUE);
140         computedValues.put(CSSConstants.CSS_SILVER_VALUE,
141                            ValueConstants.SILVER_RGB_VALUE);
142         computedValues.put(CSSConstants.CSS_GRAY_VALUE,
143                            ValueConstants.GRAY_RGB_VALUE);
144         computedValues.put(CSSConstants.CSS_WHITE_VALUE,
145                            ValueConstants.WHITE_RGB_VALUE);
146         computedValues.put(CSSConstants.CSS_MAROON_VALUE,
147                            ValueConstants.MAROON_RGB_VALUE);
148         computedValues.put(CSSConstants.CSS_RED_VALUE,
149                            ValueConstants.RED_RGB_VALUE);
150         computedValues.put(CSSConstants.CSS_PURPLE_VALUE,
151                            ValueConstants.PURPLE_RGB_VALUE);
152         computedValues.put(CSSConstants.CSS_FUCHSIA_VALUE,
153                            ValueConstants.FUCHSIA_RGB_VALUE);
154         computedValues.put(CSSConstants.CSS_GREEN_VALUE,
155                            ValueConstants.GREEN_RGB_VALUE);
156         computedValues.put(CSSConstants.CSS_LIME_VALUE,
157                            ValueConstants.LIME_RGB_VALUE);
158         computedValues.put(CSSConstants.CSS_OLIVE_VALUE,
159                            ValueConstants.OLIVE_RGB_VALUE);
160         computedValues.put(CSSConstants.CSS_YELLOW_VALUE,
161                            ValueConstants.YELLOW_RGB_VALUE);
162         computedValues.put(CSSConstants.CSS_NAVY_VALUE,
163                            ValueConstants.NAVY_RGB_VALUE);
164         computedValues.put(CSSConstants.CSS_BLUE_VALUE,
165                            ValueConstants.BLUE_RGB_VALUE);
166         computedValues.put(CSSConstants.CSS_TEAL_VALUE,
167                            ValueConstants.TEAL_RGB_VALUE);
168         computedValues.put(CSSConstants.CSS_AQUA_VALUE,
169                            ValueConstants.AQUA_RGB_VALUE);
170     }
171
172     /**
173      * Implements {@link ValueManager#createValue(LexicalUnit,CSSEngine)}.
174      */

175     public Value createValue(LexicalUnit lu, CSSEngine engine)
176         throws DOMException JavaDoc {
177         if (lu.getLexicalUnitType() == LexicalUnit.SAC_RGBCOLOR) {
178             lu = lu.getParameters();
179             Value red = createColorComponent(lu);
180             lu = lu.getNextLexicalUnit().getNextLexicalUnit();
181             Value green = createColorComponent(lu);
182             lu = lu.getNextLexicalUnit().getNextLexicalUnit();
183             Value blue = createColorComponent(lu);
184             return createRGBColor(red, green, blue);
185         }
186         return super.createValue(lu, engine);
187     }
188
189     /**
190      * Implements {@link
191      * ValueManager#computeValue(CSSStylableElement,String,CSSEngine,int,StyleMap,Value)}.
192      */

193     public Value computeValue(CSSStylableElement elt,
194                               String JavaDoc pseudo,
195                               CSSEngine engine,
196                               int idx,
197                               StyleMap sm,
198                               Value value) {
199         if (value.getPrimitiveType() == CSSPrimitiveValue.CSS_IDENT) {
200             String JavaDoc ident = value.getStringValue();
201             // Search for a direct computed value.
202
Value v = (Value)computedValues.get(ident);
203             if (v != null) {
204                 return v;
205             }
206             // Must be a system color...
207
if (values.get(ident) == null) {
208                 throw new InternalError JavaDoc();
209             }
210             return engine.getCSSContext().getSystemColor(ident);
211         }
212         return super.computeValue(elt, pseudo, engine, idx, sm, value);
213     }
214
215     /**
216      * Creates an RGB color.
217      */

218     protected Value createRGBColor(Value r, Value g, Value b) {
219         return new RGBColorValue(r, g, b);
220     }
221
222     /**
223      * Creates a color component from a lexical unit.
224      */

225     protected Value createColorComponent(LexicalUnit lu) throws DOMException JavaDoc {
226     switch (lu.getLexicalUnitType()) {
227     case LexicalUnit.SAC_INTEGER:
228         return new FloatValue(CSSPrimitiveValue.CSS_NUMBER,
229                                   lu.getIntegerValue());
230
231     case LexicalUnit.SAC_REAL:
232         return new FloatValue(CSSPrimitiveValue.CSS_NUMBER,
233                                   lu.getFloatValue());
234
235     case LexicalUnit.SAC_PERCENTAGE:
236         return new FloatValue(CSSPrimitiveValue.CSS_PERCENTAGE,
237                                   lu.getFloatValue());
238         }
239         throw createInvalidRGBComponentUnitDOMException
240             (lu.getLexicalUnitType());
241     }
242
243     /**
244      * Implements {@link IdentifierManager#getIdentifiers()}.
245      */

246     public StringMap getIdentifiers() {
247         return values;
248     }
249
250     private DOMException JavaDoc createInvalidRGBComponentUnitDOMException
251         (short type) {
252         Object JavaDoc[] p = new Object JavaDoc[] { getPropertyName(),
253                                     new Integer JavaDoc(type) };
254         String JavaDoc s = Messages.formatMessage("invalid.rgb.component.unit", p);
255         return new DOMException JavaDoc(DOMException.NOT_SUPPORTED_ERR, s);
256     }
257
258 }
259
Popular Tags