ca.mcgill.cs.swevo.qualyzer.handlers.AddParticipantHandler.java Source code

Java tutorial

Introduction

Here is the source code for ca.mcgill.cs.swevo.qualyzer.handlers.AddParticipantHandler.java

Source

/*******************************************************************************
 * Copyright (c) 2010 McGill University
 * 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:
 *     Jonathan Faubert
 *******************************************************************************/
package ca.mcgill.cs.swevo.qualyzer.handlers;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.navigator.CommonNavigator;

import ca.mcgill.cs.swevo.qualyzer.QualyzerActivator;
import ca.mcgill.cs.swevo.qualyzer.dialogs.QualyzerWizardDialog;
import ca.mcgill.cs.swevo.qualyzer.editors.IDialogTester;
import ca.mcgill.cs.swevo.qualyzer.editors.NullTester;
import ca.mcgill.cs.swevo.qualyzer.model.Project;
import ca.mcgill.cs.swevo.qualyzer.providers.WrapperParticipant;
import ca.mcgill.cs.swevo.qualyzer.ui.ResourcesUtil;
import ca.mcgill.cs.swevo.qualyzer.wizards.AddParticipantWizard;

/**
 * Launches a wizard whenever the New Participant Command is clicked.
 *
 */
public class AddParticipantHandler extends AbstractHandler implements ITestableHandler {

    private IDialogTester fTester = new NullTester();
    private boolean fTesting = false;

    @Override
    public Object execute(ExecutionEvent event) throws ExecutionException {
        IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
        CommonNavigator view = (CommonNavigator) page.findView(QualyzerActivator.PROJECT_EXPLORER_VIEW_ID);
        ISelection selection = view.getCommonViewer().getSelection();

        if (selection != null && selection instanceof IStructuredSelection) {
            IStructuredSelection strucSelection = (IStructuredSelection) selection;
            Object element = strucSelection.getFirstElement();

            Project project = ResourcesUtil.getProject(element);

            AddParticipantWizard wizard = new AddParticipantWizard(project);

            QualyzerWizardDialog dialog = new QualyzerWizardDialog(wizard);
            dialog.setBlockOnOpen(!fTesting);
            dialog.open();
            fTester.execute(dialog);

            if (dialog.getReturnCode() == Window.OK) {
                view.getCommonViewer().refresh();
                WrapperParticipant wrapper = new WrapperParticipant(project);
                IProject wProject = ResourcesPlugin.getWorkspace().getRoot().getProject(project.getFolderName());
                view.getCommonViewer().expandToLevel(wProject, IResource.DEPTH_ONE);
                view.getCommonViewer().expandToLevel(wrapper, IResource.DEPTH_INFINITE);

                ResourcesUtil.openEditor(page, wizard.getParticipant());
            }
        }

        return null;
    }

    /* (non-Javadoc)
     * @see ca.mcgill.cs.swevo.qualyzer.handlers.ITestableHandler#getTester()
     */
    @Override
    public IDialogTester getTester() {
        return fTester;
    }

    /* (non-Javadoc)
     * @see ca.mcgill.cs.swevo.qualyzer.handlers.ITestableHandler#isWindowsBlock()
     */
    @Override
    public boolean isTesting() {
        return fTesting;
    }

    /* (non-Javadoc)
     * @see ca.mcgill.cs.swevo.qualyzer.handlers.ITestableHandler#setTester(
     * ca.mcgill.cs.swevo.qualyzer.editors.IDialogTester)
     */
    @Override
    public void setTester(IDialogTester tester) {
        fTester = tester;
    }

    /* (non-Javadoc)
     * @see ca.mcgill.cs.swevo.qualyzer.handlers.ITestableHandler#setWindowsBlock(boolean)
     */
    @Override
    public void setTesting(boolean testing) {
        fTesting = testing;
    }

}