Skip to content

Commit 50044db

Browse files
committed
Maven - simplify LaunchShortcut
1 parent 46ba731 commit 50044db

File tree

4 files changed

+22
-262
lines changed

4 files changed

+22
-262
lines changed

org.nodeclipse.enide.maven/src/org/nodeclipse/enide/maven/launch/LaunchShortcut.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,21 @@
1212
import org.eclipse.jface.dialogs.MessageDialog;
1313
import org.eclipse.jface.viewers.ISelection;
1414
import org.eclipse.jface.viewers.IStructuredSelection;
15-
import org.eclipse.ui.IEditorPart;
1615
import org.eclipse.ui.IEditorInput;
16+
import org.eclipse.ui.IEditorPart;
1717
import org.eclipse.ui.IFileEditorInput;
1818
import org.nodeclipse.enide.maven.preferences.MavenConstants;
1919
import org.nodeclipse.enide.maven.util.NodeclipseLogger;
20-
//import org.nodeclipse.ui.util.NodeclipseConsole;
2120

2221
/**
2322
* Using "Run As" --> "mvn package Maven build" will lead here
2423
**/
2524
public class LaunchShortcut implements ILaunchShortcut {
25+
26+
protected String getLaunchConfigurationTypeConstant() {
27+
return MavenConstants.LAUNCH_CONFIGURATION_TYPE_ID;
28+
}
29+
2630

2731
/**
2832
* (non-Javadoc)
@@ -78,7 +82,7 @@ private void launchFile(IFile file, String mode) throws CoreException {
7882
// check for an existing launch config for the file
7983
String path = file.getFullPath().toString();
8084
ILaunchManager launchManager = DebugPlugin.getDefault().getLaunchManager();
81-
ILaunchConfigurationType type = launchManager.getLaunchConfigurationType(MavenConstants.LAUNCH_CONFIGURATION_TYPE_ID);
85+
ILaunchConfigurationType type = launchManager.getLaunchConfigurationType(getLaunchConfigurationTypeConstant());
8286
ILaunchConfiguration configuration = createLaunchConfiguration(type, path, file);
8387
DebugUITools.launch(configuration, mode);
8488
// then execution goes in LaunchConfigurationDelegate.java launch() method
Lines changed: 5 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -1,141 +1,17 @@
11
package org.nodeclipse.enide.maven.launch.jetty;
22

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;
123
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;
204
import org.nodeclipse.enide.maven.preferences.MavenConstants;
21-
import org.nodeclipse.enide.maven.util.NodeclipseLogger;
22-
//import org.nodeclipse.ui.util.NodeclipseConsole;
235

246
/**
257
* Using "Run As" --> "mvn tomcat6:start Maven start" will lead here</br>
268
* http://tomcat.apache.org/maven-plugin-2.2/context-goals.html
279
**/
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_JETTY_CONFIGURATION_TYPE_ID;
6715
}
6816

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_JETTY_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-
}
14117
}
Lines changed: 5 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -1,141 +1,17 @@
11
package org.nodeclipse.enide.maven.launch.tomcat;
22

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;
123
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;
204
import org.nodeclipse.enide.maven.preferences.MavenConstants;
21-
import org.nodeclipse.enide.maven.util.NodeclipseLogger;
22-
//import org.nodeclipse.ui.util.NodeclipseConsole;
235

246
/**
257
* Using "Run As" --> "mvn tomcat6:start Maven start" will lead here</br>
268
* http://tomcat.apache.org/maven-plugin-2.2/context-goals.html
279
**/
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;
6715
}
6816

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-
}
14117
}

org.nodeclipse.enide.maven/src/org/nodeclipse/enide/maven/launchexec/LaunchShortcut.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
**/
2727
public class LaunchShortcut implements ILaunchShortcut {
2828

29+
protected String getLaunchConfigurationTypeConstant() {
30+
return MavenConstants.LAUNCH_EXEC_CONFIGURATION_TYPE_ID;
31+
}
32+
2933
/**
3034
* (non-Javadoc)
3135
*
@@ -98,7 +102,7 @@ private void launchFile(IFile file, String mode) throws CoreException {
98102
// check for an existing launch config for the file
99103
String path = file.getFullPath().toString();
100104
ILaunchManager launchManager = DebugPlugin.getDefault().getLaunchManager();
101-
ILaunchConfigurationType type = launchManager.getLaunchConfigurationType(MavenConstants.LAUNCH_EXEC_CONFIGURATION_TYPE_ID);
105+
ILaunchConfigurationType type = launchManager.getLaunchConfigurationType(getLaunchConfigurationTypeConstant());
102106
ILaunchConfiguration configuration = createLaunchConfiguration(type, path, file);
103107
DebugUITools.launch(configuration, mode);
104108
// then execution goes in LaunchConfigurationDelegate.java launch() method

0 commit comments

Comments
 (0)