2 * Copyright (c) 2003-2004 Christopher Lenz and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * Christopher Lenz - initial API and implementation
11 * $Id: CssColorManager.java,v 1.1 2004-09-02 18:11:48 jsurfer Exp $
14 package net.sourceforge.phpeclipse.css.ui.internal.text;
16 import java.util.HashMap;
17 import java.util.Iterator;
20 import net.sourceforge.phpeclipse.css.ui.text.IColorManager;
22 import org.eclipse.swt.graphics.Color;
23 import org.eclipse.swt.graphics.RGB;
24 import org.eclipse.swt.widgets.Display;
29 public class CssColorManager implements IColorManager {
31 // Instance Variables ------------------------------------------------------
33 private Map keyTable = new HashMap(10);
35 private Map displayTable = new HashMap(2);
38 * Flag which tells if the colors are automatically disposed when the
39 * current display gets disposed.
41 private boolean autoDisposeOnDisplayDispose;
43 // Constructors ------------------------------------------------------------
46 * Creates a new CSS color manager which automatically disposes the
47 * allocated colors when the current display gets disposed.
49 public CssColorManager() {
54 * Creates a new CSS color manager.
56 * @param autoDisposeOnDisplayDispose if <code>true</code> the color manager
57 * automatically disposes all managed colors when the current display
58 * gets disposed and all calls to
59 * {@link org.eclipse.jface.text.source.ISharedTextColors#dispose()}
62 public CssColorManager(boolean autoDisposeOnDisplayDispose) {
63 this.autoDisposeOnDisplayDispose = autoDisposeOnDisplayDispose;
66 // IColorManager Implementation --------------------------------------------
69 * @see org.eclipse.jface.text.source.ISharedTextColors#dispose()
71 public void dispose() {
72 if (!this.autoDisposeOnDisplayDispose) {
73 dispose(Display.getCurrent());
78 * @see org.eclipse.jface.text.source.ISharedTextColors#getColor(RGB)
80 public Color getColor(RGB rgb) {
84 final Display display = Display.getCurrent();
85 Map colorTable = (Map) this.displayTable.get(display);
86 if (colorTable == null) {
87 colorTable = new HashMap(10);
88 this.displayTable.put(display, colorTable);
89 if (this.autoDisposeOnDisplayDispose) {
90 display.disposeExec(new Runnable() {
97 Color color = (Color) colorTable.get(rgb);
99 color = new Color(Display.getCurrent(), rgb);
100 colorTable.put(rgb, color);
106 * @see IColorManager#bindColor(String, RGB)
108 public void bindColor(String key, RGB rgb) {
109 Object value = this.keyTable.get(key);
111 throw new IllegalStateException();
113 this.keyTable.put(key, rgb);
117 * @see IColorManager#getColor(String)
119 public Color getColor(String key) {
123 RGB rgb = (RGB) this.keyTable.get(key);
124 return getColor(rgb);
128 * @see IColorManager#unbindColor(String)
130 public void unbindColor(String key) {
131 this.keyTable.remove(key);
134 // Private Methods ---------------------------------------------------------
136 private void dispose(Display display) {
137 Map colorTable = (Map) this.displayTable.get(display);
138 if (colorTable != null) {
139 for (Iterator i = colorTable.values().iterator(); i.hasNext(); ) {
140 Color color = (Color) i.next();
141 if (color != null && !color.isDisposed()) {