From 79b406fbdc5a22a70a2c061860e8255e48b36981 Mon Sep 17 00:00:00 2001 From: Mathieu Baudier Date: Sun, 29 Jun 2008 11:01:51 +0000 Subject: [PATCH] Improve shortcut git-svn-id: https://svn.argeo.org/slc/trunk@1323 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc --- .../SlcScriptLaunchConfigurationTab.java | 8 --- .../script/SlcScriptLaunchDelegate.java | 29 +++++---- .../script/SlcScriptLaunchShortcut.java | 62 +++++++++++++++---- 3 files changed, 67 insertions(+), 32 deletions(-) diff --git a/eclipse/plugins/org.argeo.slc.ui.launch/src/main/java/org/argeo/slc/ui/launch/script/SlcScriptLaunchConfigurationTab.java b/eclipse/plugins/org.argeo.slc.ui.launch/src/main/java/org/argeo/slc/ui/launch/script/SlcScriptLaunchConfigurationTab.java index 08e4ca52f..d79986690 100644 --- a/eclipse/plugins/org.argeo.slc.ui.launch/src/main/java/org/argeo/slc/ui/launch/script/SlcScriptLaunchConfigurationTab.java +++ b/eclipse/plugins/org.argeo.slc.ui.launch/src/main/java/org/argeo/slc/ui/launch/script/SlcScriptLaunchConfigurationTab.java @@ -1,17 +1,9 @@ package org.argeo.slc.ui.launch.script; -import java.io.IOException; -import java.io.StringReader; -import java.io.StringWriter; -import java.util.HashMap; -import java.util.Map; -import java.util.Properties; - import org.eclipse.core.resources.IFile; import org.eclipse.core.runtime.CoreException; import org.eclipse.debug.core.ILaunchConfiguration; import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; -import org.eclipse.debug.internal.ui.SWTFactory; import org.eclipse.debug.ui.AbstractLaunchConfigurationTab; import org.eclipse.swt.SWT; import org.eclipse.swt.events.ModifyEvent; diff --git a/eclipse/plugins/org.argeo.slc.ui.launch/src/main/java/org/argeo/slc/ui/launch/script/SlcScriptLaunchDelegate.java b/eclipse/plugins/org.argeo.slc.ui.launch/src/main/java/org/argeo/slc/ui/launch/script/SlcScriptLaunchDelegate.java index be9fdb437..a6569b911 100644 --- a/eclipse/plugins/org.argeo.slc.ui.launch/src/main/java/org/argeo/slc/ui/launch/script/SlcScriptLaunchDelegate.java +++ b/eclipse/plugins/org.argeo.slc.ui.launch/src/main/java/org/argeo/slc/ui/launch/script/SlcScriptLaunchDelegate.java @@ -3,7 +3,6 @@ package org.argeo.slc.ui.launch.script; import java.io.IOException; import java.io.StringReader; import java.util.List; -import java.util.Map; import java.util.Properties; import java.util.Vector; @@ -31,7 +30,6 @@ import org.eclipse.jdt.launching.IVMRunner; import org.eclipse.jdt.launching.VMRunnerConfiguration; import org.eclipse.jface.dialogs.ErrorDialog; import org.eclipse.swt.widgets.Shell; -import org.omg.CORBA.VM_CUSTOM; public class SlcScriptLaunchDelegate extends AbstractJavaLaunchConfigurationDelegate { @@ -43,8 +41,8 @@ public class SlcScriptLaunchDelegate extends public final static String ATTR_TARGETS = "targets"; public final static String ATTR_PRE093 = "pre093"; - private final static String ANT_MAIN = "org.apache.tools.ant.Main"; - private final static String SLC_MAIN = "org.argeo.slc.cli.SlcMain"; + public final static String ANT_MAIN = "org.apache.tools.ant.Main"; + public final static String SLC_MAIN = "org.argeo.slc.cli.SlcMain"; public void launch(ILaunchConfiguration configuration, String mode, ILaunch launch, IProgressMonitor monitor) throws CoreException { @@ -62,7 +60,7 @@ public class SlcScriptLaunchDelegate extends boolean pre093 = configuration.getAttribute(ATTR_PRE093, false); // Retrieve SLC Runtime - SlcSystem slcSystem = findSlcSystem(file); + SlcSystem slcSystem = findSlcSystem(file, pre093); if (slcSystem == null) return; @@ -73,16 +71,17 @@ public class SlcScriptLaunchDelegate extends } else { vmConfig = createConfig(slcSystem, file, mode, configuration); } - vmRunner.run(vmConfig, launch, null); + vmRunner.run(vmConfig, launch, monitor); } - protected SlcSystem findSlcSystem(IFile file) throws CoreException { + protected SlcSystem findSlcSystem(IFile file, boolean pre093) + throws CoreException { SlcSystem slcSystem = null; IProject project = file.getProject(); if (project.getNature("org.eclipse.jdt.core.javanature") != null) { IJavaProject javaProject = JavaCore.create(project); - if (checkProjectForEmbedded(javaProject)) { + if (checkProjectForEmbedded(javaProject, pre093)) { slcSystem = new EmbeddedSlcSystem(javaProject); } } @@ -102,10 +101,16 @@ public class SlcScriptLaunchDelegate extends return slcSystem; } - protected boolean checkProjectForEmbedded(IJavaProject project) { + protected boolean checkProjectForEmbedded(IJavaProject project, + boolean pre093) { try { - IType antmainType = project.findType(ANT_MAIN); - if (antmainType == null) + IType mainType = null; + if (pre093) + mainType = project.findType(ANT_MAIN); + else + mainType = project.findType(SLC_MAIN); + + if (mainType == null) return false; else return true; @@ -221,7 +226,7 @@ public class SlcScriptLaunchDelegate extends } // Utilities - private void showError(String message) { + private static void showError(String message) { Shell shell = SlcUiLaunchPlugin.getDefault().getWorkbench() .getActiveWorkbenchWindow().getShell(); diff --git a/eclipse/plugins/org.argeo.slc.ui.launch/src/main/java/org/argeo/slc/ui/launch/script/SlcScriptLaunchShortcut.java b/eclipse/plugins/org.argeo.slc.ui.launch/src/main/java/org/argeo/slc/ui/launch/script/SlcScriptLaunchShortcut.java index ccc29b4f9..a5311ed2d 100644 --- a/eclipse/plugins/org.argeo.slc.ui.launch/src/main/java/org/argeo/slc/ui/launch/script/SlcScriptLaunchShortcut.java +++ b/eclipse/plugins/org.argeo.slc.ui.launch/src/main/java/org/argeo/slc/ui/launch/script/SlcScriptLaunchShortcut.java @@ -1,7 +1,8 @@ package org.argeo.slc.ui.launch.script; +import org.argeo.slc.ui.launch.SlcUiLaunchPlugin; import org.eclipse.core.resources.IFile; -import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IResource; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; import org.eclipse.debug.core.DebugPlugin; @@ -10,8 +11,10 @@ import org.eclipse.debug.core.ILaunchConfigurationType; import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; import org.eclipse.debug.core.ILaunchManager; import org.eclipse.debug.ui.ILaunchShortcut; +import org.eclipse.jface.dialogs.ErrorDialog; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.IEditorPart; public class SlcScriptLaunchShortcut implements ILaunchShortcut { @@ -31,28 +34,63 @@ public class SlcScriptLaunchShortcut implements ILaunchShortcut { throw new RuntimeException("Can only launch files."); } IFile file = ((IFile) obj); - IProject project = file.getProject(); - IPath relativePath = file.getProjectRelativePath(); - String name = "[" + project.getName() + "] - " - + relativePath.toString(); - name = name.replace('/', '_');// otherwise not properly saved - - System.out.println(name); ILaunchManager manager = DebugPlugin.getDefault() .getLaunchManager(); ILaunchConfigurationType type = manager .getLaunchConfigurationType(SlcScriptLaunchDelegate.ID); - ILaunchConfigurationWorkingCopy wc = type.newInstance(null, name); - wc.setMappedResources(new IFile[] { file }); - ILaunchConfiguration config = wc.doSave(); + + // Find or create config + ILaunchConfiguration config = findLaunchConfiguration(file, manager + .getLaunchConfigurations(type)); + if (config == null) { + ILaunchConfigurationWorkingCopy wc = type.newInstance(null, + generateName(file)); + wc.setMappedResources(new IFile[] { file }); + config = wc.doSave(); + } + + // Launch config.launch(mode, null); } catch (CoreException e) { - e.printStackTrace(); + Shell shell = SlcUiLaunchPlugin.getDefault().getWorkbench() + .getActiveWorkbenchWindow().getShell(); + ErrorDialog.openError(shell, "Error", + "Cannot execute SLC launch shortcut", e.getStatus()); } } + protected String generateName(IFile file) { + IPath relativePath = file.getProjectRelativePath(); + String name = relativePath.toString(); + int idx = name.lastIndexOf(".xml"); + if (idx > 0) + name = name.substring(0, idx); + + if (name.startsWith("src/main/slc/root/")) + name = name.substring("src/main/slc/root/".length()); + else if (name.startsWith("src/main/slc/")) + name = name.substring("src/main/slc/".length()); + + name = name.replace('/', '.');// otherwise not properly saved + return name; + } + + protected ILaunchConfiguration findLaunchConfiguration(IFile file, + ILaunchConfiguration[] configs) throws CoreException { + for (ILaunchConfiguration config : configs) { + IResource[] res = config.getMappedResources(); + if (res.length > 1) { + IFile fileT = (IFile) res[0]; + if (fileT.equals(file)) { + return config; + } + } + } + return null; + } + public void launch(IEditorPart editor, String mode) { // not (yet) implemented } -- 2.39.2