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
;
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
;
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
{
52 public static final int EXISTING_PROJECTS_WIZARD
= 0;
55 public static final int NEW_WIZARD
= 1;
58 public static final int GENERAL_WIZARD
= 2;
60 // TODO check if we need/can support Import... wizard
61 // see also remarks in GitCreateProjectViaWizardWizard
64 public static final int ACTION_DIALOG_SHARE
= 0;
67 public static final int ACTION_AUTO_SHARE
= 1;
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
;
82 Button actionAutoShare
;
84 Button actionDialogShare
;
88 private TreeViewer tv
;
90 private final Repository initialRepository
;
92 private final String initialPath
;
97 public GitSelectWizardPage() {
98 super(GitSelectWizardPage
.class.getName());
99 setTitle(UIText
.GitImportWithDirectoriesPage_PageTitle
);
100 setMessage(UIText
.GitImportWithDirectoriesPage_PageMessage
);
101 initialRepository
= null;
106 * Default constructor
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
;
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();
135 public void setRepository(Repository repo
) {
136 List
<WorkingDirNode
> input
= new ArrayList
<WorkingDirNode
>();
138 input
.add(new WorkingDirNode(null, repo
));
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() {
153 public void widgetSelected(SelectionEvent e
) {
154 tv
.getTree().setEnabled(!newProjectWizard
.getSelection());
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
);
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(
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
);
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();
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);
209 generalWizard
.setSelection(true);
212 newProjectWizard
.setSelection(true);
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);
227 case ACTION_DIALOG_SHARE
:
228 actionDialogShare
.setSelection(true);
230 case ACTION_NO_SHARE
:
231 actionNothing
.setSelection(true);
235 tv
= new TreeViewer(main
, SWT
.SINGLE
| SWT
.H_SCROLL
| SWT
.V_SCROLL
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
) {
250 if (initialRepository
!= null) {
251 List
<WorkingDirNode
> input
= new ArrayList
<WorkingDirNode
>();
252 WorkingDirNode node
= new WorkingDirNode(null, initialRepository
);
255 // select the working directory as default
256 if (initialPath
== null)
257 tv
.setSelection(new StructuredSelection(input
.get(0)));
259 RepositoryTreeNode parentNode
= node
;
261 IPath fullPath
= new Path(initialPath
);
262 IPath workdirPath
= new Path(initialRepository
.getWorkDir()
264 if (workdirPath
.isPrefixOf(fullPath
)) {
265 IPath relPath
= fullPath
.removeFirstSegments(workdirPath
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(
273 parentNode
= childFolder
;
279 tv
.setSelection(new StructuredSelection(parentNode
));
283 tv
.getTree().setEnabled(!newProjectWizard
.getSelection());
289 * @return the wizard selection
291 public int getWizardSelection() {
292 if (importExisting
.getSelection())
293 return EXISTING_PROJECTS_WIZARD
;
294 if (newProjectWizard
.getSelection())
296 if (generalWizard
.getSelection())
297 return GENERAL_WIZARD
;
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
;
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);
332 IStructuredSelection sel
= (IStructuredSelection
) tv
.getSelection();
335 setErrorMessage(UIText
.GitImportWithDirectoriesPage_SelectFolderMessage
);
338 RepositoryTreeNode node
= (RepositoryTreeNode
) sel
340 if (node
.getType() != RepositoryTreeNodeType
.FOLDER
341 && node
.getType() != RepositoryTreeNodeType
.WORKINGDIR
) {
342 setErrorMessage(UIText
.GitImportWithDirectoriesPage_SelectFolderMessage
);
346 setPageComplete(getErrorMessage() == null);