blob: 7dec05da34ac518f3b930ab8647b243267d726e8 [file] [log] [blame]
/*******************************************************************************
* 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 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Sonatype, Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.m2e.refactoring.dependencyset;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.resources.IFile;
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.swt.widgets.Shell;
import org.eclipse.ui.IActionDelegate;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.PlatformUI;
import org.eclipse.m2e.core.embedder.ArtifactKey;
/**
* This action is intended to be used in popup menus
*
* @author Milos Kleint
*/
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;
@Override
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) {
// do nothing
}
}
}
@Override
public void selectionChanged(IAction action, ISelection selection) {
file = null;
keys = new ArrayList<>();
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.eclipse.aether.graph.DependencyNode) {
file = getFileFromEditor();
org.eclipse.aether.graph.DependencyNode selected2 = (org.eclipse.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);
}
}
//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;
}
}