KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > resource > ColorRegistry


1 /*******************************************************************************
2  * Copyright (c) 2003, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jface.resource;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Collections JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Map JavaDoc;
19 import java.util.Set JavaDoc;
20
21 import org.eclipse.core.runtime.Assert;
22 import org.eclipse.swt.graphics.Color;
23 import org.eclipse.swt.graphics.RGB;
24 import org.eclipse.swt.widgets.Display;
25
26 /**
27  * A color registry maintains a mapping between symbolic color names and SWT
28  * <code>Color</code>s.
29  * <p>
30  * A color registry owns all of the <code>Color</code> objects registered with
31  * it, and automatically disposes of them when the SWT Display that creates the
32  * <code>Color</code>s is disposed. Because of this, clients do not need to
33  * (indeed, must not attempt to) dispose of <code>Color</code> objects
34  * themselves.
35  * </p>
36  * <p>
37  * Methods are provided for registering listeners that will be kept
38  * apprised of changes to list of registed colors.
39  * </p>
40  * <p>
41  * Clients may instantiate this class (it was not designed to be subclassed).
42  * </p>
43  *
44  * @since 3.0
45  */

46 public class ColorRegistry extends ResourceRegistry {
47
48     /**
49      * This registries <code>Display</code>. All colors will be allocated using
50      * it.
51      */

52     protected Display display;
53
54     /**
55      * Collection of <code>Color</code> that are now stale to be disposed when
56      * it is safe to do so (i.e. on shutdown).
57      */

58     private List JavaDoc staleColors = new ArrayList JavaDoc();
59
60     /**
61      * Table of known colors, keyed by symbolic color name (key type: <code>String</code>,
62      * value type: <code>org.eclipse.swt.graphics.Color</code>.
63      */

64     private Map JavaDoc stringToColor = new HashMap JavaDoc(7);
65
66     /**
67      * Table of known color data, keyed by symbolic color name (key type:
68      * <code>String</code>, value type: <code>org.eclipse.swt.graphics.RGB</code>).
69      */

70     private Map JavaDoc stringToRGB = new HashMap JavaDoc(7);
71
72     /**
73      * Runnable that cleans up the manager on disposal of the display.
74      */

75     protected Runnable JavaDoc displayRunnable = new Runnable JavaDoc() {
76         public void run() {
77             clearCaches();
78         }
79     };
80
81     /**
82      * Create a new instance of the receiver that is hooked to the current
83      * display.
84      *
85      * @see org.eclipse.swt.widgets.Display#getCurrent()
86      */

87     public ColorRegistry() {
88         this(Display.getCurrent(), true);
89     }
90
91     /**
92      * Create a new instance of the receiver.
93      *
94      * @param display the <code>Display</code> to hook into.
95      */

96     public ColorRegistry(Display display) {
97         this (display, true);
98     }
99
100     /**
101      * Create a new instance of the receiver.
102      *
103      * @param display the <code>Display</code> to hook into
104      * @param cleanOnDisplayDisposal
105      * whether all fonts allocated by this <code>ColorRegistry</code>
106      * should be disposed when the display is disposed
107      * @since 3.1
108      */

109     public ColorRegistry(Display display, boolean cleanOnDisplayDisposal) {
110         Assert.isNotNull(display);
111         this.display = display;
112         if (cleanOnDisplayDisposal) {
113             hookDisplayDispose();
114         }
115     }
116
117     /**
118      * Create a new <code>Color</code> on the receivers <code>Display</code>.
119      *
120      * @param rgb the <code>RGB</code> data for the color.
121      * @return the new <code>Color</code> object.
122      *
123      * @since 3.1
124      */

125     private Color createColor(RGB rgb) {
126         return new Color(display, rgb);
127     }
128
129     /**
130      * Dispose of all of the <code>Color</code>s in this iterator.
131      *
132      * @param iterator over <code>Collection</code> of <code>Color</code>
133      */

134     private void disposeColors(Iterator JavaDoc iterator) {
135         while (iterator.hasNext()) {
136             Object JavaDoc next = iterator.next();
137             ((Color) next).dispose();
138         }
139     }
140
141     /**
142      * Returns the <code>color</code> associated with the given symbolic color
143      * name, or <code>null</code> if no such definition exists.
144      *
145      * @param symbolicName symbolic color name
146      * @return the <code>Color</code> or <code>null</code>
147      */

148     public Color get(String JavaDoc symbolicName) {
149
150         Assert.isNotNull(symbolicName);
151         Object JavaDoc result = stringToColor.get(symbolicName);
152         if (result != null) {
153             return (Color) result;
154         }
155
156         Color color = null;
157
158         result = stringToRGB.get(symbolicName);
159         if (result == null) {
160             return null;
161         }
162
163         color = createColor((RGB) result);
164
165         stringToColor.put(symbolicName, color);
166
167         return color;
168     }
169
170     /* (non-Javadoc)
171      * @see org.eclipse.jface.resource.ResourceRegistry#getKeySet()
172      */

173     public Set JavaDoc getKeySet() {
174         return Collections.unmodifiableSet(stringToRGB.keySet());
175     }
176
177     /**
178      * Returns the color data associated with the given symbolic color name.
179      *
180      * @param symbolicName symbolic color name.
181      * @return the <code>RGB</code> data.
182      */

183     public RGB getRGB(String JavaDoc symbolicName) {
184         Assert.isNotNull(symbolicName);
185         return (RGB) stringToRGB.get(symbolicName);
186     }
187     
188     /**
189      * Returns the color descriptor associated with the given symbolic color name.
190      * @since 3.1
191      *
192      * @param symbolicName
193      * @return the color descriptor associated with the given symbolic color name.
194      */

195     public ColorDescriptor getColorDescriptor(String JavaDoc symbolicName) {
196         return ColorDescriptor.createFrom(getRGB(symbolicName));
197     }
198
199     /* (non-Javadoc)
200      * @see org.eclipse.jface.resource.ResourceRegistry#clearCaches()
201      */

202     protected void clearCaches() {
203         disposeColors(stringToColor.values().iterator());
204         disposeColors(staleColors.iterator());
205         stringToColor.clear();
206         staleColors.clear();
207     }
208
209     /* (non-Javadoc)
210      * @see org.eclipse.jface.resource.ResourceRegistry#hasValueFor(java.lang.String)
211      */

212     public boolean hasValueFor(String JavaDoc colorKey) {
213         return stringToRGB.containsKey(colorKey);
214     }
215
216     /**
217      * Hook a dispose listener on the SWT display.
218      */

219     private void hookDisplayDispose() {
220         display.disposeExec(displayRunnable);
221     }
222
223     /**
224      * Adds (or replaces) a color to this color registry under the given
225      * symbolic name.
226      * <p>
227      * A property change event is reported whenever the mapping from a symbolic
228      * name to a color changes. The source of the event is this registry; the
229      * property name is the symbolic color name.
230      * </p>
231      *
232      * @param symbolicName the symbolic color name
233      * @param colorData an <code>RGB</code> object
234      */

235     public void put(String JavaDoc symbolicName, RGB colorData) {
236         put(symbolicName, colorData, true);
237     }
238
239     /**
240      * Adds (or replaces) a color to this color registry under the given
241      * symbolic name.
242      * <p>
243      * A property change event is reported whenever the mapping from a symbolic
244      * name to a color changes. The source of the event is this registry; the
245      * property name is the symbolic color name.
246      * </p>
247      *
248      * @param symbolicName the symbolic color name
249      * @param colorData an <code>RGB</code> object
250      * @param update - fire a color mapping changed if true. False if this
251      * method is called from the get method as no setting has
252      * changed.
253      */

254     private void put(String JavaDoc symbolicName, RGB colorData, boolean update) {
255
256         Assert.isNotNull(symbolicName);
257         Assert.isNotNull(colorData);
258
259         RGB existing = (RGB) stringToRGB.get(symbolicName);
260         if (colorData.equals(existing)) {
261             return;
262         }
263
264         Color oldColor = (Color) stringToColor.remove(symbolicName);
265         stringToRGB.put(symbolicName, colorData);
266         if (update) {
267             fireMappingChanged(symbolicName, existing, colorData);
268         }
269
270         if (oldColor != null) {
271             staleColors.add(oldColor);
272         }
273     }
274 }
275
Popular Tags