ch.hsr.ifs.cutelauncher.CuteLauncherPlugin.java Source code

Java tutorial

Introduction

Here is the source code for ch.hsr.ifs.cutelauncher.CuteLauncherPlugin.java

Source

/*******************************************************************************
 * Copyright (c) 2007 Institute for Software, HSR Hochschule fr Technik  
 * Rapperswil, University of applied sciences
 * 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: 
 * Emanuel Graf - initial API and implementation 
 ******************************************************************************/
package ch.hsr.ifs.cutelauncher;

import java.net.URL;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;

import ch.hsr.ifs.cutelauncher.model.CuteModel;

/**
 * The activator class controls the plug-in life cycle
 */
public class CuteLauncherPlugin extends AbstractUIPlugin {

    // The plug-in ID
    public static final String PLUGIN_ID = "com.industriallogic.gtest.eclipse";

    // The shared instance
    private static CuteLauncherPlugin plugin;

    private static final IPath ICONS_PATH = new Path("$nl$/icons"); //$NON-NLS-1$

    private final CuteModel model = new CuteModel();

    private Display display;

    /**
     * The constructor
     */
    public CuteLauncherPlugin() {
        plugin = this;
    }

    @Override
    public void start(BundleContext context) throws Exception {
        super.start(context);
        display = Display.getCurrent();
    }

    @Override
    public void stop(BundleContext context) throws Exception {
        plugin = null;
        super.stop(context);
    }

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

    /**
     * Convenience method which returns the unique identifier of this plugin.
     */
    public static String getUniqueIdentifier() {
        if (getDefault() == null) {
            // If the default instance is not yet initialized,
            // return a static identifier. This identifier must
            // match the plugin id defined in plugin.xml
            return PLUGIN_ID;
        }
        return getDefault().getBundle().getSymbolicName();
    }

    public static ImageDescriptor getImageDescriptor(String relativePath) {
        IPath path = ICONS_PATH.append(relativePath);
        return createImageDescriptor(getDefault().getBundle(), path);
    }

    public static IWorkbenchWindow getActiveWorkbenchWindow() {
        if (plugin == null)
            return null;
        IWorkbench workBench = plugin.getWorkbench();
        if (workBench == null)
            return null;
        return workBench.getActiveWorkbenchWindow();
    }

    public static IWorkbenchPage getActivePage() {
        IWorkbenchWindow activeWorkbenchWindow = getActiveWorkbenchWindow();
        if (activeWorkbenchWindow == null)
            return null;
        return activeWorkbenchWindow.getActivePage();
    }

    private static ImageDescriptor createImageDescriptor(Bundle bundle, IPath path) {
        URL url = FileLocator.find(bundle, path, null);
        return ImageDescriptor.createFromURL(url);
    }

    public static void log(Throwable e) {
        log(new Status(IStatus.ERROR, PLUGIN_ID, IStatus.ERROR, "Error", e)); //$NON-NLS-1$
    }

    //PDE runtime:Error Log view
    public static void log(IStatus status) {
        getDefault().getLog().log(status);
    }

    public static CuteModel getModel() {
        return getDefault().model;
    }

    public static Display getDisplay() {
        return getDefault().display;
    }

}