1 /*******************************************************************************
2  * Copyright (c) 2005, 2018 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *     Bjorn Freeman-Benson - initial API and implementation
14  *******************************************************************************/
15 package org.eclipse.debug.examples.ui.pda;
16 
17 import java.net.URL;
18 import java.util.HashMap;
19 import java.util.Iterator;
20 import java.util.Map;
21 import java.util.Map.Entry;
22 import java.util.MissingResourceException;
23 import java.util.ResourceBundle;
24 
25 import org.eclipse.core.runtime.FileLocator;
26 import org.eclipse.core.runtime.Path;
27 import org.eclipse.core.runtime.Platform;
28 import org.eclipse.jface.resource.ImageDescriptor;
29 import org.eclipse.jface.resource.ImageRegistry;
30 import org.eclipse.swt.graphics.Color;
31 import org.eclipse.swt.graphics.RGB;
32 import org.eclipse.swt.widgets.Display;
33 import org.eclipse.swt.widgets.Shell;
34 import org.eclipse.ui.IWorkbenchWindow;
35 import org.eclipse.ui.PlatformUI;
36 import org.eclipse.ui.plugin.AbstractUIPlugin;
37 import org.osgi.framework.Bundle;
38 import org.osgi.framework.BundleContext;
39 
40 /**
41  * The main plugin class to be used in the desktop.
42  */
43 public class DebugUIPlugin extends AbstractUIPlugin {
44 	//The shared instance.
45 	private static DebugUIPlugin plugin;
46 	//Resource bundle.
47 	private ResourceBundle resourceBundle;
48 
49 	private final static String ICONS_PATH = "icons/full/";//$NON-NLS-1$
50 	private final static String PATH_OBJECT = ICONS_PATH + "obj16/"; //Model object icons //$NON-NLS-1$
51 	private final static String PATH_ELOCALTOOL = ICONS_PATH + "elcl16/"; //Enabled local toolbar icons //$NON-NLS-1$
52 	private final static String PATH_DLOCALTOOL = ICONS_PATH + "dlcl16/"; //Disabled local toolbar icons //$NON-NLS-1$
53 
54 	/**
55 	 * The id of the plugin
56 	 *
57 	 * @since 1.4.200
58 	 */
59 	public static final String PLUGIN_ID = "org.eclipse.debug.examples.ui"; //$NON-NLS-1$
60 	/**
61 	 * Toolbar action to pop data stack
62 	 */
63 	public final static String IMG_ELCL_POP = "IMG_ELCL_POP"; //$NON-NLS-1$
64 	public final static String IMG_DLCL_POP = "IMG_DLCL_POP"; //$NON-NLS-1$
65 
66 	/**
67 	 * Toolbar action to push onto data stack
68 	 */
69 	public final static String IMG_ELCL_PUSH = "IMG_ELCL_PUSH"; //$NON-NLS-1$
70 	public final static String IMG_DLCL_PUSH = "IMG_DLCL_PUSH"; //$NON-NLS-1$
71 
72 	/**
73 	 * PDA program image
74 	 */
75 	public final static String IMG_OBJ_PDA = "IMB_OBJ_PDA"; //$NON-NLS-1$
76 
77 	/**
78 	 * MIDI file image
79 	 */
80 	public final static String IMG_OBJ_MIDI = "IMB_OBJ_MIDI"; //$NON-NLS-1$
81 
82 	/**
83 	 * Keyword color
84 	 */
85 	public final static RGB KEYWORD = new RGB(0,0,255);
86 	public final static RGB LABEL = new RGB(128, 128, 0);
87 
88 	/**
89 	 * Managed colors
90 	 */
91 	private final Map<RGB, Color> fColors = new HashMap<>();
92 
93 	/**
94 	 * The constructor.
95 	 */
DebugUIPlugin()96 	public DebugUIPlugin() {
97 		super();
98 		plugin = this;
99 	}
100 
101 	/**
102 	 * This method is called upon plug-in activation
103 	 */
104 	@Override
start(BundleContext context)105 	public void start(BundleContext context) throws Exception {
106 		super.start(context);
107 //		Toggles single threaded adapter example
108 //		IAdapterManager adapterManager = Platform.getAdapterManager();
109 //		IAdapterFactory factory = new AdapterFactory();
110 //		adapterManager.registerAdapters(factory, PDADebugTarget.class);
111 	}
112 
113 	/**
114 	 * This method is called when the plug-in is stopped
115 	 */
116 	@Override
stop(BundleContext context)117 	public void stop(BundleContext context) throws Exception {
118 		super.stop(context);
119 		plugin = null;
120 		resourceBundle = null;
121 		Iterator<Entry<RGB, Color>> colors = fColors.entrySet().iterator();
122 		while (colors.hasNext()) {
123 			Entry<RGB, Color> entry = colors.next();
124 			entry.getValue().dispose();
125 		}
126 	}
127 
128 	/**
129 	 * Returns the shared instance.
130 	 */
getDefault()131 	public static DebugUIPlugin getDefault() {
132 		return plugin;
133 	}
134 
135 	/**
136 	 * Returns the string from the plugin's resource bundle,
137 	 * or 'key' if not found.
138 	 */
getResourceString(String key)139 	public static String getResourceString(String key) {
140 		ResourceBundle bundle = DebugUIPlugin.getDefault().getResourceBundle();
141 		try {
142 			return (bundle != null) ? bundle.getString(key) : key;
143 		} catch (MissingResourceException e) {
144 			return key;
145 		}
146 	}
147 
148 	/**
149 	 * Returns the plugin's resource bundle,
150 	 */
getResourceBundle()151 	public ResourceBundle getResourceBundle() {
152 		try {
153 			if (resourceBundle == null)
154 			 {
155 				resourceBundle = ResourceBundle.getBundle("org.eclipse.debug.examples.ui.pda.DebugUIPluginResources"); //$NON-NLS-1$
156 			}
157 		} catch (MissingResourceException x) {
158 			resourceBundle = null;
159 		}
160 		return resourceBundle;
161 	}
162 
163 	@Override
initializeImageRegistry(ImageRegistry reg)164 	protected void initializeImageRegistry(ImageRegistry reg) {
165 		declareImage(IMG_OBJ_PDA, PATH_OBJECT + "pda.gif"); //$NON-NLS-1$
166 		declareImage(IMG_OBJ_MIDI, PATH_OBJECT + "note.gif"); //$NON-NLS-1$
167 		declareImage(IMG_ELCL_POP, PATH_ELOCALTOOL + "pop.gif"); //$NON-NLS-1$
168 		declareImage(IMG_DLCL_POP, PATH_DLOCALTOOL + "pop.gif"); //$NON-NLS-1$
169 		declareImage(IMG_ELCL_PUSH, PATH_ELOCALTOOL + "push.gif"); //$NON-NLS-1$
170 		declareImage(IMG_DLCL_PUSH, PATH_DLOCALTOOL + "push.gif"); //$NON-NLS-1$
171 	}
172 
173 	/**
174 	 * Declares a workbench image given the path of the image file (relative to
175 	 * the workbench plug-in). This is a helper method that creates the image
176 	 * descriptor and passes it to the main <code>declareImage</code> method.
177 	 *
178 	 * @param symbolicName the symbolic name of the image
179 	 * @param path the path of the image file relative to the base of the workbench
180 	 * plug-ins install directory
181 	 * <code>false</code> if this is not a shared image
182 	 */
declareImage(String key, String path)183 	private void declareImage(String key, String path) {
184 		ImageDescriptor desc = ImageDescriptor.getMissingImageDescriptor();
185 		Bundle bundle = Platform.getBundle(PLUGIN_ID);
186 		URL url = null;
187 		if (bundle != null) {
188 			url = FileLocator.find(bundle, new Path(path), null);
189 			if (url != null) {
190 				desc = ImageDescriptor.createFromURL(url);
191 			}
192 		}
193 		getImageRegistry().put(key, desc);
194 	}
195 
196 	/**
197 	 * Returns the color described by the given RGB.
198 	 *
199 	 * @param rgb
200 	 * @return color
201 	 */
getColor(RGB rgb)202 	public Color getColor(RGB rgb) {
203 		Color color = fColors.get(rgb);
204 		if (color == null) {
205 			color= new Color(Display.getCurrent(), rgb);
206 			fColors.put(rgb, color);
207 		}
208 		return color;
209 	}
210 
211 	/**
212 	 * Returns the active workbench window
213 	 *
214 	 * @return the active workbench window
215 	 */
getActiveWorkbenchWindow()216 	public static IWorkbenchWindow getActiveWorkbenchWindow() {
217 		return PlatformUI.getWorkbench().getActiveWorkbenchWindow();
218 	}
219 
220 	/**
221 	 * Returns the active workbench shell or <code>null</code> if none
222 	 *
223 	 * @return the active workbench shell or <code>null</code> if none
224 	 */
getActiveWorkbenchShell()225 	public static Shell getActiveWorkbenchShell() {
226 		IWorkbenchWindow window = getActiveWorkbenchWindow();
227 		if (window != null) {
228 			return window.getShell();
229 		}
230 		return null;
231 	}
232 
233  }
234