|
1 | 1 | package org.nodeclipse.enide.maven.launch.tomcat; |
2 | 2 |
|
3 | | -import org.eclipse.core.resources.IFile; |
4 | | -import org.eclipse.core.runtime.CoreException; |
5 | | -import org.eclipse.core.runtime.IAdaptable; |
6 | | -import org.eclipse.debug.core.DebugPlugin; |
7 | | -import org.eclipse.debug.core.ILaunchConfiguration; |
8 | | -import org.eclipse.debug.core.ILaunchConfigurationType; |
9 | | -import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; |
10 | | -import org.eclipse.debug.core.ILaunchManager; |
11 | | -import org.eclipse.debug.ui.DebugUITools; |
12 | 3 | import org.eclipse.debug.ui.ILaunchShortcut; |
13 | | -import org.eclipse.jface.dialogs.MessageDialog; |
14 | | -import org.eclipse.jface.viewers.ISelection; |
15 | | -import org.eclipse.jface.viewers.IStructuredSelection; |
16 | | -import org.eclipse.jface.viewers.TreeSelection; |
17 | | -import org.eclipse.ui.IEditorPart; |
18 | | -import org.eclipse.ui.IEditorInput; |
19 | | -import org.eclipse.ui.IFileEditorInput; |
20 | 4 | import org.nodeclipse.enide.maven.preferences.MavenConstants; |
21 | | -import org.nodeclipse.enide.maven.util.NodeclipseLogger; |
22 | | -//import org.nodeclipse.ui.util.NodeclipseConsole; |
23 | 5 |
|
24 | 6 | /** |
25 | 7 | * Using "Run As" --> "mvn tomcat6:start Maven start" will lead here</br> |
26 | 8 | * http://tomcat.apache.org/maven-plugin-2.2/context-goals.html |
27 | 9 | **/ |
28 | | -public class LaunchShortcut implements ILaunchShortcut { |
29 | | - |
30 | | - /** |
31 | | - * (non-Javadoc) |
32 | | - * |
33 | | - * @see org.eclipse.debug.ui.ILaunchShortcut#launch(org.eclipse.jface.viewers |
34 | | - * .ISelection, java.lang.String) |
35 | | - **/ |
36 | | - @Override |
37 | | - public void launch(ISelection selection, String mode) { |
38 | | - try { |
39 | | - Object selectObj = ((IStructuredSelection) selection).getFirstElement(); |
40 | | - if (selectObj instanceof IFile) { |
41 | | - launchFile((IFile) selectObj, mode); |
42 | | - }else if (selection instanceof TreeSelection) { // selectObj instanceof org.eclipse.jface.viewers.TreeSelection |
43 | | - // see http://stackoverflow.com/questions/775709/eclipse-pde-navigator-view-treeselection-obtaining-the-file-type-and-name |
44 | | - TreeSelection treeSelection = (TreeSelection) selection; |
45 | | - IAdaptable firstElement = (IAdaptable) treeSelection.getFirstElement(); |
46 | | - |
47 | | - IFile file = (IFile) firstElement.getAdapter(IFile.class); |
48 | | - if (file != null && file.isAccessible()) { |
49 | | - launchFile(file, mode); |
50 | | - }else{ |
51 | | - NodeclipseLogger.log("Impossible to get File for selection: "+selection+"\n"); |
52 | | - } |
53 | | - } else { |
54 | | - showDialogNotImplemented(selection.getClass().getName()); |
55 | | - } |
56 | | - } catch (CoreException e) { |
57 | | - //NodeclipseConsole.write(e.getLocalizedMessage()+"\n"); |
58 | | - NodeclipseLogger.log(e.getLocalizedMessage()+"\n"); |
59 | | - } |
60 | | - } |
61 | | - |
62 | | - private void showDialogNotImplemented(String what) { |
63 | | - //TODO CommonDialog "Not Implemented" |
64 | | - MessageDialog.openWarning(null, "Warning", |
65 | | - "Launching of type "+what+" is not implemeneted yet!\n"+ |
66 | | - "Search/raise an issue if you care at https://github.com/nodeclipse/nodeclipse-1/issues/"); |
| 10 | +public class LaunchShortcut extends org.nodeclipse.enide.maven.launchexec.LaunchShortcut implements ILaunchShortcut { |
| 11 | + |
| 12 | + @Override |
| 13 | + protected String getLaunchConfigurationTypeConstant() { |
| 14 | + return MavenConstants.LAUNCH_TOMCAT_CONFIGURATION_TYPE_ID; |
67 | 15 | } |
68 | 16 |
|
69 | | - /** |
70 | | - * (non-Javadoc) |
71 | | - * |
72 | | - * @see org.eclipse.debug.ui.ILaunchShortcut#launch(org.eclipse.ui.IEditorPart, |
73 | | - * java.lang.String) |
74 | | - **/ |
75 | | - @Override |
76 | | - public void launch(IEditorPart editor, String mode) { |
77 | | - try { |
78 | | - IEditorInput editorInput = editor.getEditorInput(); |
79 | | - if (editorInput instanceof IFileEditorInput) { |
80 | | - IFile selectObj = ((IFileEditorInput) editorInput).getFile(); |
81 | | - launchFile((IFile) selectObj, mode); |
82 | | - } else { |
83 | | - showDialogNotImplemented(editor.getClass().getName()+" from Editor"); |
84 | | - } |
85 | | - } catch (CoreException e) { |
86 | | - //NodeclipseConsole.write(e.getLocalizedMessage()+"\n"); |
87 | | - NodeclipseLogger.log(e.getLocalizedMessage()+"\n"); |
88 | | - } |
89 | | - } |
90 | | - |
91 | | - /** |
92 | | - * Launch an file,using the file information, which means using default |
93 | | - * launch configurations. |
94 | | - * |
95 | | - * @param file |
96 | | - * @param mode |
97 | | - */ |
98 | | - private void launchFile(IFile file, String mode) throws CoreException { |
99 | | - // check for an existing launch config for the file |
100 | | - String path = file.getFullPath().toString(); |
101 | | - ILaunchManager launchManager = DebugPlugin.getDefault().getLaunchManager(); |
102 | | - ILaunchConfigurationType type = launchManager.getLaunchConfigurationType(MavenConstants.LAUNCH_TOMCAT_CONFIGURATION_TYPE_ID); |
103 | | - ILaunchConfiguration configuration = createLaunchConfiguration(type, path, file); |
104 | | - DebugUITools.launch(configuration, mode); |
105 | | - // then execution goes in LaunchConfigurationDelegate.java launch() method |
106 | | - } |
107 | | - |
108 | | - /** |
109 | | - * Create a new configuration and set useful data. |
110 | | - * |
111 | | - * @param type |
112 | | - * @param path |
113 | | - * @param file |
114 | | - * @return |
115 | | - * @throws CoreException |
116 | | - */ |
117 | | - private ILaunchConfiguration createLaunchConfiguration(ILaunchConfigurationType type, String path, IFile file) throws CoreException { |
118 | | - String configname = file.getFullPath().toString().replace('/', '-'); |
119 | | - if(configname.startsWith("-")) { |
120 | | - configname = configname.substring(1); |
121 | | - } |
122 | | - |
123 | | - ILaunchConfiguration[] configs = DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurations(type); |
124 | | - for(ILaunchConfiguration config : configs) { |
125 | | - if(configname.equals(config.getName())) { |
126 | | - return config; |
127 | | - } |
128 | | - } |
129 | | - |
130 | | - // create a new configuration for the file |
131 | | - ILaunchConfigurationWorkingCopy workingCopy = type.newInstance(null, configname); |
132 | | - workingCopy.setAttribute("KEY_FILE_PATH", path); //Constants. |
133 | | - setMoreAttributes(workingCopy); |
134 | | - return workingCopy.doSave(); |
135 | | - } |
136 | | - |
137 | | - protected void setMoreAttributes(ILaunchConfigurationWorkingCopy workingCopy) { |
138 | | - //NodeclipseConsole.write(this.getClass().getName()+"\n"); |
139 | | - NodeclipseLogger.log(this.getClass().getName()+"\n"); |
140 | | - } |
141 | 17 | } |
0 commit comments