summaryrefslogtreecommitdiffstatsabout
diff options
context:
space:
mode:
authorMatthew Piggott2011-02-18 18:03:32 (EST)
committer Milos Kleint2011-02-21 01:32:09 (EST)
commit6ebbf4907dd0b0c5d110f7dda28cae363f214adc (patch)
tree76009d9a272dd9a9d1c6474837141bff88eae331
parent2d7018c221b0fdfeb1999284144cff0a144afd82 (diff)
downloadm2e-core-6ebbf4907dd0b0c5d110f7dda28cae363f214adc.zip
m2e-core-6ebbf4907dd0b0c5d110f7dda28cae363f214adc.tar.gz
m2e-core-6ebbf4907dd0b0c5d110f7dda28cae363f214adc.tar.bz2
336511 - Exclude Refactoring
-rw-r--r--org.eclipse.m2e.core.ui/META-INF/MANIFEST.MF3
-rw-r--r--org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/editing/AddExclusionOperation.java45
-rw-r--r--org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/editing/ChangeCreator.java190
-rw-r--r--org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/editing/PomEdits.java30
-rw-r--r--org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/editing/PomHelper.java84
-rw-r--r--org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/editing/RemoveDependencyOperation.java40
-rw-r--r--org.eclipse.m2e.refactoring/src/org/eclipse/m2e/refactoring/AbstractPomHeirarchyRefactoring.java257
-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
9 files changed, 516 insertions, 349 deletions
diff --git a/org.eclipse.m2e.core.ui/META-INF/MANIFEST.MF b/org.eclipse.m2e.core.ui/META-INF/MANIFEST.MF
index 01c11ee..0360405 100644
--- a/org.eclipse.m2e.core.ui/META-INF/MANIFEST.MF
+++ b/org.eclipse.m2e.core.ui/META-INF/MANIFEST.MF
@@ -42,3 +42,6 @@ Require-Bundle: org.eclipse.osgi;bundle-version="3.5.2",
org.eclipse.search;bundle-version="3.5.1",
org.eclipse.ui.forms;bundle-version="3.4.1",
org.slf4j.api;bundle-version="1.5.11"
+Import-Package: org.eclipse.ltk.core.refactoring,
+ org.eclipse.ltk.core.refactoring.resource,
+ org.eclipse.compare.rangedifferencer
diff --git a/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/editing/AddExclusionOperation.java b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/editing/AddExclusionOperation.java
new file mode 100644
index 0000000..9eb75d9
--- /dev/null
+++ b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/editing/AddExclusionOperation.java
@@ -0,0 +1,45 @@
+/*******************************************************************************
+ * Copyright (c) 2011 Sonatype, Inc.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *******************************************************************************/
+
+package org.eclipse.m2e.core.ui.internal.editing;
+
+import org.apache.maven.model.Dependency;
+import org.eclipse.m2e.core.embedder.ArtifactKey;
+import org.eclipse.m2e.core.ui.internal.editing.PomEdits.Operation;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+public class AddExclusionOperation implements Operation {
+
+ private Dependency dependency;
+
+ private ArtifactKey exclusion;
+
+ public AddExclusionOperation(Dependency dependency, ArtifactKey exclusion) {
+ this.dependency = dependency;
+ this.exclusion = exclusion;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.m2e.core.ui.internal.editing.PomEdits.Operation#process(org.w3c.dom.Document)
+ */
+ public void process(Document document) {
+ Element depElement = PomHelper.findDependency(document, dependency);
+
+ if(depElement == null) {
+ throw new IllegalArgumentException("Dependency does not exist in this pom");
+ }
+ Element exclusionsElement = PomEdits.getChild(depElement, PomHelper.EXCLUSIONS);
+
+ Element exclusionElement = PomEdits.createElement(exclusionsElement, PomHelper.EXCLUSION);
+
+ PomEdits.createElementWithText(exclusionElement, PomHelper.ARTIFACT_ID, exclusion.getArtifactId());
+ PomEdits.createElementWithText(exclusionElement, PomHelper.GROUP_ID, exclusion.getGroupId());
+ PomEdits.createElementWithText(exclusionElement, PomHelper.VERSION, exclusion.getVersion());
+ }
+}
diff --git a/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/editing/ChangeCreator.java b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/editing/ChangeCreator.java
new file mode 100644
index 0000000..13c68a1
--- /dev/null
+++ b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/editing/ChangeCreator.java
@@ -0,0 +1,190 @@
+/*******************************************************************************
+ * Copyright (c) 2008-2010 Sonatype, Inc.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Sonatype, Inc. - initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.m2e.core.ui.internal.editing;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+
+import org.eclipse.compare.rangedifferencer.IRangeComparator;
+import org.eclipse.compare.rangedifferencer.RangeDifference;
+import org.eclipse.compare.rangedifferencer.RangeDifferencer;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.jface.text.BadLocationException;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.IRegion;
+import org.eclipse.ltk.core.refactoring.TextFileChange;
+import org.eclipse.text.edits.DeleteEdit;
+import org.eclipse.text.edits.InsertEdit;
+import org.eclipse.text.edits.MultiTextEdit;
+import org.eclipse.text.edits.ReplaceEdit;
+import org.eclipse.text.edits.TextEdit;
+import org.eclipse.text.edits.TextEditGroup;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * This class creates an org.eclipse.ltk.core.refactoring.DocumentChange instance based on old and new text values
+ *
+ * @author Anton Kraev
+ */
+public class ChangeCreator {
+ private static final Logger log = LoggerFactory.getLogger(ChangeCreator.class);
+
+ private String label;
+
+ private IDocument oldDocument;
+
+ private IDocument newDocument;
+
+ private IFile oldFile;
+
+ public ChangeCreator(IFile oldFile, IDocument oldDocument, IDocument newDocument, String label) {
+ this.newDocument = newDocument;
+ this.oldDocument = oldDocument;
+ this.oldFile = oldFile;
+ this.label = label;
+ }
+
+ public TextFileChange createChange() throws Exception {
+ TextFileChange change = new TextFileChange(label, oldFile);
+ // change.setSaveMode(TextFileChange.FORCE_SAVE);
+ change.setEdit(new MultiTextEdit());
+ Object leftSide = new LineComparator(oldDocument);
+ Object rightSide = new LineComparator(newDocument);
+
+ RangeDifference[] differences = RangeDifferencer.findDifferences((IRangeComparator) leftSide, (IRangeComparator) rightSide);
+ int insertOffset = 0;
+ for(int i = 0; i < differences.length; i++ ) {
+ RangeDifference curr = differences[i];
+ int startLine = 0;
+ // when comparing 2 files, only RangeDifference.CHANGE is possible, no need to test
+ if (curr.rightLength() == curr.leftLength()) {
+ // replace
+ startLine = curr.rightStart();
+ int endLine = curr.rightEnd() - 1;
+ for(int j = startLine; j <= endLine; j++ ) {
+ int newPos = curr.leftStart() - startLine + j;
+ String newText = newDocument.get(newDocument.getLineOffset(newPos), newDocument.getLineLength(newPos));
+ addEdit(change, startLine, new ReplaceEdit(oldDocument.getLineOffset(j), oldDocument.getLineLength(j), newText));
+ }
+ } else if (curr.rightLength() > 0 && curr.leftLength() == 0) {
+ // insert
+ startLine = curr.rightStart();
+ int endLine = curr.rightEnd() - 1;
+ int posInsert = oldDocument.getLineOffset(curr.leftStart());
+ String newText = ""; //$NON-NLS-1$
+ for(int j = startLine; j <= endLine; j++ ) {
+ int newPos = curr.leftStart() - startLine + j + insertOffset;
+ newText += newDocument.get(newDocument.getLineOffset(newPos), newDocument.getLineLength(newPos));
+ }
+ if(newText.length() > 0){
+ addEdit(change, startLine, new InsertEdit(posInsert, newText));
+ }
+ insertOffset += curr.rightEnd() - curr.rightStart();
+ } else if (curr.leftLength() > 0 && curr.rightLength() == 0) {
+ // delete
+ startLine = curr.leftStart();
+ int endLine = curr.leftEnd() - 1;
+ int startOffset = oldDocument.getLineOffset(startLine);
+ int endOffset = 0;
+ for(int j = startLine; j <= endLine; j++ ) {
+ endOffset += oldDocument.getLineLength(j);
+ }
+ addEdit(change, startLine, new DeleteEdit(startOffset, endOffset));
+ insertOffset -= (curr.leftEnd() - curr.leftStart());
+ } else {
+ // unhandled
+ }
+ }
+ return change;
+ }
+
+ private void addEdit(TextFileChange change, int startLine, TextEdit edit) {
+ change.addTextEditGroup(new TextEditGroup("Line " + (startLine + 1), edit));
+ change.addEdit(edit);
+ }
+
+ public static class LineComparator implements IRangeComparator {
+ private final IDocument document;
+ private final ArrayList<Integer> hashes;
+
+ /**
+ * Create a line comparator for the given document.
+ *
+ * @param document
+ */
+ public LineComparator(IDocument document) {
+ this.document = document;
+ this.hashes = new ArrayList<Integer>(Arrays.asList(new Integer[document.getNumberOfLines()]));
+ }
+
+ /*
+ * @see org.eclipse.compare.rangedifferencer.IRangeComparator#getRangeCount()
+ */
+ public int getRangeCount() {
+ return document.getNumberOfLines();
+ }
+
+ /*
+ * @see org.eclipse.compare.rangedifferencer.IRangeComparator#rangesEqual(int, org.eclipse.compare.rangedifferencer.IRangeComparator, int)
+ */
+ public boolean rangesEqual(int thisIndex, IRangeComparator other, int otherIndex) {
+ try {
+ return getHash(thisIndex).equals(((LineComparator) other).getHash(otherIndex));
+ } catch (BadLocationException e) {
+ log.error("Problem comparing", e);
+ return false;
+ }
+ }
+
+ /*
+ * @see org.eclipse.compare.rangedifferencer.IRangeComparator#skipRangeComparison(int, int, org.eclipse.compare.rangedifferencer.IRangeComparator)
+ */
+ public boolean skipRangeComparison(int length, int maxLength, IRangeComparator other) {
+ return false;
+ }
+
+ /**
+ * @param line the number of the line in the document to get the hash for
+ * @return the hash of the line
+ * @throws BadLocationException if the line number is invalid
+ */
+ private Integer getHash(int line) throws BadLocationException {
+ Integer hash = hashes.get(line);
+ if (hash == null) {
+ IRegion lineRegion;
+ lineRegion = document.getLineInformation(line);
+ String lineContents= document.get(lineRegion.getOffset(), lineRegion.getLength());
+ hash = new Integer(computeDJBHash(lineContents));
+ hashes.set(line, hash);
+ }
+ return hash;
+ }
+
+ /**
+ * Compute a hash using the DJB hash algorithm
+ *
+ * @param string the string for which to compute a hash
+ * @return the DJB hash value of the string
+ */
+ private int computeDJBHash(String string) {
+ int hash = 5381;
+ int len = string.length();
+ for (int i = 0; i < len; i++) {
+ hash = (hash << 5) + hash + string.charAt(i);
+ }
+ return hash;
+ }
+ }
+
+}
diff --git a/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/editing/PomEdits.java b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/editing/PomEdits.java
index 5647598..46d829d 100644
--- a/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/editing/PomEdits.java
+++ b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/editing/PomEdits.java
@@ -16,12 +16,6 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
-import org.w3c.dom.Node;
-import org.w3c.dom.NodeList;
-import org.w3c.dom.Text;
-
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.text.DocumentRewriteSession;
@@ -34,6 +28,11 @@ import org.eclipse.wst.sse.core.internal.undo.IStructuredTextUndoManager;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel;
import org.eclipse.wst.xml.core.internal.provisional.format.FormatProcessorXML;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.Text;
/**
* this class contains tools for editing the pom files using dom tree operations.
@@ -194,7 +193,21 @@ public class PomEdits {
newElement.appendChild(doc.createTextNode(value));
return newElement;
}
-
+
+ /**
+ * helper method, creates a subelement
+ *
+ * @param parent the parent element
+ * @param name the name of the new element
+ * @return the created element
+ */
+ public static Element createElement(Element parent, String name) {
+ Document doc = parent.getOwnerDocument();
+ Element newElement = doc.createElement(name);
+ parent.appendChild(newElement);
+ return newElement;
+ }
+
/**
* sets text value to the given element. any existing text children are removed and replaced by this new one.
* @param element
@@ -467,7 +480,7 @@ public class PomEdits {
*
*/
public static interface Operation {
- void process(Document document);
+ void process(Document document);
}
/**
@@ -488,7 +501,6 @@ public class PomEdits {
oper.process(document);
}
}
-
}
/**
diff --git a/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/editing/PomHelper.java b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/editing/PomHelper.java
new file mode 100644
index 0000000..12bb6fe
--- /dev/null
+++ b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/editing/PomHelper.java
@@ -0,0 +1,84 @@
+/*******************************************************************************
+ * Copyright (c) 2011 Sonatype, Inc.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *******************************************************************************/
+
+package org.eclipse.m2e.core.ui.internal.editing;
+
+import static org.eclipse.m2e.core.ui.internal.editing.PomEdits.childEquals;
+import static org.eclipse.m2e.core.ui.internal.editing.PomEdits.findChild;
+import static org.eclipse.m2e.core.ui.internal.editing.PomEdits.performOnDOMDocument;
+
+import org.apache.maven.model.Dependency;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.ltk.core.refactoring.TextFileChange;
+import org.eclipse.m2e.core.ui.internal.M2EUIPluginActivator;
+import org.eclipse.m2e.core.ui.internal.editing.PomEdits.Operation;
+import org.eclipse.m2e.core.ui.internal.editing.PomEdits.OperationTuple;
+import org.eclipse.wst.sse.core.StructuredModelManager;
+import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;
+import org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+
+public final class PomHelper {
+
+ private static final Logger LOG = LoggerFactory.getLogger(PomHelper.class);
+
+ public static final String DEPENDENCIES = "dependencies"; //$NON-NLS-1$
+
+ public static final String GROUP_ID = "groupId";//$NON-NLS-1$
+
+ public static final String ARTIFACT_ID = "artifactId"; //$NON-NLS-1$
+
+ public static final String DEPENDENCY = "dependency"; //$NON-NLS-1$
+
+ public static final String EXCLUSIONS = "exclusions"; //$NON-NLS-1$
+
+ public static final String EXCLUSION = "exclusion"; //$NON-NLS-1$
+
+ public static final String VERSION = "version"; //$NON-NLS-1$
+
+ /*
+ * Return the Element matching the dependency or null.
+ */
+ public static Element findDependency(Document document, Dependency dependency) {
+ Element dependenciesElement = findChild(document.getDocumentElement(), DEPENDENCIES);
+ return findChild(dependenciesElement, DEPENDENCY, childEquals(GROUP_ID, dependency.getGroupId()),
+ childEquals(ARTIFACT_ID, dependency.getArtifactId()));
+ }
+
+ @SuppressWarnings("restriction")
+ public static TextFileChange createChange(IFile file, Operation operation, String label) throws CoreException {
+ IStructuredModel model = null;
+ try {
+ model = StructuredModelManager.getModelManager().getModelForRead(file);
+ IDocument document = model.getStructuredDocument();
+ IStructuredModel tempModel = StructuredModelManager.getModelManager().createUnManagedStructuredModelFor(
+ "org.eclipse.m2e.core.pomFile");
+ tempModel.getStructuredDocument().setText(StructuredModelManager.getModelManager(), document.get());
+ IDocument tempDocument = tempModel.getStructuredDocument();
+ performOnDOMDocument(new OperationTuple((IDOMModel) tempModel, operation));
+
+ return new ChangeCreator(file, document, tempDocument, label).createChange();
+ } catch(Exception exc) {
+ LOG.error("An error occurred creating change", exc);
+ throw new CoreException(new Status(IStatus.ERROR, M2EUIPluginActivator.PLUGIN_ID,
+ "An error occurred creating change", exc));
+ } finally {
+ if(model != null) {
+ model.releaseFromRead();
+ }
+ }
+ }
+}
diff --git a/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/editing/RemoveDependencyOperation.java b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/editing/RemoveDependencyOperation.java
new file mode 100644
index 0000000..9ca8eab
--- /dev/null
+++ b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/editing/RemoveDependencyOperation.java
@@ -0,0 +1,40 @@
+/*******************************************************************************
+ * Copyright (c) 2008 Sonatype, Inc.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *******************************************************************************/
+
+package org.eclipse.m2e.core.ui.internal.editing;
+
+import org.apache.maven.model.Dependency;
+import org.eclipse.m2e.core.ui.internal.editing.PomEdits.Operation;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+public class RemoveDependencyOperation implements Operation {
+
+ private Dependency dependency;
+
+ public RemoveDependencyOperation(Dependency dependency) {
+ this.dependency = dependency;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.m2e.core.ui.internal.editing.PomEdits.Operation#process(org.w3c.dom.Document)
+ */
+ public void process(Document document) {
+ Element dependencyElement = PomHelper.findDependency(document, dependency);
+ if(dependencyElement == null) {
+ throw new IllegalArgumentException("Dependency does not exist in pom");
+ }
+ Element dependencies = PomEdits.findChild(document.getDocumentElement(), PomHelper.DEPENDENCIES);
+ PomEdits.removeChild(dependencies, dependencyElement);
+ // Remove dependencies element if it is empty
+
+ if(PomEdits.findDependencies(document.getDocumentElement()).isEmpty()) {
+ PomEdits.removeChild(document.getDocumentElement(), dependencies);
+ }
+ }
+}
diff --git a/org.eclipse.m2e.refactoring/src/org/eclipse/m2e/refactoring/AbstractPomHeirarchyRefactoring.java b/org.eclipse.m2e.refactoring/src/org/eclipse/m2e/refactoring/AbstractPomHeirarchyRefactoring.java
index e667960..4afbb7c 100644
--- a/org.eclipse.m2e.refactoring/src/org/eclipse/m2e/refactoring/AbstractPomHeirarchyRefactoring.java
+++ b/org.eclipse.m2e.refactoring/src/org/eclipse/m2e/refactoring/AbstractPomHeirarchyRefactoring.java
@@ -10,77 +10,43 @@
*******************************************************************************/
package org.eclipse.m2e.refactoring;
-import java.io.IOException;
import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
-import java.util.Map;
import org.apache.maven.execution.MavenExecutionRequest;
import org.apache.maven.project.MavenProject;
import org.eclipse.core.resources.IFile;
-import org.eclipse.core.resources.IResource;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
-import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubMonitor;
-import org.eclipse.emf.common.command.BasicCommandStack;
-import org.eclipse.emf.common.command.Command;
-import org.eclipse.emf.common.notify.impl.AdapterFactoryImpl;
-import org.eclipse.emf.ecore.resource.Resource;
-import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
-import org.eclipse.emf.edit.domain.EditingDomain;
-import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
-import org.eclipse.emf.edit.provider.ReflectiveItemProviderAdapterFactory;
-import org.eclipse.emf.edit.provider.resource.ResourceItemProviderAdapterFactory;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.CompositeChange;
import org.eclipse.ltk.core.refactoring.Refactoring;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.ltk.core.refactoring.RefactoringStatusEntry;
-import org.eclipse.ltk.core.refactoring.resource.ResourceChange;
import org.eclipse.m2e.core.MavenPlugin;
import org.eclipse.m2e.core.embedder.IMaven;
import org.eclipse.m2e.core.project.IMavenProjectFacade;
import org.eclipse.m2e.core.project.MavenProjectManager;
-import org.eclipse.m2e.editor.pom.MavenPomEditor;
-import org.eclipse.m2e.model.edit.pom.Model;
-import org.eclipse.m2e.model.edit.pom.util.PomResourceImpl;
-import org.eclipse.swt.widgets.Display;
-import org.eclipse.ui.IEditorPart;
-import org.eclipse.ui.IEditorReference;
-import org.eclipse.ui.IWorkbenchPage;
-import org.eclipse.ui.IWorkbenchWindow;
-import org.eclipse.ui.PlatformUI;
public abstract class AbstractPomHeirarchyRefactoring extends Refactoring {
public static final String PLUGIN_ID = "org.eclipse.m2e.refactoring";
- protected IMavenProjectFacade projectFacade;
-
- protected EditingDomain editingDomain;
-
- protected Model model;
-
protected IFile file;
- protected List<MavenProject> hierarchy;
+ protected List<IFile> hierarchy;
- protected List<MavenProject> targets;
+ protected List<IFile> targets;
- private Map<IFile, Model> modelCache = new HashMap<IFile, Model>();
-
- public AbstractPomHeirarchyRefactoring(IMavenProjectFacade projectFacade, Model model, EditingDomain editingDomain, IFile file) {
- this.editingDomain = editingDomain;
+ public AbstractPomHeirarchyRefactoring(IFile file) {
this.file = file;
- this.model = model;
- this.projectFacade = projectFacade;
- modelCache.put(file, model);
}
/*
@@ -99,39 +65,37 @@ public abstract class AbstractPomHeirarchyRefactoring extends Refactoring {
protected abstract RefactoringStatusEntry[] isReady(IProgressMonitor pm);
/*
- * Is the project a target for this refactoring
+ * Is the pom a target for this refactoring
*/
- protected abstract boolean isChanged(EditingDomain editingDomain, MavenProject project, IProgressMonitor pm)
- throws CoreException, OperationCanceledException, IOException;
+ protected abstract boolean isAffected(IFile pom, IProgressMonitor monitor) throws CoreException;
/*
* Change associated with the MavenProject
*/
- protected abstract Change getChange(MavenProject project, IProgressMonitor pm) throws CoreException;
+ protected abstract Change getChange(IFile file, IProgressMonitor pm) throws CoreException;
/* (non-Javadoc)
* @see org.eclipse.ltk.core.refactoring.Refactoring#checkInitialConditions(org.eclipse.core.runtime.IProgressMonitor)
*/
public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException, OperationCanceledException {
- SubMonitor monitor = SubMonitor.convert(pm, 103);
+ SubMonitor monitor = SubMonitor.convert(pm, 104);
try {
checkInitial(monitor.newChild(1));
RefactoringStatus status = new RefactoringStatus();
- if(model == null && file == null) {
+ if(file == null) {
status.addEntry(new RefactoringStatusEntry(RefactoringStatus.FATAL,
Messages.AbstractPomHeirarchyRefactoring_noModelOrPom));
return status;
}
- loadWorkspaceAncestors(monitor.newChild(1));
+ gatherHeirarchy(monitor.newChild(1));
if(monitor.isCanceled()) {
throw new OperationCanceledException();
}
- targets = new ArrayList<MavenProject>();
- for(MavenProject project : hierarchy) {
- IMavenProjectFacade facade = getMavenProjectFacade(project);
- if(isChanged(getEditingDomain(facade), project, monitor.newChild(100 / hierarchy.size()))) {
- targets.add(project);
+ targets = new ArrayList<IFile>();
+ for(IFile pom : hierarchy) {
+ if(isAffected(pom, monitor.newChild(100 / hierarchy.size()))) {
+ targets.add(pom);
}
if(monitor.isCanceled()) {
throw new OperationCanceledException();
@@ -145,10 +109,6 @@ public abstract class AbstractPomHeirarchyRefactoring extends Refactoring {
status.addEntry(entry);
}
return status;
- } catch(IOException e) {
- throw new CoreException(new Status(IStatus.ERROR, PLUGIN_ID,
- Messages.AbstractPomHeirarchyRefactoring_failedToLoadModel,
- e));
} finally {
monitor.done();
}
@@ -170,8 +130,8 @@ public abstract class AbstractPomHeirarchyRefactoring extends Refactoring {
SubMonitor monitor = SubMonitor.convert(pm, targets.size() * 2);
try {
- for(MavenProject project : targets) {
- Change change = getChange(project, monitor.newChild(1));
+ for(IFile file : targets) {
+ Change change = getChange(file, pm);
if(change != null) {
compositeChange.add(change);
}
@@ -185,66 +145,8 @@ public abstract class AbstractPomHeirarchyRefactoring extends Refactoring {
}
}
- /*
- * Get the EditingDomain for a given project
- */
- protected EditingDomain getEditingDomain(IMavenProjectFacade facade) {
- if(facade.getMavenProject().equals(hierarchy.get(0)) && editingDomain != null) {
- return editingDomain;
- }
- // Check if an editor is open
- MavenPomEditor editor = getOpenEditor(facade);
- if(editor != null) {
- return editor.getEditingDomain();
- }
- // Create a fake one
- List<AdapterFactoryImpl> factories = new ArrayList<AdapterFactoryImpl>();
- factories.add(new ResourceItemProviderAdapterFactory());
- factories.add(new ReflectiveItemProviderAdapterFactory());
-
- ComposedAdapterFactory adapterFactory = new ComposedAdapterFactory(factories);
- BasicCommandStack commandStack = new BasicCommandStack();
- return new AdapterFactoryEditingDomain(adapterFactory, //
- commandStack, new HashMap<Resource, Boolean>());
- }
-
- private MavenPomEditor getOpenEditor(IMavenProjectFacade facade) {
- for(IWorkbenchWindow window : PlatformUI.getWorkbench().getWorkbenchWindows()) {
- for(IWorkbenchPage page : window.getPages()) {
- for(IEditorReference editor : page.getEditorReferences()) {
- if(MavenPomEditor.EDITOR_ID.equals(editor.getId())) {
- IEditorPart part = editor.getEditor(false);
- if(part instanceof MavenPomEditor) {
- MavenPomEditor mpe = (MavenPomEditor) part;
- if(facade.getPom().equals(mpe.getPomFile())) {
- return mpe;
- }
- }
- }
- }
- }
- }
- return null;
- }
-
- /*
- * Get the model associated with the Project
- */
- protected Model getModel(MavenProject mavenProject) throws CoreException, IOException {
- IFile pomFile = getMavenProjectFacade(mavenProject).getPom();
- Model m = modelCache.get(pomFile);
- if(m == null) {
- MavenPomEditor editor = getOpenEditor(getMavenProjectFacade(mavenProject));
- if(editor != null) {
- m = editor.readProjectDocument();
- } else {
- PomResourceImpl resource = MavenPlugin.getDefault().getMavenModelManager().loadResource(pomFile);
- resource.load(Collections.EMPTY_MAP);
- m = resource.getModel();
- }
- modelCache.put(pomFile, m);
- }
- return m;
+ protected IMavenProjectFacade getMavenProjectFacade(IFile pom) {
+ return MavenPlugin.getDefault().getMavenProjectManager().create(pom, true, new NullProgressMonitor());
}
protected IMavenProjectFacade getMavenProjectFacade(MavenProject mavenProject) {
@@ -252,38 +154,24 @@ public abstract class AbstractPomHeirarchyRefactoring extends Refactoring {
.getMavenProject(mavenProject.getGroupId(), mavenProject.getArtifactId(), mavenProject.getVersion());
}
- protected IFile getPomFile(MavenProject project) {
- IMavenProjectFacade facade = getMavenProjectFacade(project);
- if(facade.equals(projectFacade)) {
- return file;
- }
- return facade.getPom();
- }
-
/*
* Get the heirarchy of parents that exist in the workspace
*/
- private List<MavenProject> loadWorkspaceAncestors(IProgressMonitor progressMonitor) throws CoreException {
- SubMonitor monitor = SubMonitor.convert(progressMonitor);
+ private List<IFile> gatherHeirarchy(IProgressMonitor progressMonitor) throws CoreException {
+ SubMonitor monitor = SubMonitor.convert(progressMonitor, 3);
try {
IMaven maven = MavenPlugin.getDefault().getMaven();
MavenProjectManager projectManager = MavenPlugin.getDefault().getMavenProjectManager();
- MavenProject project = projectFacade.getMavenProject();
+ IMavenProjectFacade projectFacade = getMavenProjectFacade(file);
+ MavenProject project = projectFacade.getMavenProject(monitor.newChild(1));
maven.detachFromSession(project);
- hierarchy = new LinkedList<MavenProject>();
- hierarchy.add(project);
- while(project.getModel().getParent() != null) {
- if(monitor.isCanceled()) {
- return null;
- }
- MavenExecutionRequest request = projectManager.createExecutionRequest(projectFacade, monitor);
- project = maven.resolveParentProject(request, project, monitor);
- if(getMavenProjectFacade(project) != null) {
- hierarchy.add(project);
- }
- }
+ hierarchy = new LinkedList<IFile>();
+ hierarchy.add(file);
+
+ gatherDescendants(projectFacade, projectManager, monitor);
+ gatherAncestors(projectFacade, projectManager, monitor);
return hierarchy;
} finally {
@@ -291,66 +179,39 @@ public abstract class AbstractPomHeirarchyRefactoring extends Refactoring {
}
}
- /*
- * Wraps a {@link org.eclipse.emf.common.command.Command} to the pom in a Resource
- */
- protected static class PomResourceChange extends ResourceChange {
- private Command command;
-
- private IFile pom;
-
- private EditingDomain domain;
-
- private PomResourceChange redo;
-
- private String name;
-
- private PomResourceChange(PomResourceChange redo, EditingDomain domain, Command command, IFile pom, String name) {
- this(domain, command, pom, name);
- this.redo = redo;
- }
-
- public PomResourceChange(EditingDomain domain, Command command, IFile pom, String changes) {
- this.command = command;
- this.domain = domain;
- this.pom = pom;
- this.name = pom.getFullPath().toString() + " - " + changes;
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.ltk.core.refactoring.resource.ResourceChange#getModifiedResource()
- */
- protected IResource getModifiedResource() {
- return pom;
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.ltk.core.refactoring.Change#getName()
- */
- public String getName() {
- return name;
+ private void gatherAncestors(IMavenProjectFacade projectFacade, MavenProjectManager projectManager,
+ IProgressMonitor pm)
+ throws CoreException {
+ MavenExecutionRequest request = projectManager.createExecutionRequest(projectFacade, pm);
+ MavenProject project = MavenPlugin.getDefault().getMaven()
+ .resolveParentProject(request, projectFacade.getMavenProject(pm), pm);
+ pm.worked(1);
+ IMavenProjectFacade parentFacade = getMavenProjectFacade(project);
+ if(parentFacade != null) {
+ hierarchy.add(parentFacade.getPom());
+ gatherDescendants(parentFacade, projectManager, pm);
+ gatherAncestors(parentFacade, projectManager, pm);
}
+ }
- /* (non-Javadoc)
- * @see org.eclipse.ltk.core.refactoring.Change#perform(org.eclipse.core.runtime.IProgressMonitor)
- */
- public Change perform(IProgressMonitor pm) throws CoreException {
- SubMonitor monitor = SubMonitor.convert(pm, 3);
- try {
- Display.getDefault().syncExec(new Runnable() {
- public void run() {
- domain.getCommandStack().execute(command);
+ private void gatherDescendants(IMavenProjectFacade projectFacade, MavenProjectManager projectManager,
+ IProgressMonitor pm) throws CoreException {
+
+ for(String module : projectFacade.getMavenProjectModules()) {
+ IPath modulePath = projectFacade.getProject().getFullPath().append(module);
+ modulePath = ResourcesPlugin.getWorkspace().getRoot().getLocation().append(modulePath);
+ ResourcesPlugin.getWorkspace().getRoot().getContainerForLocation(modulePath);
+
+ for(IProject p : ResourcesPlugin.getWorkspace().getRoot().getProjects()) {
+ if(p.getLocation().equals(modulePath)) {
+ IMavenProjectFacade facade = projectManager.getProject(p);
+ if(facade != null && !hierarchy.contains(facade)) {
+ if(!hierarchy.contains(facade.getPom())) {
+ hierarchy.add(facade.getPom());
+ gatherDescendants(facade, projectManager, pm);
+ }
}
- });
- monitor.worked(2);
-
- if(redo == null && domain.getCommandStack().canUndo()) {
- redo = new PomResourceChange(this, domain, domain.getCommandStack().getUndoCommand(), pom, name);
}
- monitor.worked(1);
- return redo;
- } finally {
- monitor.done();
}
}
}
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 d23eb9e..490aabc 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 34ec2c8..1e95e8d 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