1 /*******************************************************************************
2 * Copyright (c) 2010, 2013, 2017 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 * Wim Jongman (wim.jongman@remainsoftware.com) - Bug 358152
13 *******************************************************************************/
14 package org
.eclipse
.egit
.ui
.internal
.clone
;
17 import java
.lang
.reflect
.InvocationTargetException
;
18 import java
.util
.ArrayList
;
19 import java
.util
.Arrays
;
20 import java
.util
.Collections
;
21 import java
.util
.HashSet
;
22 import java
.util
.List
;
25 import org
.eclipse
.core
.resources
.IProject
;
26 import org
.eclipse
.core
.resources
.IProjectDescription
;
27 import org
.eclipse
.core
.resources
.IResource
;
28 import org
.eclipse
.core
.resources
.IWorkspaceRunnable
;
29 import org
.eclipse
.core
.resources
.ResourcesPlugin
;
30 import org
.eclipse
.core
.runtime
.CoreException
;
31 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
32 import org
.eclipse
.core
.runtime
.Path
;
33 import org
.eclipse
.core
.runtime
.SubMonitor
;
34 import org
.eclipse
.egit
.core
.op
.ConnectProviderOperation
;
35 import org
.eclipse
.egit
.ui
.Activator
;
36 import org
.eclipse
.egit
.ui
.internal
.UIText
;
37 import org
.eclipse
.jface
.operation
.IRunnableWithProgress
;
38 import org
.eclipse
.jface
.wizard
.IWizardPage
;
39 import org
.eclipse
.jface
.wizard
.Wizard
;
40 import org
.eclipse
.jgit
.lib
.Repository
;
41 import org
.eclipse
.osgi
.util
.NLS
;
42 import org
.eclipse
.ui
.IWorkingSet
;
43 import org
.eclipse
.ui
.PlatformUI
;
44 import org
.eclipse
.ui
.actions
.NewProjectAction
;
47 * A wizard used to import existing projects from a {@link Repository}
49 public class GitCreateProjectViaWizardWizard
extends Wizard
{
50 private final Repository myRepository
;
52 private final String myGitDir
;
54 private GitSelectWizardPage mySelectionPage
;
56 private GitCreateGeneralProjectPage myCreateGeneralProjectPage
;
58 private GitProjectsImportPage myProjectsImportPage
;
60 private List
<String
> myFilter
;
66 public GitCreateProjectViaWizardWizard(Repository repository
, String path
) {
68 myRepository
= repository
;
70 setNeedsProgressMonitor(true);
71 setWindowTitle(NLS
.bind(
72 UIText
.GitCreateProjectViaWizardWizard_WizardTitle
,
73 myRepository
.getDirectory().getPath()));
74 setDialogSettings(GitImportWizard
.getImportWizardDialogSettings());
78 public void addPages() {
79 mySelectionPage
= new GitSelectWizardPage(myRepository
, myGitDir
);
80 addPage(mySelectionPage
);
81 myCreateGeneralProjectPage
= new GitCreateGeneralProjectPage(myGitDir
) {
83 public void setVisible(boolean visible
) {
84 setPath(mySelectionPage
.getPath());
85 super.setVisible(visible
);
88 addPage(myCreateGeneralProjectPage
);
89 myProjectsImportPage
= new GitProjectsImportPage() {
91 public void setVisible(boolean visible
) {
92 setProjectsList(mySelectionPage
.getPath());
93 super.setVisible(visible
);
96 addPage(myProjectsImportPage
);
100 public IWizardPage
getNextPage(IWizardPage page
) {
101 if (page
== mySelectionPage
) {
102 switch (mySelectionPage
.getWizardSelection()) {
103 case GitSelectWizardPage
.EXISTING_PROJECTS_WIZARD
:
104 return myProjectsImportPage
;
105 case GitSelectWizardPage
.NEW_WIZARD
:
107 case GitSelectWizardPage
.GENERAL_WIZARD
:
108 return myCreateGeneralProjectPage
;
110 return super.getNextPage(page
);
111 } else if (page
== myCreateGeneralProjectPage
112 || page
== myProjectsImportPage
) {
115 return super.getNextPage(page
);
119 public boolean canFinish() {
120 switch (mySelectionPage
.getWizardSelection()) {
121 case GitSelectWizardPage
.EXISTING_PROJECTS_WIZARD
:
122 return myProjectsImportPage
.isPageComplete();
123 case GitSelectWizardPage
.NEW_WIZARD
:
125 case GitSelectWizardPage
.GENERAL_WIZARD
:
126 return myCreateGeneralProjectPage
.isPageComplete();
128 return super.canFinish();
132 public boolean performFinish() {
134 getContainer().run(true, true, new IRunnableWithProgress() {
136 public void run(IProgressMonitor monitor
)
137 throws InvocationTargetException
, InterruptedException
{
138 importProjects(monitor
);
141 } catch (InvocationTargetException e
) {
143 .handleError(e
.getCause().getMessage(), e
.getCause(), true);
145 } catch (InterruptedException e
) {
146 Activator
.handleError(
147 UIText
.GitCreateProjectViaWizardWizard_AbortedMessage
, e
,
154 private void importProjects(IProgressMonitor monitor
)
155 throws InvocationTargetException
, InterruptedException
{
156 switch (mySelectionPage
.getWizardSelection()) {
157 case GitSelectWizardPage
.EXISTING_PROJECTS_WIZARD
: {
158 final Set
<ProjectRecord
> projectsToCreate
= new HashSet
<>();
159 final List
<IWorkingSet
> workingSets
= new ArrayList
<>();
160 // get the data from the page in the UI thread
161 PlatformUI
.getWorkbench().getDisplay().syncExec(new Runnable() {
164 projectsToCreate
.addAll(myProjectsImportPage
165 .getCheckedProjects());
166 IWorkingSet
[] workingSetArray
= myProjectsImportPage
167 .getSelectedWorkingSets();
168 workingSets
.addAll(Arrays
.asList(workingSetArray
));
169 myProjectsImportPage
.saveWidgetValues();
172 ProjectUtils
.createProjects(projectsToCreate
,
173 workingSets
.toArray(new IWorkingSet
[0]),
177 case GitSelectWizardPage
.NEW_WIZARD
: {
178 final List
<IProject
> previousProjects
= Arrays
179 .asList(ResourcesPlugin
.getWorkspace().getRoot()
181 PlatformUI
.getWorkbench().getDisplay().syncExec(new Runnable() {
184 new NewProjectAction(PlatformUI
.getWorkbench()
185 .getActiveWorkbenchWindow()).run();
188 IWorkspaceRunnable wsr
= new IWorkspaceRunnable() {
190 public void run(IProgressMonitor actMonitor
)
191 throws CoreException
{
192 IProject
[] currentProjects
= ResourcesPlugin
.getWorkspace()
193 .getRoot().getProjects();
194 SubMonitor progress
= SubMonitor
.convert(monitor
,
195 currentProjects
.length
);
196 for (IProject current
: currentProjects
) {
197 if (!previousProjects
.contains(current
)) {
198 ConnectProviderOperation cpo
= new ConnectProviderOperation(
199 current
, myRepository
.getDirectory());
200 cpo
.execute(progress
.newChild(1));
209 ResourcesPlugin
.getWorkspace().run(wsr
, monitor
);
210 } catch (CoreException e
) {
211 throw new InvocationTargetException(e
);
215 case GitSelectWizardPage
.GENERAL_WIZARD
: {
216 final String
[] projectName
= new String
[1];
217 final boolean[] defaultLocation
= new boolean[1];
218 // get the data from the page in the UI thread
219 PlatformUI
.getWorkbench().getDisplay().syncExec(new Runnable() {
222 projectName
[0] = myCreateGeneralProjectPage
224 defaultLocation
[0] = myCreateGeneralProjectPage
225 .isDefaultLocation();
230 IWorkspaceRunnable wsr
= new IWorkspaceRunnable() {
232 public void run(IProgressMonitor actMonitor
)
233 throws CoreException
{
234 final IProjectDescription desc
= ResourcesPlugin
235 .getWorkspace().newProjectDescription(
237 if (!defaultLocation
[0]) {
238 desc
.setLocation(new Path(myGitDir
));
240 SubMonitor progress
= SubMonitor
.convert(actMonitor
, 4);
241 IProject prj
= ResourcesPlugin
.getWorkspace().getRoot()
242 .getProject(desc
.getName());
243 prj
.create(desc
, progress
.newChild(1));
244 prj
.open(progress
.newChild(1));
246 ResourcesPlugin
.getWorkspace().getRoot().refreshLocal(
247 IResource
.DEPTH_ONE
, progress
.newChild(1));
249 File repoDir
= myRepository
.getDirectory();
250 ConnectProviderOperation cpo
= new ConnectProviderOperation(
252 cpo
.execute(progress
.newChild(1));
255 ResourcesPlugin
.getWorkspace().run(wsr
, monitor
);
257 } catch (CoreException e
) {
258 throw new InvocationTargetException(e
);
266 * Add a list of paths that may match projects that this wizard can show. If
267 * no filter is set or if the filter is empty then all projects will show.
268 * If a non empty filter is set and no projects match then the wizard will
269 * not show any projects.
274 public void setFilter(List
<String
> filter
) {
279 * Gets the list of projects that will filter in a subset of all eligible
282 * @return an unmodifiable list of projects which could be empty but never
284 * @see #setFilter(List)
286 public List
<String
> getFilter() {
287 if (myFilter
== null) {
288 return Collections
.emptyList();
290 return Collections
.unmodifiableList(myFilter
);