Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 91aaaf2b8d88569dd4ae0a6840604b50a8e7faca (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*******************************************************************************
 * 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.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;

  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
      }
    }
  }

  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);
    }
  }

  //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;
  }
}

Back to the top