001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.text;
011:
012: import java.util.HashMap;
013: import java.util.Iterator;
014: import java.util.Map;
015:
016: import org.eclipse.swt.graphics.Color;
017: import org.eclipse.swt.graphics.RGB;
018: import org.eclipse.swt.widgets.Display;
019:
020: import org.eclipse.jdt.ui.text.IColorManager;
021: import org.eclipse.jdt.ui.text.IColorManagerExtension;
022:
023: /**
024: * Java color manager.
025: */
026: public class JavaColorManager implements IColorManager,
027: IColorManagerExtension {
028:
029: protected Map fKeyTable = new HashMap(10);
030: protected Map fDisplayTable = new HashMap(2);
031:
032: /**
033: * Flag which tells if the colors are automatically disposed when
034: * the current display gets disposed.
035: */
036: private boolean fAutoDisposeOnDisplayDispose;
037:
038: /**
039: * Creates a new Java color manager which automatically
040: * disposes the allocated colors when the current display
041: * gets disposed.
042: */
043: public JavaColorManager() {
044: this (true);
045: }
046:
047: /**
048: * Creates a new Java color manager.
049: *
050: * @param autoDisposeOnDisplayDispose if <code>true</code> the color manager
051: * automatically disposes all managed colors when the current display gets disposed
052: * and all calls to {@link org.eclipse.jface.text.source.ISharedTextColors#dispose()} are ignored.
053: *
054: * @since 2.1
055: */
056: public JavaColorManager(boolean autoDisposeOnDisplayDispose) {
057: fAutoDisposeOnDisplayDispose = autoDisposeOnDisplayDispose;
058: }
059:
060: public void dispose(Display display) {
061: Map colorTable = (Map) fDisplayTable.get(display);
062: if (colorTable != null) {
063: Iterator e = colorTable.values().iterator();
064: while (e.hasNext()) {
065: Color color = (Color) e.next();
066: if (color != null && !color.isDisposed())
067: color.dispose();
068: }
069: }
070: }
071:
072: /*
073: * @see IColorManager#getColor(RGB)
074: */
075: public Color getColor(RGB rgb) {
076:
077: if (rgb == null)
078: return null;
079:
080: final Display display = Display.getCurrent();
081: Map colorTable = (Map) fDisplayTable.get(display);
082: if (colorTable == null) {
083: colorTable = new HashMap(10);
084: fDisplayTable.put(display, colorTable);
085: if (fAutoDisposeOnDisplayDispose) {
086: display.disposeExec(new Runnable() {
087: public void run() {
088: dispose(display);
089: }
090: });
091: }
092: }
093:
094: Color color = (Color) colorTable.get(rgb);
095: if (color == null) {
096: color = new Color(Display.getCurrent(), rgb);
097: colorTable.put(rgb, color);
098: }
099:
100: return color;
101: }
102:
103: /*
104: * @see IColorManager#dispose
105: */
106: public void dispose() {
107: if (!fAutoDisposeOnDisplayDispose)
108: dispose(Display.getCurrent());
109: }
110:
111: /*
112: * @see IColorManager#getColor(String)
113: */
114: public Color getColor(String key) {
115:
116: if (key == null)
117: return null;
118:
119: RGB rgb = (RGB) fKeyTable.get(key);
120: return getColor(rgb);
121: }
122:
123: /*
124: * @see IColorManagerExtension#bindColor(String, RGB)
125: */
126: public void bindColor(String key, RGB rgb) {
127: Object value = fKeyTable.get(key);
128: if (value != null)
129: throw new UnsupportedOperationException();
130:
131: fKeyTable.put(key, rgb);
132: }
133:
134: /*
135: * @see IColorManagerExtension#unbindColor(String)
136: */
137: public void unbindColor(String key) {
138: fKeyTable.remove(key);
139: }
140: }
|