Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: df7d3eaeb697f0c6292f8f800983b15ca2395309 (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
/*******************************************************************************
 * 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.ui.internal.actions;

import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.m2e.core.MavenPlugin;
import org.eclipse.m2e.core.core.IMavenConstants;
import org.eclipse.m2e.core.ui.internal.UpdateConfigurationJob;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkingSet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class UpdateConfigurationAction implements IObjectActionDelegate {
  private static final Logger log = LoggerFactory.getLogger(UpdateConfigurationAction.class);

  public static final String ID = "org.eclipse.m2e.updateConfigurationAction"; //$NON-NLS-1$

  private IStructuredSelection selection;

  private Shell shell;

  public UpdateConfigurationAction() {
  }
  
  public UpdateConfigurationAction(Shell shell) {
    this.shell = shell;
  }

  public void setActivePart(IAction action, IWorkbenchPart targetPart) {
    if (targetPart != null) {
      shell = targetPart.getSite().getShell();
    }
  }

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

  public void run(IAction action) {
    final Set<IProject> projects = getProjects();
    final MavenPlugin plugin = MavenPlugin.getDefault();
    new UpdateConfigurationJob(plugin, projects.toArray(new IProject[projects.size()])).schedule();
  }

  private Set<IProject> getProjects() {
    Set<IProject> projects = new LinkedHashSet<IProject>();
    if(selection != null) {
      for(Iterator<?> it = selection.iterator(); it.hasNext();) {
        Object element = it.next();
        if(element instanceof IProject) {
          projects.add((IProject) element);
        } else if(element instanceof IWorkingSet) {
          IWorkingSet workingSet = (IWorkingSet) element;
          for(IAdaptable adaptable : workingSet.getElements()) {
            IProject project = (IProject) adaptable.getAdapter(IProject.class);
            try {
              if(project != null && project.isAccessible() && project.hasNature(IMavenConstants.NATURE_ID)) {
                projects.add(project);
              }
            } catch(CoreException ex) {
              log.error(ex.getMessage(), ex);
            }
          }
        } else if(element instanceof IAdaptable) {
          IProject project = (IProject) ((IAdaptable) element).getAdapter(IProject.class);
          if(project != null) {
            projects.add(project);
          }
        }
      }
    }
    return projects;
  }

}

Back to the top