KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > JavaColorManager


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.jdt.internal.ui.text;
12
13
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.eclipse.swt.graphics.Color;
19 import org.eclipse.swt.graphics.RGB;
20 import org.eclipse.swt.widgets.Display;
21
22 import org.eclipse.jdt.ui.text.IColorManager;
23 import org.eclipse.jdt.ui.text.IColorManagerExtension;
24
25 /**
26  * Java color manager.
27  */

28 public class JavaColorManager implements IColorManager, IColorManagerExtension {
29
30     protected Map JavaDoc fKeyTable= new HashMap JavaDoc(10);
31     protected Map JavaDoc fDisplayTable= new HashMap JavaDoc(2);
32
33     /**
34      * Flag which tells if the colors are automatically disposed when
35      * the current display gets disposed.
36      */

37     private boolean fAutoDisposeOnDisplayDispose;
38
39
40     /**
41      * Creates a new Java color manager which automatically
42      * disposes the allocated colors when the current display
43      * gets disposed.
44      */

45     public JavaColorManager() {
46         this(true);
47     }
48
49     /**
50      * Creates a new Java color manager.
51      *
52      * @param autoDisposeOnDisplayDispose if <code>true</code> the color manager
53      * automatically disposes all managed colors when the current display gets disposed
54      * and all calls to {@link org.eclipse.jface.text.source.ISharedTextColors#dispose()} are ignored.
55      *
56      * @since 2.1
57      */

58     public JavaColorManager(boolean autoDisposeOnDisplayDispose) {
59         fAutoDisposeOnDisplayDispose= autoDisposeOnDisplayDispose;
60     }
61
62     public void dispose(Display display) {
63         Map JavaDoc colorTable= (Map JavaDoc) fDisplayTable.get(display);
64         if (colorTable != null) {
65             Iterator JavaDoc e= colorTable.values().iterator();
66             while (e.hasNext()) {
67                 Color color= (Color)e.next();
68                 if (color != null && !color.isDisposed())
69                     color.dispose();
70             }
71         }
72     }
73
74     /*
75      * @see IColorManager#getColor(RGB)
76      */

77     public Color getColor(RGB rgb) {
78
79         if (rgb == null)
80             return null;
81
82         final Display display= Display.getCurrent();
83         Map JavaDoc colorTable= (Map JavaDoc) fDisplayTable.get(display);
84         if (colorTable == null) {
85             colorTable= new HashMap JavaDoc(10);
86             fDisplayTable.put(display, colorTable);
87             if (fAutoDisposeOnDisplayDispose) {
88                 display.disposeExec(new Runnable JavaDoc() {
89                     public void run() {
90                         dispose(display);
91                     }
92                 });
93             }
94         }
95
96         Color color= (Color) colorTable.get(rgb);
97         if (color == null) {
98             color= new Color(Display.getCurrent(), rgb);
99             colorTable.put(rgb, color);
100         }
101
102         return color;
103     }
104
105     /*
106      * @see IColorManager#dispose
107      */

108     public void dispose() {
109         if (!fAutoDisposeOnDisplayDispose)
110             dispose(Display.getCurrent());
111     }
112
113     /*
114      * @see IColorManager#getColor(String)
115      */

116     public Color getColor(String JavaDoc key) {
117
118         if (key == null)
119             return null;
120
121         RGB rgb= (RGB) fKeyTable.get(key);
122         return getColor(rgb);
123     }
124
125     /*
126      * @see IColorManagerExtension#bindColor(String, RGB)
127      */

128     public void bindColor(String JavaDoc key, RGB rgb) {
129         Object JavaDoc value= fKeyTable.get(key);
130         if (value != null)
131             throw new UnsupportedOperationException JavaDoc();
132
133         fKeyTable.put(key, rgb);
134     }
135
136     /*
137      * @see IColorManagerExtension#unbindColor(String)
138      */

139     public void unbindColor(String JavaDoc key) {
140         fKeyTable.remove(key);
141     }
142 }
143
Popular Tags