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
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
;
38 * A wizard used to import existing projects from a {@link Repository}
40 public class GitCreateProjectViaWizardWizard
extends Wizard
implements
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
;
61 public GitCreateProjectViaWizardWizard(Repository repository
, String path
) {
63 previousProjects
= ResourcesPlugin
.getWorkspace().getRoot()
65 myRepository
= repository
;
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",
85 public void addPages() {
86 mySelectionPage
= new GitSelectWizardPage(myRepository
, myGitDir
);
87 addPage(mySelectionPage
);
88 myCreateGeneralProjectPage
= new GitCreateGeneralProjectPage(myGitDir
);
89 addPage(myCreateGeneralProjectPage
);
90 myProjectsImportPage
= new GitProjectsImportPage() {
93 public void setVisible(boolean visible
) {
94 setProjectsList(mySelectionPage
.getPath());
95 super.setVisible(visible
);
99 addPage(myProjectsImportPage
);
100 mySharePage
= new GitShareProjectsPage();
101 addPage(mySharePage
);
105 public IWizardPage
getNextPage(IWizardPage page
) {
107 if (page
== mySelectionPage
) {
109 switch (mySelectionPage
.getWizardSelection()) {
110 case GitSelectWizardPage
.EXISTING_PROJECTS_WIZARD
:
111 return myProjectsImportPage
;
112 case GitSelectWizardPage
.NEW_WIZARD
:
113 if (mySelectionPage
.getActionSelection() != GitSelectWizardPage
.ACTION_DIALOG_SHARE
)
118 case GitSelectWizardPage
.GENERAL_WIZARD
:
119 return myCreateGeneralProjectPage
;
123 return super.getNextPage(page
);
125 } else if (page
== myCreateGeneralProjectPage
126 || page
== myProjectsImportPage
) {
128 if (mySelectionPage
.getActionSelection() != GitSelectWizardPage
.ACTION_DIALOG_SHARE
)
133 return super.getNextPage(page
);
137 public boolean canFinish() {
139 boolean showSharePage
= mySelectionPage
.getActionSelection() == GitSelectWizardPage
.ACTION_DIALOG_SHARE
;
140 boolean showShareComplete
= !showSharePage
141 || mySharePage
.isPageComplete();
143 switch (mySelectionPage
.getWizardSelection()) {
144 case GitSelectWizardPage
.EXISTING_PROJECTS_WIZARD
:
145 return myProjectsImportPage
.isPageComplete() && showShareComplete
;
146 case GitSelectWizardPage
.NEW_WIZARD
:
147 return showShareComplete
;
148 case GitSelectWizardPage
.GENERAL_WIZARD
:
149 return myCreateGeneralProjectPage
.isPageComplete()
150 && showShareComplete
;
152 return super.canFinish();
157 public boolean performFinish() {
161 final int actionSelection
= mySelectionPage
.getActionSelection();
163 final IProject
[] projectsToShare
;
164 if (actionSelection
== GitSelectWizardPage
.ACTION_DIALOG_SHARE
)
165 projectsToShare
= mySharePage
.getSelectedProjects();
167 projectsToShare
= null;
169 getContainer().run(true, true, new IRunnableWithProgress() {
171 public void run(IProgressMonitor monitor
)
172 throws InvocationTargetException
, InterruptedException
{
174 if (actionSelection
!= GitSelectWizardPage
.ACTION_DIALOG_SHARE
) {
175 // in case of the share page, the import is done by the
177 // TODO this currently must be run in the UI Thread due
183 if (actionSelection
!= GitSelectWizardPage
.ACTION_NO_SHARE
) {
185 // TODO scheduling rule?
187 if (projectsToShare
== null)
188 projects
= getAddedProjects();
190 projects
= projectsToShare
;
191 for (IProject prj
: projects
) {
192 if (monitor
.isCanceled())
193 throw new InterruptedException();
195 ConnectProviderOperation connectProviderOperation
= new ConnectProviderOperation(
196 prj
, myRepository
.getDirectory());
198 connectProviderOperation
.execute(monitor
);
199 } catch (CoreException e
) {
200 throw new InvocationTargetException(e
);
208 } catch (InvocationTargetException e
) {
210 .handleError(e
.getCause().getMessage(), e
.getCause(), true);
212 } catch (InterruptedException e
) {
213 Activator
.handleError(
214 UIText
.GitCreateProjectViaWizardWizard_AbortedMessage
, e
,
225 public void importProjects() {
227 // TODO progress monitoring and cancellation
228 Display
.getDefault().syncExec(new Runnable() {
232 switch (mySelectionPage
.getWizardSelection()) {
233 case GitSelectWizardPage
.EXISTING_PROJECTS_WIZARD
:
234 myProjectsImportPage
.createProjects();
236 case GitSelectWizardPage
.NEW_WIZARD
:
237 new NewWizardAction(PlatformUI
.getWorkbench()
238 .getActiveWorkbenchWindow()).run();
240 case GitSelectWizardPage
.GENERAL_WIZARD
:
242 final String projectName
= myCreateGeneralProjectPage
244 getContainer().run(true, false,
245 new WorkspaceModifyOperation() {
248 protected void execute(
249 IProgressMonitor monitor
)
250 throws CoreException
,
251 InvocationTargetException
,
252 InterruptedException
{
254 final IProjectDescription desc
= ResourcesPlugin
256 .newProjectDescription(
258 desc
.setLocation(new Path(myGitDir
));
260 IProject prj
= ResourcesPlugin
261 .getWorkspace().getRoot()
262 .getProject(desc
.getName());
263 prj
.create(desc
, monitor
);
266 ResourcesPlugin
.getWorkspace()
267 .getRoot().refreshLocal(
273 } catch (InvocationTargetException e1
) {
274 Activator
.handleError(e1
.getMessage(), e1
275 .getTargetException(), true);
276 } catch (InterruptedException e1
) {
277 Activator
.handleError(e1
.getMessage(), e1
, true);
286 public IProject
[] getAddedProjects() {
288 IProject
[] currentProjects
= ResourcesPlugin
.getWorkspace().getRoot()
291 List
<IProject
> newProjects
= new ArrayList
<IProject
>();
293 for (IProject current
: currentProjects
) {
294 boolean found
= false;
295 for (IProject previous
: previousProjects
) {
296 if (previous
.equals(current
)) {
302 newProjects
.add(current
);
306 return newProjects
.toArray(new IProject
[0]);