| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2012, 2013 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 | *******************************************************************************/ |
| 12 | |
| 13 | package org.eclipse.linuxtools.internal.lttng2.control.ui; |
| 14 | |
| 15 | import java.net.URL; |
| 16 | |
| 17 | import org.eclipse.core.runtime.IStatus; |
| 18 | import org.eclipse.core.runtime.Status; |
| 19 | import org.eclipse.jface.resource.ImageDescriptor; |
| 20 | import org.eclipse.jface.resource.ImageRegistry; |
| 21 | import org.eclipse.linuxtools.internal.lttng2.control.ui.relayd.LttngRelaydConnectionManager; |
| 22 | import org.eclipse.linuxtools.internal.lttng2.control.ui.views.preferences.ControlPreferences; |
| 23 | import org.eclipse.swt.graphics.Image; |
| 24 | import org.eclipse.ui.plugin.AbstractUIPlugin; |
| 25 | import org.osgi.framework.BundleContext; |
| 26 | |
| 27 | /** |
| 28 | * The activator class controls the plug-in life cycle |
| 29 | */ |
| 30 | public class Activator extends AbstractUIPlugin { |
| 31 | |
| 32 | // ------------------------------------------------------------------------ |
| 33 | // Attributes |
| 34 | // ------------------------------------------------------------------------ |
| 35 | |
| 36 | /** |
| 37 | * The plug-in ID |
| 38 | */ |
| 39 | public static final String PLUGIN_ID = "org.eclipse.linuxtools.lttng2.control.ui"; //$NON-NLS-1$ |
| 40 | |
| 41 | /** |
| 42 | * The shared instance |
| 43 | */ |
| 44 | private static Activator plugin; |
| 45 | |
| 46 | // ------------------------------------------------------------------------ |
| 47 | // Constructors |
| 48 | // ------------------------------------------------------------------------ |
| 49 | |
| 50 | /** |
| 51 | * The constructor |
| 52 | */ |
| 53 | public Activator() { |
| 54 | } |
| 55 | |
| 56 | // ------------------------------------------------------------------------ |
| 57 | // Accessors |
| 58 | // ------------------------------------------------------------------------ |
| 59 | |
| 60 | /** |
| 61 | * Returns the shared instance |
| 62 | * |
| 63 | * @return the shared instance |
| 64 | */ |
| 65 | public static Activator getDefault() { |
| 66 | return plugin; |
| 67 | } |
| 68 | |
| 69 | // ------------------------------------------------------------------------ |
| 70 | // AbstractUIPlugin |
| 71 | // ------------------------------------------------------------------------ |
| 72 | |
| 73 | @Override |
| 74 | public void start(BundleContext context) throws Exception { |
| 75 | super.start(context); |
| 76 | plugin = this; |
| 77 | ControlPreferences.getInstance().init(getPreferenceStore()); |
| 78 | // This registers the connection manager with the signal manager |
| 79 | LttngRelaydConnectionManager.getInstance(); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public void stop(BundleContext context) throws Exception { |
| 84 | ControlPreferences.getInstance().dispose(); |
| 85 | LttngRelaydConnectionManager.getInstance().dispose(); |
| 86 | plugin = null; |
| 87 | super.stop(context); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | protected void initializeImageRegistry(ImageRegistry reg) { |
| 92 | } |
| 93 | |
| 94 | // ------------------------------------------------------------------------ |
| 95 | // Operations |
| 96 | // ------------------------------------------------------------------------ |
| 97 | |
| 98 | /** |
| 99 | * Gets an image object using given path within plug-in. |
| 100 | * |
| 101 | * @param path path to image file |
| 102 | * |
| 103 | * @return image object |
| 104 | */ |
| 105 | public Image getImageFromPath(String path) { |
| 106 | return getImageDescripterFromPath(path).createImage(); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Gets an image descriptor using given path within plug-in. |
| 111 | * |
| 112 | * @param path path to image file |
| 113 | * |
| 114 | * @return image descriptor object |
| 115 | */ |
| 116 | public ImageDescriptor getImageDescripterFromPath(String path) { |
| 117 | return AbstractUIPlugin.imageDescriptorFromPlugin(PLUGIN_ID, path); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Gets a image object from the image registry based on the given path. |
| 122 | * If the image is not in the registry it will be registered. |
| 123 | * |
| 124 | * @param path to the image file |
| 125 | * @return image object |
| 126 | */ |
| 127 | public Image getImageFromImageRegistry(String path) { |
| 128 | Image icon = getImageRegistry().get(path); |
| 129 | if (icon == null) { |
| 130 | icon = getImageDescripterFromPath(path).createImage(); |
| 131 | plugin.getImageRegistry().put(path, icon); |
| 132 | } |
| 133 | return icon; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Loads the image in the plug-ins image registry (if necessary) and returns the image |
| 138 | * @param url - URL relative to the Bundle |
| 139 | * @return the image |
| 140 | */ |
| 141 | public Image loadIcon(String url) { |
| 142 | String key = plugin.getBundle().getSymbolicName() + "/" + url; //$NON-NLS-1$ |
| 143 | Image icon = plugin.getImageRegistry().get(key); |
| 144 | if (icon == null) { |
| 145 | URL imageURL = plugin.getBundle().getResource(url); |
| 146 | ImageDescriptor descriptor = ImageDescriptor.createFromURL(imageURL); |
| 147 | icon = descriptor.createImage(); |
| 148 | plugin.getImageRegistry().put(key, icon); |
| 149 | } |
| 150 | return icon; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Logs a message with severity INFO in the runtime log of the plug-in. |
| 155 | * |
| 156 | * @param message A message to log |
| 157 | */ |
| 158 | public void logInfo(String message) { |
| 159 | getLog().log(new Status(IStatus.INFO, PLUGIN_ID, message)); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Logs a message and exception with severity INFO in the runtime log of the plug-in. |
| 164 | * |
| 165 | * @param message A message to log |
| 166 | * @param exception A exception to log |
| 167 | */ |
| 168 | public void logInfo(String message, Throwable exception) { |
| 169 | getLog().log(new Status(IStatus.INFO, PLUGIN_ID, message, exception)); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Logs a message and exception with severity WARNING in the runtime log of the plug-in. |
| 174 | * |
| 175 | * @param message A message to log |
| 176 | */ |
| 177 | public void logWarning(String message) { |
| 178 | getLog().log(new Status(IStatus.WARNING, PLUGIN_ID, message)); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Logs a message and exception with severity WARNING in the runtime log of the plug-in. |
| 183 | * |
| 184 | * @param message A message to log |
| 185 | * @param exception A exception to log |
| 186 | */ |
| 187 | public void logWarning(String message, Throwable exception) { |
| 188 | getLog().log(new Status(IStatus.WARNING, PLUGIN_ID, message, exception)); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Logs a message and exception with severity ERROR in the runtime log of the plug-in. |
| 193 | * |
| 194 | * @param message A message to log |
| 195 | */ |
| 196 | public void logError(String message) { |
| 197 | getLog().log(new Status(IStatus.ERROR, PLUGIN_ID, message)); |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Logs a message and exception with severity ERROR in the runtime log of the plug-in. |
| 202 | * |
| 203 | * @param message A message to log |
| 204 | * @param exception A exception to log |
| 205 | */ |
| 206 | public void logError(String message, Throwable exception) { |
| 207 | getLog().log(new Status(IStatus.ERROR, PLUGIN_ID, message, exception)); |
| 208 | } |
| 209 | |
| 210 | } |