Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9e9ff69c001de6be3e66911c064a1cd4165faae5 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*******************************************************************************
 * 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.actions;

import java.util.Collections;
import java.util.Set;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;

import org.eclipse.m2e.core.MavenPlugin;
import org.eclipse.m2e.core.core.IMavenConstants;
import org.eclipse.m2e.core.core.MavenLogger;
import org.eclipse.m2e.core.embedder.ArtifactKey;
import org.eclipse.m2e.core.embedder.ArtifactRef;
import org.eclipse.m2e.core.project.IMavenProjectFacade;
import org.eclipse.m2e.core.project.MavenProjectManager;

/**
 * 
 * MavenActionSupport
 *
 * @author Jason van Zyl
 */
public abstract class MavenActionSupport implements IObjectActionDelegate {
  protected IStructuredSelection selection;

  protected IWorkbenchPart targetPart;

  protected Set<ArtifactKey> getArtifacts(IFile file, MavenPlugin plugin) {
    try {
      MavenProjectManager projectManager = plugin.getMavenProjectManager();
      IMavenProjectFacade projectFacade = projectManager.create(file, true, new NullProgressMonitor());
      if(projectFacade != null) {
        return ArtifactRef.toArtifactKey(projectFacade.getMavenProjectArtifacts());
      }
    } catch(Exception ex) {
      String msg = "Can't read Maven project";
      MavenLogger.log(msg, ex);
      plugin.getConsole().logError(msg + "; " + ex.toString());
    }
    return Collections.emptySet();
  }

  public void setActivePart(IAction action, IWorkbenchPart targetPart) {
    this.targetPart = targetPart;
  }

  public void selectionChanged(IAction action, ISelection selection) {
    if(selection instanceof IStructuredSelection) {
      this.selection = (IStructuredSelection) selection;
    }
  }

  protected Shell getShell() {
    Shell shell = null;
    if(targetPart != null) {
      shell = targetPart.getSite().getShell();
    }
    if(shell != null) {
      return shell;
    }

    IWorkbench workbench = MavenPlugin.getDefault().getWorkbench();
    if(workbench == null) {
      return null;
    }

    IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
    return window == null ? null : window.getShell();
  }

  protected IFile getPomFileFromPomEditorOrViewSelection() {
    IFile file = null;    
    //
    // If I am in the POM editor I want to get hold of the IFile that is currently in the buffer
    //
    IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();

    if(window != null) {
      IWorkbenchPage page = window.getActivePage();
      if(page != null) {
        IEditorPart editor = page.getActiveEditor();
        if(editor != null) {
          IEditorInput input = editor.getEditorInput();
          if(input instanceof IFileEditorInput) {
            IFileEditorInput fileInput = (IFileEditorInput) input;
            file = fileInput.getFile();
            if(file.getName().equals(IMavenConstants.POM_FILE_NAME)) {
              return file;
            }
          }
        }
      }
    }    

    //
    // Otherwise we will assume a pom.xml file or IProject is being selected in the
    // package explorer and we'll get the IFile from that. Otherwise we'll bail.
    //
    Object o = selection.iterator().next();

    if(o instanceof IProject) {
      file = ((IProject) o).getFile(IMavenConstants.POM_FILE_NAME);
    } else if(o instanceof IFile) {
      file = (IFile) o;
    } else {
      file = null;
    }

    return file;
  }
}

Back to the top