|
| 1 | +package org.nodeclipse.enide.gradle.launch.jettyeclipse; |
| 2 | + |
| 3 | +import org.eclipse.core.resources.IFile; |
| 4 | +import org.eclipse.core.runtime.CoreException; |
| 5 | +import org.eclipse.debug.core.DebugPlugin; |
| 6 | +import org.eclipse.debug.core.ILaunchConfiguration; |
| 7 | +import org.eclipse.debug.core.ILaunchConfigurationType; |
| 8 | +import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; |
| 9 | +import org.eclipse.debug.core.ILaunchManager; |
| 10 | +import org.eclipse.debug.ui.DebugUITools; |
| 11 | +import org.eclipse.debug.ui.ILaunchShortcut; |
| 12 | +import org.eclipse.jface.dialogs.MessageDialog; |
| 13 | +import org.eclipse.jface.viewers.ISelection; |
| 14 | +import org.eclipse.jface.viewers.IStructuredSelection; |
| 15 | +import org.eclipse.ui.IEditorPart; |
| 16 | +import org.eclipse.ui.IEditorInput; |
| 17 | +import org.eclipse.ui.IFileEditorInput; |
| 18 | +import org.nodeclipse.enide.gradle.preferences.GradleConstants; |
| 19 | +import org.nodeclipse.enide.gradle.util.NodeclipseLogger; |
| 20 | +//import org.nodeclipse.ui.util.NodeclipseConsole; |
| 21 | + |
| 22 | +/** |
| 23 | + * Using "Run As" --> "gradle jettyRun" will lead here |
| 24 | + **/ |
| 25 | +public class LaunchShortcut implements ILaunchShortcut { |
| 26 | + |
| 27 | + /** |
| 28 | + * (non-Javadoc) |
| 29 | + * |
| 30 | + * @see org.eclipse.debug.ui.ILaunchShortcut#launch(org.eclipse.jface.viewers |
| 31 | + * .ISelection, java.lang.String) |
| 32 | + **/ |
| 33 | + @Override |
| 34 | + public void launch(ISelection selection, String mode) { |
| 35 | + try { |
| 36 | + Object selectObj = ((IStructuredSelection) selection).getFirstElement(); |
| 37 | + if (selectObj instanceof IFile) { |
| 38 | + launchFile((IFile) selectObj, mode); |
| 39 | + } else { |
| 40 | + MessageDialog.openWarning(null, "Warning", "Not implemeneted yet!"); |
| 41 | + } |
| 42 | + } catch (CoreException e) { |
| 43 | + //NodeclipseConsole.write(e.getLocalizedMessage()+"\n"); |
| 44 | + NodeclipseLogger.log(e.getLocalizedMessage()+"\n"); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * (non-Javadoc) |
| 50 | + * |
| 51 | + * @see org.eclipse.debug.ui.ILaunchShortcut#launch(org.eclipse.ui.IEditorPart, |
| 52 | + * java.lang.String) |
| 53 | + **/ |
| 54 | + @Override |
| 55 | + public void launch(IEditorPart editor, String mode) { |
| 56 | + try { |
| 57 | + IEditorInput editorInput = editor.getEditorInput(); |
| 58 | + if (editorInput instanceof IFileEditorInput) { |
| 59 | + IFile selectObj = ((IFileEditorInput) editorInput).getFile(); |
| 60 | + launchFile((IFile) selectObj, mode); |
| 61 | + } else { |
| 62 | + MessageDialog.openWarning(null, "Warning", "Not implemeneted yet!"); |
| 63 | + } |
| 64 | + } catch (CoreException e) { |
| 65 | + //NodeclipseConsole.write(e.getLocalizedMessage()+"\n"); |
| 66 | + NodeclipseLogger.log(e.getLocalizedMessage()+"\n"); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Launch an file,using the file information, which means using default |
| 72 | + * launch configurations. |
| 73 | + * |
| 74 | + * @param file |
| 75 | + * @param mode |
| 76 | + */ |
| 77 | + private void launchFile(IFile file, String mode) throws CoreException { |
| 78 | + // check for an existing launch config for the file |
| 79 | + String path = file.getFullPath().toString(); |
| 80 | + ILaunchManager launchManager = DebugPlugin.getDefault().getLaunchManager(); |
| 81 | + ILaunchConfigurationType type = launchManager.getLaunchConfigurationType(GradleConstants.LAUNCH_JETTYECLIPSE_CONFIGURATION_TYPE_ID); |
| 82 | + ILaunchConfiguration configuration = createLaunchConfiguration(type, path, file); |
| 83 | + DebugUITools.launch(configuration, mode); |
| 84 | + // then execution goes in LaunchConfigurationDelegate.java launch() method |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Create a new configuration and set useful data. |
| 89 | + * |
| 90 | + * @param type |
| 91 | + * @param path |
| 92 | + * @param file |
| 93 | + * @return |
| 94 | + * @throws CoreException |
| 95 | + */ |
| 96 | + private ILaunchConfiguration createLaunchConfiguration(ILaunchConfigurationType type, String path, IFile file) throws CoreException { |
| 97 | + String configname = file.getFullPath().toString().replace('/', '-'); |
| 98 | + if(configname.startsWith("-")) { |
| 99 | + configname = configname.substring(1); |
| 100 | + } |
| 101 | + |
| 102 | + ILaunchConfiguration[] configs = DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurations(type); |
| 103 | + for(ILaunchConfiguration config : configs) { |
| 104 | + if(configname.equals(config.getName())) { |
| 105 | + return config; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + // create a new configuration for the file |
| 110 | + ILaunchConfigurationWorkingCopy workingCopy = type.newInstance(null, configname); |
| 111 | + workingCopy.setAttribute(GradleConstants.KEY_FILE_PATH, path); |
| 112 | + setMoreAttributes(workingCopy); |
| 113 | + return workingCopy.doSave(); |
| 114 | + } |
| 115 | + |
| 116 | + protected void setMoreAttributes(ILaunchConfigurationWorkingCopy workingCopy) { |
| 117 | + //NodeclipseConsole.write(this.getClass().getName()+"\n"); |
| 118 | + NodeclipseLogger.log(this.getClass().getName()+"\n"); |
| 119 | + } |
| 120 | +} |
0 commit comments