1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
/*******************************************************************************
* Copyright (c) 2000, 2014 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
* QNX Software System
* Anton Leherbauer (Wind River Systems)
*******************************************************************************/
package org.eclipse.cdt.internal.ui.text.util;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.eclipse.cdt.ui.text.IColorManager;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Display;
/**
* CDT color manager.
*/
public class CColorManager implements IColorManager {
protected Map<String, RGB> fKeyTable = new HashMap<>(10);
protected Map<Display, Map<RGB, Color>> fDisplayTable = new HashMap<>(2);
/**
* Flag which tells if the colors are automatically disposed when
* the current display gets disposed.
*/
private boolean fAutoDisposeOnDisplayDispose;
/**
* Creates a new CDT color manager which automatically
* disposes the allocated colors when the current display
* gets disposed.
*/
public CColorManager() {
this(true);
}
/**
* Creates a new CDT color manager.
*
* @param autoDisposeOnDisplayDispose if <code>true</code> the color manager
* automatically disposes all managed colors when the current display gets disposed
* and all calls to {@link org.eclipse.jface.text.source.ISharedTextColors#dispose()} are
* ignored.
*
* @since 4.0
*/
public CColorManager(boolean autoDisposeOnDisplayDispose) {
fAutoDisposeOnDisplayDispose = autoDisposeOnDisplayDispose;
}
protected void dispose(Display display) {
Map<RGB, Color> colorTable = fDisplayTable.get(display);
if (colorTable != null) {
Iterator<Color> e = colorTable.values().iterator();
while (e.hasNext())
(e.next()).dispose();
}
}
@Override
public Color getColor(RGB rgb) {
if (rgb == null)
return null;
final Display display = Display.getCurrent();
Map<RGB, Color> colorTable = fDisplayTable.get(display);
if (colorTable == null) {
colorTable = new HashMap<>(10);
fDisplayTable.put(display, colorTable);
if (fAutoDisposeOnDisplayDispose) {
display.disposeExec(() -> dispose(display));
}
}
Color color = colorTable.get(rgb);
if (color == null) {
color = new Color(Display.getCurrent(), rgb);
colorTable.put(rgb, color);
}
return color;
}
@Override
public void dispose() {
if (!fAutoDisposeOnDisplayDispose)
dispose(Display.getCurrent());
}
@Override
public Color getColor(String key) {
if (key == null)
return null;
RGB rgb = fKeyTable.get(key);
return getColor(rgb);
}
@Override
public void bindColor(String key, RGB rgb) {
Object value = fKeyTable.get(key);
if (value != null)
throw new UnsupportedOperationException();
fKeyTable.put(key, rgb);
}
@Override
public void unbindColor(String key) {
fKeyTable.remove(key);
}
}
|