Git Import Projects: always show directories
[egit.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / clone / GitSelectWizardPage.java
blob6469f40f3384224f1ada7e35a2bfd6a01fcd003b
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.io.File;
14 import java.util.ArrayList;
15 import java.util.List;
17 import org.eclipse.core.runtime.IPath;
18 import org.eclipse.core.runtime.Path;
19 import org.eclipse.egit.ui.Activator;
20 import org.eclipse.egit.ui.UIText;
21 import org.eclipse.egit.ui.internal.repository.RepositoriesViewContentProvider;
22 import org.eclipse.egit.ui.internal.repository.RepositoriesViewLabelProvider;
23 import org.eclipse.egit.ui.internal.repository.tree.FolderNode;
24 import org.eclipse.egit.ui.internal.repository.tree.RepositoryTreeNode;
25 import org.eclipse.egit.ui.internal.repository.tree.RepositoryTreeNodeType;
26 import org.eclipse.egit.ui.internal.repository.tree.WorkingDirNode;
27 import org.eclipse.jface.dialogs.IDialogSettings;
28 import org.eclipse.jface.layout.GridDataFactory;
29 import org.eclipse.jface.viewers.ISelectionChangedListener;
30 import org.eclipse.jface.viewers.IStructuredSelection;
31 import org.eclipse.jface.viewers.SelectionChangedEvent;
32 import org.eclipse.jface.viewers.StructuredSelection;
33 import org.eclipse.jface.viewers.TreeViewer;
34 import org.eclipse.jface.wizard.WizardPage;
35 import org.eclipse.jgit.lib.Repository;
36 import org.eclipse.swt.SWT;
37 import org.eclipse.swt.events.SelectionAdapter;
38 import org.eclipse.swt.events.SelectionEvent;
39 import org.eclipse.swt.events.SelectionListener;
40 import org.eclipse.swt.layout.GridLayout;
41 import org.eclipse.swt.widgets.Button;
42 import org.eclipse.swt.widgets.Composite;
43 import org.eclipse.swt.widgets.Group;
45 /**
46 * Asks the user to select a wizard and what to do with the imported projects
47 * (automatic/manual/no share)
49 public class GitSelectWizardPage extends WizardPage {
51 /** */
52 public static final int EXISTING_PROJECTS_WIZARD = 0;
54 /** */
55 public static final int NEW_WIZARD = 1;
57 /** */
58 public static final int GENERAL_WIZARD = 2;
60 // TODO check if we need/can support Import... wizard
61 // see also remarks in GitCreateProjectViaWizardWizard
63 /** */
64 public static final int ACTION_DIALOG_SHARE = 0;
66 /** */
67 public static final int ACTION_AUTO_SHARE = 1;
69 /** */
70 public static final int ACTION_NO_SHARE = 2;
72 private final String PREF_WIZ = getName() + "WizardSel"; //$NON-NLS-1$
74 private final String PREF_ACT = getName() + "ActionSel"; //$NON-NLS-1$
76 Button importExisting;
78 Button newProjectWizard;
80 Button generalWizard;
82 Button actionAutoShare;
84 Button actionDialogShare;
86 Button actionNothing;
88 private TreeViewer tv;
90 private final Repository initialRepository;
92 private final String initialPath;
94 /**
95 * Default constructor
97 public GitSelectWizardPage() {
98 super(GitSelectWizardPage.class.getName());
99 setTitle(UIText.GitImportWithDirectoriesPage_PageTitle);
100 setMessage(UIText.GitImportWithDirectoriesPage_PageMessage);
101 initialRepository = null;
102 initialPath = null;
106 * Default constructor
108 * @param repository
109 * @param path
111 public GitSelectWizardPage(Repository repository, String path) {
112 super(GitSelectWizardPage.class.getName());
113 setTitle(UIText.GitImportWithDirectoriesPage_PageTitle);
114 setMessage(UIText.GitImportWithDirectoriesPage_PageMessage);
115 initialRepository = repository;
116 initialPath = path;
120 * @return the selected path
122 public String getPath() {
123 IStructuredSelection sel = (IStructuredSelection) tv.getSelection();
124 RepositoryTreeNode node = (RepositoryTreeNode) sel.getFirstElement();
125 if (node != null && node.getType() == RepositoryTreeNodeType.FOLDER)
126 return ((File) node.getObject()).getPath();
127 if (node != null && node.getType() == RepositoryTreeNodeType.WORKINGDIR)
128 return node.getRepository().getWorkDir().getPath();
129 return null;
133 * @param repo
135 public void setRepository(Repository repo) {
136 List<WorkingDirNode> input = new ArrayList<WorkingDirNode>();
137 if (repo != null)
138 input.add(new WorkingDirNode(null, repo));
139 tv.setInput(input);
140 // select the working directory as default
141 tv.setSelection(new StructuredSelection(input.get(0)));
144 public void createControl(Composite parent) {
146 Composite main = new Composite(parent, SWT.NO_RADIO_GROUP);
148 main.setLayout(new GridLayout(1, false));
150 SelectionListener sl = new SelectionAdapter() {
152 @Override
153 public void widgetSelected(SelectionEvent e) {
154 tv.getTree().setEnabled(!newProjectWizard.getSelection());
155 checkPage();
159 Group wizardType = new Group(main, SWT.SHADOW_ETCHED_IN);
160 GridDataFactory.fillDefaults().grab(true, false).applyTo(wizardType);
161 wizardType.setText(UIText.GitSelectWizardPage_ProjectCreationHeader);
162 wizardType.setLayout(new GridLayout(1, false));
164 importExisting = new Button(wizardType, SWT.RADIO);
165 importExisting.setText(UIText.GitSelectWizardPage_ImportExistingButton);
166 importExisting.addSelectionListener(sl);
168 newProjectWizard = new Button(wizardType, SWT.RADIO);
169 newProjectWizard
170 .setText(UIText.GitSelectWizardPage_UseNewProjectsWizardButton);
171 newProjectWizard.addSelectionListener(sl);
173 generalWizard = new Button(wizardType, SWT.RADIO);
174 generalWizard.setText(UIText.GitSelectWizardPage_ImportAsGeneralButton);
175 generalWizard.addSelectionListener(sl);
177 Group afterImportAction = new Group(main, SWT.SHADOW_ETCHED_IN);
178 GridDataFactory.fillDefaults().grab(true, false).applyTo(
179 afterImportAction);
180 afterImportAction
181 .setText(UIText.GitSelectWizardPage_SharingProjectsHeader);
182 afterImportAction.setLayout(new GridLayout(1, false));
184 actionAutoShare = new Button(afterImportAction, SWT.RADIO);
185 actionAutoShare.setText(UIText.GitSelectWizardPage_AutoShareButton);
186 actionAutoShare.addSelectionListener(sl);
188 actionDialogShare = new Button(afterImportAction, SWT.RADIO);
189 actionDialogShare
190 .setText(UIText.GitSelectWizardPage_InteractiveShareButton);
191 actionDialogShare.addSelectionListener(sl);
193 actionNothing = new Button(afterImportAction, SWT.RADIO);
194 actionNothing.setText(UIText.GitSelectWizardPage_NoShareButton);
195 actionNothing.addSelectionListener(sl);
197 IDialogSettings settings = Activator.getDefault().getDialogSettings();
198 int previousWiz;
199 try {
200 previousWiz = settings.getInt(PREF_WIZ);
201 } catch (NumberFormatException e) {
202 previousWiz = EXISTING_PROJECTS_WIZARD;
204 switch (previousWiz) {
205 case EXISTING_PROJECTS_WIZARD:
206 importExisting.setSelection(true);
207 break;
208 case GENERAL_WIZARD:
209 generalWizard.setSelection(true);
210 break;
211 case NEW_WIZARD:
212 newProjectWizard.setSelection(true);
213 break;
217 int previousAct;
218 try {
219 previousAct = settings.getInt(PREF_ACT);
220 } catch (NumberFormatException e) {
221 previousAct = ACTION_AUTO_SHARE;
223 switch (previousAct) {
224 case ACTION_AUTO_SHARE:
225 actionAutoShare.setSelection(true);
226 break;
227 case ACTION_DIALOG_SHARE:
228 actionDialogShare.setSelection(true);
229 break;
230 case ACTION_NO_SHARE:
231 actionNothing.setSelection(true);
232 break;
235 tv = new TreeViewer(main, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL
236 | SWT.BORDER);
237 RepositoriesViewContentProvider cp = new RepositoriesViewContentProvider();
238 tv.setContentProvider(cp);
239 GridDataFactory.fillDefaults().grab(true, true).hint(SWT.DEFAULT, 200)
240 .applyTo(tv.getTree());
241 new RepositoriesViewLabelProvider(tv);
243 tv.addSelectionChangedListener(new ISelectionChangedListener() {
245 public void selectionChanged(SelectionChangedEvent event) {
246 checkPage();
250 if (initialRepository != null) {
251 List<WorkingDirNode> input = new ArrayList<WorkingDirNode>();
252 WorkingDirNode node = new WorkingDirNode(null, initialRepository);
253 input.add(node);
254 tv.setInput(input);
255 // select the working directory as default
256 if (initialPath == null)
257 tv.setSelection(new StructuredSelection(input.get(0)));
258 else {
259 RepositoryTreeNode parentNode = node;
261 IPath fullPath = new Path(initialPath);
262 IPath workdirPath = new Path(initialRepository.getWorkDir()
263 .getPath());
264 if (workdirPath.isPrefixOf(fullPath)) {
265 IPath relPath = fullPath.removeFirstSegments(workdirPath
266 .segmentCount());
267 for (String segment : relPath.segments()) {
268 for (Object child : cp.getChildren(parentNode)) {
269 if (child instanceof FolderNode) {
270 FolderNode childFolder = (FolderNode) child;
271 if (childFolder.getObject().getName().equals(
272 segment)) {
273 parentNode = childFolder;
274 break;
279 tv.setSelection(new StructuredSelection(parentNode));
283 tv.getTree().setEnabled(!newProjectWizard.getSelection());
284 setControl(main);
289 * @return the wizard selection
291 public int getWizardSelection() {
292 if (importExisting.getSelection())
293 return EXISTING_PROJECTS_WIZARD;
294 if (newProjectWizard.getSelection())
295 return NEW_WIZARD;
296 if (generalWizard.getSelection())
297 return GENERAL_WIZARD;
298 return -1;
302 * @return the action selection
304 public int getActionSelection() {
305 if (actionAutoShare.getSelection())
306 return ACTION_AUTO_SHARE;
307 if (actionDialogShare.getSelection())
308 return ACTION_DIALOG_SHARE;
309 if (actionNothing.getSelection())
310 return ACTION_NO_SHARE;
311 return -1;
315 * check routine
317 protected void checkPage() {
319 // we save the selected radio button in the preferences
320 IDialogSettings settings = Activator.getDefault().getDialogSettings();
322 settings.put(PREF_WIZ, getWizardSelection());
323 settings.put(PREF_ACT, getActionSelection());
325 setErrorMessage(null);
327 if (newProjectWizard.getSelection()) {
328 setPageComplete(true);
329 return;
332 IStructuredSelection sel = (IStructuredSelection) tv.getSelection();
333 try {
334 if (sel.isEmpty()) {
335 setErrorMessage(UIText.GitImportWithDirectoriesPage_SelectFolderMessage);
336 return;
338 RepositoryTreeNode node = (RepositoryTreeNode) sel
339 .getFirstElement();
340 if (node.getType() != RepositoryTreeNodeType.FOLDER
341 && node.getType() != RepositoryTreeNodeType.WORKINGDIR) {
342 setErrorMessage(UIText.GitImportWithDirectoriesPage_SelectFolderMessage);
343 return;
345 } finally {
346 setPageComplete(getErrorMessage() == null);