Package ch.ethz.prose.eclipse.internal.core

Source Code of ch.ethz.prose.eclipse.internal.core.ProsePlugin

//
//  This file is part of the Prose Development Tools for Eclipse package.
//
//  The contents of this file are subject to the Mozilla Public License
//  Version 1.1 (the "License"); you may not use this file except in
//  compliance with the License. You may obtain a copy of the License at
//  http://www.mozilla.org/MPL/
//
//  Software distributed under the License is distributed on an "AS IS" basis,
//  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
//  for the specific language governing rights and limitations under the
//  License.
//
//  The Original Code is Prose Development Tools for Eclipse.
//
//  The Initial Developer of the Original Code is Angela Nicoara. Portions
//  created by Angela Nicoara are Copyright (C) 2006 Angela Nicoara.
//  All Rights Reserved.
//
//  Contributor(s):
//  $Id: ProsePlugin.java,v 1.1 2008/11/18 12:28:36 anicoara Exp $
//  ==============================================================================
//

package ch.ethz.prose.eclipse.internal.core;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.util.SafeRunnable;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.Bundle;

import ch.ethz.prose.eclipse.IProseRunListener;
import ch.ethz.prose.eclipse.internal.run.AspectManagerNode;
import ch.ethz.prose.eclipse.internal.run.AspectNode;
import ch.ethz.prose.eclipse.internal.run.ProseRunNode;

/**
* Main plug-in class.
*
* @author Angela Nicoara
* @author Johann Gyger
* @version $Id: ProsePlugin.java,v 1.1 2008/11/18 12:28:36 anicoara Exp $
*/
public class ProsePlugin extends AbstractUIPlugin {

    /** Plugin ID */
    public static final String PLUGIN_ID = "ch.ethz.prose.eclipse"; //$NON-NLS-1$

    /** Run listener ID */
    public static final String RUN_LISTENER_ID = "ch.ethz.prose.eclipse.run_listeners"; //$NON-NLS-1$
   
    private static ProsePlugin plugin;

    protected ResourceBundle resourceBundle;

    protected List runs = new ArrayList();

    protected List runListeners;

    /**
     * @return Shared plug-in instance (singleton).
     */
    public static ProsePlugin getDefault() {
        return plugin;
    }

    /**
     * Log exception to workspace log file.
     *
     * @param e Exception to log
     */
    public static void log(Throwable e) {
        log(new Status(IStatus.ERROR, PLUGIN_ID, IStatus.ERROR, "Error", e)); //$NON-NLS-1$
    }

    /**
     * Log status to workspace log file.
     *
     * @param status Status to log
     */
    public static void log(IStatus status) {
        getDefault().getLog().log(status);
    }

    /**
     * @return Active workbench window
     */
    public static IWorkbenchWindow getWorkbenchWindow() {
        return getDefault().getWorkbench().getActiveWorkbenchWindow();
    }

    /**
     * @return Active workbench shell or <code>null</code> if none
     */
    public static Shell getShell() {
        IWorkbenchWindow window = getWorkbenchWindow();
        if (window != null) { return window.getShell(); }
        return null;
    }

    /**
     * Open an error dialog.
     *
     * @param title Dialog title
     * @param message Error message
     */
    public static void openErrorDialog(String title, String message) {
        MessageDialog.openError(getShell(), title, message);
    }

    /**
     * @param key Resource key
     * @return String from the plug-in's resource bundle, or 'key' if not found.
     */
    public static String getResourceString(String key) {
        ResourceBundle bundle = ProsePlugin.getDefault().getResourceBundle();
        try {
            return (bundle != null) ? bundle.getString(key) : key;
        } catch (MissingResourceException e) {
            return key;
        }
    }

    /**
     * Create new plug-in instance.
     */
    public ProsePlugin() {
        plugin = this;
        try {
            resourceBundle = ResourceBundle.getBundle("ch.ethz.prose.eclipse.ProsePluginResources");
        } catch (MissingResourceException x) {
            resourceBundle = null;
        }
    }

    /**
     * @return Plug-in's resource bundle,
     */
    public ResourceBundle getResourceBundle() {
        return resourceBundle;
    }

    /**
     * @return Path location of plug-in "ch.ethz.prose"
     * @throws CoreException If the path couldn't be determined
     */
    public String getProsePath() throws CoreException {
        try {
            Bundle bundle = Platform.getBundle("ch.ethz.prose"); //$NON-NLS-1$
            URL url = bundle.getEntry("/");
            return Platform.asLocalURL(url).getPath();
        } catch (IOException e) {
            IStatus status = new Status(IStatus.ERROR, PLUGIN_ID, IStatus.OK,
                    "Could not determine path", e); //$NON-NLS-1$
            throw new CoreException(status);
        }
    }

