+++ /dev/null
-package org.argeo.slc.ide.ui.launch.script;\r
-\r
-import org.argeo.slc.ide.ui.SlcIdeUiPlugin;\r
-import org.eclipse.core.resources.IFile;\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
- if (!(selection instanceof IStructuredSelection)) {\r
- throw new RuntimeException("Unknown selection "\r
- + selection.getClass());\r
- }\r
- IStructuredSelection sSelection = (IStructuredSelection) selection;\r
- if (sSelection.size() != 1) {\r
- throw new RuntimeException("Can only launch one SLC script.");\r
- }\r
- Object obj = sSelection.iterator().next();\r
- if (!(obj instanceof IFile)) {\r
- throw new RuntimeException("Can only launch files.");\r
- }\r
- IFile file = ((IFile) obj);\r
-\r
- ILaunchManager manager = DebugPlugin.getDefault()\r
- .getLaunchManager();\r
- ILaunchConfigurationType type = manager\r
- .getLaunchConfigurationType(SlcScriptLaunchDelegate.ID);\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
- Shell shell = SlcIdeUiPlugin.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, SlcIdeUiPlugin.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(SlcIdeUiPlugin\r
- .getDefault().getWorkbench().getActiveWorkbenchWindow()\r
- .getShell(), configuration, groupId, status);\r
- } else {\r
- DebugUITools.launch(configuration, mode);\r
- }\r
-\r
- }\r
-\r
- public void launch(IEditorPart editor, String mode) {\r
- // not (yet) implemented\r
- }\r
-\r
- public void setShowDialog(boolean showDialog) {\r
- this.showDialog = showDialog;\r
- }\r
-\r
-}\r