1 /*******************************************************************************
2 * Copyright (C) 2009, Robin Rosenberg
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * See LICENSE for the full license text, also available.
7 *******************************************************************************/
8 package org
.spearce
.egit
.ui
.internal
.sharing
;
11 import java
.io
.IOException
;
12 import java
.util
.Collection
;
13 import java
.util
.Iterator
;
15 import org
.eclipse
.core
.resources
.IProject
;
16 import org
.eclipse
.core
.resources
.IResource
;
17 import org
.eclipse
.core
.runtime
.CoreException
;
18 import org
.eclipse
.core
.runtime
.IPath
;
19 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
20 import org
.eclipse
.core
.runtime
.Path
;
21 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
22 import org
.eclipse
.jface
.layout
.GridDataFactory
;
23 import org
.eclipse
.jface
.wizard
.WizardPage
;
24 import org
.eclipse
.swt
.SWT
;
25 import org
.eclipse
.swt
.events
.SelectionEvent
;
26 import org
.eclipse
.swt
.events
.SelectionListener
;
27 import org
.eclipse
.swt
.layout
.GridLayout
;
28 import org
.eclipse
.swt
.widgets
.Button
;
29 import org
.eclipse
.swt
.widgets
.Composite
;
30 import org
.eclipse
.swt
.widgets
.Event
;
31 import org
.eclipse
.swt
.widgets
.Group
;
32 import org
.eclipse
.swt
.widgets
.Listener
;
33 import org
.eclipse
.swt
.widgets
.Text
;
34 import org
.eclipse
.swt
.widgets
.Tree
;
35 import org
.eclipse
.swt
.widgets
.TreeColumn
;
36 import org
.eclipse
.swt
.widgets
.TreeItem
;
37 import org
.spearce
.egit
.core
.Activator
;
38 import org
.spearce
.egit
.core
.project
.RepositoryFinder
;
39 import org
.spearce
.egit
.core
.project
.RepositoryMapping
;
40 import org
.spearce
.egit
.ui
.UIIcons
;
41 import org
.spearce
.egit
.ui
.UIText
;
42 import org
.spearce
.jgit
.lib
.Repository
;
45 * Wizard page for connecting projects to Git repositories.
47 class ExistingOrNewPage
extends WizardPage
{
49 private final SharingWizard myWizard
;
50 private Button button
;
52 private Text repositoryToCreate
;
53 private IPath minumumPath
;
55 ExistingOrNewPage(SharingWizard w
) {
56 super(ExistingOrNewPage
.class.getName());
57 setTitle(UIText
.ExistingOrNewPage_title
);
58 setDescription(UIText
.ExistingOrNewPage_description
);
59 setImageDescriptor(UIIcons
.WIZBAN_CONNECT_REPO
);
63 public void createControl(Composite parent
) {
64 Group g
= new Group(parent
, SWT
.NONE
);
65 g
.setLayout(new GridLayout(3,false));
66 g
.setLayoutData(GridDataFactory
.fillDefaults().grab(true, true).create());
67 tree
= new Tree(g
, SWT
.BORDER
|SWT
.MULTI
);
68 tree
.setHeaderVisible(true);
69 tree
.setLayout(new GridLayout());
70 tree
.setLayoutData(GridDataFactory
.fillDefaults().grab(true, true).span(3,1).create());
71 TreeColumn c1
= new TreeColumn(tree
,SWT
.NONE
);
72 c1
.setText(UIText
.ExistingOrNewPage_HeaderProject
);
74 TreeColumn c2
= new TreeColumn(tree
,SWT
.NONE
);
75 c2
.setText(UIText
.ExistingOrNewPage_HeaderPath
);
77 TreeColumn c3
= new TreeColumn(tree
,SWT
.NONE
);
78 c3
.setText(UIText
.ExistingOrNewPage_HeaderRepository
);
80 for (IProject project
: myWizard
.projects
) {
81 TreeItem treeItem
= new TreeItem(tree
, SWT
.NONE
);
82 treeItem
.setData(project
);
83 treeItem
.setText(0, project
.getName());
84 treeItem
.setText(1, project
.getLocation().toOSString());
85 RepositoryFinder repositoryFinder
= new RepositoryFinder(project
);
86 Collection
<RepositoryMapping
> find
;
88 find
= repositoryFinder
.find(new NullProgressMonitor());
90 treeItem
.setText(2, ""); //$NON-NLS-1$
92 Iterator
<RepositoryMapping
> mi
= find
.iterator();
93 RepositoryMapping m
= mi
.next();
94 if (m
.getGitDir() == null)
95 treeItem
.setText(2,UIText
.ExistingOrNewPage_SymbolicValueEmptyMapping
);
97 treeItem
.setText(2, m
.getGitDir());
98 while (mi
.hasNext()) {
99 TreeItem treeItem2
= new TreeItem(treeItem
, SWT
.NONE
);
100 if (m
.getGitDir() == null)
101 treeItem2
.setText(2,UIText
.ExistingOrNewPage_SymbolicValueEmptyMapping
);
103 treeItem2
.setText(2,m
.getGitDir());
106 } catch (CoreException e
) {
107 TreeItem treeItem2
= new TreeItem(treeItem
, SWT
.BOLD
|SWT
.ITALIC
);
108 treeItem2
.setText(e
.getMessage());
112 button
= new Button(g
, SWT
.PUSH
);
113 button
.setLayoutData(GridDataFactory
.fillDefaults().create());
114 button
.setText(UIText
.ExistingOrNewPage_CreateButton
);
115 button
.addSelectionListener(new SelectionListener() {
116 public void widgetSelected(SelectionEvent e
) {
117 File gitDir
= new File(repositoryToCreate
.getText(),".git");
119 Repository repository
= new Repository(gitDir
);
121 for (IProject project
: getProjects()) {
122 // If we don't refresh the project directories right
123 // now we won't later know that a .git directory
124 // exists within it and we won't mark the .git
125 // directory as a team-private member. Failure
126 // to do so might allow someone to delete
127 // the .git directory without us stopping them.
128 // (Half lie, we should optimize so we do not
129 // refresh when the .git is not within the project)
131 if (!gitDir
.toString().contains("..")) //$NON-NLS-1$
132 project
.refreshLocal(IResource
.DEPTH_ONE
,
133 new NullProgressMonitor());
135 } catch (IOException e1
) {
136 MessageDialog
.openError(getShell(), UIText
.ExistingOrNewPage_ErrorFailedToCreateRepository
, gitDir
.toString() + ":\n" + e1
.getMessage());
137 Activator
.logError("Failed to create repository at " + gitDir
, e1
); //$NON-NLS-1$
138 } catch (CoreException e2
) {
139 Activator
.logError(UIText
.ExistingOrNewPage_ErrorFailedToRefreshRepository
+ gitDir
, e2
);
141 for (TreeItem ti
: tree
.getSelection()) {
142 ti
.setText(2, gitDir
.toString());
144 updateCreateOptions();
145 getContainer().updateButtons();
147 public void widgetDefaultSelected(SelectionEvent e
) {
150 repositoryToCreate
= new Text(g
, SWT
.SINGLE
| SWT
.BORDER
);
151 repositoryToCreate
.setLayoutData(GridDataFactory
.fillDefaults().grab(true, false).span(1,1).create());
152 repositoryToCreate
.addListener(SWT
.Modify
, new Listener() {
153 public void handleEvent(Event e
) {
156 IPath fromOSString
= Path
.fromOSString(e
.text
);
157 button
.setEnabled(minumumPath
158 .matchingFirstSegments(fromOSString
) == fromOSString
162 Text l
= new Text(g
,SWT
.NONE
);
164 l
.setEditable(false);
165 l
.setText(File
.separatorChar
+ ".git"); //$NON-NLS-1$
166 l
.setLayoutData(GridDataFactory
.fillDefaults().create());
167 tree
.addSelectionListener(new SelectionListener() {
168 public void widgetSelected(SelectionEvent e
) {
169 updateCreateOptions();
171 public void widgetDefaultSelected(SelectionEvent e
) {
175 updateCreateOptions();
179 private void updateCreateOptions() {
182 for (TreeItem ti
: tree
.getSelection()) {
183 String path
= ti
.getText(2);
184 if (!path
.equals("")) { //$NON-NLS-1$
188 String gitDirParentCandidate
= ti
.getText(1);
189 IPath thisPath
= Path
.fromOSString(gitDirParentCandidate
);
193 int n
= p
.matchingFirstSegments(thisPath
);
194 p
= p
.removeLastSegments(p
.segmentCount() - n
);
199 repositoryToCreate
.setText(p
.toOSString());
201 repositoryToCreate
.setText(""); //$NON-NLS-1$
203 button
.setEnabled(p
!= null);
204 repositoryToCreate
.setEnabled(p
!= null);
205 getContainer().updateButtons();
209 public boolean isPageComplete() {
210 if (tree
.getSelectionCount() == 0)
212 for (TreeItem ti
: tree
.getSelection()) {
213 String path
= ti
.getText(2);
214 if (path
.equals("")) { //$NON-NLS-1$
221 public IProject
[] getProjects() {
222 IProject
[] ret
= new IProject
[tree
.getSelection().length
];
223 for (int i
= 0; i
< ret
.length
; ++i
)
224 ret
[i
] = (IProject
)tree
.getSelection()[i
].getData();