67b70903208e8631e8695193b61838d56d26a93a
[deliverable/tracecompass.git] / org.eclipse.tracecompass.lttng2.control.ui / src / org / eclipse / tracecompass / internal / lttng2 / control / ui / Activator.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2015 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
11 * Bernd Hufmann - Add Utility to get a OSGI service
12 *******************************************************************************/
13
14 package org.eclipse.tracecompass.internal.lttng2.control.ui;
15
16 import java.net.URL;
17
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.core.runtime.Status;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.eclipse.jface.resource.ImageDescriptor;
22 import org.eclipse.jface.resource.ImageRegistry;
23 import org.eclipse.swt.graphics.Image;
24 import org.eclipse.tracecompass.internal.lttng2.control.ui.relayd.LttngRelaydConnectionManager;
25 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.preferences.ControlPreferences;
26 import org.eclipse.ui.plugin.AbstractUIPlugin;
27 import org.osgi.framework.BundleContext;
28 import org.osgi.framework.ServiceReference;
29
30 /**
31 * The activator class controls the plug-in life cycle
32 */
33 public class Activator extends AbstractUIPlugin {
34
35 // ------------------------------------------------------------------------
36 // Attributes
37 // ------------------------------------------------------------------------
38
39 /**
40 * The plug-in ID
41 */
42 public static final String PLUGIN_ID = "org.eclipse.linuxtools.lttng2.control.ui"; //$NON-NLS-1$
43
44 /**
45 * The shared instance
46 */
47 private static Activator plugin;
48
49 // ------------------------------------------------------------------------
50 // Constructors
51 // ------------------------------------------------------------------------
52
53 /**
54 * The constructor
55 */
56 public Activator() {
57 }
58
59 // ------------------------------------------------------------------------
60 // Accessors
61 // ------------------------------------------------------------------------
62
63 /**
64 * Returns the shared instance
65 *
66 * @return the shared instance
67 */
68 public static Activator getDefault() {
69 return plugin;
70 }
71
72 // ------------------------------------------------------------------------
73 // AbstractUIPlugin
74 // ------------------------------------------------------------------------
75
76 @Override
77 public void start(BundleContext context) throws Exception {
78 super.start(context);
79 plugin = this;
80 ControlPreferences.getInstance().init(getPreferenceStore());
81 // This registers the connection manager with the signal manager
82 LttngRelaydConnectionManager.getInstance();
83 }
84
85 @Override
86 public void stop(BundleContext context) throws Exception {
87 ControlPreferences.getInstance().dispose();
88 LttngRelaydConnectionManager.getInstance().dispose();
89 plugin = null;
90 super.stop(context);
91 }
92
93 @Override
94 protected void initializeImageRegistry(ImageRegistry reg) {
95 }
96
97 // ------------------------------------------------------------------------
98 // Operations
99 // ------------------------------------------------------------------------
100
101 /**
102 * Gets an image object using given path within plug-in.
103 *
104 * @param path path to image file
105 *
106 * @return image object
107 */
108 public Image getImageFromPath(String path) {
109 return getImageDescripterFromPath(path).createImage();
110 }
111
112 /**
113 * Gets an image descriptor using given path within plug-in.
114 *
115 * @param path path to image file
116 *
117 * @return image descriptor object
118 */
119 public ImageDescriptor getImageDescripterFromPath(String path) {
120 return AbstractUIPlugin.imageDescriptorFromPlugin(PLUGIN_ID, path);
121 }
122
123 /**
124 * Gets a image object from the image registry based on the given path.
125 * If the image is not in the registry it will be registered.
126 *
127 * @param path to the image file
128 * @return image object
129 */
130 public Image getImageFromImageRegistry(String path) {
131 Image icon = getImageRegistry().get(path);
132 if (icon == null) {
133 icon = getImageDescripterFromPath(path).createImage();
134 plugin.getImageRegistry().put(path, icon);
135 }
136 return icon;
137 }
138
139 /**
140 * Loads the image in the plug-ins image registry (if necessary) and returns the image
141 * @param url - URL relative to the Bundle
142 * @return the image
143 */
144 public Image loadIcon(String url) {
145 String key = plugin.getBundle().getSymbolicName() + "/" + url; //$NON-NLS-1$
146 Image icon = plugin.getImageRegistry().get(key);
147 if (icon == null) {
148 URL imageURL = plugin.getBundle().getResource(url);
149 ImageDescriptor descriptor = ImageDescriptor.createFromURL(imageURL);
150 icon = descriptor.createImage();
151 plugin.getImageRegistry().put(key, icon);
152 }
153 return icon;
154 }
155
156 /**
157 * Logs a message with severity INFO in the runtime log of the plug-in.
158 *
159 * @param message A message to log
160 */
161 public void logInfo(String message) {
162 getLog().log(new Status(IStatus.INFO, PLUGIN_ID, message));
163 }
164
165 /**
166 * Logs a message and exception with severity INFO in the runtime log of the plug-in.
167 *
168 * @param message A message to log
169 * @param exception A exception to log
170 */
171 public void logInfo(String message, Throwable exception) {
172 getLog().log(new Status(IStatus.INFO, PLUGIN_ID, message, exception));
173 }
174
175 /**
176 * Logs a message and exception with severity WARNING in the runtime log of the plug-in.
177 *
178 * @param message A message to log
179 */
180 public void logWarning(String message) {
181 getLog().log(new Status(IStatus.WARNING, PLUGIN_ID, message));
182 }
183
184 /**
185 * Logs a message and exception with severity WARNING in the runtime log of the plug-in.
186 *
187 * @param message A message to log
188 * @param exception A exception to log
189 */
190 public void logWarning(String message, Throwable exception) {
191 getLog().log(new Status(IStatus.WARNING, PLUGIN_ID, message, exception));
192 }
193
194 /**
195 * Logs a message and exception with severity ERROR in the runtime log of the plug-in.
196 *
197 * @param message A message to log
198 */
199 public void logError(String message) {
200 getLog().log(new Status(IStatus.ERROR, PLUGIN_ID, message));
201 }
202
203 /**
204 * Logs a message and exception with severity ERROR in the runtime log of the plug-in.
205 *
206 * @param message A message to log
207 * @param exception A exception to log
208 */
209 public void logError(String message, Throwable exception) {
210 getLog().log(new Status(IStatus.ERROR, PLUGIN_ID, message, exception));
211 }
212
213 /**
214 * Return the OSGi service with the given service interface.
215 *
216 * @param service
217 * service interface
218 * @return the specified service or null if it's not registered
219 */
220 public static @Nullable <T> T getService(Class<T> service) {
221 BundleContext context = plugin.getBundle().getBundleContext();
222 ServiceReference<T> ref = context.getServiceReference(service);
223 return ((ref != null) ? context.getService(ref) : null);
224 }
225
226 }
This page took 0.035066 seconds and 4 git commands to generate.