Package ch.ethz.prose.eclipse.internal.ui

Source Code of ch.ethz.prose.eclipse.internal.ui.InsertAspectAction

//
//  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: InsertAspectAction.java,v 1.1 2008/11/18 12:26:05 anicoara Exp $
//  ==============================================================================
//

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

import java.util.List;

import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IType;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;

import ch.ethz.prose.eclipse.internal.core.ProsePlugin;
import ch.ethz.prose.eclipse.internal.run.ProseRunSelectionDialog;
import ch.ethz.prose.eclipse.internal.run.UnreachableException;

/**
* Object action delegate to insert a Prose aspect.
*
* @author Angela Nicoara
* @author Johann Gyger
* @version $Id: InsertAspectAction.java,v 1.1 2008/11/18 12:26:05 anicoara Exp $
*/
public class InsertAspectAction implements IObjectActionDelegate {

    protected ISelection selection;

    /* (non-Javadoc)
     * @see org.eclipse.ui.IObjectActionDelegate#setActivePart(org.eclipse.jface.action.IAction, org.eclipse.ui.IWorkbenchPart)
     */
    public void setActivePart(IAction action, IWorkbenchPart targetPart) {
        // Do nothing.
    }

    /* (non-Javadoc)
     * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
     */
    public void selectionChanged(IAction action, ISelection selection) {
        this.selection = selection;
    }

    /* (non-Javadoc)
     * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
     */
    public void run(IAction action) {
        IStructuredSelection structured = (IStructuredSelection) selection;
        Object element = structured.getFirstElement();
        IType type = null;
        if (element instanceof IType) {
            type = (IType) element;
        } else if (element instanceof ICompilationUnit) {
            type = ((ICompilationUnit) element).findPrimaryType();
            if (type == null) {
                ProsePlugin.openErrorDialog("No type found", "Compilation unit does not contain a type.");
                return;
            }
        }

        // TODO Check that `type' is a proper Prose aspect.

        List managers = ProsePlugin.getDefault().getAspectManagers();

        if (managers.isEmpty()) {
            ProsePlugin.openErrorDialog("No Prose application", "No Prose application available to insert aspect.");
            return;
        }

        ProseRunSelectionDialog dialog = new ProseRunSelectionDialog(ProsePlugin.getShell(), managers);
        dialog.setTitle("Aspect manager selection");
        dialog.open();
        if (dialog.getReturnCode() == Dialog.OK) {
            try {
                dialog.getAspectManager().insertAspect(type);
            } catch (UnreachableException e) {
                ProsePlugin.openErrorDialog("Aspect insertion failed",
                        "Could not insert aspect because Prose application is not reachable.");
            }
        }

    }

}
TOP

Related Classes of ch.ethz.prose.eclipse.internal.ui.InsertAspectAction

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.