org.eclipse.richbeans.widgets.file.Activator.java Source code

Java tutorial

Introduction

Here is the source code for org.eclipse.richbeans.widgets.file.Activator.java

Source

/*-
 *******************************************************************************
 * Copyright (c) 2011, 2016 Diamond Light Source Ltd.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Matthew Gerring - initial API and implementation and/or initial documentation
 *******************************************************************************/
package org.eclipse.richbeans.widgets.file;

import java.io.File;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

public class Activator extends AbstractUIPlugin {

    // The plug-in ID
    public static final String PLUGIN_ID = "org.eclipse.richbeans.widgets.file"; //$NON-NLS-1$

    // The shared instance
    private static Activator plugin;

    /**
     * The constructor
     */
    public Activator() {
    }

    /*
     * (non-Javadoc)
     * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
     */
    public void start(BundleContext context) throws Exception {
        super.start(context);
        plugin = this;
    }

    /*
     * (non-Javadoc)
     * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
     */
    public void stop(BundleContext context) throws Exception {
        plugin = null;
        super.stop(context);
    }

    /**
     * Returns the shared instance
     *
     * @return the shared instance
     */
    public static Activator getDefault() {
        return plugin;
    }

    public static Image getImage(String path) {
        ImageDescriptor des = imageDescriptorFromPlugin(PLUGIN_ID, path);
        return des.createImage();
    }

    public static ImageDescriptor getImageDescriptor(String path) {

        if (plugin == null) { // Testing
            final File file = new File("../" + PLUGIN_ID + "/" + path);
            final ImageData data = file.exists() ? new ImageData(file.getAbsolutePath())
                    : new ImageData(16, 16, 24, new PaletteData(0xff0000, 0x00ff00, 0x0000ff));
            return new ImageDescriptor() {
                @Override
                public ImageData getImageData() {
                    return data;
                }
            };
        }
        return imageDescriptorFromPlugin(PLUGIN_ID, path);
    }

    public static <T> T getService(Class<T> serviceClass) {
        BundleContext context = plugin.getBundle().getBundleContext();
        ServiceReference<T> ref = context.getServiceReference(serviceClass);
        return context.getService(ref);
    }
}