Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMilos Kleint2011-02-18 12:23:28 -0500
committerMilos Kleint2011-02-18 12:24:55 -0500
commite68022dc9bd22a8d03dca7ebedbf587630d6a0d8 (patch)
treeece1f62c9d1863814f071b637596b2e689c8a9b3
parent936a2a78cf096696689ce4876381e47cda9c87d9 (diff)
downloadm2e-core-e68022dc9bd22a8d03dca7ebedbf587630d6a0d8.tar.gz
m2e-core-e68022dc9bd22a8d03dca7ebedbf587630d6a0d8.tar.xz
m2e-core-e68022dc9bd22a8d03dca7ebedbf587630d6a0d8.zip
337186 have a simple refactoring for setting a version of transitive dependency
-rw-r--r--org.eclipse.m2e.refactoring/plugin.xml20
-rw-r--r--org.eclipse.m2e.refactoring/src/org/eclipse/m2e/refactoring/dependencyset/DependencySetAction.java117
-rw-r--r--org.eclipse.m2e.refactoring/src/org/eclipse/m2e/refactoring/dependencyset/DependencySetRefactoring.java153
-rw-r--r--org.eclipse.m2e.refactoring/src/org/eclipse/m2e/refactoring/dependencyset/MavenDependencySetWizard.java35
4 files changed, 318 insertions, 7 deletions
diff --git a/org.eclipse.m2e.refactoring/plugin.xml b/org.eclipse.m2e.refactoring/plugin.xml
index 651902a3..64f1fa54 100644
--- a/org.eclipse.m2e.refactoring/plugin.xml
+++ b/org.eclipse.m2e.refactoring/plugin.xml
@@ -49,27 +49,33 @@
label="%action.exclude1.label"
menubarPath="additions"/>
</objectContribution>
+
<objectContribution
- id="org.eclipse.m2e.refactoring.exclude.dependencyContributor"
- objectClass="org.apache.maven.shared.dependency.tree.DependencyNode"
+ id="org.eclipse.m2e.refactoring.exclude.dependencyContributorAether"
+ objectClass="org.sonatype.aether.graph.DependencyNode"
adaptable="true">
<action id="org.eclipse.m2e.projectMenu.action"
class="org.eclipse.m2e.refactoring.exclude.DependencyExcludeAction"
enablesFor="+"
icon="icons/exclude.gif"
- label="%action.exclude2.label"
+ label="%action.exclude3.label"
menubarPath="additions"/>
</objectContribution>
+
<objectContribution
- id="org.eclipse.m2e.refactoring.exclude.dependencyContributorAether"
+ id="org.eclipse.m2e.refactoring.set.dependencyContributorAether"
objectClass="org.sonatype.aether.graph.DependencyNode"
adaptable="true">
- <action id="org.eclipse.m2e.projectMenu.action"
- class="org.eclipse.m2e.refactoring.exclude.DependencyExcludeAction"
+ <action id="org.eclipse.m2e.projectMenu.depSetAction"
+ class="org.eclipse.m2e.refactoring.dependencyset.DependencySetAction"
enablesFor="+"
icon="icons/exclude.gif"
- label="%action.exclude3.label"
+ label="Lock Transitive Dependency Version..."
menubarPath="additions"/>
+ <enablement>
+ <test property="org.eclipse.m2e.isTransitiveDependencyTreeNode"/>
+ </enablement>
+
</objectContribution>
diff --git a/org.eclipse.m2e.refactoring/src/org/eclipse/m2e/refactoring/dependencyset/DependencySetAction.java b/org.eclipse.m2e.refactoring/src/org/eclipse/m2e/refactoring/dependencyset/DependencySetAction.java
new file mode 100644
index 00000000..3487d21c
--- /dev/null
+++ b/org.eclipse.m2e.refactoring/src/org/eclipse/m2e/refactoring/dependencyset/DependencySetAction.java
@@ -0,0 +1,117 @@
+/*******************************************************************************
+ * 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.dependencyset;
+
+import java.util.ArrayList;
+import java.util.List;
+
+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.embedder.ArtifactKey;
+import org.eclipse.m2e.core.ui.internal.actions.SelectionUtil;
+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 Milos Kleint
+ */
+@SuppressWarnings("restriction")
+public class DependencySetAction implements IActionDelegate {
+
+ public static final String ID = "org.eclipse.m2e.refactoring.DependencySet"; //$NON-NLS-1$
+
+ private IFile file;
+ private List<ArtifactKey> keys;
+
+ public void run(IAction action) {
+ if (keys != null && keys.size() > 0 && file != null) {
+ Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
+ MavenDependencySetWizard wizard = new MavenDependencySetWizard(file, keys);
+ 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;
+ keys = new ArrayList<ArtifactKey>();
+
+ if (selection instanceof IStructuredSelection) {
+ IStructuredSelection structuredSelection = (IStructuredSelection) selection;
+ for(Object selected : structuredSelection.toArray()) {
+ /*if (selected instanceof Artifact) {
+ file = getFileFromEditor();
+ keys.add(new ArtifactKey((Artifact) selected));
+
+ } else*/ if (selected instanceof org.sonatype.aether.graph.DependencyNode) {
+ file = getFileFromEditor();
+ org.sonatype.aether.graph.DependencyNode selected2 = (org.sonatype.aether.graph.DependencyNode) selected;
+ if (selected2.getData().get("LEVEL") == null) {
+ keys.add(new ArtifactKey(selected2.getDependency().getArtifact()));
+ }
+
+ } /*else if (selected instanceof RequiredProjectWrapper) {
+ RequiredProjectWrapper w = (RequiredProjectWrapper) selected;
+ file = getFileFromProject(w.getParentClassPathContainer().getJavaProject());
+ 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());
+ }
+
+ }
+ */
+ }
+ }
+
+ if (keys.size() > 0 && file != null) {
+ action.setEnabled(true);
+ } else {
+ action.setEnabled(false);
+ }
+ }
+
+ private IFile getFileFromProject(IJavaProject javaProject) {
+ return javaProject.getProject().getFile("pom.xml"); //$NON-NLS-1$
+ }
+
+ //mkleint: scary
+ 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/dependencyset/DependencySetRefactoring.java b/org.eclipse.m2e.refactoring/src/org/eclipse/m2e/refactoring/dependencyset/DependencySetRefactoring.java
new file mode 100644
index 00000000..651afba9
--- /dev/null
+++ b/org.eclipse.m2e.refactoring/src/org/eclipse/m2e/refactoring/dependencyset/DependencySetRefactoring.java
@@ -0,0 +1,153 @@
+/*******************************************************************************
+ * Copyright (c) 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.dependencyset;
+
+import static org.eclipse.m2e.core.ui.internal.editing.PomEdits.*;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.OperationCanceledException;
+import org.eclipse.jface.text.IDocument;
+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.m2e.core.embedder.ArtifactKey;
+import org.eclipse.m2e.core.ui.internal.editing.PomEdits.Operation;
+import org.eclipse.m2e.refactoring.ChangeCreator;
+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;
+
+/**
+ * @author mkleint
+ *
+ */
+public class DependencySetRefactoring extends Refactoring {
+
+ private static final Logger LOG = LoggerFactory.getLogger(DependencySetRefactoring.class);
+ private final IFile file;
+ private final List<ArtifactKey> keys;
+
+ /**
+ * @param file
+ * @param groupId
+ * @param artifactId
+ * @param version
+ */
+ public DependencySetRefactoring(IFile file, List<ArtifactKey> keys) {
+ this.file = file;
+ this.keys = keys;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.ltk.core.refactoring.Refactoring#getName()
+ */
+ public String getName() {
+ // TODO Auto-generated method stub
+ return "Set dependency version";
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.ltk.core.refactoring.Refactoring#checkInitialConditions(org.eclipse.core.runtime.IProgressMonitor)
+ */
+ public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException, OperationCanceledException {
+ return new RefactoringStatus();
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.ltk.core.refactoring.Refactoring#checkFinalConditions(org.eclipse.core.runtime.IProgressMonitor)
+ */
+ public RefactoringStatus checkFinalConditions(IProgressMonitor pm) throws CoreException, OperationCanceledException {
+ return new RefactoringStatus();
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.ltk.core.refactoring.Refactoring#createChange(org.eclipse.core.runtime.IProgressMonitor)
+ */
+ public Change createChange(IProgressMonitor pm) throws CoreException, OperationCanceledException {
+ CompositeChange res = new CompositeChange(getName());
+ 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();
+ List<Operation> operations = new ArrayList<Operation>();
+ for (ArtifactKey key : keys) {
+ operations.add(new OneDependency(key));
+ }
+ CompoundOperation compound = new CompoundOperation(operations.toArray(new Operation[0]));
+ performOnDOMDocument(new OperationTuple((IDOMModel) tempModel, compound));
+
+ ChangeCreator chc = new ChangeCreator(file, document, tempDocument, getName());
+ res.add(chc.createChange());
+ } catch(Exception exc) {
+ LOG.error("", exc);
+ } finally {
+ if(model != null) {
+ model.releaseFromRead();
+ }
+ }
+ return res;
+ }
+
+ private static class OneDependency implements Operation {
+
+ private final String groupId;
+ private final String artifactId;
+ private final String version;
+
+ public OneDependency(ArtifactKey key) {
+ this.groupId = key.getGroupId();
+ this.artifactId = key.getArtifactId();
+ this.version = key.getVersion();
+ }
+ /* (non-Javadoc)
+ * @see org.eclipse.m2e.core.ui.internal.editing.PomEdits.Operation#process(org.w3c.dom.Document)
+ */
+ public void process(Document document) {
+ //TODO handle activated profiles?
+ Element deps = getChild(document.getDocumentElement(), "dependencies");
+ Element existing = findChild(deps, "dependency", childEquals("groupId", groupId),
+ childEquals("artifactId", artifactId));
+ if(existing != null) {
+ //it's a direct dependency
+ //TODO check the version value.. not to overwrite the existing version..
+ //even better, have the action only available on transitive dependencies
+ setText(getChild(existing, "version"), version);
+ } else {
+ //is transitive dependency
+ Element dm = getChild(document.getDocumentElement(), "dependencyManagement", "dependencies");
+ existing = findChild(dm, "dependency", childEquals("groupId", groupId),
+ childEquals("artifactId", artifactId));
+ if(existing != null) {
+ setText(getChild(existing, "version"), version);
+ } else {
+ createDependency(dm, groupId, artifactId, version);
+ }
+ }
+
+ }
+
+ }
+
+}
diff --git a/org.eclipse.m2e.refactoring/src/org/eclipse/m2e/refactoring/dependencyset/MavenDependencySetWizard.java b/org.eclipse.m2e.refactoring/src/org/eclipse/m2e/refactoring/dependencyset/MavenDependencySetWizard.java
new file mode 100644
index 00000000..5f92957b
--- /dev/null
+++ b/org.eclipse.m2e.refactoring/src/org/eclipse/m2e/refactoring/dependencyset/MavenDependencySetWizard.java
@@ -0,0 +1,35 @@
+/*******************************************************************************
+ * 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.dependencyset;
+
+import java.util.List;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.ltk.ui.refactoring.RefactoringWizard;
+import org.eclipse.m2e.core.embedder.ArtifactKey;
+
+
+/**
+ * @author Milos Kleint
+ */
+public class MavenDependencySetWizard extends RefactoringWizard {
+
+ public MavenDependencySetWizard(IFile file, List<ArtifactKey> keys) {
+ super(new DependencySetRefactoring(file, keys), DIALOG_BASED_USER_INTERFACE);
+ }
+
+ @Override
+ protected void addUserInputPages() {
+ setDefaultPageTitle(getRefactoring().getName());
+ }
+
+}

Back to the top