]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - eclipse/plugins/org.argeo.slc.ui.launch/src/main/java/org/argeo/slc/ui/launch/script/SlcScriptLaunchShortcut.java
Improve shortcut
[gpl/argeo-slc.git] / eclipse / plugins / org.argeo.slc.ui.launch / src / main / java / org / argeo / slc / ui / launch / script / SlcScriptLaunchShortcut.java
index ccc29b4f9c33a26bc1a432a7cda3125bd4aded78..a5311ed2d8fb3241206839f84def168875efd2fc 100644 (file)
@@ -1,7 +1,8 @@
 package org.argeo.slc.ui.launch.script;\r
 \r
+import org.argeo.slc.ui.launch.SlcUiLaunchPlugin;\r
 import org.eclipse.core.resources.IFile;\r
-import org.eclipse.core.resources.IProject;\r
+import org.eclipse.core.resources.IResource;\r
 import org.eclipse.core.runtime.CoreException;\r
 import org.eclipse.core.runtime.IPath;\r
 import org.eclipse.debug.core.DebugPlugin;\r
@@ -10,8 +11,10 @@ import org.eclipse.debug.core.ILaunchConfigurationType;
 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;\r
 import org.eclipse.debug.core.ILaunchManager;\r
 import org.eclipse.debug.ui.ILaunchShortcut;\r
+import org.eclipse.jface.dialogs.ErrorDialog;\r
 import org.eclipse.jface.viewers.ISelection;\r
 import org.eclipse.jface.viewers.IStructuredSelection;\r
+import org.eclipse.swt.widgets.Shell;\r
 import org.eclipse.ui.IEditorPart;\r
 \r
 public class SlcScriptLaunchShortcut implements ILaunchShortcut {\r
@@ -31,28 +34,63 @@ public class SlcScriptLaunchShortcut implements ILaunchShortcut {
                                throw new RuntimeException("Can only launch files.");\r
                        }\r
                        IFile file = ((IFile) obj);\r
-                       IProject project = file.getProject();\r
-                       IPath relativePath = file.getProjectRelativePath();\r
-                       String name = "[" + project.getName() + "] - "\r
-                                       + relativePath.toString();\r
-                       name = name.replace('/', '_');// otherwise not properly saved\r
-\r
-                       System.out.println(name);\r
 \r
                        ILaunchManager manager = DebugPlugin.getDefault()\r
                                        .getLaunchManager();\r
                        ILaunchConfigurationType type = manager\r
                                        .getLaunchConfigurationType(SlcScriptLaunchDelegate.ID);\r
-                       ILaunchConfigurationWorkingCopy wc = type.newInstance(null, name);\r
-                       wc.setMappedResources(new IFile[] { file });\r
-                       ILaunchConfiguration config = wc.doSave();\r
+\r
+                       // Find or create config\r
+                       ILaunchConfiguration config = findLaunchConfiguration(file, manager\r
+                                       .getLaunchConfigurations(type));\r
+                       if (config == null) {\r
+                               ILaunchConfigurationWorkingCopy wc = type.newInstance(null,\r
+                                               generateName(file));\r
+                               wc.setMappedResources(new IFile[] { file });\r
+                               config = wc.doSave();\r
+                       }\r
+                       \r
+                       // Launch\r
                        config.launch(mode, null);\r
                } catch (CoreException e) {\r
-                       e.printStackTrace();\r
+                       Shell shell = SlcUiLaunchPlugin.getDefault().getWorkbench()\r
+                                       .getActiveWorkbenchWindow().getShell();\r
+                       ErrorDialog.openError(shell, "Error",\r
+                                       "Cannot execute SLC launch shortcut", e.getStatus());\r
                }\r
 \r
        }\r
 \r
+       protected String generateName(IFile file) {\r
+               IPath relativePath = file.getProjectRelativePath();\r
+               String name = relativePath.toString();\r
+               int idx = name.lastIndexOf(".xml");\r
+               if (idx > 0)\r
+                       name = name.substring(0, idx);\r
+\r
+               if (name.startsWith("src/main/slc/root/"))\r
+                       name = name.substring("src/main/slc/root/".length());\r
+               else if (name.startsWith("src/main/slc/"))\r
+                       name = name.substring("src/main/slc/".length());\r
+\r
+               name = name.replace('/', '.');// otherwise not properly saved\r
+               return name;\r
+       }\r
+\r
+       protected ILaunchConfiguration findLaunchConfiguration(IFile file,\r
+                       ILaunchConfiguration[] configs) throws CoreException {\r
+               for (ILaunchConfiguration config : configs) {\r
+                       IResource[] res = config.getMappedResources();\r
+                       if (res.length > 1) {\r
+                               IFile fileT = (IFile) res[0];\r
+                               if (fileT.equals(file)) {\r
+                                       return config;\r
+                               }\r
+                       }\r
+               }\r
+               return null;\r
+       }\r
+\r
        public void launch(IEditorPart editor, String mode) {\r
                // not (yet) implemented\r
        }\r