1 /*******************************************************************************
2 * Copyright (c) 2010-2013 SAP AG and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License 2.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/legal/epl-2.0/
8 * SPDX-License-Identifier: EPL-2.0
11 * Mathias Kinzler (SAP AG) - initial implementation
12 * Stefan Lay (SAP AG) - improvements
13 *******************************************************************************/
14 package org
.eclipse
.egit
.ui
.internal
.clone
;
17 import java
.io
.IOException
;
18 import java
.lang
.reflect
.InvocationTargetException
;
19 import java
.net
.URISyntaxException
;
20 import java
.util
.ArrayList
;
21 import java
.util
.Arrays
;
22 import java
.util
.HashSet
;
23 import java
.util
.List
;
26 import org
.eclipse
.core
.resources
.IProject
;
27 import org
.eclipse
.core
.resources
.IProjectDescription
;
28 import org
.eclipse
.core
.resources
.IResource
;
29 import org
.eclipse
.core
.resources
.IWorkspaceRunnable
;
30 import org
.eclipse
.core
.resources
.ResourcesPlugin
;
31 import org
.eclipse
.core
.runtime
.CoreException
;
32 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
33 import org
.eclipse
.core
.runtime
.Path
;
34 import org
.eclipse
.core
.runtime
.SubMonitor
;
35 import org
.eclipse
.egit
.core
.RepositoryCache
;
36 import org
.eclipse
.egit
.core
.op
.ConnectProviderOperation
;
37 import org
.eclipse
.egit
.ui
.Activator
;
38 import org
.eclipse
.egit
.ui
.internal
.UIIcons
;
39 import org
.eclipse
.egit
.ui
.internal
.UIText
;
40 import org
.eclipse
.egit
.ui
.internal
.clone
.GitCloneSourceProviderExtension
.CloneSourceProvider
;
41 import org
.eclipse
.egit
.ui
.internal
.provisional
.wizards
.GitRepositoryInfo
;
42 import org
.eclipse
.egit
.ui
.internal
.provisional
.wizards
.IRepositorySearchResult
;
43 import org
.eclipse
.egit
.ui
.internal
.provisional
.wizards
.NoRepositoryInfoException
;
44 import org
.eclipse
.jface
.dialogs
.IDialogSettings
;
45 import org
.eclipse
.jface
.operation
.IRunnableWithProgress
;
46 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
47 import org
.eclipse
.jface
.wizard
.IWizardPage
;
48 import org
.eclipse
.jgit
.lib
.Constants
;
49 import org
.eclipse
.jgit
.lib
.Repository
;
50 import org
.eclipse
.ui
.IImportWizard
;
51 import org
.eclipse
.ui
.IWorkbench
;
52 import org
.eclipse
.ui
.IWorkingSet
;
53 import org
.eclipse
.ui
.PlatformUI
;
54 import org
.eclipse
.ui
.actions
.NewProjectAction
;
57 * A wizard which allows to optionally clone a repository and to import projects from a repository.
59 public class GitImportWizard
extends AbstractGitCloneWizard
implements IImportWizard
{
60 private static final String GIT_IMPORT_SECTION
= "GitImportWizard"; //$NON-NLS-1$
62 private GitSelectRepositoryPage selectRepoPage
= new GitSelectRepositoryPage(
65 private GitSelectWizardPage importWithDirectoriesPage
= new GitSelectWizardPage(){
67 public void setVisible(boolean visible
) {
68 if (existingRepo
== null && visible
&& (cloneDestination
.cloneSettingsChanged())) {
69 setCallerRunsCloneOperation(true);
71 final GitRepositoryInfo repositoryInfo
= currentSearchResult
.getGitRepositoryInfo();
72 performClone(repositoryInfo
);
73 importWithDirectoriesPage
.getControl().getDisplay().asyncExec(new Runnable() {
77 runCloneOperation(getContainer(), repositoryInfo
);
78 cloneDestination
.saveSettingsForClonedRepo();
80 } catch (URISyntaxException e
) {
81 Activator
.error(UIText
.GitImportWizard_errorParsingURI
, e
);
82 } catch (NoRepositoryInfoException e
) {
83 Activator
.error(UIText
.GitImportWizard_noRepositoryInfo
, e
);
84 } catch (Exception e
) {
85 Activator
.error(e
.getMessage(), e
);
88 super.setVisible(visible
);
92 private GitProjectsImportPage projectsImportPage
= new GitProjectsImportPage() {
94 public void setVisible(boolean visible
) {
96 setProjectsList(importWithDirectoriesPage
.getPath());
97 super.setVisible(visible
);
101 private GitCreateGeneralProjectPage createGeneralProjectPage
= new GitCreateGeneralProjectPage() {
103 public void setVisible(boolean visible
) {
105 setPath(importWithDirectoriesPage
.getPath());
106 super.setVisible(visible
);
110 private Repository existingRepo
;
113 * The default constructor
115 public GitImportWizard() {
120 * Construct the import wizard based on given repository search result. The
121 * wizard skips the repository location page in this case.
123 * @param searchResult
124 * the search result to initialize the import wizard with.
126 public GitImportWizard(IRepositorySearchResult searchResult
) {
128 setWindowTitle(UIText
.GitImportWizard_WizardTitle
);
129 setDefaultPageImageDescriptor(UIIcons
.WIZBAN_IMPORT_REPO
);
130 setDialogSettings(getImportWizardDialogSettings());
134 protected void addPreClonePages() {
135 if (!hasSearchResult())
136 addPage(selectRepoPage
);
140 protected void addPostClonePages() {
141 addPage(importWithDirectoriesPage
);
142 addPage(projectsImportPage
);
143 addPage(createGeneralProjectPage
);
147 protected List
<CloneSourceProvider
> getCloneSourceProviders() {
148 List
<CloneSourceProvider
> cloneSourceProvider
= super.getCloneSourceProviders();
149 cloneSourceProvider
.add(0, CloneSourceProvider
.LOCAL
);
150 return cloneSourceProvider
;
154 public void init(IWorkbench workbench
, IStructuredSelection selection
) {
159 public IWizardPage
getNextPage(IWizardPage page
) {
160 if (page
== selectRepoPage
) {
161 existingRepo
= selectRepoPage
.getRepository();
162 importWithDirectoriesPage
.setRepository(selectRepoPage
164 return importWithDirectoriesPage
;
165 } else if (page
== cloneDestination
) {
167 importWithDirectoriesPage
.setRepository(getTargetRepository());
168 return importWithDirectoriesPage
;
169 } else if (page
== importWithDirectoriesPage
)
170 switch (importWithDirectoriesPage
.getWizardSelection()) {
171 case GitSelectWizardPage
.EXISTING_PROJECTS_WIZARD
:
172 return projectsImportPage
;
173 case GitSelectWizardPage
.NEW_WIZARD
:
175 case GitSelectWizardPage
.GENERAL_WIZARD
:
176 return createGeneralProjectPage
;
178 else if (page
== createGeneralProjectPage
179 || page
== projectsImportPage
)
181 return super.getNextPage(page
);
184 private Repository
getTargetRepository() {
185 if (existingRepo
!= null)
189 return RepositoryCache
.INSTANCE
.lookupRepository(
190 new File(cloneDestination
.getDestinationFile(),
192 } catch (IOException e
) {
193 Activator
.error("Error looking up repository at " //$NON-NLS-1$
194 + cloneDestination
.getDestinationFile(), e
);
200 public boolean performFinish() {
202 getContainer().run(true, true, new IRunnableWithProgress() {
204 public void run(IProgressMonitor monitor
)
205 throws InvocationTargetException
, InterruptedException
{
206 importProjects(monitor
);
209 } catch (InvocationTargetException e
) {
211 .handleError(e
.getCause().getMessage(), e
.getCause(), true);
213 } catch (InterruptedException e
) {
214 Activator
.handleError(
215 UIText
.GitCreateProjectViaWizardWizard_AbortedMessage
, e
,
223 public boolean canFinish() {
224 switch (importWithDirectoriesPage
.getWizardSelection()) {
225 case GitSelectWizardPage
.EXISTING_PROJECTS_WIZARD
:
226 return projectsImportPage
.isPageComplete();
227 case GitSelectWizardPage
.NEW_WIZARD
:
229 case GitSelectWizardPage
.GENERAL_WIZARD
:
230 return createGeneralProjectPage
.isPageComplete();
232 return super.canFinish();
236 private void importProjects(IProgressMonitor monitor
)
237 throws InvocationTargetException
, InterruptedException
{
238 switch (importWithDirectoriesPage
.getWizardSelection()) {
239 case GitSelectWizardPage
.EXISTING_PROJECTS_WIZARD
: {
240 final Set
<ProjectRecord
> projectsToCreate
= new HashSet
<>();
241 final List
<IWorkingSet
> workingSets
= new ArrayList
<>();
242 // get the data from the pages in the UI thread
243 PlatformUI
.getWorkbench().getDisplay().syncExec(new Runnable() {
246 projectsToCreate
.addAll(projectsImportPage
247 .getCheckedProjects());
248 IWorkingSet
[] workingSetArray
= projectsImportPage
249 .getSelectedWorkingSets();
250 workingSets
.addAll(Arrays
.asList(workingSetArray
));
251 projectsImportPage
.saveWidgetValues();
254 ProjectUtils
.createProjects(projectsToCreate
,
255 workingSets
.toArray(new IWorkingSet
[0]),
259 case GitSelectWizardPage
.NEW_WIZARD
: {
260 final File
[] repoDir
= new File
[1];
261 PlatformUI
.getWorkbench().getDisplay().syncExec(new Runnable() {
264 repoDir
[0] = getTargetRepository().getDirectory();
267 final List
<IProject
> previousProjects
= Arrays
268 .asList(ResourcesPlugin
.getWorkspace().getRoot()
270 PlatformUI
.getWorkbench().getDisplay().syncExec(new Runnable() {
273 new NewProjectAction(PlatformUI
.getWorkbench()
274 .getActiveWorkbenchWindow()).run();
277 IWorkspaceRunnable wsr
= new IWorkspaceRunnable() {
279 public void run(IProgressMonitor actMonitor
)
280 throws CoreException
{
281 IProject
[] currentProjects
= ResourcesPlugin
.getWorkspace()
282 .getRoot().getProjects();
283 SubMonitor progress
= SubMonitor
.convert(actMonitor
,
284 currentProjects
.length
);
285 for (IProject current
: currentProjects
)
286 if (!previousProjects
.contains(current
)) {
287 ConnectProviderOperation cpo
= new ConnectProviderOperation(
288 current
, repoDir
[0]);
289 cpo
.execute(progress
.newChild(1));
296 ResourcesPlugin
.getWorkspace().run(wsr
, monitor
);
297 } catch (CoreException e
) {
298 throw new InvocationTargetException(e
);
302 case GitSelectWizardPage
.GENERAL_WIZARD
: {
303 final String
[] projectName
= new String
[1];
304 final boolean[] defaultLocation
= new boolean[1];
305 final String
[] path
= new String
[1];
306 final File
[] repoDir
= new File
[1];
307 // get the data from the page in the UI thread
308 PlatformUI
.getWorkbench().getDisplay().syncExec(new Runnable() {
311 projectName
[0] = createGeneralProjectPage
.getProjectName();
312 defaultLocation
[0] = createGeneralProjectPage
313 .isDefaultLocation();
314 path
[0] = importWithDirectoriesPage
.getPath();
315 repoDir
[0] = getTargetRepository().getDirectory();
319 IWorkspaceRunnable wsr
= new IWorkspaceRunnable() {
321 public void run(IProgressMonitor actMonitor
)
322 throws CoreException
{
323 final IProjectDescription desc
= ResourcesPlugin
324 .getWorkspace().newProjectDescription(
326 desc
.setLocation(new Path(path
[0]));
327 SubMonitor progress
= SubMonitor
.convert(actMonitor
, 4);
328 IProject prj
= ResourcesPlugin
.getWorkspace().getRoot()
329 .getProject(desc
.getName());
330 prj
.create(desc
, progress
.newChild(1));
331 prj
.open(progress
.newChild(1));
332 ConnectProviderOperation cpo
= new ConnectProviderOperation(
334 cpo
.execute(progress
.newChild(1));
336 ResourcesPlugin
.getWorkspace().getRoot().refreshLocal(
337 IResource
.DEPTH_ONE
, progress
.newChild(1));
340 ResourcesPlugin
.getWorkspace().run(wsr
, monitor
);
341 } catch (CoreException e
) {
342 throw new InvocationTargetException(e
);
349 static IDialogSettings
getImportWizardDialogSettings() {
350 IDialogSettings settings
= Activator
.getDefault().getDialogSettings();
352 IDialogSettings wizardSettings
= settings
353 .getSection(GitImportWizard
.GIT_IMPORT_SECTION
);
354 if (wizardSettings
== null) {
355 wizardSettings
= settings
356 .addNewSection(GitImportWizard
.GIT_IMPORT_SECTION
);
358 return wizardSettings
;