Git Repositories View: add CollapseAll action
[egit.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / clone / GitCreateProjectViaWizardWizard.java
blob0068c41d297657a50a93163bffaf889a4ad9919c
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 {
42 private final Repository myRepository;
44 private final String myGitDir;
46 private final IProject[] previousProjects;
48 private GitSelectWizardPage mySelectionPage;
50 private GitCreateGeneralProjectPage myCreateGeneralProjectPage;
52 private GitProjectsImportPage myProjectsImportPage;
54 private GitShareProjectsPage mySharePage;
56 /**
57 * @param repository
58 * @param path
60 public GitCreateProjectViaWizardWizard(Repository repository, String path) {
61 super();
62 previousProjects = ResourcesPlugin.getWorkspace().getRoot()
63 .getProjects();
64 myRepository = repository;
65 myGitDir = path;
66 setWindowTitle(NLS.bind(
67 UIText.GitCreateProjectViaWizardWizard_WizardTitle,
68 myRepository.getDirectory().getPath()));
70 // the "Import" wizard could be started like this,
71 // but throws an Exception if started within a wizard
72 // context (no active workbench window found) and the
73 // list of available wizards is empty
74 // -> investigate if we can include that wizard
76 // IHandlerService handlerService = (IHandlerService)
77 // PlatformUI.getWorkbench().getService(IHandlerService.class);
79 // handlerService.executeCommand("org.eclipse.ui.file.import",
83 @Override
84 public void addPages() {
86 mySelectionPage = new GitSelectWizardPage();
87 addPage(mySelectionPage);
88 myCreateGeneralProjectPage = new GitCreateGeneralProjectPage(myGitDir);
89 addPage(myCreateGeneralProjectPage);
90 // for "Import Existing Projects"
91 // TODO new constructor with repository and directory once we
92 // remove this page from the GitCloneWizard
93 myProjectsImportPage = new GitProjectsImportPage(false) {
95 @Override
96 public void setVisible(boolean visible) {
97 setGitDir(myRepository.getDirectory());
98 setProjectsList(myGitDir);
99 super.setVisible(visible);
103 addPage(myProjectsImportPage);
104 mySharePage = new GitShareProjectsPage();
105 addPage(mySharePage);
108 @Override
109 public IWizardPage getNextPage(IWizardPage page) {
111 if (page == mySelectionPage) {
113 switch (mySelectionPage.getWizardSelection()) {
114 case GitSelectWizardPage.EXISTING_PROJECTS_WIZARD:
115 return myProjectsImportPage;
116 case GitSelectWizardPage.NEW_WIZARD:
117 if (mySelectionPage.getActionSelection() != GitSelectWizardPage.ACTION_DIALOG_SHARE)
118 return null;
119 else
120 return mySharePage;
122 case GitSelectWizardPage.GENERAL_WIZARD:
123 return myCreateGeneralProjectPage;
127 return super.getNextPage(page);
129 } else if (page == myCreateGeneralProjectPage
130 || page == myProjectsImportPage) {
132 if (mySelectionPage.getActionSelection() != GitSelectWizardPage.ACTION_DIALOG_SHARE)
133 return null;
134 else
135 return mySharePage;
137 return super.getNextPage(page);
140 @Override
141 public boolean canFinish() {
143 boolean showSharePage = mySelectionPage.getActionSelection() == GitSelectWizardPage.ACTION_DIALOG_SHARE;
144 boolean showShareComplete = !showSharePage
145 || mySharePage.isPageComplete();
147 switch (mySelectionPage.getWizardSelection()) {
148 case GitSelectWizardPage.EXISTING_PROJECTS_WIZARD:
149 return myProjectsImportPage.isPageComplete() && showShareComplete;
150 case GitSelectWizardPage.NEW_WIZARD:
151 return showShareComplete;
152 case GitSelectWizardPage.GENERAL_WIZARD:
153 return myCreateGeneralProjectPage.isPageComplete()
154 && showShareComplete;
156 return super.canFinish();
160 @Override
161 public boolean performFinish() {
163 try {
165 final int actionSelection = mySelectionPage.getActionSelection();
167 final IProject[] projectsToShare;
168 if (actionSelection != GitSelectWizardPage.ACTION_DIALOG_SHARE) {
169 projectsToShare = getAddedProjects();
170 } else {
171 projectsToShare = mySharePage.getSelectedProjects();
174 getContainer().run(true, true, new IRunnableWithProgress() {
176 public void run(IProgressMonitor monitor)
177 throws InvocationTargetException, InterruptedException {
179 if (actionSelection != GitSelectWizardPage.ACTION_DIALOG_SHARE) {
180 // in case of the share page, the import is done by the
181 // share page itself
182 // TODO this currently must be run in the UI Thread due
183 // to access to
184 // SWT widgets
185 importProjects();
188 if (actionSelection != GitSelectWizardPage.ACTION_NO_SHARE) {
190 // TODO scheduling rule?
191 for (IProject prj : projectsToShare) {
192 if (monitor.isCanceled())
193 throw new InterruptedException();
195 ConnectProviderOperation connectProviderOperation = new ConnectProviderOperation(
196 prj, myRepository.getDirectory());
197 try {
198 connectProviderOperation.execute(monitor);
199 } catch (CoreException e) {
200 throw new InvocationTargetException(e);
208 } catch (InvocationTargetException e) {
209 Activator
210 .handleError(e.getCause().getMessage(), e.getCause(), true);
211 return false;
212 } catch (InterruptedException e) {
213 Activator.handleError(
214 UIText.GitCreateProjectViaWizardWizard_AbortedMessage, e,
215 true);
216 return false;
218 return true;
225 public void importProjects() {
227 // TODO progress monitoring and cancellation
228 Display.getDefault().syncExec(new Runnable() {
230 public void run() {
232 switch (mySelectionPage.getWizardSelection()) {
233 case GitSelectWizardPage.EXISTING_PROJECTS_WIZARD:
234 myProjectsImportPage.createProjects();
235 break;
236 case GitSelectWizardPage.NEW_WIZARD:
237 new NewWizardAction(PlatformUI.getWorkbench()
238 .getActiveWorkbenchWindow()).run();
239 break;
240 case GitSelectWizardPage.GENERAL_WIZARD:
241 try {
242 final String projectName = myCreateGeneralProjectPage
243 .getProjectName();
244 getContainer().run(true, false,
245 new WorkspaceModifyOperation() {
247 @Override
248 protected void execute(
249 IProgressMonitor monitor)
250 throws CoreException,
251 InvocationTargetException,
252 InterruptedException {
254 final IProjectDescription desc = ResourcesPlugin
255 .getWorkspace()
256 .newProjectDescription(
257 projectName);
258 desc.setLocation(new Path(myGitDir));
260 IProject prj = ResourcesPlugin
261 .getWorkspace().getRoot()
262 .getProject(desc.getName());
263 prj.create(desc, monitor);
264 prj.open(monitor);
266 ResourcesPlugin.getWorkspace()
267 .getRoot().refreshLocal(
268 IResource.DEPTH_ONE,
269 monitor);
273 } catch (InvocationTargetException e1) {
274 Activator.handleError(e1.getMessage(), e1
275 .getTargetException(), true);
276 } catch (InterruptedException e1) {
277 Activator.handleError(e1.getMessage(), e1, true);
279 break;
287 * @return the projects added to the workspace since the start of this
288 * wizard
290 public IProject[] getAddedProjects() {
292 IProject[] currentProjects = ResourcesPlugin.getWorkspace().getRoot()
293 .getProjects();
295 List<IProject> newProjects = new ArrayList<IProject>();
297 for (IProject current : currentProjects) {
298 boolean found = false;
299 for (IProject previous : previousProjects) {
300 if (previous.equals(current)) {
301 found = true;
302 break;
305 if (!found) {
306 newProjects.add(current);
310 return newProjects.toArray(new IProject[0]);