Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthew Piggott2011-02-18 23:03:32 +0000
committerMilos Kleint2011-02-21 06:32:09 +0000
commit6ebbf4907dd0b0c5d110f7dda28cae363f214adc (patch)
tree76009d9a272dd9a9d1c6474837141bff88eae331 /org.eclipse.m2e.refactoring/src/org/eclipse/m2e/refactoring/exclude
parent2d7018c221b0fdfeb1999284144cff0a144afd82 (diff)
downloadm2e-core-6ebbf4907dd0b0c5d110f7dda28cae363f214adc.tar.gz
m2e-core-6ebbf4907dd0b0c5d110f7dda28cae363f214adc.tar.xz
m2e-core-6ebbf4907dd0b0c5d110f7dda28cae363f214adc.zip
336511 - Exclude Refactoring
Diffstat (limited to 'org.eclipse.m2e.refactoring/src/org/eclipse/m2e/refactoring/exclude')
-rw-r--r--org.eclipse.m2e.refactoring/src/org/eclipse/m2e/refactoring/exclude/DependencyExcludeAction.java53
-rw-r--r--org.eclipse.m2e.refactoring/src/org/eclipse/m2e/refactoring/exclude/ExcludeArtifactRefactoring.java163
2 files changed, 74 insertions, 142 deletions
diff --git a/org.eclipse.m2e.refactoring/src/org/eclipse/m2e/refactoring/exclude/DependencyExcludeAction.java b/org.eclipse.m2e.refactoring/src/org/eclipse/m2e/refactoring/exclude/DependencyExcludeAction.java
index d23eb9e3..490aabc5 100644
--- a/org.eclipse.m2e.refactoring/src/org/eclipse/m2e/refactoring/exclude/DependencyExcludeAction.java
+++ b/org.eclipse.m2e.refactoring/src/org/eclipse/m2e/refactoring/exclude/DependencyExcludeAction.java
@@ -16,9 +16,6 @@ import java.util.List;
import org.apache.maven.artifact.Artifact;
import org.eclipse.core.resources.IFile;
-import org.eclipse.core.runtime.CoreException;
-import org.eclipse.core.runtime.NullProgressMonitor;
-import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.internal.ui.packageview.ClassPathContainer.RequiredProjectWrapper;
@@ -26,13 +23,8 @@ import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ltk.ui.refactoring.RefactoringWizardOpenOperation;
-import org.eclipse.m2e.core.MavenPlugin;
import org.eclipse.m2e.core.embedder.ArtifactKey;
-import org.eclipse.m2e.core.project.IMavenProjectFacade;
-import org.eclipse.m2e.core.project.MavenProjectManager;
import org.eclipse.m2e.core.ui.internal.actions.SelectionUtil;
-import org.eclipse.m2e.editor.pom.MavenPomEditor;
-import org.eclipse.m2e.model.edit.pom.Model;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IActionDelegate;
import org.eclipse.ui.IEditorPart;
@@ -53,16 +45,10 @@ public class DependencyExcludeAction implements IActionDelegate {
private ArtifactKey[] keys;
- private Model model;
-
- private IMavenProjectFacade projectFacade;
-
- private EditingDomain editingDomain;
-
public void run(IAction action) {
if(keys != null && file != null) {
Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
- ExcludeArtifactRefactoring r = new ExcludeArtifactRefactoring(projectFacade, model, editingDomain, keys, file);
+ ExcludeArtifactRefactoring r = new ExcludeArtifactRefactoring(keys, file);
MavenExcludeWizard wizard = new MavenExcludeWizard(r);
try {
String titleForFailedChecks = ""; //$NON-NLS-1$
@@ -77,8 +63,6 @@ public class DependencyExcludeAction implements IActionDelegate {
public void selectionChanged(IAction action, ISelection selection) {
file = null;
keys = null;
- model = null;
- editingDomain = null;
// TODO move logic into adapters
if (selection instanceof IStructuredSelection) {
@@ -89,26 +73,18 @@ public class DependencyExcludeAction implements IActionDelegate {
if (selected instanceof Artifact) {
file = getFileFromEditor();
keys.add(new ArtifactKey((Artifact) selected));
- model = getModelFromEditor();
- projectFacade = getFacade(file);
- editingDomain = getEditingDomain();
} else if (selected instanceof org.sonatype.aether.graph.DependencyNode) {
file = getFileFromEditor();
keys.add(new ArtifactKey(((org.sonatype.aether.graph.DependencyNode) selected).getDependency().getArtifact()));
- model = getModelFromEditor();
- projectFacade = getFacade(file);
- editingDomain = getEditingDomain();
} else if (selected instanceof RequiredProjectWrapper) {
RequiredProjectWrapper w = (RequiredProjectWrapper) selected;
file = getFileFromProject(w.getParentClassPathContainer().getJavaProject());
- projectFacade = getFacade(file);
keys.add(SelectionUtil.getType(selected, ArtifactKey.class));
} else {
keys.add(SelectionUtil.getType(selected, ArtifactKey.class));
if (selected instanceof IJavaElement) {
IJavaElement el = (IJavaElement) selected;
file = getFileFromProject(el.getParent().getJavaProject());
- projectFacade = getFacade(file);
}
}
}
@@ -134,31 +110,4 @@ public class DependencyExcludeAction implements IActionDelegate {
}
return null;
}
-
- //mkleint: scary
- private Model getModelFromEditor() {
- IEditorPart part = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
- if(part != null && part instanceof MavenPomEditor) {
- try {
- return ((MavenPomEditor) part).readProjectDocument();
- } catch(CoreException ex) {
- // TODO Should we do something here, or do we not care
- }
- }
- return null;
- }
-
- private EditingDomain getEditingDomain() {
- IEditorPart part = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
- if(part != null && part instanceof MavenPomEditor) {
- return ((MavenPomEditor) part).getEditingDomain();
- }
- return null;
- }
-
- private IMavenProjectFacade getFacade(IFile file) {
- MavenProjectManager projectManager = MavenPlugin.getDefault().getMavenProjectManager();
- return projectManager.create(file, true, new NullProgressMonitor());
- }
-
}
diff --git a/org.eclipse.m2e.refactoring/src/org/eclipse/m2e/refactoring/exclude/ExcludeArtifactRefactoring.java b/org.eclipse.m2e.refactoring/src/org/eclipse/m2e/refactoring/exclude/ExcludeArtifactRefactoring.java
index 34ec2c83..1e95e8d7 100644
--- a/org.eclipse.m2e.refactoring/src/org/eclipse/m2e/refactoring/exclude/ExcludeArtifactRefactoring.java
+++ b/org.eclipse.m2e.refactoring/src/org/eclipse/m2e/refactoring/exclude/ExcludeArtifactRefactoring.java
@@ -10,7 +10,6 @@
*******************************************************************************/
package org.eclipse.m2e.refactoring.exclude;
-import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
@@ -23,13 +22,8 @@ import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
-import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubMonitor;
-import org.eclipse.emf.common.command.CompoundCommand;
-import org.eclipse.emf.edit.command.AddCommand;
-import org.eclipse.emf.edit.command.RemoveCommand;
-import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.ltk.core.refactoring.RefactoringStatusEntry;
@@ -38,11 +32,11 @@ import org.eclipse.m2e.core.core.IMavenConstants;
import org.eclipse.m2e.core.embedder.ArtifactKey;
import org.eclipse.m2e.core.embedder.MavenModelManager;
import org.eclipse.m2e.core.project.IMavenProjectFacade;
-import org.eclipse.m2e.model.edit.pom.Dependency;
-import org.eclipse.m2e.model.edit.pom.Exclusion;
-import org.eclipse.m2e.model.edit.pom.Model;
-import org.eclipse.m2e.model.edit.pom.PomPackage;
-import org.eclipse.m2e.model.edit.pom.impl.PomFactoryImpl;
+import org.eclipse.m2e.core.ui.internal.editing.AddExclusionOperation;
+import org.eclipse.m2e.core.ui.internal.editing.PomEdits.CompoundOperation;
+import org.eclipse.m2e.core.ui.internal.editing.PomEdits.Operation;
+import org.eclipse.m2e.core.ui.internal.editing.PomHelper;
+import org.eclipse.m2e.core.ui.internal.editing.RemoveDependencyOperation;
import org.eclipse.m2e.refactoring.AbstractPomHeirarchyRefactoring;
import org.eclipse.m2e.refactoring.Messages;
import org.eclipse.osgi.util.NLS;
@@ -56,13 +50,12 @@ public class ExcludeArtifactRefactoring extends AbstractPomHeirarchyRefactoring
private final ArtifactKey[] keys;
- private Map<MavenProject, Change> changeMap;
-
private Set<ArtifactKey> locatedKeys;
- public ExcludeArtifactRefactoring(IMavenProjectFacade projectFacade, Model model, EditingDomain editingDomain,
- ArtifactKey[] keys, IFile pom) {
- super(projectFacade, model, editingDomain, pom);
+ private Map<IFile, Change> operationMap;
+
+ public ExcludeArtifactRefactoring(ArtifactKey[] keys, IFile pom) {
+ super(pom);
this.keys = keys;
}
@@ -78,18 +71,64 @@ public class ExcludeArtifactRefactoring extends AbstractPomHeirarchyRefactoring
return NLS.bind(Messages.ExcludeArtifactRefactoring_refactoringName, builder.toString());
}
- protected boolean isChanged(final EditingDomain editingDomain, final MavenProject project,
- final IProgressMonitor progressMonitor) throws CoreException,
- OperationCanceledException, IOException {
+ /* (non-Javadoc)
+ * @see org.eclipse.m2e.refactoring.exclude.AbstractRefactoring#isReady(org.eclipse.core.runtime.IProgressMonitor)
+ */
+ protected RefactoringStatusEntry[] isReady(IProgressMonitor pm) {
+ if(keys == null || keys.length == 0) {
+ return new RefactoringStatusEntry[] {new RefactoringStatusEntry(RefactoringStatus.FATAL,
+ Messages.ExcludeArtifactRefactoring_noArtifactsSet)};
+ }
+ List<RefactoringStatusEntry> entries = new ArrayList<RefactoringStatusEntry>();
+ for (ArtifactKey key : keys) {
+ if (!locatedKeys.contains(key)) {
+ entries.add(new RefactoringStatusEntry(RefactoringStatus.FATAL, NLS.bind(
+ Messages.ExcludeArtifactRefactoring_failedToLocateArtifact, key.toString())));
+ }
+ }
+ return entries.toArray(new RefactoringStatusEntry[entries.size()]);
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.m2e.refactoring.AbstractRefactoring#getChange(org.apache.maven.project.MavenProject, org.eclipse.core.runtime.IProgressMonitor)
+ */
+ protected Change getChange(IFile file, IProgressMonitor pm) {
+ return operationMap.get(file);
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.m2e.refactoring.AbstractPomHeirarchyRefactoring#checkInitial(org.eclipse.core.runtime.IProgressMonitor)
+ */
+ protected void checkInitial(IProgressMonitor pm) {
+ locatedKeys = new HashSet<ArtifactKey>(keys.length);
+ operationMap = new HashMap<IFile, Change>();
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.m2e.refactoring.AbstractPomHeirarchyRefactoring#checkFinal(org.eclipse.core.runtime.IProgressMonitor)
+ */
+ protected void checkFinal(IProgressMonitor pm) {
+ // Do nothing
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.m2e.refactoring.AbstractPomHeirarchyRefactoring#isAffected(org.eclipse.m2e.core.project.IMavenProjectFacade, org.eclipse.core.runtime.IProgressMonitor)
+ */
+ protected boolean isAffected(IFile pomFile, IProgressMonitor progressMonitor) throws CoreException {
+
final SubMonitor monitor = SubMonitor.convert(progressMonitor);
- final Model m = getModel(project);
- final List<Dependency> deps = m.getDependencies();
final IStatus[] status = new IStatus[1];
- final CompoundCommand exclusionCommand = new CompoundCommand();
- final List<Dependency> toRemove = new ArrayList<Dependency>();
+
+ final IMavenProjectFacade facade = MavenPlugin.getDefault().getMavenProjectManagerImpl()
+ .create(pomFile, true, monitor);
+ final MavenProject project = facade.getMavenProject(progressMonitor);
+ final org.apache.maven.model.Model m = project.getModel();
+
+ final List<Operation> operations = new ArrayList<Operation>();
final StringBuilder msg = new StringBuilder();
-
+ final List<org.apache.maven.model.Dependency> dependencies = m.getDependencies();
+
MavenModelManager modelManager = MavenPlugin.getDefault().getMavenModelManager();
DependencyNode root = modelManager.readDependencyTree(project, JavaScopes.TEST, monitor.newChild(1));
root.accept(new DependencyVisitor() {
@@ -118,17 +157,17 @@ public class ExcludeArtifactRefactoring extends AbstractPomHeirarchyRefactoring
} else if(node == topLevel) {
msg.append(key.toString()).append(',');
// need to remove top-level dependency
- toRemove.add(findDependency(topLevel));
+ operations.add(new RemoveDependencyOperation(findDependency(topLevel)));
locatedKeys.add(key);
} else {
// need to add exclusion to top-level dependency
- Dependency dependency = findDependency(topLevel);
+ org.apache.maven.model.Dependency dependency = findDependency(topLevel);
if(dependency == null) {
status[0] = new Status(IStatus.ERROR, IMavenConstants.PLUGIN_ID, NLS.bind(
Messages.ExcludeRefactoring_error_parent, topLevel.getDependency().getArtifact().getGroupId(),
topLevel.getDependency().getArtifact().getArtifactId()));
} else {
- addExclusion(exclusionCommand, dependency, key);
+ operations.add(new AddExclusionOperation(dependency, key));
locatedKeys.add(key);
}
}
@@ -140,17 +179,8 @@ public class ExcludeArtifactRefactoring extends AbstractPomHeirarchyRefactoring
return true;
}
- private void addExclusion(CompoundCommand command, Dependency dep, ArtifactKey key) {
- Exclusion exclusion = PomFactoryImpl.eINSTANCE.createExclusion();
- exclusion.setArtifactId(key.getArtifactId());
- exclusion.setGroupId(key.getGroupId());
- command.append(AddCommand.create(editingDomain, dep,
- PomPackage.eINSTANCE.getDependency_Exclusions(), exclusion));
- msg.append(key.toString()).append(',');
- }
-
- private Dependency findDependency(String groupId, String artifactId) {
- for(Dependency d : deps) {
+ private org.apache.maven.model.Dependency findDependency(String groupId, String artifactId) {
+ for(org.apache.maven.model.Dependency d : dependencies) {
if(d.getGroupId().equals(groupId) && d.getArtifactId().equals(artifactId)) {
return d;
}
@@ -158,7 +188,7 @@ public class ExcludeArtifactRefactoring extends AbstractPomHeirarchyRefactoring
return null;
}
- private Dependency findDependency(DependencyNode node) {
+ private org.apache.maven.model.Dependency findDependency(DependencyNode node) {
Artifact artifact;
if(node.getRelocations().isEmpty()) {
artifact = node.getDependency().getArtifact();
@@ -169,57 +199,10 @@ public class ExcludeArtifactRefactoring extends AbstractPomHeirarchyRefactoring
}
});
- for(Dependency remove : toRemove) {
- exclusionCommand.append(RemoveCommand.create(editingDomain, remove));
- }
-// for(Iterator<Dependency> rem = toRemove.iterator(); rem.hasNext();) {
-// RemoveCommand.create(editingDomain, model, null, rem.next());
-// exclusionCommand.append(new RemoveCommand(editingDomain, model.getDependencies(), rem.next()));
-// }
- if(!exclusionCommand.isEmpty()) {
- changeMap.put(project, new PomResourceChange(editingDomain, exclusionCommand, getPomFile(project),//
- msg.delete(msg.length() - 1, msg.length()).toString()));
- }
- return !exclusionCommand.isEmpty();
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.m2e.refactoring.exclude.AbstractRefactoring#isReady(org.eclipse.core.runtime.IProgressMonitor)
- */
- protected RefactoringStatusEntry[] isReady(IProgressMonitor pm) {
- if(keys == null || keys.length == 0) {
- return new RefactoringStatusEntry[] {new RefactoringStatusEntry(RefactoringStatus.FATAL,
- Messages.ExcludeArtifactRefactoring_noArtifactsSet)};
- }
- List<RefactoringStatusEntry> entries = new ArrayList<RefactoringStatusEntry>();
- for (ArtifactKey key : keys) {
- if (!locatedKeys.contains(key)) {
- entries.add(new RefactoringStatusEntry(RefactoringStatus.FATAL, NLS.bind(
- Messages.ExcludeArtifactRefactoring_failedToLocateArtifact, key.toString())));
- }
+ if(operations.size() > 0) {
+ operationMap.put(pomFile, PomHelper.createChange(pomFile,
+ new CompoundOperation(operations.toArray(new Operation[operations.size()])), msg.toString()));
}
- return entries.toArray(new RefactoringStatusEntry[entries.size()]);
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.m2e.refactoring.AbstractRefactoring#getChange(org.apache.maven.project.MavenProject, org.eclipse.core.runtime.IProgressMonitor)
- */
- protected Change getChange(MavenProject project, IProgressMonitor pm) {
- return changeMap.get(project);
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.m2e.refactoring.AbstractPomHeirarchyRefactoring#checkInitial(org.eclipse.core.runtime.IProgressMonitor)
- */
- protected void checkInitial(IProgressMonitor pm) {
- locatedKeys = new HashSet<ArtifactKey>(keys.length);
- changeMap = new HashMap<MavenProject, Change>();
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.m2e.refactoring.AbstractPomHeirarchyRefactoring#checkFinal(org.eclipse.core.runtime.IProgressMonitor)
- */
- protected void checkFinal(IProgressMonitor pm) {
- // Do nothing
+ return !operations.isEmpty();
}
} \ No newline at end of file

Back to the top