refactor: simplify collection.toArray()
[egit/eclipse.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / clone / GitCreateProjectViaWizardWizard.java
blob0c4f3a82d149ea25e81ab3853d08bfb080d09424
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
10 * Contributors:
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;
16 import java.io.File;
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;
23 import java.util.Set;
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;
46 /**
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;
62 /**
63 * @param repository
64 * @param path
66 public GitCreateProjectViaWizardWizard(Repository repository, String path) {
67 super();
68 myRepository = repository;
69 myGitDir = path;
70 setNeedsProgressMonitor(true);
71 setWindowTitle(NLS.bind(
72 UIText.GitCreateProjectViaWizardWizard_WizardTitle,
73 myRepository.getDirectory().getPath()));
74 setDialogSettings(GitImportWizard.getImportWizardDialogSettings());
77 @Override
78 public void addPages() {
79 mySelectionPage = new GitSelectWizardPage(myRepository, myGitDir);
80 addPage(mySelectionPage);
81 myCreateGeneralProjectPage = new GitCreateGeneralProjectPage(myGitDir) {
82 @Override
83 public void setVisible(boolean visible) {
84 setPath(mySelectionPage.getPath());
85 super.setVisible(visible);
88 addPage(myCreateGeneralProjectPage);
89 myProjectsImportPage = new GitProjectsImportPage() {
90 @Override
91 public void setVisible(boolean visible) {
92 setProjectsList(mySelectionPage.getPath());
93 super.setVisible(visible);
96 addPage(myProjectsImportPage);
99 @Override
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:
106 return null;
107 case GitSelectWizardPage.GENERAL_WIZARD:
108 return myCreateGeneralProjectPage;
110 return super.getNextPage(page);
111 } else if (page == myCreateGeneralProjectPage
112 || page == myProjectsImportPage) {
113 return null;
115 return super.getNextPage(page);
118 @Override
119 public boolean canFinish() {
120 switch (mySelectionPage.getWizardSelection()) {
121 case GitSelectWizardPage.EXISTING_PROJECTS_WIZARD:
122 return myProjectsImportPage.isPageComplete();
123 case GitSelectWizardPage.NEW_WIZARD:
124 return true;
125 case GitSelectWizardPage.GENERAL_WIZARD:
126 return myCreateGeneralProjectPage.isPageComplete();
128 return super.canFinish();
131 @Override
132 public boolean performFinish() {
133 try {
134 getContainer().run(true, true, new IRunnableWithProgress() {
135 @Override
136 public void run(IProgressMonitor monitor)
137 throws InvocationTargetException, InterruptedException {
138 importProjects(monitor);
141 } catch (InvocationTargetException e) {
142 Activator
143 .handleError(e.getCause().getMessage(), e.getCause(), true);
144 return false;
145 } catch (InterruptedException e) {
146 Activator.handleError(
147 UIText.GitCreateProjectViaWizardWizard_AbortedMessage, e,
148 true);
149 return false;
151 return true;
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() {
162 @Override
163 public void run() {
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]),
174 monitor);
175 break;
177 case GitSelectWizardPage.NEW_WIZARD: {
178 final List<IProject> previousProjects = Arrays
179 .asList(ResourcesPlugin.getWorkspace().getRoot()
180 .getProjects());
181 PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
182 @Override
183 public void run() {
184 new NewProjectAction(PlatformUI.getWorkbench()
185 .getActiveWorkbenchWindow()).run();
188 IWorkspaceRunnable wsr = new IWorkspaceRunnable() {
189 @Override
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));
201 } else {
202 progress.worked(1);
208 try {
209 ResourcesPlugin.getWorkspace().run(wsr, monitor);
210 } catch (CoreException e) {
211 throw new InvocationTargetException(e);
213 break;
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() {
220 @Override
221 public void run() {
222 projectName[0] = myCreateGeneralProjectPage
223 .getProjectName();
224 defaultLocation[0] = myCreateGeneralProjectPage
225 .isDefaultLocation();
229 try {
230 IWorkspaceRunnable wsr = new IWorkspaceRunnable() {
231 @Override
232 public void run(IProgressMonitor actMonitor)
233 throws CoreException {
234 final IProjectDescription desc = ResourcesPlugin
235 .getWorkspace().newProjectDescription(
236 projectName[0]);
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(
251 prj, repoDir);
252 cpo.execute(progress.newChild(1));
255 ResourcesPlugin.getWorkspace().run(wsr, monitor);
257 } catch (CoreException e) {
258 throw new InvocationTargetException(e);
260 break;
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.
271 * @param filter
272 * a list of paths
274 public void setFilter(List<String> filter) {
275 myFilter = filter;
279 * Gets the list of projects that will filter in a subset of all eligible
280 * projects.
282 * @return an unmodifiable list of projects which could be empty but never
283 * null.
284 * @see #setFilter(List)
286 public List<String> getFilter() {
287 if (myFilter == null) {
288 return Collections.emptyList();
290 return Collections.unmodifiableList(myFilter);