Improve shortcut
authorMathieu Baudier <mbaudier@argeo.org>
Sun, 29 Jun 2008 11:01:51 +0000 (11:01 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Sun, 29 Jun 2008 11:01:51 +0000 (11:01 +0000)
git-svn-id: https://svn.argeo.org/slc/trunk@1323 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

eclipse/plugins/org.argeo.slc.ui.launch/src/main/java/org/argeo/slc/ui/launch/script/SlcScriptLaunchConfigurationTab.java
eclipse/plugins/org.argeo.slc.ui.launch/src/main/java/org/argeo/slc/ui/launch/script/SlcScriptLaunchDelegate.java
eclipse/plugins/org.argeo.slc.ui.launch/src/main/java/org/argeo/slc/ui/launch/script/SlcScriptLaunchShortcut.java

index 08e4ca52f9a5ba3bef8106aae9619bd512172d1d..d7998669032aaaa432e3a594dbc5793aa5390900 100644 (file)
@@ -1,17 +1,9 @@
 package org.argeo.slc.ui.launch.script;\r
 \r
-import java.io.IOException;\r
-import java.io.StringReader;\r
-import java.io.StringWriter;\r
-import java.util.HashMap;\r
-import java.util.Map;\r
-import java.util.Properties;\r
-\r
 import org.eclipse.core.resources.IFile;\r
 import org.eclipse.core.runtime.CoreException;\r
 import org.eclipse.debug.core.ILaunchConfiguration;\r
 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;\r
-import org.eclipse.debug.internal.ui.SWTFactory;\r
 import org.eclipse.debug.ui.AbstractLaunchConfigurationTab;\r
 import org.eclipse.swt.SWT;\r
 import org.eclipse.swt.events.ModifyEvent;\r
index be9fdb43712484c0610de024facf206f934467a5..a6569b911a582176720ed236dee97115a22f8226 100644 (file)
@@ -3,7 +3,6 @@ package org.argeo.slc.ui.launch.script;
 import java.io.IOException;\r
 import java.io.StringReader;\r
 import java.util.List;\r
-import java.util.Map;\r
 import java.util.Properties;\r
 import java.util.Vector;\r
 \r
@@ -31,7 +30,6 @@ import org.eclipse.jdt.launching.IVMRunner;
 import org.eclipse.jdt.launching.VMRunnerConfiguration;\r
 import org.eclipse.jface.dialogs.ErrorDialog;\r
 import org.eclipse.swt.widgets.Shell;\r
-import org.omg.CORBA.VM_CUSTOM;\r
 \r
 public class SlcScriptLaunchDelegate extends\r
                AbstractJavaLaunchConfigurationDelegate {\r
@@ -43,8 +41,8 @@ public class SlcScriptLaunchDelegate extends
        public final static String ATTR_TARGETS = "targets";\r
        public final static String ATTR_PRE093 = "pre093";\r
 \r
-       private final static String ANT_MAIN = "org.apache.tools.ant.Main";\r
-       private final static String SLC_MAIN = "org.argeo.slc.cli.SlcMain";\r
+       public final static String ANT_MAIN = "org.apache.tools.ant.Main";\r
+       public final static String SLC_MAIN = "org.argeo.slc.cli.SlcMain";\r
 \r
        public void launch(ILaunchConfiguration configuration, String mode,\r
                        ILaunch launch, IProgressMonitor monitor) throws CoreException {\r
@@ -62,7 +60,7 @@ public class SlcScriptLaunchDelegate extends
                boolean pre093 = configuration.getAttribute(ATTR_PRE093, false);\r
 \r
                // Retrieve SLC Runtime\r
-               SlcSystem slcSystem = findSlcSystem(file);\r
+               SlcSystem slcSystem = findSlcSystem(file, pre093);\r
                if (slcSystem == null)\r
                        return;\r
 \r
@@ -73,16 +71,17 @@ public class SlcScriptLaunchDelegate extends
                } else {\r
                        vmConfig = createConfig(slcSystem, file, mode, configuration);\r
                }\r
-               vmRunner.run(vmConfig, launch, null);\r
+               vmRunner.run(vmConfig, launch, monitor);\r
        }\r
 \r
-       protected SlcSystem findSlcSystem(IFile file) throws CoreException {\r
+       protected SlcSystem findSlcSystem(IFile file, boolean pre093)\r
+                       throws CoreException {\r
                SlcSystem slcSystem = null;\r
 \r
                IProject project = file.getProject();\r
                if (project.getNature("org.eclipse.jdt.core.javanature") != null) {\r
                        IJavaProject javaProject = JavaCore.create(project);\r
-                       if (checkProjectForEmbedded(javaProject)) {\r
+                       if (checkProjectForEmbedded(javaProject, pre093)) {\r
                                slcSystem = new EmbeddedSlcSystem(javaProject);\r
                        }\r
                }\r
@@ -102,10 +101,16 @@ public class SlcScriptLaunchDelegate extends
                return slcSystem;\r
        }\r
 \r
-       protected boolean checkProjectForEmbedded(IJavaProject project) {\r
+       protected boolean checkProjectForEmbedded(IJavaProject project,\r
+                       boolean pre093) {\r
                try {\r
-                       IType antmainType = project.findType(ANT_MAIN);\r
-                       if (antmainType == null)\r
+                       IType mainType = null;\r
+                       if (pre093)\r
+                               mainType = project.findType(ANT_MAIN);\r
+                       else\r
+                               mainType = project.findType(SLC_MAIN);\r
+\r
+                       if (mainType == null)\r
                                return false;\r
                        else\r
                                return true;\r
@@ -221,7 +226,7 @@ public class SlcScriptLaunchDelegate extends
        }\r
 \r
        // Utilities\r
-       private void showError(String message) {\r
+       private static void showError(String message) {\r
                Shell shell = SlcUiLaunchPlugin.getDefault().getWorkbench()\r
                                .getActiveWorkbenchWindow().getShell();\r
 \r
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