Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cb012b88a75eefb95141a88f7f7d999c4b19fab3 (plain) (blame)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*******************************************************************************
 * Copyright (c) 2000, 2013 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.debug.internal.ui.views.launch;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.debug.internal.ui.DelegatingModelPresentation;
import org.eclipse.debug.ui.IDebugModelPresentation;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.IColorProvider;
import org.eclipse.jface.viewers.IFontProvider;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.RGB;

/**
 * Translates images, colors, and fonts into image descriptors, RGBs, and font
 * data for workbench adapters. Also provides labels.
 *
 * @since 3.1
 */
public class DebugElementHelper {

	// a model presentation that can provide images & labels for debug elements
	private static DelegatingModelPresentation fgPresenetation;

	// map of images to image descriptors
	private static Map<Image, ImageDescriptor> fgImages = new HashMap<Image, ImageDescriptor>();

	/**
	 * Disposes this adapter
	 */
	public static void dispose() {
		fgImages.clear();
		if (fgPresenetation != null) {
			fgPresenetation.dispose();
			fgPresenetation = null;
		}
	}

	/**
	 * Returns an image descriptor for the given debug element.
	 *
	 * @param object object for which an image descriptor is required
	 */
	public static ImageDescriptor getImageDescriptor(Object object) {
		Image image = getPresentation().getImage(object);
		return getImageDescriptor(image);
	}

	/**
	 * Returns an image descriptor for the given debug element.
	 *
	 * @param presentation presentation to obtain image from
	 * @param object object for which an image descriptor is required
	 * @since 3.3
	 */
	public static ImageDescriptor getImageDescriptor(Object object, IDebugModelPresentation presentation) {
		Image image = presentation.getImage(object);
		return getImageDescriptor(image);
	}

	public static ImageDescriptor getImageDescriptor(Image image) {
		if (image != null) {
			ImageDescriptor descriptor = fgImages.get(image);
			if (descriptor == null) {
				descriptor = new ImageImageDescriptor(image);
				fgImages.put(image, descriptor);
			}
			return descriptor;
		}
		return null;
	}

	/**
	 * Returns a label for the given debug element.
	 *
	 * @param o object for which a label is required
	 */
	public static String getLabel(Object o) {
		return getPresentation().getText(o);
	}

	/**
	 * Returns a model presentation to use to retrieve lables & images.
	 *
	 * @return a model presentation to use to retrieve lables & images
	 */
	public static DelegatingModelPresentation getPresentation() {
		if (fgPresenetation == null) {
			fgPresenetation = new DelegatingModelPresentation();
		}
		return fgPresenetation;
	}

	/**
	 * Returns the RGB of the foreground color for the given element, or
	 * <code>null</code> if none.
	 *
	 * @param element object for which a foreground color is required
	 * @return the RGB of the foreground color for the given element, or
	 *         <code>null</code> if none
	 */
	public static RGB getForeground(Object element) {
		Color color = getPresentation().getForeground(element);
		if (color != null) {
			return color.getRGB();
		}
		return null;
	}

	/**
	 * Returns the RGB of the foreground color for the given element, or
	 * <code>null</code> if none.
	 *
	 * @param element object for which a foreground color is required
	 * @param presentation presentation to obtain color from
	 * @return the RGB of the foreground color for the given element, or
	 *         <code>null</code> if none
	 * @since 3.3
	 */
	public static RGB getForeground(Object element, IDebugModelPresentation presentation) {
		Color color = null;
		if (presentation instanceof IColorProvider) {
			IColorProvider colorProvider = (IColorProvider) presentation;
			color = colorProvider.getForeground(element);
		} else {
			color = getPresentation().getForeground(element);
		}
		if (color != null) {
			return color.getRGB();
		}
		return null;
	}

	/**
	 * Returns the RGB of the background color for the given element, or
	 * <code>null</code> if none.
	 *
	 * @param element object for which a background color is required
	 * @return the RGB of the background color for the given element, or
	 *         <code>null</code> if none
	 */
	public static RGB getBackground(Object element) {
		Color color = getPresentation().getBackground(element);
		if (color != null) {
			return color.getRGB();
		}
		return null;
	}

	/**
	 * Returns the RGB of the background color for the given element, or
	 * <code>null</code> if none.
	 *
	 * @param element object for which a background color is required
	 * @param presentation presentation to use to retrieve color
	 * @return the RGB of the background color for the given element, or
	 *         <code>null</code> if none
	 * @since 3.3
	 */
	public static RGB getBackground(Object element, IDebugModelPresentation presentation) {
		Color color = null;
		if (presentation instanceof IColorProvider) {
			IColorProvider colorProvider = (IColorProvider) presentation;
			color = colorProvider.getBackground(element);
		} else {
			color = getPresentation().getBackground(element);
		}
		if (color != null) {
			return color.getRGB();
		}
		return null;
	}

	/**
	 * Returns the font data for the given element, or <code>null</code> if
	 * none.
	 *
	 * @param element object for which font data is required
	 * @return the font data for the given element, or <code>null</code> if none
	 */
	public static FontData getFont(Object element) {
		Font font = getPresentation().getFont(element);
		if (font != null) {
			return font.getFontData()[0];
		}
		return null;
	}

	/**
	 * Returns the font data for the given element, or <code>null</code> if
	 * none.
	 *
	 * @param element object for which font data is required
	 * @param presentation presentation to obtain font from
	 * @return the font data for the given element, or <code>null</code> if none
	 * @since 3.3
	 */
	public static FontData getFont(Object element, IDebugModelPresentation presentation) {
		Font font = null;
		if (presentation instanceof IFontProvider) {
			IFontProvider provider = (IFontProvider) presentation;
			font = provider.getFont(element);
		} else {
			font = getPresentation().getFont(element);
		}
		if (font != null) {
			return font.getFontData()[0];
		}
		return null;
	}

	/**
	 * Returns whether the UI thread is required for computing the label for the
	 * given object.
	 *
	 * @param object object a label is being computed for
	 * @return whether the UI thread is required
	 */
	public static boolean requiresUIThread(Object object) {
		DelegatingModelPresentation presentation = getPresentation();
		return presentation.requiresUIThread(object);
	}
}

Back to the top