Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d960cc7c7d78a75146c011c5f40a4dbbea3ebff6 (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
152
153
154
155
/*******************************************************************************
 * 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.scm.internal.wizards;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IImportWizard;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;

import org.apache.maven.model.Dependency;

import org.eclipse.m2e.core.embedder.ArtifactKey;
import org.eclipse.m2e.core.project.ProjectImportConfiguration;
import org.eclipse.m2e.core.ui.internal.actions.SelectionUtil;
import org.eclipse.m2e.core.ui.internal.wizards.AbstractMavenProjectWizard;
import org.eclipse.m2e.core.ui.internal.wizards.MavenDependenciesWizardPage;
import org.eclipse.m2e.core.ui.internal.wizards.MavenProjectWizardLocationPage;
import org.eclipse.m2e.scm.MavenProjectPomScanner;
import org.eclipse.m2e.scm.MavenProjectScmInfo;
import org.eclipse.m2e.scm.internal.Messages;


/**
 * A wizard used to import projects for Maven artifacts
 * 
 * @author Eugene Kuleshov
 */
public class MavenMaterializePomWizard extends AbstractMavenProjectWizard implements IImportWizard, INewWizard {

  MavenDependenciesWizardPage selectionPage;

  MavenProjectWizardLocationPage locationPage;

  Button checkOutAllButton;

  Button useDeveloperConnectionButton;

  // TODO replace with ArtifactKey
  private Dependency[] dependencies;

  public MavenMaterializePomWizard() {
    importConfiguration = new ProjectImportConfiguration();
    setNeedsProgressMonitor(true);
    setWindowTitle(Messages.MavenMaterializePomWizard_title);
  }

  public void setDependencies(Dependency[] dependencies) {
    this.dependencies = dependencies;
  }

  public Dependency[] getDependencies() {
    return dependencies;
  }

  public void init(IWorkbench workbench, IStructuredSelection selection) {
    super.init(workbench, selection);

    ArrayList<Dependency> dependencies = new ArrayList<Dependency>();

    for(Iterator<?> it = selection.iterator(); it.hasNext();) {
      Object element = it.next();
      ArtifactKey artifactKey = SelectionUtil.getType(element, ArtifactKey.class);
      if(artifactKey != null) {
        Dependency d = new Dependency();
        d.setGroupId(artifactKey.getGroupId());
        d.setArtifactId(artifactKey.getArtifactId());
        d.setVersion(artifactKey.getVersion());
        d.setClassifier(artifactKey.getClassifier());
        dependencies.add(d);
      }
    }

    setDependencies(dependencies.toArray(new Dependency[dependencies.size()]));
  }

  public void addPages() {
    selectionPage = new MavenDependenciesWizardPage(importConfiguration, //
        Messages.MavenMaterializePomWizard_dialog_title, //
        Messages.MavenMaterializePomWizard_dialog_message) {
      protected void createAdvancedSettings(Composite composite, GridData gridData) {
        checkOutAllButton = new Button(composite, SWT.CHECK);
        checkOutAllButton.setText(Messages.MavenMaterializePomWizard_btnCheckout);
        checkOutAllButton.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, false, 3, 1));

        useDeveloperConnectionButton = new Button(composite, SWT.CHECK);
        useDeveloperConnectionButton.setText(Messages.MavenMaterializePomWizard_btnDev);
        useDeveloperConnectionButton.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, false, 3, 1));

        super.createAdvancedSettings(composite, gridData);
      }
    };
    selectionPage.setDependencies(dependencies);

    locationPage = new MavenProjectWizardLocationPage(
        importConfiguration, //
        Messages.MavenMaterializePomWizard_location_title, Messages.MavenMaterializePomWizard_location_message,
        workingSets);
    locationPage.setLocationPath(SelectionUtil.getSelectedLocation(selection));

    addPage(selectionPage);
    addPage(locationPage);
  }

  public boolean canFinish() {
    return super.canFinish();
  }

  public boolean performFinish() {
    if(!canFinish()) {
      return false;
    }

    final Dependency[] dependencies = selectionPage.getDependencies();

    final boolean checkoutAllProjects = checkOutAllButton.getSelection();
    final boolean developer = useDeveloperConnectionButton.getSelection();

    MavenProjectCheckoutJob job = new MavenProjectCheckoutJob(importConfiguration, checkoutAllProjects, workingSets) {
      protected List<MavenProjectScmInfo> getProjects(IProgressMonitor monitor) throws InterruptedException {
        MavenProjectPomScanner<MavenProjectScmInfo> scanner = new MavenProjectPomScanner<MavenProjectScmInfo>(
            developer, dependencies);
        scanner.run(monitor);
        // XXX handle errors/warnings

        return scanner.getProjects();
      }
    };

    if(!locationPage.isInWorkspace()) {
      job.setLocation(locationPage.getLocationPath().toFile());
    }

    job.schedule();

    return true;
  }
}

Back to the top