Skip to content

Commit

Permalink
Asks confirmation before starting a second Application for same project.
Browse files Browse the repository at this point in the history
Fix Issue 496:	warn if project is already running
  • Loading branch information
laurentpetit committed Dec 27, 2012
1 parent 9a445cb commit c5fa18e
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 25 deletions.
78 changes: 53 additions & 25 deletions ccw.core/src/java/ccw/launching/ClojureLaunchShortcut.java
Expand Up @@ -10,6 +10,8 @@
*******************************************************************************/
package ccw.launching;

import static ccw.launching.LaunchUtils.findRunningLaunchesFor;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand All @@ -33,16 +35,22 @@
import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin;
import org.eclipse.jdt.internal.debug.ui.launcher.LauncherMessages;
import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.dialogs.ElementListSelectionDialog;
import org.eclipse.ui.part.FileEditorInput;

import ccw.CCWPlugin;
import ccw.ClojureCore;
import ccw.repl.Actions;
import ccw.util.DisplayUtil;

public class ClojureLaunchShortcut implements ILaunchShortcut, IJavaLaunchConfigurationConstants {
private static final HashMap<String, Long> tempLaunchCounters = new HashMap();
Expand All @@ -55,34 +63,19 @@ private static int incTempLaunchCount (String projectName) {
return cnt.intValue();
}
}


public void launch(IEditorPart editor, String mode) {
launchEditorPart(editor, mode);
}

private ILaunch launchEditorPart(IEditorPart editor, String mode) {
IEditorInput input = editor.getEditorInput();
if (input instanceof FileEditorInput) {
FileEditorInput fei = (FileEditorInput) input;
return launchProject(fei.getFile().getProject(), new IFile[] { fei
launchProject(fei.getFile().getProject(), new IFile[] { fei
.getFile() }, mode);
} else {
return null;
return;
}
}

public void launch(ISelection selection, String mode) {
launchSelection(selection, mode);
}

/**
*
* @param selection
* @param mode
* @return
*/
public ILaunch launchSelection(ISelection selection, String mode) {
if (selection instanceof IStructuredSelection) {
IStructuredSelection strSel = (IStructuredSelection) selection;
List<IFile> files = new ArrayList<IFile>();
Expand All @@ -99,16 +92,51 @@ public ILaunch launchSelection(ISelection selection, String mode) {
}
}
if (proj != null) {
return launchProject(proj, files.toArray(new IFile[] {}), mode);
launchProject(proj, files.toArray(new IFile[] {}), mode);
}
}
return null;
}
public ILaunch launchProject(IProject project, String mode) {
return launchProject(project, new IFile[] {}, mode);

public void launchProject(IProject project, String mode) {
launchProject(project, new IFile[] {}, mode);
}

protected void launchProject(IProject project, IFile[] filesToLaunch, String mode) {

String projectName = project.getName();
List<ILaunch> running = findRunningLaunchesFor(projectName);

if (running.size() == 0) {
launchProject2(project, filesToLaunch, mode);
} else {
if (userConfirmsNewLaunch(project, running.size())) {
launchProject2(project, filesToLaunch, mode);
} else {
IViewPart replView = CCWPlugin.getDefault().getProjectREPL(project);
replView.getViewSite().getPage().activate(replView);
}
}
}

private boolean userConfirmsNewLaunch(final IProject project, final int nb) {
final boolean[] ret = new boolean[1];
final String title = "Clojure Application Launcher";
final String msg = (nb==1?"A":nb) + " REPL" + (nb==1?" is":"s are")
+ " already running for this project. Changes you made can "
+ "be evaluated in an existing REPL (see Clojure menu). "
+ "\n\nAre you sure you want to start up another REPL for this project?\n"
+ "(Cancel will open existing REPL)";
DisplayUtil.syncExec(new Runnable() {
public void run() {
Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
ret[0] = MessageDialog.openConfirm(shell, title, msg);
}
});
return ret[0];
}

protected ILaunch launchProject(IProject project, IFile[] filesToLaunch, String mode) {
protected void launchProject2(IProject project, IFile[] filesToLaunch, String mode) {

Boolean activateAutoReload = CCWPlugin.isAutoReloadEnabled();
try {
ILaunchConfiguration config = findLaunchConfiguration(project);
Expand All @@ -125,19 +153,19 @@ protected ILaunch launchProject(IProject project, IFile[] filesToLaunch, String
runnableConfiguration.setAttribute(LaunchUtils.ATTR_NS_TO_START_IN, ClojureCore.findMaybeLibNamespace(filesToLaunch[0]));
}
ILaunch launch = runnableConfiguration.launch(mode, null);
return launch;
return;
} finally {
runnableConfiguration.delete();
}
} else {
return null;
return;
}
} catch (CoreException e) {
throw new RuntimeException(e);
}
}

private ILaunchConfiguration findLaunchConfiguration(IProject project) {
private ILaunchConfiguration findLaunchConfiguration(IProject project) {
ILaunchManager lm = DebugPlugin.getDefault().getLaunchManager();
ILaunchConfigurationType type =
lm.getLaunchConfigurationType(LaunchUtils.LAUNCH_CONFIG_ID);
Expand Down
14 changes: 14 additions & 0 deletions ccw.core/src/java/ccw/launching/LaunchUtils.java
Expand Up @@ -20,9 +20,11 @@
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.JavaModelException;
Expand Down Expand Up @@ -181,4 +183,16 @@ static public void setFilesToLaunchString(ILaunchConfigurationWorkingCopy config
}
config.setAttribute(LaunchUtils.ATTR_FILES_LAUNCHED_AT_STARTUP, filesAsString.toString());
}

public static List<ILaunch> findRunningLaunchesFor(String projectName) {
List<ILaunch> ret = new ArrayList<ILaunch>();
ILaunchManager launchManager = DebugPlugin.getDefault().getLaunchManager();
for (ILaunch launch: launchManager.getLaunches()) {
if (!launch.isTerminated()
&& projectName.equals(getProjectName(launch))) {
ret.add(launch);
}
}
return ret;
}
}

0 comments on commit c5fa18e

Please sign in to comment.