Git Import Wizard
[egit.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / clone / GitCreateProjectViaWizardWizard.java
blobaa1bdb790ed665590ff9d32f6161c053497ab30b
1 /*******************************************************************************
2 * Copyright (c) 2010 SAP AG.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
8 * Contributors:
9 * Mathias Kinzler (SAP AG) - initial implementation
10 *******************************************************************************/
11 package org.eclipse.egit.ui.internal.clone;
13 import java.lang.reflect.InvocationTargetException;
14 import java.util.ArrayList;
15 import java.util.List;
17 import org.eclipse.core.resources.IProject;
18 import org.eclipse.core.resources.IProjectDescription;
19 import org.eclipse.core.resources.IResource;
20 import org.eclipse.core.resources.ResourcesPlugin;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IProgressMonitor;
23 import org.eclipse.core.runtime.Path;
24 import org.eclipse.egit.core.op.ConnectProviderOperation;
25 import org.eclipse.egit.ui.Activator;
26 import org.eclipse.egit.ui.UIText;
27 import org.eclipse.jface.operation.IRunnableWithProgress;
28 import org.eclipse.jface.wizard.IWizardPage;
29 import org.eclipse.jface.wizard.Wizard;
30 import org.eclipse.jgit.lib.Repository;
31 import org.eclipse.osgi.util.NLS;
32 import org.eclipse.swt.widgets.Display;
33 import org.eclipse.ui.PlatformUI;
34 import org.eclipse.ui.actions.NewWizardAction;
35 import org.eclipse.ui.actions.WorkspaceModifyOperation;
37 /**
38 * A wizard used to import existing projects from a {@link Repository}
40 public class GitCreateProjectViaWizardWizard extends Wizard implements
41 ProjectCreator {
43 private final Repository myRepository;
45 private final String myGitDir;
47 private final IProject[] previousProjects;
49 private GitSelectWizardPage mySelectionPage;
51 private GitCreateGeneralProjectPage myCreateGeneralProjectPage;
53 private GitProjectsImportPage myProjectsImportPage;
55 private GitShareProjectsPage mySharePage;
57 /**
58 * @param repository
59 * @param path
61 public GitCreateProjectViaWizardWizard(Repository repository, String path) {
62 super();
63 previousProjects = ResourcesPlugin.getWorkspace().getRoot()
64 .getProjects();
65 myRepository = repository;
66 myGitDir = path;
67 setWindowTitle(NLS.bind(
68 UIText.GitCreateProjectViaWizardWizard_WizardTitle,
69 myRepository.getDirectory().getPath()));
71 // the "Import" wizard could be started like this,
72 // but throws an Exception if started within a wizard
73 // context (no active workbench window found) and the
74 // list of available wizards is empty
75 // -> investigate if we can include that wizard
77 // IHandlerService handlerService = (IHandlerService)
78 // PlatformUI.getWorkbench().getService(IHandlerService.class);
80 // handlerService.executeCommand("org.eclipse.ui.file.import",
84 @Override
85 public void addPages() {
87 mySelectionPage = new GitSelectWizardPage();
88 addPage(mySelectionPage);
89 myCreateGeneralProjectPage = new GitCreateGeneralProjectPage(myGitDir);
90 addPage(myCreateGeneralProjectPage);
91 myProjectsImportPage = new GitProjectsImportPage() {
93 @Override
94 public void setVisible(boolean visible) {
95 setProjectsList(myGitDir);
96 super.setVisible(visible);
100 addPage(myProjectsImportPage);
101 mySharePage = new GitShareProjectsPage();
102 addPage(mySharePage);
105 @Override
106 public IWizardPage getNextPage(IWizardPage page) {
108 if (page == mySelectionPage) {
110 switch (mySelectionPage.getWizardSelection()) {
111 case GitSelectWizardPage.EXISTING_PROJECTS_WIZARD:
112 return myProjectsImportPage;
113 case GitSelectWizardPage.NEW_WIZARD:
114 if (mySelectionPage.getActionSelection() != GitSelectWizardPage.ACTION_DIALOG_SHARE)
115 return null;
116 else
117 return mySharePage;
119 case GitSelectWizardPage.GENERAL_WIZARD:
120 return myCreateGeneralProjectPage;
124 return super.getNextPage(page);
126 } else if (page == myCreateGeneralProjectPage
127 || page == myProjectsImportPage) {
129 if (mySelectionPage.getActionSelection() != GitSelectWizardPage.ACTION_DIALOG_SHARE)
130 return null;
131 else
132 return mySharePage;
134 return super.getNextPage(page);
137 @Override
138 public boolean canFinish() {
140 boolean showSharePage = mySelectionPage.getActionSelection() == GitSelectWizardPage.ACTION_DIALOG_SHARE;
141 boolean showShareComplete = !showSharePage
142 || mySharePage.isPageComplete();
144 switch (mySelectionPage.getWizardSelection()) {
145 case GitSelectWizardPage.EXISTING_PROJECTS_WIZARD:
146 return myProjectsImportPage.isPageComplete() && showShareComplete;
147 case GitSelectWizardPage.NEW_WIZARD:
148 return showShareComplete;
149 case GitSelectWizardPage.GENERAL_WIZARD:
150 return myCreateGeneralProjectPage.isPageComplete()
151 && showShareComplete;
153 return super.canFinish();
157 @Override
158 public boolean performFinish() {
160 try {
162 final int actionSelection = mySelectionPage.getActionSelection();
164 final IProject[] projectsToShare;
165 if (actionSelection == GitSelectWizardPage.ACTION_DIALOG_SHARE)
166 projectsToShare = mySharePage.getSelectedProjects();
167 else
168 projectsToShare = null;
170 getContainer().run(true, true, new IRunnableWithProgress() {
172 public void run(IProgressMonitor monitor)
173 throws InvocationTargetException, InterruptedException {
175 if (actionSelection != GitSelectWizardPage.ACTION_DIALOG_SHARE) {
176 // in case of the share page, the import is done by the
177 // share page itself
178 // TODO this currently must be run in the UI Thread due
179 // to access to
180 // SWT widgets
181 importProjects();
184 if (actionSelection != GitSelectWizardPage.ACTION_NO_SHARE) {
186 // TODO scheduling rule?
187 IProject[] projects;
188 if (projectsToShare == null)
189 projects = getAddedProjects();
190 else
191 projects = projectsToShare;
192 for (IProject prj : projects) {
193 if (monitor.isCanceled())
194 throw new InterruptedException();
196 ConnectProviderOperation connectProviderOperation = new ConnectProviderOperation(
197 prj, myRepository.getDirectory());
198 try {
199 connectProviderOperation.execute(monitor);
200 } catch (CoreException e) {
201 throw new InvocationTargetException(e);
209 } catch (InvocationTargetException e) {
210 Activator
211 .handleError(e.getCause().getMessage(), e.getCause(), true);
212 return false;
213 } catch (InterruptedException e) {
214 Activator.handleError(
215 UIText.GitCreateProjectViaWizardWizard_AbortedMessage, e,
216 true);
217 return false;
219 return true;
226 public void importProjects() {
228 // TODO progress monitoring and cancellation
229 Display.getDefault().syncExec(new Runnable() {
231 public void run() {
233 switch (mySelectionPage.getWizardSelection()) {
234 case GitSelectWizardPage.EXISTING_PROJECTS_WIZARD:
235 myProjectsImportPage.createProjects();
236 break;
237 case GitSelectWizardPage.NEW_WIZARD:
238 new NewWizardAction(PlatformUI.getWorkbench()
239 .getActiveWorkbenchWindow()).run();
240 break;
241 case GitSelectWizardPage.GENERAL_WIZARD:
242 try {
243 final String projectName = myCreateGeneralProjectPage
244 .getProjectName();
245 getContainer().run(true, false,
246 new WorkspaceModifyOperation() {
248 @Override
249 protected void execute(
250 IProgressMonitor monitor)
251 throws CoreException,
252 InvocationTargetException,
253 InterruptedException {
255 final IProjectDescription desc = ResourcesPlugin
256 .getWorkspace()
257 .newProjectDescription(
258 projectName);
259 desc.setLocation(new Path(myGitDir));
261 IProject prj = ResourcesPlugin
262 .getWorkspace().getRoot()
263 .getProject(desc.getName());
264 prj.create(desc, monitor);
265 prj.open(monitor);
267 ResourcesPlugin.getWorkspace()
268 .getRoot().refreshLocal(
269 IResource.DEPTH_ONE,
270 monitor);
274 } catch (InvocationTargetException e1) {
275 Activator.handleError(e1.getMessage(), e1
276 .getTargetException(), true);
277 } catch (InterruptedException e1) {
278 Activator.handleError(e1.getMessage(), e1, true);
280 break;
287 public IProject[] getAddedProjects() {
289 IProject[] currentProjects = ResourcesPlugin.getWorkspace().getRoot()
290 .getProjects();
292 List<IProject> newProjects = new ArrayList<IProject>();
294 for (IProject current : currentProjects) {
295 boolean found = false;
296 for (IProject previous : previousProjects) {
297 if (previous.equals(current)) {
298 found = true;
299 break;
302 if (!found) {
303 newProjects.add(current);
307 return newProjects.toArray(new IProject[0]);