Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPascal Rapicault2010-12-06 22:10:52 +0000
committerPascal Rapicault2010-12-06 22:10:52 +0000
commitfbb4f5902cade92cc1689e73209c60a6413a5ef7 (patch)
treeace0fb107bafe980fc14fd7f3429e5c9aac2d795 /org.eclipse.m2e.refactoring/src/org/eclipse/m2e/refactoring/exclude
parenta9c878c2624b33d8c74062717bf75302f0ae1fa7 (diff)
downloadm2e-core-fbb4f5902cade92cc1689e73209c60a6413a5ef7.tar.gz
m2e-core-fbb4f5902cade92cc1689e73209c60a6413a5ef7.tar.xz
m2e-core-fbb4f5902cade92cc1689e73209c60a6413a5ef7.zip
Initial commit at Eclipse
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.java112
-rw-r--r--org.eclipse.m2e.refactoring/src/org/eclipse/m2e/refactoring/exclude/ExcludeRefactoring.java195
-rw-r--r--org.eclipse.m2e.refactoring/src/org/eclipse/m2e/refactoring/exclude/MavenExcludeWizard.java32
3 files changed, 339 insertions, 0 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
new file mode 100644
index 00000000..54eaf0e9
--- /dev/null
+++ b/org.eclipse.m2e.refactoring/src/org/eclipse/m2e/refactoring/exclude/DependencyExcludeAction.java
@@ -0,0 +1,112 @@
+/*******************************************************************************
+ * 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.refactoring.exclude;
+
+import org.apache.maven.artifact.Artifact;
+import org.eclipse.core.resources.IFile;
+import org.eclipse.jdt.core.IJavaElement;
+import org.eclipse.jdt.core.IJavaProject;
+import org.eclipse.jdt.internal.ui.packageview.ClassPathContainer.RequiredProjectWrapper;
+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.actions.SelectionUtil;
+import org.eclipse.m2e.core.embedder.ArtifactKey;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.ui.IActionDelegate;
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.IFileEditorInput;
+import org.eclipse.ui.PlatformUI;
+
+/**
+ * This action is intended to be used in popup menus
+ *
+ * @author Anton Kraev
+ */
+@SuppressWarnings("restriction")
+public class DependencyExcludeAction implements IActionDelegate {
+
+ public static final String ID = "org.eclipse.m2e.refactoring.DependencyExclude"; //$NON-NLS-1$
+
+ private IFile file;
+ private ArtifactKey artifactKey;
+
+ public void run(IAction action) {
+ if (artifactKey != null && file != null) {
+ Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
+ MavenExcludeWizard wizard = new MavenExcludeWizard(file, //
+ artifactKey.getGroupId(), artifactKey.getArtifactId());
+ try {
+ String titleForFailedChecks = ""; //$NON-NLS-1$
+ RefactoringWizardOpenOperation op = new RefactoringWizardOpenOperation(wizard);
+ op.run(shell, titleForFailedChecks);
+ } catch(InterruptedException e) {
+ // XXX
+ }
+ }
+ }
+
+ public void selectionChanged(IAction action, ISelection selection) {
+ file = null;
+ artifactKey = null;
+
+ // TODO move logic into adapters
+ if (selection instanceof IStructuredSelection) {
+ IStructuredSelection structuredSelection = (IStructuredSelection) selection;
+ if(structuredSelection.size()==1) {
+ Object selected = structuredSelection.getFirstElement();
+ if (selected instanceof Artifact) {
+ file = getFileFromEditor();
+ artifactKey = new ArtifactKey((Artifact) selected);
+
+ } else if (selected instanceof org.sonatype.aether.graph.DependencyNode) {
+ file = getFileFromEditor();
+ artifactKey = new ArtifactKey(((org.sonatype.aether.graph.DependencyNode) selected).getDependency().getArtifact());
+
+ } else if (selected instanceof RequiredProjectWrapper) {
+ RequiredProjectWrapper w = (RequiredProjectWrapper) selected;
+ file = getFileFromProject(w.getParentClassPathContainer().getJavaProject());
+ artifactKey = SelectionUtil.getType(selected, ArtifactKey.class);
+
+ } else {
+ artifactKey = SelectionUtil.getType(selected, ArtifactKey.class);
+ if (selected instanceof IJavaElement) {
+ IJavaElement el = (IJavaElement) selected;
+ file = getFileFromProject(el.getParent().getJavaProject());
+ }
+
+ }
+ }
+ }
+
+ if (artifactKey != null && file != null) {
+ action.setEnabled(true);
+ } else {
+ action.setEnabled(false);
+ }
+ }
+
+ private IFile getFileFromProject(IJavaProject javaProject) {
+ return javaProject.getProject().getFile("pom.xml"); //$NON-NLS-1$
+ }
+
+ private IFile getFileFromEditor() {
+ IEditorPart part = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
+ if (part != null && part.getEditorInput() instanceof IFileEditorInput) {
+ IFileEditorInput input = (IFileEditorInput) part.getEditorInput();
+ return input.getFile();
+ }
+ return null;
+ }
+
+}
diff --git a/org.eclipse.m2e.refactoring/src/org/eclipse/m2e/refactoring/exclude/ExcludeRefactoring.java b/org.eclipse.m2e.refactoring/src/org/eclipse/m2e/refactoring/exclude/ExcludeRefactoring.java
new file mode 100644
index 00000000..a0524178
--- /dev/null
+++ b/org.eclipse.m2e.refactoring/src/org/eclipse/m2e/refactoring/exclude/ExcludeRefactoring.java
@@ -0,0 +1,195 @@
+/*******************************************************************************
+ * 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.refactoring.exclude;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+
+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.emf.common.command.CompoundCommand;
+import org.eclipse.emf.edit.command.AddCommand;
+import org.eclipse.emf.edit.command.RemoveCommand;
+import org.eclipse.ltk.core.refactoring.RefactoringStatus;
+import org.eclipse.m2e.core.MavenPlugin;
+import org.eclipse.m2e.core.core.IMavenConstants;
+import org.eclipse.m2e.core.embedder.MavenModelManager;
+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.impl.PomFactoryImpl;
+import org.eclipse.m2e.refactoring.AbstractPomRefactoring;
+import org.eclipse.m2e.refactoring.Messages;
+import org.eclipse.m2e.refactoring.PomRefactoringException;
+import org.eclipse.m2e.refactoring.PomVisitor;
+import org.eclipse.m2e.refactoring.RefactoringModelResources;
+import org.eclipse.osgi.util.NLS;
+import org.sonatype.aether.artifact.Artifact;
+import org.sonatype.aether.graph.DependencyNode;
+import org.sonatype.aether.graph.DependencyVisitor;
+import org.sonatype.aether.util.artifact.JavaScopes;
+
+
+/**
+ * Exclude artifact refactoring implementation
+ *
+ * @author Anton Kraev
+ */
+public class ExcludeRefactoring extends AbstractPomRefactoring {
+
+ private String excludedArtifactId;
+
+ private String excludedGroupId;
+
+ /**
+ * @param file
+ */
+ public ExcludeRefactoring(IFile file, String excludedGroupId, String excludedArtifactId) {
+ super(file);
+ this.excludedGroupId = excludedGroupId;
+ this.excludedArtifactId = excludedArtifactId;
+ }
+
+ public PomVisitor getVisitor() {
+ return new PomVisitor() {
+
+ public CompoundCommand applyChanges(RefactoringModelResources resources, IProgressMonitor pm)
+ throws CoreException, IOException {
+ final CompoundCommand command = new CompoundCommand();
+
+ final List<Dependency> toRemove = new ArrayList<Dependency>();
+
+ Model model = resources.getTmpModel();
+
+ final List<Dependency> deps = model.getDependencies();
+
+ final IStatus[] status = new IStatus[] {null};
+
+ pm.beginTask(Messages.ExcludeRefactoring_task_loading, 1);
+ MavenModelManager modelManager = MavenPlugin.getDefault().getMavenModelManager();
+ DependencyNode root = modelManager.readDependencyTree(resources.getPomFile(), JavaScopes.TEST, pm);
+ pm.worked(1);
+ root.accept(new DependencyVisitor() {
+
+ private Dependency findDependency(String groupId, String artifactId) {
+ for(Dependency d : deps) {
+ if(d.getGroupId().equals(groupId) && d.getArtifactId().equals(artifactId)) {
+ return d;
+ }
+ }
+ return null;
+ }
+
+ private Dependency findDependency(DependencyNode node) {
+ Artifact artifact;
+ if(node.getRelocations().isEmpty()) {
+ artifact = node.getDependency().getArtifact();
+ } else {
+ artifact = node.getRelocations().get(0);
+ }
+ return findDependency(artifact.getGroupId(), artifact.getArtifactId());
+ }
+
+ private int depth;
+
+ private DependencyNode topLevel;
+
+ private Set<Dependency> excluded = new HashSet<Dependency>();
+
+ public boolean visitLeave(DependencyNode node) {
+ depth-- ;
+ return status[0] == null;
+ }
+
+ public boolean visitEnter(DependencyNode node) {
+ if(depth == 1) {
+ topLevel = node;
+ }
+ depth++ ;
+
+ if(node.getDependency() != null) {
+ Artifact a = node.getDependency().getArtifact();
+ if(a.getGroupId().equals(excludedGroupId) && a.getArtifactId().equals(excludedArtifactId)) {
+ if(topLevel == null) {
+ // do not touch itself
+ } else if(node == topLevel) {
+ // need to remove top-level dependency
+ toRemove.add(findDependency(topLevel));
+ } else {
+ // need to add exclusion to top-level dependency
+ 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()));
+ }
+ if(excluded.add(dependency)) {
+ addExclusion(command, dependency);
+ }
+ }
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ });
+
+ if(status[0] != null) {
+ throw new PomRefactoringException(status[0]);
+ }
+
+ for(Iterator<Dependency> rem = toRemove.iterator(); rem.hasNext();) {
+ command.append(new RemoveCommand(editingDomain, model.getDependencies(), rem.next()));
+ }
+
+ // XXX scan management as well
+
+ return command;
+ }
+
+ private void addExclusion(CompoundCommand command, Dependency dep) {
+ Exclusion exclusion = PomFactoryImpl.eINSTANCE.createExclusion();
+ exclusion.setArtifactId(excludedArtifactId);
+ exclusion.setGroupId(excludedGroupId);
+ command.append(new AddCommand(editingDomain, dep.getExclusions(), exclusion));
+ }
+ };
+ }
+
+ public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException, OperationCanceledException {
+ return new RefactoringStatus();
+ }
+
+ public String getName() {
+ return Messages.ExcludeRefactoring_name;
+ }
+
+ public String getTitle() {
+ return NLS.bind(Messages.ExcludeRefactoring_title, new Object[] {excludedGroupId, excludedArtifactId, file.getParent().getName()});
+ }
+
+ public boolean scanAllArtifacts() {
+ //do not scan other artifacts
+ return false;
+ }
+
+}
diff --git a/org.eclipse.m2e.refactoring/src/org/eclipse/m2e/refactoring/exclude/MavenExcludeWizard.java b/org.eclipse.m2e.refactoring/src/org/eclipse/m2e/refactoring/exclude/MavenExcludeWizard.java
new file mode 100644
index 00000000..8bfd857a
--- /dev/null
+++ b/org.eclipse.m2e.refactoring/src/org/eclipse/m2e/refactoring/exclude/MavenExcludeWizard.java
@@ -0,0 +1,32 @@
+/*******************************************************************************
+ * 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.refactoring.exclude;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.ltk.ui.refactoring.RefactoringWizard;
+
+
+/**
+ * @author Anton Kraev
+ */
+public class MavenExcludeWizard extends RefactoringWizard {
+
+ public MavenExcludeWizard(IFile file, String excludedGroupId, String excludedArtifactId) {
+ super(new ExcludeRefactoring(file, excludedGroupId, excludedArtifactId), DIALOG_BASED_USER_INTERFACE);
+ }
+
+ @Override
+ protected void addUserInputPages() {
+ setDefaultPageTitle(getRefactoring().getName());
+ }
+
+}

Back to the top