| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2012 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.ui; |
| 14 | |
| 15 | import java.net.URL; |
| 16 | |
| 17 | import org.eclipse.jface.resource.ImageDescriptor; |
| 18 | import org.eclipse.jface.resource.ImageRegistry; |
| 19 | import org.eclipse.swt.graphics.Image; |
| 20 | import org.eclipse.ui.plugin.AbstractUIPlugin; |
| 21 | import org.osgi.framework.BundleContext; |
| 22 | |
| 23 | /** |
| 24 | * The activator class controls the plug-in life cycle |
| 25 | */ |
| 26 | public class Activator extends AbstractUIPlugin { |
| 27 | |
| 28 | // ------------------------------------------------------------------------ |
| 29 | // Attributes |
| 30 | // ------------------------------------------------------------------------ |
| 31 | |
| 32 | /** |
| 33 | * The plug-in ID |
| 34 | */ |
| 35 | public static final String PLUGIN_ID = "org.eclipse.linuxtools.lttng2.ui"; //$NON-NLS-1$ |
| 36 | |
| 37 | /** |
| 38 | * The shared instance |
| 39 | */ |
| 40 | private static Activator plugin; |
| 41 | |
| 42 | // ------------------------------------------------------------------------ |
| 43 | // Constructors |
| 44 | // ------------------------------------------------------------------------ |
| 45 | |
| 46 | /** |
| 47 | * The constructor |
| 48 | */ |
| 49 | public Activator() { |
| 50 | } |
| 51 | |
| 52 | // ------------------------------------------------------------------------ |
| 53 | // Accessors |
| 54 | // ------------------------------------------------------------------------ |
| 55 | |
| 56 | /** |
| 57 | * Returns the shared instance |
| 58 | * |
| 59 | * @return the shared instance |
| 60 | */ |
| 61 | public static Activator getDefault() { |
| 62 | return plugin; |
| 63 | } |
| 64 | |
| 65 | // ------------------------------------------------------------------------ |
| 66 | // AbstractUIPlugin |
| 67 | // ------------------------------------------------------------------------ |
| 68 | |
| 69 | /* (non-Javadoc) |
| 70 | * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext) |
| 71 | */ |
| 72 | @Override |
| 73 | public void start(BundleContext context) throws Exception { |
| 74 | super.start(context); |
| 75 | plugin = this; |
| 76 | } |
| 77 | |
| 78 | /* (non-Javadoc) |
| 79 | * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext) |
| 80 | */ |
| 81 | @Override |
| 82 | public void stop(BundleContext context) throws Exception { |
| 83 | plugin = null; |
| 84 | super.stop(context); |
| 85 | } |
| 86 | |
| 87 | /* (non-Javadoc) |
| 88 | * @see org.eclipse.ui.plugin.AbstractUIPlugin#initializeImageRegistry(org.eclipse.jface.resource.ImageRegistry) |
| 89 | */ |
| 90 | @Override |
| 91 | protected void initializeImageRegistry(ImageRegistry reg) { |
| 92 | } |
| 93 | |
| 94 | // ------------------------------------------------------------------------ |
| 95 | // Operations |
| 96 | // ------------------------------------------------------------------------ |
| 97 | |
| 98 | public Image getImageFromPath(String path) { |
| 99 | return getImageDescripterFromPath(path).createImage(); |
| 100 | } |
| 101 | |
| 102 | public ImageDescriptor getImageDescripterFromPath(String path) { |
| 103 | return AbstractUIPlugin.imageDescriptorFromPlugin(PLUGIN_ID, path); |
| 104 | } |
| 105 | |
| 106 | public Image getImageFromImageRegistry(String path) { |
| 107 | Image icon = getImageRegistry().get(path); |
| 108 | if (icon == null) { |
| 109 | icon = getImageDescripterFromPath(path).createImage(); |
| 110 | plugin.getImageRegistry().put(path, icon); |
| 111 | } |
| 112 | return icon; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Loads the image in the plug-ins image registry (if necessary) and returns the image |
| 117 | * @param url - URL relative to the Bundle |
| 118 | * @return the image |
| 119 | */ |
| 120 | public Image loadIcon(String url) { |
| 121 | String key = plugin.getBundle().getSymbolicName() + "/" + url; //$NON-NLS-1$ |
| 122 | Image icon = plugin.getImageRegistry().get(key); |
| 123 | if (icon == null) { |
| 124 | URL imageURL = plugin.getBundle().getResource(url); |
| 125 | ImageDescriptor descriptor = ImageDescriptor.createFromURL(imageURL); |
| 126 | icon = descriptor.createImage(); |
| 127 | plugin.getImageRegistry().put(key, icon); |
| 128 | } |
| 129 | return icon; |
| 130 | } |
| 131 | |
| 132 | } |