]> 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 ergonomics
[gpl/argeo-slc.git] / eclipse / plugins / org.argeo.slc.ui.launch / src / main / java / org / argeo / slc / ui / launch / script / SlcScriptLaunchShortcut.java
index b25d303466d17ed3090a123980d0bf769860b8f4..755ba760b54f71f4f4cf79f8b6a7ac60ad9fe519 100644 (file)
@@ -1,20 +1,27 @@
 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.runtime.CoreException;\r
 import org.eclipse.core.runtime.IPath;\r
+import org.eclipse.core.runtime.IStatus;\r
+import org.eclipse.core.runtime.Status;\r
 import org.eclipse.debug.core.DebugPlugin;\r
 import org.eclipse.debug.core.ILaunchConfiguration;\r
 import org.eclipse.debug.core.ILaunchConfigurationType;\r
 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;\r
 import org.eclipse.debug.core.ILaunchManager;\r
+import org.eclipse.debug.ui.DebugUITools;\r
+import org.eclipse.debug.ui.IDebugUIConstants;\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
+       private boolean showDialog = false;\r
 \r
        public void launch(ISelection selection, String mode) {\r
                try {\r
@@ -31,23 +38,80 @@ 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() + "] - " + 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
-                       config.launch(mode, null);\r
+\r
+                       // Find or create config\r
+                       String configLocation = SlcScriptUtils\r
+                                       .convertToWorkspaceLocation(file);\r
+                       ILaunchConfiguration config = findLaunchConfiguration(\r
+                                       configLocation, manager.getLaunchConfigurations(type));\r
+                       if (config == null) {\r
+                               ILaunchConfigurationWorkingCopy wc = type.newInstance(null,\r
+                                               generateName(file));\r
+                               wc.setAttribute(SlcScriptUtils.ATTR_SCRIPT, configLocation);\r
+                               wc.setMappedResources(new IFile[] { file });\r
+                               config = wc.doSave();\r
+                       }\r
+\r
+                       // Launch\r
+                       launch(config, mode);\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(\r
+                       String configLocation, ILaunchConfiguration[] configs)\r
+                       throws CoreException {\r
+               for (ILaunchConfiguration config : configs) {\r
+                       String loc = config.getAttribute(SlcScriptUtils.ATTR_SCRIPT, "");\r
+                       if (loc.equals(configLocation)) {\r
+                               return config;\r
+                       }\r
+               }\r
+               return null;\r
+       }\r
+\r
+       protected void launch(ILaunchConfiguration configuration, String mode)\r
+                       throws CoreException {\r
+               if (showDialog) {\r
+                       IStatus status = new Status(IStatus.INFO, SlcUiLaunchPlugin.ID,\r
+                                       "Configure SLC Launch");\r
+                       String groupId;\r
+                       if (mode.equals(ILaunchManager.DEBUG_MODE)) {\r
+                               groupId = IDebugUIConstants.ID_DEBUG_LAUNCH_GROUP;\r
+                       } else {\r
+                               groupId = IDebugUIConstants.ID_RUN_LAUNCH_GROUP;\r
+                       }\r
+                       DebugUITools.openLaunchConfigurationDialog(SlcUiLaunchPlugin\r
+                                       .getDefault().getWorkbench().getActiveWorkbenchWindow()\r
+                                       .getShell(), configuration, groupId, status);\r
+               } else {\r
+                       DebugUITools.launch(configuration, mode);\r
                }\r
 \r
        }\r
@@ -56,4 +120,8 @@ public class SlcScriptLaunchShortcut implements ILaunchShortcut {
                // not (yet) implemented\r
        }\r
 \r
+       public void setShowDialog(boolean showDialog) {\r
+               this.showDialog = showDialog;\r
+       }\r
+\r
 }\r