diff options
author | Igor Fedorenko | 2013-10-14 03:59:22 +0000 |
---|---|---|
committer | Igor Fedorenko | 2013-10-14 03:59:53 +0000 |
commit | 88c6ce38e4b60ee6e08ab7cd21312b0d31bc5b52 (patch) | |
tree | 3f67ecc899fc9a853867d8c1e031a6f19830e3ee | |
parent | 44caa378b65b905f4d27e48a968b0a5802fb5fb7 (diff) | |
download | m2e-core-88c6ce38e4b60ee6e08ab7cd21312b0d31bc5b52.tar.gz m2e-core-88c6ce38e4b60ee6e08ab7cd21312b0d31bc5b52.tar.xz m2e-core-88c6ce38e4b60ee6e08ab7cd21312b0d31bc5b52.zip |
408042 fixed nested project import working set selection
... some more WorkingSets consolidation.
Signed-off-by: Igor Fedorenko <igor@ifedorenko.com>
5 files changed, 91 insertions, 56 deletions
diff --git a/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/WorkingSets.java b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/WorkingSets.java index 44ebbb40..6d76743e 100644 --- a/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/WorkingSets.java +++ b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/WorkingSets.java @@ -18,6 +18,7 @@ import java.util.List; import java.util.Set; import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IResource; import org.eclipse.core.runtime.IAdaptable; import org.eclipse.ui.IWorkingSet; import org.eclipse.ui.IWorkingSetManager; @@ -91,7 +92,7 @@ public class WorkingSets { IAdaptable[] oldElements = workingSet.getElements(); IAdaptable[] newElements = new IAdaptable[oldElements.length + projects.length]; System.arraycopy(oldElements, 0, newElements, 0, oldElements.length); - System.arraycopy(newElements, oldElements.length, projects, 0, projects.length); + System.arraycopy(projects, 0, newElements, oldElements.length, projects.length); workingSet.setElements(newElements); } } @@ -130,4 +131,41 @@ public class WorkingSets { return projects; } + /** + * Returns one of the working sets the element directly belongs to. Returns {@code null} if the element does not + * belong to any working set. + * + * @since 1.5 + */ + public static IWorkingSet getAssignedWorkingSet(IResource element) { + IWorkingSetManager workingSetManager = PlatformUI.getWorkbench().getWorkingSetManager(); + for(IWorkingSet workingSet : workingSetManager.getWorkingSets()) { + for(IAdaptable adaptable : workingSet.getElements()) { + if(adaptable.getAdapter(IResource.class) == element) { + return workingSet; + } + } + } + return null; + } + + /** + * Returns all working sets the element directly belongs to. Returns empty collection if the element does not belong + * to any working set. The order of returned working sets is not specified. + * + * @since 1.5 + */ + public static List<IWorkingSet> getAssignedWorkingSets(IResource element) { + List<IWorkingSet> list = new ArrayList<IWorkingSet>(); + IWorkingSetManager workingSetManager = PlatformUI.getWorkbench().getWorkingSetManager(); + for(IWorkingSet workingSet : workingSetManager.getWorkingSets()) { + for(IAdaptable adaptable : workingSet.getElements()) { + if(adaptable.getAdapter(IResource.class) == element) { + list.add(workingSet); + } + } + } + return list; + } + } 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 1d77e9dc..edab488b 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 @@ -46,7 +46,6 @@ import org.eclipse.ui.IStorageEditorInput; import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.ui.IWorkingSet; -import org.eclipse.ui.IWorkingSetManager; import org.eclipse.ui.PlatformUI; import org.codehaus.plexus.util.IOUtil; @@ -65,6 +64,7 @@ import org.eclipse.m2e.core.internal.IMavenConstants; import org.eclipse.m2e.core.project.IMavenProjectFacade; import org.eclipse.m2e.core.project.IMavenProjectRegistry; import org.eclipse.m2e.core.ui.internal.Messages; +import org.eclipse.m2e.core.ui.internal.WorkingSets; import org.eclipse.m2e.core.ui.internal.util.Util; import org.eclipse.m2e.core.ui.internal.util.Util.FileStoreEditorInputStub; @@ -196,7 +196,7 @@ public class SelectionUtil { IResource resource = getType(element, IResource.class); if(resource != null) { - return getWorkingSet(resource.getProject()); + return WorkingSets.getAssignedWorkingSet(resource.getProject()); } return null; @@ -218,31 +218,6 @@ public class SelectionUtil { // } } - public static IWorkingSet getWorkingSet(Object element) { - IWorkingSetManager workingSetManager = PlatformUI.getWorkbench().getWorkingSetManager(); - for(IWorkingSet workingSet : workingSetManager.getWorkingSets()) { - for(IAdaptable adaptable : workingSet.getElements()) { - if(adaptable.getAdapter(IResource.class) == element) { - return workingSet; - } - } - } - return null; - } - - public static List<IWorkingSet> getAssignedWorkingSets(Object element) { - List<IWorkingSet> list = new ArrayList<IWorkingSet>(); - IWorkingSetManager workingSetManager = PlatformUI.getWorkbench().getWorkingSetManager(); - for(IWorkingSet workingSet : workingSetManager.getWorkingSets()) { - for(IAdaptable adaptable : workingSet.getElements()) { - if(adaptable.getAdapter(IResource.class) == element) { - list.add(workingSet); - } - } - } - return list; - } - public static ArtifactKey getArtifactKey(Object element) throws CoreException { if(element instanceof Artifact) { return new ArtifactKey(((Artifact) element)); diff --git a/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/wizards/MavenImportWizard.java b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/wizards/MavenImportWizard.java index 44c6445d..74a473b9 100644 --- a/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/wizards/MavenImportWizard.java +++ b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/wizards/MavenImportWizard.java @@ -12,6 +12,7 @@ package org.eclipse.m2e.core.ui.internal.wizards; +import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; @@ -99,6 +100,7 @@ public class MavenImportWizard extends AbstractMavenProjectWizard implements IIm } Collection<MavenProjectInfo> projects = getProjects(); + List<IWorkingSet> workingSets = new ArrayList<IWorkingSet>(); // ignore any preselected working set if(page.shouldCreateWorkingSet() && !projects.isEmpty()) { IWorkingSet workingSet = WorkingSets.getOrCreateWorkingSet(page.getWorkingSetName()); if(!workingSets.contains(workingSet)) { diff --git a/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/wizards/MavenImportWizardPage.java b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/wizards/MavenImportWizardPage.java index 1d294d3e..f5c23831 100644 --- a/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/wizards/MavenImportWizardPage.java +++ b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/wizards/MavenImportWizardPage.java @@ -19,6 +19,7 @@ import java.net.URISyntaxException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.HashSet; import java.util.LinkedHashSet; import java.util.List; import java.util.Set; @@ -26,6 +27,7 @@ import java.util.Set; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.eclipse.core.resources.IContainer; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IWorkspace; import org.eclipse.core.resources.IWorkspaceRoot; @@ -67,6 +69,7 @@ import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Menu; import org.eclipse.swt.widgets.MenuItem; import org.eclipse.swt.widgets.Tree; +import org.eclipse.ui.IWorkingSet; import org.apache.maven.model.Model; import org.apache.maven.model.Parent; @@ -450,33 +453,7 @@ public class MavenImportWizardPage extends AbstractMavenWizardPage { setMessage(null); loadingErrorMessage = null; - // update name of working set - MavenProjectInfo rootProject = null; - if(projects != null && projects.size() == 1) { - rootProject = projects.get(0); - } - - String name; - Set<String> workingSetNames = new LinkedHashSet<String>(); - if(rootProject != null) { - name = getImportConfiguration().getProjectName(rootProject.getModel()); - workingSetNames.add(name); - } else { - name = "";//$NON-NLS-1$ - } - workingSetNames.addAll(Arrays.asList(WorkingSets.getWorkingSets())); - - String[] workingSetNameArray = new String[workingSetNames.size()]; - workingSetName.setItems(workingSetNames.toArray(workingSetNameArray)); - workingSetName.setText(name); - - if(rootProject != null && !rootProject.getProjects().isEmpty()) { - createWorkingSet.setSelection(true); - workingSetName.setEnabled(true); - } else { - createWorkingSet.setSelection(false); - workingSetName.setEnabled(false); - } + updateWorkingSet(projects); //mkleint: XXX this sort of error handling is rather unfortunate @@ -518,6 +495,48 @@ public class MavenImportWizardPage extends AbstractMavenWizardPage { } } + private void updateWorkingSet(List<MavenProjectInfo> projects) { + MavenProjectInfo rootProject = null; + if(projects != null && projects.size() == 1) { + rootProject = projects.get(0); + } + + // check if imported project(s) are nested inside existing workspace project + String rootDirectory = rootDirectoryCombo.getText().trim(); + if(rootDirectory.length() > 0) { + Set<IWorkingSet> workingSets = new HashSet<IWorkingSet>(); + for(IContainer container : workspaceRoot.findContainersForLocationURI(new File(rootDirectory).toURI())) { + workingSets.addAll(WorkingSets.getAssignedWorkingSets(container.getProject())); + } + if(workingSets.size() == 1) { + updateWorkingSet(workingSets.iterator().next().getName(), true); + return; + } + } + + // derive working set name from project name + if(rootProject != null) { + updateWorkingSet(getImportConfiguration().getProjectName(rootProject.getModel()), // + !rootProject.getProjects().isEmpty()); + } else { + updateWorkingSet(null, false); + } + } + + private void updateWorkingSet(String name, boolean enabled) { + Set<String> workingSetNames = new LinkedHashSet<String>(); + if(name == null) { + name = ""; //$NON-NLS-1$ + } else { + workingSetNames.add(name); + } + workingSetNames.addAll(Arrays.asList(WorkingSets.getWorkingSets())); + workingSetName.setItems(workingSetNames.toArray(new String[workingSetNames.size()])); + workingSetName.setText(name); + createWorkingSet.setSelection(enabled); + workingSetName.setEnabled(enabled); + } + void setAllChecked(boolean state) { @SuppressWarnings("unchecked") List<MavenProjectInfo> input = (List<MavenProjectInfo>) projectTreeViewer.getInput(); diff --git a/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/wizards/MavenModuleWizardParentPage.java b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/wizards/MavenModuleWizardParentPage.java index 6a726486..3c6e6d88 100644 --- a/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/wizards/MavenModuleWizardParentPage.java +++ b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/wizards/MavenModuleWizardParentPage.java @@ -47,6 +47,7 @@ import org.eclipse.m2e.core.MavenPlugin; import org.eclipse.m2e.core.internal.IMavenConstants; import org.eclipse.m2e.core.project.ProjectImportConfiguration; import org.eclipse.m2e.core.ui.internal.Messages; +import org.eclipse.m2e.core.ui.internal.WorkingSets; import org.eclipse.m2e.core.ui.internal.actions.SelectionUtil; import org.eclipse.m2e.core.ui.internal.components.WorkingSetGroup; @@ -218,7 +219,7 @@ public class MavenModuleWizardParentPage extends AbstractMavenWizardPage { IProject project = SelectionUtil.getType(parentObject, IProject.class); pom = project.getFile(IMavenConstants.POM_FILE_NAME); - workingSetGroup.selectWorkingSets(SelectionUtil.getAssignedWorkingSets(project)); + workingSetGroup.selectWorkingSets(WorkingSets.getAssignedWorkingSets(project)); } else if(parentObject instanceof IContainer) { pom = ((IContainer) parentObject).getFile(new Path(IMavenConstants.POM_FILE_NAME)); } |