Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFred Bricon2013-05-05 18:04:24 +0000
committerFred Bricon2013-05-05 18:04:24 +0000
commit1c99280d0a24636941bc3f45f95b183a30dfa1db (patch)
treecd0d4a0cfd3da5ce079700343a2971580dcd68e9
parentee0138ec58e89d59387b238bbc3f7c4265976264 (diff)
downloadm2e-core-1c99280d0a24636941bc3f45f95b183a30dfa1db.tar.gz
m2e-core-1c99280d0a24636941bc3f45f95b183a30dfa1db.tar.xz
m2e-core-1c99280d0a24636941bc3f45f95b183a30dfa1db.zip
407153 : improve maven projects selection
Before selecting all projects from the workspace if the current selection is empty, we look at the current active file editor from the current active workbench part. If nothing found, then we fall back on returning all workspace projects. This allows a user to hit Alt+F5 while editing a pom.xml (or any resource in the current project) and NOT select all workspace projects. Signed-off-by: Fred Bricon <fbricon@gmail.com>
-rw-r--r--org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/actions/MavenProjectActionSupport.java2
-rw-r--r--org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/actions/SelectionUtil.java11
-rw-r--r--org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/actions/UpdateMavenProjectCommandHandler.java45
3 files changed, 51 insertions, 7 deletions
diff --git a/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/actions/MavenProjectActionSupport.java b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/actions/MavenProjectActionSupport.java
index fbf9521c..1e328d0b 100644
--- a/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/actions/MavenProjectActionSupport.java
+++ b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/actions/MavenProjectActionSupport.java
@@ -19,7 +19,7 @@ import org.eclipse.ui.IWorkbenchWindowActionDelegate;
public abstract class MavenProjectActionSupport extends MavenActionSupport implements IWorkbenchWindowActionDelegate {
protected IProject[] getProjects() {
- return SelectionUtil.getProjects(selection);
+ return SelectionUtil.getProjects(selection, true);
}
public void dispose() {
diff --git a/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/actions/SelectionUtil.java b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/actions/SelectionUtil.java
index e26ac3f6..e8711c3c 100644
--- a/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/actions/SelectionUtil.java
+++ b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/actions/SelectionUtil.java
@@ -411,15 +411,16 @@ public class SelectionUtil {
}
/**
- * Returns all the Maven projects found in the given selection, or all the workspace projects if no Maven project was
- * found.
+ * Returns all the Maven projects found in the given selection. If no projects are found in the selection and
+ * <code>includeAll</code> is true, all workspace projects are returned.
*
* @param selection
+ * @param includeAll flag to return all workspace projects if selection doesn't contain any Maven projects.
* @return an array of {@link IProject} containing all the Maven projects found in the given selection, or all the
- * workspace projects if no Maven project was found.
+ * workspace projects if no Maven project was found and <code>includeAll</code> is true.
* @since 1.4.0
*/
- public static IProject[] getProjects(ISelection selection) {
+ public static IProject[] getProjects(ISelection selection, boolean includeAll) {
ArrayList<IProject> projectList = new ArrayList<IProject>();
if(selection instanceof IStructuredSelection) {
for(Iterator<?> it = ((IStructuredSelection) selection).iterator(); it.hasNext();) {
@@ -442,7 +443,7 @@ public class SelectionUtil {
}
}
- if(projectList.isEmpty()) {
+ if(projectList.isEmpty() && includeAll) {
return ResourcesPlugin.getWorkspace().getRoot().getProjects();
}
return projectList.toArray(new IProject[projectList.size()]);
diff --git a/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/actions/UpdateMavenProjectCommandHandler.java b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/actions/UpdateMavenProjectCommandHandler.java
index 9b534f25..a4c4847c 100644
--- a/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/actions/UpdateMavenProjectCommandHandler.java
+++ b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/actions/UpdateMavenProjectCommandHandler.java
@@ -12,14 +12,23 @@
package org.eclipse.m2e.core.ui.internal.actions;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.Shell;
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.IFileEditorInput;
+import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.handlers.HandlerUtil;
+import org.eclipse.m2e.core.internal.IMavenConstants;
import org.eclipse.m2e.core.ui.internal.UpdateMavenProjectJob;
import org.eclipse.m2e.core.ui.internal.dialogs.UpdateMavenProjectsDialog;
@@ -33,19 +42,53 @@ import org.eclipse.m2e.core.ui.internal.dialogs.UpdateMavenProjectsDialog;
*/
public class UpdateMavenProjectCommandHandler extends AbstractHandler {
+ private static final Logger log = LoggerFactory.getLogger(UpdateMavenProjectCommandHandler.class);
+
public Object execute(final ExecutionEvent event) {
ISelection selection = HandlerUtil.getCurrentSelection(event);
Shell shell = HandlerUtil.getActiveWorkbenchWindow(event).getShell();
- IProject[] projects = SelectionUtil.getProjects(selection);
+ IProject[] projects = SelectionUtil.getProjects(selection, false);
+
+ //If no projects in the current selection, look at the active editor
+ if(projects == null || projects.length == 0) {
+ projects = getProjectInActiveEditor(event);
+ }
+
+ //If no projects found, select all projects in the workspace
+ if(projects == null || projects.length == 0) {
+ projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
+ }
openUpdateProjectsDialog(shell, projects);
return null;
}
+ /**
+ * get the (maven) project in current active editor
+ */
+ private IProject[] getProjectInActiveEditor(ExecutionEvent event) {
+ try {
+ IWorkbenchPart activePart = HandlerUtil.getActivePart(event);
+ if(activePart instanceof IEditorPart) {
+ IEditorPart editorPart = (IEditorPart) activePart;
+ if(editorPart.getEditorInput() instanceof IFileEditorInput) {
+ IFileEditorInput fileInput = (IFileEditorInput) editorPart.getEditorInput();
+ IProject project = fileInput.getFile().getProject();
+ if(project != null && project.isAccessible() && project.hasNature(IMavenConstants.NATURE_ID)) {
+ return new IProject[] {project};
+ }
+ }
+ }
+ } catch(CoreException ex) {
+ log.error(ex.getMessage(), ex);
+ }
+ return null;
+ }
+
/* package */static void openUpdateProjectsDialog(Shell shell, IProject[] projects) {
UpdateMavenProjectsDialog dialog = new UpdateMavenProjectsDialog(shell, projects);
if(dialog.open() == Window.OK) {

Back to the top