Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 62e8f73d8e6a28821e5e76e581c18757abafa664 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*******************************************************************************
 * Copyright (C) 2009, Robin Rosenberg
 * Copyright (C) 2009, Mykola Nikishov <mn@mn.com.ua>
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
package org.eclipse.egit.ui.internal.sharing;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.egit.core.Activator;
import org.eclipse.egit.core.project.RepositoryFinder;
import org.eclipse.egit.core.project.RepositoryMapping;
import org.eclipse.egit.ui.UIIcons;
import org.eclipse.egit.ui.UIText;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.swt.widgets.TreeItem;

/**
 * Wizard page for connecting projects to Git repositories.
 */
class ExistingOrNewPage extends WizardPage {

	private final SharingWizard myWizard;
	private Button button;
	private Tree tree;
	private Text repositoryToCreate;
	private IPath minumumPath;
	private Text dotGitSegment;

	ExistingOrNewPage(SharingWizard w) {
		super(ExistingOrNewPage.class.getName());
		setTitle(UIText.ExistingOrNewPage_title);
		setDescription(UIText.ExistingOrNewPage_description);
		setImageDescriptor(UIIcons.WIZBAN_CONNECT_REPO);
		this.myWizard = w;
	}

	public void createControl(Composite parent) {
		Group g = new Group(parent, SWT.NONE);
		g.setLayout(new GridLayout(3,false));
		g.setLayoutData(GridDataFactory.fillDefaults().grab(true, true).create());
		tree = new Tree(g, SWT.BORDER|SWT.MULTI|SWT.FULL_SELECTION);
		tree.setHeaderVisible(true);
		tree.setLayout(new GridLayout());
		tree.setLayoutData(GridDataFactory.fillDefaults().grab(true, true).span(3,1).create());
		tree.addSelectionListener(new SelectionAdapter() {

			public void widgetSelected(SelectionEvent e) {
				TreeItem t = (TreeItem) e.item;
				for(TreeItem ti : t.getItems())
					tree.deselect(ti);
				if (t.getParentItem() != null) {
					tree.deselect(t.getParentItem());
					for(TreeItem ti : t.getParentItem().getItems())
						if (ti != t)
							tree.deselect(ti);
				}
				Set<IProject> projects = new HashSet<IProject>();
				for (TreeItem treeItem : tree.getSelection()) {
					if (treeItem.getData() ==  null && treeItem.getParentItem() != null) {
						treeItem = treeItem.getParentItem();
					}
					final IProject project = (IProject) treeItem.getData();
					if (projects.contains(project))
							tree.deselect(treeItem);
					projects.add(project);
				}
			}
		});
		TreeColumn c1 = new TreeColumn(tree,SWT.NONE);
		c1.setText(UIText.ExistingOrNewPage_HeaderProject);
		c1.setWidth(100);
		TreeColumn c2 = new TreeColumn(tree,SWT.NONE);
		c2.setText(UIText.ExistingOrNewPage_HeaderPath);
		c2.setWidth(400);
		TreeColumn c3 = new TreeColumn(tree,SWT.NONE);
		c3.setText(UIText.ExistingOrNewPage_HeaderRepository);
		c3.setWidth(200);
		for (IProject project : myWizard.projects) {
			TreeItem treeItem = new TreeItem(tree, SWT.NONE);
			treeItem.setData(project);
			treeItem.setText(0, project.getName());
			treeItem.setText(1, project.getLocation().toOSString());
			RepositoryFinder repositoryFinder = new RepositoryFinder(project);
			Collection<RepositoryMapping> mappings;
			try {
				mappings = repositoryFinder.find(new NullProgressMonitor());
				Iterator<RepositoryMapping> mi = mappings.iterator();
				RepositoryMapping m = mi.hasNext() ? mi.next() : null;
				if (m == null) {
					// no mapping found, enable repository creation
					treeItem.setText(2, ""); //$NON-NLS-1$
				} else {
					// at least one mapping found
					fillTreeItemWithGitDirectory(m, treeItem, false);
				}

				while (mi.hasNext()) {	// fill in additional mappings
					m = mi.next();
					TreeItem treeItem2 = new TreeItem(treeItem, SWT.NONE);
					treeItem2.setData(m.getContainer().getProject());
					fillTreeItemWithGitDirectory(m, treeItem2, true);
				}
			} catch (CoreException e) {
				TreeItem treeItem2 = new TreeItem(treeItem, SWT.BOLD|SWT.ITALIC);
				treeItem2.setText(e.getMessage());
			}
		}

		button = new Button(g, SWT.PUSH);
		button.setLayoutData(GridDataFactory.fillDefaults().create());
		button.setText(UIText.ExistingOrNewPage_CreateButton);
		button.addSelectionListener(new SelectionListener() {
			public void widgetSelected(SelectionEvent e) {
				File gitDir = new File(repositoryToCreate.getText(),Constants.DOT_GIT);
				try {
					Repository repository = new Repository(gitDir);
					repository.create();
					for (IProject project : getProjects().keySet()) {
						// If we don't refresh the project directories right
						// now we won't later know that a .git directory
						// exists within it and we won't mark the .git
						// directory as a team-private member. Failure
						// to do so might allow someone to delete
						// the .git directory without us stopping them.
						// (Half lie, we should optimize so we do not
						// refresh when the .git is not within the project)
						//
						if (!gitDir.toString().contains("..")) //$NON-NLS-1$
							project.refreshLocal(IResource.DEPTH_ONE,
									new NullProgressMonitor());
					}
				} catch (IOException e1) {
					MessageDialog.openError(getShell(), UIText.ExistingOrNewPage_ErrorFailedToCreateRepository, gitDir.toString() + ":\n" + e1.getMessage()); //$NON-NLS-1$
					Activator.logError("Failed to create repository at " + gitDir, e1); //$NON-NLS-1$
				} catch (CoreException e2) {
					Activator.logError(UIText.ExistingOrNewPage_ErrorFailedToRefreshRepository + gitDir, e2);
				}
				for (TreeItem ti : tree.getSelection()) {
					ti.setText(2, gitDir.toString());
				}
				updateCreateOptions();
				getContainer().updateButtons();
			}
			public void widgetDefaultSelected(SelectionEvent e) {
			}
		});
		repositoryToCreate = new Text(g, SWT.SINGLE | SWT.BORDER);
		repositoryToCreate.setLayoutData(GridDataFactory.fillDefaults().grab(true, false).span(1,1).create());
		repositoryToCreate.addListener(SWT.Modify, new Listener() {
			public void handleEvent(Event e) {
				if (e.text == null)
					return;
				IPath fromOSString = Path.fromOSString(e.text);
				button.setEnabled(minumumPath
						.matchingFirstSegments(fromOSString) == fromOSString
						.segmentCount());
			}
		});
		dotGitSegment = new Text(g ,SWT.NONE);
		dotGitSegment.setEnabled(false);
		dotGitSegment.setEditable(false);
		dotGitSegment.setText(File.separatorChar + Constants.DOT_GIT);
		dotGitSegment.setLayoutData(GridDataFactory.fillDefaults().align(SWT.LEFT, SWT.CENTER).create());

		tree.addSelectionListener(new SelectionListener() {
			public void widgetSelected(SelectionEvent e) {
				updateCreateOptions();
			}
			public void widgetDefaultSelected(SelectionEvent e) {
				// Empty
			}
		});
		updateCreateOptions();
		setControl(g);
	}

