KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > editors > text > SharedTextColors


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.ui.internal.editors.text;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import org.eclipse.swt.graphics.Color;
18 import org.eclipse.swt.graphics.RGB;
19 import org.eclipse.swt.widgets.Display;
20
21 import org.eclipse.jface.text.source.ISharedTextColors;
22
23 /*
24  * @see org.eclipse.jface.text.source.ISharedTextColors
25  * @since 2.1
26  */

27 class SharedTextColors implements ISharedTextColors {
28
29     /** The display table. */
30     private Map JavaDoc fDisplayTable;
31
32     /** Creates an returns a shared color manager. */
33     public SharedTextColors() {
34         super();
35     }
36
37     /*
38      * @see ISharedTextColors#getColor(RGB)
39      */

40     public Color getColor(RGB rgb) {
41         if (rgb == null)
42             return null;
43
44         if (fDisplayTable == null)
45             fDisplayTable= new HashMap JavaDoc(2);
46
47         final Display display= Display.getCurrent();
48
49         Map JavaDoc colorTable= (Map JavaDoc) fDisplayTable.get(display);
50         if (colorTable == null) {
51             colorTable= new HashMap JavaDoc(10);
52             fDisplayTable.put(display, colorTable);
53             display.disposeExec(new Runnable JavaDoc() {
54                 public void run() {
55                     dispose(display);
56                 }
57             });
58         }
59
60         Color color= (Color) colorTable.get(rgb);
61         if (color == null) {
62             color= new Color(display, rgb);
63             colorTable.put(rgb, color);
64         }
65
66         return color;
67     }
68
69     /*
70      * @see ISharedTextColors#dispose()
71      */

72     public void dispose() {
73         if (fDisplayTable == null)
74             return;
75         
76         Iterator JavaDoc iter= fDisplayTable.values().iterator();
77         while (iter.hasNext())
78             dispose((Map JavaDoc)iter.next());
79         fDisplayTable= null;
80     }
81     
82     /**
83      * Disposes the colors for the given display.
84      *
85      * @param display the display for which to dispose the colors
86      * @since 3.3
87      */

88     private void dispose(Display display) {
89         if (fDisplayTable != null)
90             dispose((Map JavaDoc)fDisplayTable.remove(display));
91     }
92     
93     /**
94      * Disposes the given color table.
95      *
96      * @param colorTable the color table that maps <code>RGB</code> to <code>Color</code>
97      * @since 3.3
98      */

99     private void dispose(Map JavaDoc colorTable) {
100         if (colorTable == null)
101             return;
102         
103         Iterator JavaDoc iter= colorTable.values().iterator();
104         while (iter.hasNext())
105             ((Color) iter.next()).dispose();
106         
107         colorTable.clear();
108     }
109
110 }
111
Popular Tags