KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > ui > views > launch > DebugElementHelper


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.debug.internal.ui.views.launch;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import org.eclipse.debug.internal.ui.DelegatingModelPresentation;
17 import org.eclipse.debug.ui.IDebugModelPresentation;
18 import org.eclipse.jface.resource.ImageDescriptor;
19 import org.eclipse.jface.viewers.IColorProvider;
20 import org.eclipse.jface.viewers.IFontProvider;
21 import org.eclipse.swt.graphics.Color;
22 import org.eclipse.swt.graphics.Font;
23 import org.eclipse.swt.graphics.FontData;
24 import org.eclipse.swt.graphics.Image;
25 import org.eclipse.swt.graphics.RGB;
26
27 /**
28  * Translates images, colors, and fonts into image descriptors, RGBs, and font
29  * datas for workbench adapaters. Also provides labels.
30  *
31  * @since 3.1
32  */

33 public class DebugElementHelper {
34     
35     // a model presentation that can provide images & labels for debug elements
36
private static DelegatingModelPresentation fgPresenetation;
37     
38     // map of images to image descriptors
39
private static Map JavaDoc fgImages = new HashMap JavaDoc();
40     
41     /**
42      * Disposes this adapater
43      */

44     public static void dispose() {
45         fgImages.clear();
46         if (fgPresenetation != null) {
47             fgPresenetation.dispose();
48             fgPresenetation = null;
49         }
50     }
51     
52     
53     public static boolean isInitialized(Object JavaDoc object) {
54         DelegatingModelPresentation presentation = getPresentation();
55         return presentation.isInitialized(object);
56     }
57
58     /**
59      * Returns an image descriptor for the given debug element.
60      *
61      * @param object object for which an image descriptor is required
62      */

63     public static ImageDescriptor getImageDescriptor(Object JavaDoc object) {
64         Image image = getPresentation().getImage(object);
65         return getImageDescriptor(image);
66     }
67     
68     /**
69      * Returns an image descriptor for the given debug element.
70      *
71      * @param presentation presentation to obtain image from
72      * @param object object for which an image descriptor is required
73      * @since 3.3
74      */

75     public static ImageDescriptor getImageDescriptor(Object JavaDoc object, IDebugModelPresentation presentation) {
76         Image image = presentation.getImage(object);
77         return getImageDescriptor(image);
78     }
79     
80     public static ImageDescriptor getImageDescriptor(Image image)
81     {
82         if (image != null) {
83             ImageDescriptor descriptor = (ImageDescriptor) fgImages.get(image);
84             if (descriptor == null) {
85                 descriptor = new ImageImageDescriptor(image);
86                 fgImages.put(image, descriptor);
87             }
88             return descriptor;
89         }
90         return null;
91     }
92
93     /**
94      * Returns a label for the given debug element.
95      *
96      * @param o object for which a label is required
97      */

98     public static String JavaDoc getLabel(Object JavaDoc o) {
99         return getPresentation().getText(o);
100     }
101     
102     /**
103      * Returns a model presentation to use to retrieve lables & images.
104      *
105      * @return a model presentation to use to retrieve lables & images
106      */

107     public static DelegatingModelPresentation getPresentation() {
108         if (fgPresenetation == null) {
109             fgPresenetation = new DelegatingModelPresentation();
110         }
111         return fgPresenetation;
112     }
113
114     /**
115      * Returns the RGB of the foreground color for the given element, or
116      * <code>null</code> if none.
117      *
118      * @param element object for which a foreground color is required
119      * @return the RGB of the foreground color for the given element, or
120      * <code>null</code> if none
121      */

122     public static RGB getForeground(Object JavaDoc element) {
123         Color color = getPresentation().getForeground(element);
124         if (color != null) {
125             return color.getRGB();
126         }
127         return null;
128     }
129     
130     /**
131      * Returns the RGB of the foreground color for the given element, or
132      * <code>null</code> if none.
133      *
134      * @param element object for which a foreground color is required
135      * @param presentation presentation to obtain color from
136      * @return the RGB of the foreground color for the given element, or
137      * <code>null</code> if none
138      * @since 3.3
139      */

140     public static RGB getForeground(Object JavaDoc element, IDebugModelPresentation presentation) {
141         Color color = null;
142         if (presentation instanceof IColorProvider) {
143             IColorProvider colorProvider = (IColorProvider) presentation;
144             color = colorProvider.getForeground(element);
145         } else {
146             color = getPresentation().getForeground(element);
147         }
148         if (color != null) {
149             return color.getRGB();
150         }
151         return null;
152     }
153
154     /**
155      * Returns the RGB of the background color for the given element, or
156      * <code>null</code> if none.
157      *
158      * @param element object for which a background color is required
159      * @return the RGB of the background color for the given element, or
160      * <code>null</code> if none
161      */

162     public static RGB getBackground(Object JavaDoc element) {
163         Color color = getPresentation().getBackground(element);
164         if (color != null) {
165             return color.getRGB();
166         }
167         return null;
168     }
169     
170     /**
171      * Returns the RGB of the background color for the given element, or
172      * <code>null</code> if none.
173      *
174      * @param element object for which a background color is required
175      * @param presentation presentation to use to retrieve color
176      * @return the RGB of the background color for the given element, or
177      * <code>null</code> if none
178      * @since 3.3
179      */

180     public static RGB getBackground(Object JavaDoc element, IDebugModelPresentation presentation) {
181         Color color = null;
182         if (presentation instanceof IColorProvider) {
183             IColorProvider colorProvider = (IColorProvider) presentation;
184             color = colorProvider.getBackground(element);
185         } else {
186             color = getPresentation().getBackground(element);
187         }
188         if (color != null) {
189             return color.getRGB();
190         }
191         return null;
192     }
193
194     /**
195      * Returns the font data for the given element, or
196      * <code>null</code> if none.
197      *
198      * @param element object for which font data is required
199      * @return the font data for the given element, or
200      * <code>null</code> if none
201      */

202     public static FontData getFont(Object JavaDoc element) {
203         Font font = getPresentation().getFont(element);
204         if (font != null) {
205             return font.getFontData()[0];
206         }
207         return null;
208     }
209     
210     /**
211      * Returns the font data for the given element, or
212      * <code>null</code> if none.
213      *
214      * @param element object for which font data is required
215      * @param presentation presentation to obtain font from
216      * @return the font data for the given element, or
217      * <code>null</code> if none
218      * @since 3.3
219      */

220     public static FontData getFont(Object JavaDoc element, IDebugModelPresentation presentation) {
221         Font font = null;
222         if (presentation instanceof IFontProvider) {
223             IFontProvider provider = (IFontProvider) presentation;
224             font = provider.getFont(element);
225         } else {
226             font = getPresentation().getFont(element);
227         }
228         if (font != null) {
229             return font.getFontData()[0];
230         }
231         return null;
232     }
233 }
234
Popular Tags