    /**
     * @return List of run listeners
     */
    public List getRunListeners() {
        if (runListeners == null) runListeners = computeListeners();
        return runListeners;
    }

    protected List computeListeners() {
        IExtensionRegistry registry = Platform.getExtensionRegistry();
        IExtensionPoint extensionPoint = registry.getExtensionPoint(RUN_LISTENER_ID);
        IExtension[] extensions = extensionPoint.getExtensions();
        ArrayList results = new ArrayList();
        for (int i = 0; i < extensions.length; i++) {
            IConfigurationElement[] elements = extensions[i].getConfigurationElements();
            for (int j = 0; j < elements.length; j++) {
                try {
                    Object listener = elements[j].createExecutableExtension("class");
                    if (listener instanceof IProseRunListener) results.add(listener);
                } catch (CoreException e) {
                    e.printStackTrace();
                }
            }
        }
        return results;
    }

    /**
     * @return Prose runs
     */
    public Object[] getRuns() {
        return runs.toArray();
    }

    /**
     * Add a Prose run listener.
     *
     * @param listener Listener to add
     */
    public void addRunListener(IProseRunListener listener) {
        getRunListeners().add(listener);
    }

    /**
     * Remove a Prose run listener.
     *
     * @param listener Listener to remove
     */
    public void removeRunListener(IProseRunListener listener) {
        getRunListeners().remove(listener);
    }

    /**
     * Create an image descriptor.
     *
     * @param path Relative path to image file
     * @return New image descriptor
     */
    public ImageDescriptor createImageDescriptor(String path) {
        URL url = getBundle().getEntry("/");
        ImageDescriptor descriptor = null;
        try {
            descriptor = ImageDescriptor.createFromURL(new URL(url, path));
        } catch (MalformedURLException e) {
            descriptor = ImageDescriptor.getMissingImageDescriptor();
        }
        return descriptor;
    }

    /**
     * Add a new Prose run.
     *
     * @param run Node that contains the Prose run being added
     */
    public void addRun(final ProseRunNode run) {
        runs.add(run);
        for (Iterator all = getRunListeners().iterator(); all.hasNext();) {
            final IProseRunListener each = (IProseRunListener) all.next();
            Platform.run(new SafeRunnable() {
                public void run() throws Exception {
                    each.runAdded(run);
                }
            });
        }
    }

    /**
     * Remove a Prose run.
     *
     * @param run Node that contains the Prose run being started
     */
    public void removeRun(final ProseRunNode run) {
        runs.remove(run);
        for (Iterator all = getRunListeners().iterator(); all.hasNext();) {
            final IProseRunListener each = (IProseRunListener) all.next();
            Platform.run(new SafeRunnable() {
                public void run() throws Exception {
                    each.runRemoved(run);
                }
            });
        }
    }

    /**
     * Notify listeners that a Prose run is unreachable.
     *
     * @param run Node that contains the Prose run which is unreachable
     */
    public void fireRunUnreachable(final ProseRunNode run) {
        for (Iterator all = getRunListeners().iterator(); all.hasNext();) {
            final IProseRunListener each = (IProseRunListener) all.next();
            Platform.run(new SafeRunnable() {
                public void run() throws Exception {
                    each.runUnreachable(run);
                }
            });
        }
    }

    /**
     * Notify listeners that an aspect has been inserted.
     *
     * @param node Node that contains the aspect being inserted
     */
    public void fireAspectInserted(final AspectManagerNode node) {
        for (Iterator all = getRunListeners().iterator(); all.hasNext();) {
            final IProseRunListener each = (IProseRunListener) all.next();
            Platform.run(new SafeRunnable() {
                public void run() throws Exception {
                    each.aspectInserted(node);
                }
            });
        }
    }

    /**
     * Notify listeners that an aspect has been withdrawn.
     *
     * @param node Aspect being withdawn
     */
    public void fireAspectWithdrawn(final AspectNode node) {
        for (Iterator all = getRunListeners().iterator(); all.hasNext();) {
            final IProseRunListener each = (IProseRunListener) all.next();
            Platform.run(new SafeRunnable() {
                public void run() throws Exception {
                    each.aspectWithdrawn(node);
                }
            });
        }
    }

    /**
     * @return Reachable aspect managers
     */
    public List getAspectManagers() {
        List managers = new ArrayList();
        for (Iterator all = runs.iterator(); all.hasNext();) {
            ProseRunNode each = (ProseRunNode) all.next();
            if (each.hasChildren()) {
                Object[] nodes = each.getChildren();
                managers.add(nodes[0]);
                managers.add(nodes[1]);
            }
        }
        return managers;
    }
       
}
TOP

Related Classes of ch.ethz.prose.eclipse.internal.core.ProsePlugin

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.