]> git.argeo.org Git - gpl/argeo-slc.git/blob - eclipse/plugins/org.argeo.slc.ide.ui/src/main/java/org/argeo/slc/ide/ui/launch/script/SlcScriptLaunchShortcut.java
Clean up code
[gpl/argeo-slc.git] / eclipse / plugins / org.argeo.slc.ide.ui / src / main / java / org / argeo / slc / ide / ui / launch / script / SlcScriptLaunchShortcut.java
1 package org.argeo.slc.ide.ui.launch.script;
2
3 import org.argeo.slc.ide.ui.SlcIdeUiPlugin;
4 import org.eclipse.core.resources.IFile;
5 import org.eclipse.core.runtime.CoreException;
6 import org.eclipse.core.runtime.IPath;
7 import org.eclipse.core.runtime.IStatus;
8 import org.eclipse.core.runtime.Status;
9 import org.eclipse.debug.core.DebugPlugin;
10 import org.eclipse.debug.core.ILaunchConfiguration;
11 import org.eclipse.debug.core.ILaunchConfigurationType;
12 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
13 import org.eclipse.debug.core.ILaunchManager;
14 import org.eclipse.debug.ui.DebugUITools;
15 import org.eclipse.debug.ui.IDebugUIConstants;
16 import org.eclipse.debug.ui.ILaunchShortcut;
17 import org.eclipse.jface.dialogs.ErrorDialog;
18 import org.eclipse.jface.viewers.ISelection;
19 import org.eclipse.jface.viewers.IStructuredSelection;
20 import org.eclipse.swt.widgets.Shell;
21 import org.eclipse.ui.IEditorPart;
22
23 public class SlcScriptLaunchShortcut implements ILaunchShortcut {
24 private boolean showDialog = false;
25
26 public void launch(ISelection selection, String mode) {
27 try {
28 if (!(selection instanceof IStructuredSelection)) {
29 throw new RuntimeException("Unknown selection "
30 + selection.getClass());
31 }
32 IStructuredSelection sSelection = (IStructuredSelection) selection;
33 if (sSelection.size() != 1) {
34 throw new RuntimeException("Can only launch one SLC script.");
35 }
36 Object obj = sSelection.iterator().next();
37 if (!(obj instanceof IFile)) {
38 throw new RuntimeException("Can only launch files.");
39 }
40 IFile file = ((IFile) obj);
41
42 ILaunchManager manager = DebugPlugin.getDefault()
43 .getLaunchManager();
44 ILaunchConfigurationType type = manager
45 .getLaunchConfigurationType(SlcScriptLaunchDelegate.ID);
46
47 // Find or create config
48 String configLocation = SlcScriptUtils
49 .convertToWorkspaceLocation(file);
50 ILaunchConfiguration config = findLaunchConfiguration(
51 configLocation, manager.getLaunchConfigurations(type));
52 if (config == null) {
53 ILaunchConfigurationWorkingCopy wc = type.newInstance(null,
54 generateName(file));
55 wc.setAttribute(SlcScriptUtils.ATTR_SCRIPT, configLocation);
56 wc.setMappedResources(new IFile[] { file });
57 config = wc.doSave();
58 }
59
60 // Launch
61 launch(config, mode);
62 } catch (CoreException e) {
63 Shell shell = SlcIdeUiPlugin.getDefault().getWorkbench()
64 .getActiveWorkbenchWindow().getShell();
65 ErrorDialog.openError(shell, "Error",
66 "Cannot execute SLC launch shortcut", e.getStatus());
67 }
68
69 }
70
71 protected String generateName(IFile file) {
72 IPath relativePath = file.getProjectRelativePath();
73 String name = relativePath.toString();
74 int idx = name.lastIndexOf(".xml");
75 if (idx > 0)
76 name = name.substring(0, idx);
77
78 if (name.startsWith("src/main/slc/root/"))
79 name = name.substring("src/main/slc/root/".length());
80 else if (name.startsWith("src/main/slc/"))
81 name = name.substring("src/main/slc/".length());
82
83 name = name.replace('/', '.');// otherwise not properly saved
84 return name;
85 }
86
87 protected ILaunchConfiguration findLaunchConfiguration(
88 String configLocation, ILaunchConfiguration[] configs)
89 throws CoreException {
90 for (ILaunchConfiguration config : configs) {
91 String loc = config.getAttribute(SlcScriptUtils.ATTR_SCRIPT, "");
92 if (loc.equals(configLocation)) {
93 return config;
94 }
95 }
96 return null;
97 }
98
99 protected void launch(ILaunchConfiguration configuration, String mode)
100 throws CoreException {
101 if (showDialog) {
102 IStatus status = new Status(IStatus.INFO, SlcIdeUiPlugin.ID,
103 "Configure SLC Launch");
104 String groupId;
105 if (mode.equals(ILaunchManager.DEBUG_MODE)) {
106 groupId = IDebugUIConstants.ID_DEBUG_LAUNCH_GROUP;
107 } else {
108 groupId = IDebugUIConstants.ID_RUN_LAUNCH_GROUP;
109 }
110 DebugUITools.openLaunchConfigurationDialog(SlcIdeUiPlugin
111 .getDefault().getWorkbench().getActiveWorkbenchWindow()
112 .getShell(), configuration, groupId, status);
113 } else {
114 DebugUITools.launch(configuration, mode);
115 }
116
117 }
118
119 public void launch(IEditorPart editor, String mode) {
120 // not (yet) implemented
121 }
122
123 public void setShowDialog(boolean showDialog) {
124 this.showDialog = showDialog;
125 }
126
127 }