	private void fillTreeItemWithGitDirectory(RepositoryMapping m, TreeItem treeItem2, boolean isAlternative) {
		if (m.getGitDir() == null)
			treeItem2.setText(2, UIText.ExistingOrNewPage_SymbolicValueEmptyMapping);
		else {
			IPath container = m.getContainerPath();
			if (!container.isEmpty())
				container = container.addTrailingSeparator();
			IPath relativePath = container.append(m.getGitDir());
			if (isAlternative)
				treeItem2.setText(0, relativePath.removeLastSegments(1).addTrailingSeparator().toString());
			treeItem2.setText(2, relativePath.toString());
		}
	}

	private void updateCreateOptions() {
		minumumPath = null;
		IPath p = null;
		for (TreeItem ti : tree.getSelection()) {
			String path = ti.getText(2);
			if (!path.equals("")) { //$NON-NLS-1$
				p = null;
				break;
			}
			String gitDirParentCandidate = ti.getText(1);
			IPath thisPath = Path.fromOSString(gitDirParentCandidate);
			if (p == null)
				p = thisPath;
			else {
				int n = p.matchingFirstSegments(thisPath);
				p = p.removeLastSegments(p.segmentCount() - n);
			}
		}
		minumumPath = p;
		if (p != null) {
			repositoryToCreate.setText(p.toOSString());
		} else {
			repositoryToCreate.setText(""); //$NON-NLS-1$
		}
		button.setEnabled(p != null);
		repositoryToCreate.setEnabled(p != null);
		dotGitSegment.setEnabled(p != null);
		getContainer().updateButtons();
	}

	@Override
	public boolean isPageComplete() {
		if (tree.getSelectionCount() == 0)
			return false;
		for (TreeItem ti : tree.getSelection()) {
			String path = ti.getText(2);
			if (path.equals("")) { //$NON-NLS-1$
				return false;
			}
		}
		return true;
	}

	/**
	 * @return map between project and repository root directory (converted to an
	 *         absolute path) for all projects selected by user
	 */
	public Map<IProject, File> getProjects() {
		final TreeItem[] selection = tree.getSelection();
		Map<IProject, File> ret = new HashMap<IProject, File>(selection.length);
		for (int i = 0; i < selection.length; ++i) {
			TreeItem treeItem = selection[i];
			while (treeItem.getData() ==  null && treeItem.getParentItem() != null) {
				treeItem = treeItem.getParentItem();
			}

			final IProject project = (IProject) treeItem.getData();
			final IPath selectedRepo = Path.fromOSString(treeItem.getText(2));
			IPath localPathToRepo = selectedRepo;
			if (!selectedRepo.isAbsolute()) {
				localPathToRepo = project.getLocation().append(selectedRepo);
			}
			ret.put(project, localPathToRepo.toFile());
		}
		return ret;
	}
}

Back to the top