Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3a097b6ff98fb2a1c3f70db3e299b2ad3b4d72c3 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*******************************************************************************
 * Copyright (c) 2008-2018 Sonatype, Inc. and others.
 * 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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExecutableExtension;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPart;

import org.eclipse.m2e.core.MavenPlugin;
import org.eclipse.m2e.core.internal.IMavenConstants;
import org.eclipse.m2e.core.project.IProjectConfigurationManager;
import org.eclipse.m2e.core.project.ResolverConfiguration;
import org.eclipse.m2e.core.ui.internal.M2EUIPluginActivator;
import org.eclipse.m2e.core.ui.internal.Messages;
import org.eclipse.m2e.core.ui.internal.wizards.MavenPomWizard;


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

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

  static final String ID_WORKSPACE = "org.eclipse.m2e.enableWorkspaceResolutionAction"; //$NON-NLS-1$

  static final String ID_MODULES = "org.eclipse.m2e.enableModulesAction"; //$NON-NLS-1$

  private boolean workspaceProjects = true;

  private ISelection selection;

  public EnableNatureAction() {
  }

  public EnableNatureAction(String option) {
    setInitializationData(null, null, option);
  }

  public void setInitializationData(IConfigurationElement config, String propertyName, Object data) {
    if(IMavenConstants.NO_WORKSPACE_PROJECTS.equals(data)) {
      this.workspaceProjects = false;
    }
  }

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

  public void setActivePart(IAction action, IWorkbenchPart targetPart) {
  }

  public void run(IAction action) {
    if(selection instanceof IStructuredSelection) {
      IStructuredSelection structuredSelection = (IStructuredSelection) selection;
      for(Iterator<?> it = structuredSelection.iterator(); it.hasNext();) {
        Object element = it.next();
        IProject project = null;
        if(element instanceof IProject) {
          project = (IProject) element;
        } else if(element instanceof IAdaptable) {
          project = ((IAdaptable) element).getAdapter(IProject.class);
        }
        if(project != null) {
          enableNature(project, structuredSelection.size() == 1);
        }
      }
    }
  }

  private void enableNature(final IProject project, boolean isSingle) {
    final M2EUIPluginActivator plugin = M2EUIPluginActivator.getDefault();
    IFile pom = project.getFile(IMavenConstants.POM_FILE_NAME);
    if(!pom.exists()) {
      if(isSingle) {
        // XXX move into AbstractProjectConfigurator and use Eclipse project settings
        IWorkbench workbench = plugin.getWorkbench();

        MavenPomWizard wizard = new MavenPomWizard();
        wizard.init(workbench, (IStructuredSelection) selection);

        Shell shell = workbench.getActiveWorkbenchWindow().getShell();
        WizardDialog wizardDialog = new WizardDialog(shell, wizard);
        wizardDialog.create();
        wizardDialog.getShell().setText(Messages.EnableNatureAction_wizard_shell);
        if(wizardDialog.open() == Window.CANCEL) {
          return;
        }
      } else {
        //if we have multiple selection and this project has no pom, just skip it.
        // do not enable maven nature for projects without pom.
        log.warn(NLS.bind(
            "Skipping project {0}, no pom.xml file present, no reason to have maven nature enabled", project.getName())); //$NON-NLS-1$
        return;
      }
    }
    Job job = new Job(Messages.EnableNatureAction_job_enable) {

      protected IStatus run(IProgressMonitor monitor) {
        try {
          ResolverConfiguration configuration = new ResolverConfiguration();
          configuration.setResolveWorkspaceProjects(workspaceProjects);
          configuration.setSelectedProfiles(""); //$NON-NLS-1$

          boolean hasMavenNature = project.hasNature(IMavenConstants.NATURE_ID);

          IProjectConfigurationManager configurationManager = MavenPlugin.getProjectConfigurationManager();

          configurationManager.enableMavenNature(project, configuration, monitor);

          if(!hasMavenNature) {
            configurationManager.updateProjectConfiguration(project, monitor);
          }
        } catch(CoreException ex) {
          log.error(ex.getMessage(), ex);
        }
        return Status.OK_STATUS;
      }
    };
    job.schedule();
  }
}

Back to the top