Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b589fc3d39f581abf92194b21d5e92a2631a579a (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*******************************************************************************
 * 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.Collection;
import java.util.Iterator;

import org.apache.maven.model.Scm;
import org.eclipse.core.runtime.IAdapterManager;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.m2e.core.core.IMavenConstants;
import org.eclipse.m2e.scm.internal.Messages;
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.MavenProjectWizardLocationPage;
import org.eclipse.m2e.scm.MavenProjectScmInfo;
import org.eclipse.m2e.scm.ScmUrl;
import org.eclipse.ui.IImportWizard;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;


/**
 * Maven checkout wizard
 * 
 * @author Eugene Kuleshov
 */
public class MavenCheckoutWizard extends AbstractMavenProjectWizard implements IImportWizard, INewWizard {

  private ScmUrl[] urls;

  private String parentUrl;
  
  private MavenCheckoutLocationPage scheckoutPage;

  private MavenProjectWizardLocationPage locationPage;
  
  public MavenCheckoutWizard() {
    this(null);
    setNeedsProgressMonitor(true);
  }

  public MavenCheckoutWizard(ScmUrl[] urls) {
    setUrls(urls);
    setNeedsProgressMonitor(true);
    setWindowTitle(Messages.MavenCheckoutWizard_title);
  }

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

    this.selection = selection;

    ArrayList<ScmUrl> urls = new ArrayList<ScmUrl>();
    IAdapterManager adapterManager = Platform.getAdapterManager();
    for(Iterator<?> it = selection.iterator(); it.hasNext();) {
      ScmUrl url = (ScmUrl) adapterManager.getAdapter(it.next(), ScmUrl.class);
      if(url != null) {
        urls.add(url);
      }
    }
    setUrls(urls.toArray(new ScmUrl[urls.size()]));
  }
  
  private void setUrls(ScmUrl[] urls) {
    if(urls!=null && urls.length>0) {
      this.urls = urls;
      this.parentUrl = getParentUrl(urls);
    }
  }

  private String getParentUrl(ScmUrl[] urls) {
    if(urls.length==1) {
      return urls[0].getUrl();
    }
    
    String parent = urls[0].getParentUrl();
    for(int i = 1; parent!=null && i < urls.length; i++ ) {
      String url = urls[i].getParentUrl();
      if(!parent.equals(url)) {
        parent = null;
      }
    }
    return parent;
  }
  
  public void addPages() {
    scheckoutPage = new MavenCheckoutLocationPage(importConfiguration);
    scheckoutPage.setUrls(urls);
    scheckoutPage.setParent(parentUrl);
    
    locationPage = new MavenProjectWizardLocationPage(importConfiguration, //
        Messages.MavenCheckoutWizard_location1,
        Messages.MavenCheckoutWizard_location2, workingSets);
    locationPage.setLocationPath(SelectionUtil.getSelectedLocation(selection));
    
    addPage(scheckoutPage);
    addPage(locationPage);
  }
  
//  /** Adds the listeners after the page controls are created. */
//  public void createPageControls(Composite pageContainer) {
//    super.createPageControls(pageContainer);
//
//    locationPage.addListener(new SelectionAdapter() {
//      public void widgetSelected(SelectionEvent e) {
//        projectsPage.setScms(locationPage.getScms(new NullProgressMonitor()));
//      }
//    });
//    
//    projectsPage.setScms(locationPage.getScms(new NullProgressMonitor()));
//  }

  public boolean canFinish() {
    if(scheckoutPage.isCheckoutAllProjects() && scheckoutPage.isPageComplete()) {
      return true;
    }
    return super.canFinish();
  }

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

    final boolean checkoutAllProjects = scheckoutPage.isCheckoutAllProjects();

    Scm[] scms = scheckoutPage.getScms();
    
    final Collection<MavenProjectScmInfo> mavenProjects = new ArrayList<MavenProjectScmInfo>();
    for(int i = 0; i < scms.length; i++ ) {
      String url = scms[i].getConnection();
      String revision = scms[i].getTag();
      
      if(url.endsWith("/")) { //$NON-NLS-1$
        url = url.substring(0, url.length()-1);
      }
      
      int n = url.lastIndexOf("/"); //$NON-NLS-1$
      String label = (n == -1 ? url : url.substring(n)) + "/" + IMavenConstants.POM_FILE_NAME; //$NON-NLS-1$
      MavenProjectScmInfo projectInfo = new MavenProjectScmInfo(label, null, //
          null, revision, url, url);
      mavenProjects.add(projectInfo);
    }

    MavenProjectCheckoutJob job = new MavenProjectCheckoutJob(importConfiguration, checkoutAllProjects, workingSets) {
      protected Collection<MavenProjectScmInfo> getProjects(IProgressMonitor monitor) {
        return mavenProjects;
      }
    };

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

    return true;
  }

}

Back to the top