X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=eclipse%2Fplugins%2Forg.argeo.slc.ui.launch%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fui%2Flaunch%2Fscript%2FSlcScriptLaunchShortcut.java;h=755ba760b54f71f4f4cf79f8b6a7ac60ad9fe519;hb=de2e12af3fe6250ab511099652dc3d90243c01a3;hp=ccc29b4f9c33a26bc1a432a7cda3125bd4aded78;hpb=f6d736c058ddb7a59164295c138c2e9c38363b36;p=gpl%2Fargeo-slc.git diff --git a/eclipse/plugins/org.argeo.slc.ui.launch/src/main/java/org/argeo/slc/ui/launch/script/SlcScriptLaunchShortcut.java b/eclipse/plugins/org.argeo.slc.ui.launch/src/main/java/org/argeo/slc/ui/launch/script/SlcScriptLaunchShortcut.java index ccc29b4f9..755ba760b 100644 --- a/eclipse/plugins/org.argeo.slc.ui.launch/src/main/java/org/argeo/slc/ui/launch/script/SlcScriptLaunchShortcut.java +++ b/eclipse/plugins/org.argeo.slc.ui.launch/src/main/java/org/argeo/slc/ui/launch/script/SlcScriptLaunchShortcut.java @@ -1,20 +1,27 @@ package org.argeo.slc.ui.launch.script; +import org.argeo.slc.ui.launch.SlcUiLaunchPlugin; import org.eclipse.core.resources.IFile; -import org.eclipse.core.resources.IProject; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; import org.eclipse.debug.core.DebugPlugin; import org.eclipse.debug.core.ILaunchConfiguration; import org.eclipse.debug.core.ILaunchConfigurationType; import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; import org.eclipse.debug.core.ILaunchManager; +import org.eclipse.debug.ui.DebugUITools; +import org.eclipse.debug.ui.IDebugUIConstants; import org.eclipse.debug.ui.ILaunchShortcut; +import org.eclipse.jface.dialogs.ErrorDialog; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.IEditorPart; public class SlcScriptLaunchShortcut implements ILaunchShortcut { + private boolean showDialog = false; public void launch(ISelection selection, String mode) { try { @@ -31,24 +38,80 @@ public class SlcScriptLaunchShortcut implements ILaunchShortcut { throw new RuntimeException("Can only launch files."); } IFile file = ((IFile) obj); - IProject project = file.getProject(); - IPath relativePath = file.getProjectRelativePath(); - String name = "[" + project.getName() + "] - " - + relativePath.toString(); - name = name.replace('/', '_');// otherwise not properly saved - - System.out.println(name); ILaunchManager manager = DebugPlugin.getDefault() .getLaunchManager(); ILaunchConfigurationType type = manager .getLaunchConfigurationType(SlcScriptLaunchDelegate.ID); - ILaunchConfigurationWorkingCopy wc = type.newInstance(null, name); - wc.setMappedResources(new IFile[] { file }); - ILaunchConfiguration config = wc.doSave(); - config.launch(mode, null); + + // Find or create config + String configLocation = SlcScriptUtils + .convertToWorkspaceLocation(file); + ILaunchConfiguration config = findLaunchConfiguration( + configLocation, manager.getLaunchConfigurations(type)); + if (config == null) { + ILaunchConfigurationWorkingCopy wc = type.newInstance(null, + generateName(file)); + wc.setAttribute(SlcScriptUtils.ATTR_SCRIPT, configLocation); + wc.setMappedResources(new IFile[] { file }); + config = wc.doSave(); + } + + // Launch + launch(config, mode); } catch (CoreException e) { - e.printStackTrace(); + Shell shell = SlcUiLaunchPlugin.getDefault().getWorkbench() + .getActiveWorkbenchWindow().getShell(); + ErrorDialog.openError(shell, "Error", + "Cannot execute SLC launch shortcut", e.getStatus()); + } + + } + + protected String generateName(IFile file) { + IPath relativePath = file.getProjectRelativePath(); + String name = relativePath.toString(); + int idx = name.lastIndexOf(".xml"); + if (idx > 0) + name = name.substring(0, idx); + + if (name.startsWith("src/main/slc/root/")) + name = name.substring("src/main/slc/root/".length()); + else if (name.startsWith("src/main/slc/")) + name = name.substring("src/main/slc/".length()); + + name = name.replace('/', '.');// otherwise not properly saved + return name; + } + + protected ILaunchConfiguration findLaunchConfiguration( + String configLocation, ILaunchConfiguration[] configs) + throws CoreException { + for (ILaunchConfiguration config : configs) { + String loc = config.getAttribute(SlcScriptUtils.ATTR_SCRIPT, ""); + if (loc.equals(configLocation)) { + return config; + } + } + return null; + } + + protected void launch(ILaunchConfiguration configuration, String mode) + throws CoreException { + if (showDialog) { + IStatus status = new Status(IStatus.INFO, SlcUiLaunchPlugin.ID, + "Configure SLC Launch"); + String groupId; + if (mode.equals(ILaunchManager.DEBUG_MODE)) { + groupId = IDebugUIConstants.ID_DEBUG_LAUNCH_GROUP; + } else { + groupId = IDebugUIConstants.ID_RUN_LAUNCH_GROUP; + } + DebugUITools.openLaunchConfigurationDialog(SlcUiLaunchPlugin + .getDefault().getWorkbench().getActiveWorkbenchWindow() + .getShell(), configuration, groupId, status); + } else { + DebugUITools.launch(configuration, mode); } } @@ -57,4 +120,8 @@ public class SlcScriptLaunchShortcut implements ILaunchShortcut { // not (yet) implemented } + public void setShowDialog(boolean showDialog) { + this.showDialog = showDialog; + } + }