Package com.googlecode.grinderstone.debug.ui.launching

Source Code of com.googlecode.grinderstone.debug.ui.launching.GrinderLaunchConfigurationDelegate

package com.googlecode.grinderstone.debug.ui.launching;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.swt.widgets.Display;
import org.python.pydev.core.MisconfigurationException;
import org.python.pydev.core.log.Log;
import org.python.pydev.debug.ui.launching.AbstractLaunchConfigurationDelegate;
import org.python.pydev.debug.ui.launching.InvalidRunException;
import org.python.pydev.debug.ui.launching.PythonRunner;
import org.python.pydev.debug.ui.launching.PythonRunnerConfig;
import org.python.pydev.editor.actions.PyAction;

import com.googlecode.grinderstone.GrinderStonePlugin;

/**
* Class preparates configuration for launching.
*
* @author Borislav Andruschuk
* @since GrinderStone 1.0
*/
public class GrinderLaunchConfigurationDelegate extends AbstractLaunchConfigurationDelegate {

    // @Override
    protected String getRunnerConfigRun() {
        return GrinderRunnerConfig.RUN_GRINDER;
    }

    // @Override
    protected String getRunnerConfigRun(ILaunchConfiguration conf, String mode, ILaunch launch) {
        return getRunnerConfigRun();
    }

    @Override
    public void launch(ILaunchConfiguration conf, String mode, ILaunch launch,
                    IProgressMonitor monitor) throws CoreException {
        if (monitor == null) {
            monitor = new NullProgressMonitor();
        }

        monitor.beginTask("Preparing configuration", 3);
        try {

            cleanCompiledFiles();

            PythonRunnerConfig runConfig = new GrinderRunnerConfig(conf, mode);

            monitor.worked(1);
            try {
                PythonRunner.run(runConfig, launch, monitor);
            } catch (IOException e) {
                Log.log(e);
                throw new CoreException(GrinderStonePlugin.makeStatus(IStatus.ERROR,
                                "Unexpected IO Exception in GrinderStone debugger", null));
            }
        } catch (final InvalidRunException e) {
            handleError(launch, e);
        } catch (MisconfigurationException e) {
            handleError(launch, e);
        }
    }

    private void cleanCompiledFiles() throws CoreException {

        File pySrcPath = GrinderStonePlugin.getPySrcPath();
        File[] compiled = pySrcPath.listFiles(new FilenameFilter() {

            public boolean accept(File dir, String name) {
                return name.endsWith("$py.class");
            }
        });

        if (compiled != null) {
            for (int i = 0; i < compiled.length; i++) {
                File file = compiled[i];
                if (!file.delete()) {
                    throw new CoreException(GrinderStonePlugin.makeStatus(IStatus.ERROR,
                                    "Cannot remove file: " + file.getAbsolutePath(), null));
                }
            }
        }
    }

    private void handleError(ILaunch launch, final Exception e) {
        Display.getDefault().asyncExec(new Runnable() {

            public void run() {
                ErrorDialog.openError(PyAction.getShell(), "Invalid launch configuration",
                                "Unable to make launch because launch configuration is not valid",
                                GrinderStonePlugin.makeStatus(IStatus.ERROR, e.getMessage(), e));
            }
        });
        finishLaunchWithError(launch);
    }

    private void finishLaunchWithError(ILaunch launch) {
        try {
            launch.terminate();

            ILaunchManager launchManager = DebugPlugin.getDefault().getLaunchManager();
            launchManager.removeLaunch(launch);
        } catch (Throwable x) {
            GrinderStonePlugin.log(x);
        }
    }

}
TOP

Related Classes of com.googlecode.grinderstone.debug.ui.launching.GrinderLaunchConfigurationDelegate

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.