Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: eabc4fa5128ee2426a09790fc9b1443336063ab5 (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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*******************************************************************************
 * Copyright (c) 2010 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Dariusz Luksza <dariusz@luksza.org>
 *******************************************************************************/
package org.eclipse.egit.ui.internal.synchronize;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.egit.core.project.RepositoryMapping;
import org.eclipse.egit.ui.Activator;
import org.eclipse.egit.ui.UIPreferences;
import org.eclipse.egit.ui.internal.CommonUtils;
import org.eclipse.egit.ui.internal.UIIcons;
import org.eclipse.egit.ui.internal.UIText;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.preference.JFacePreferences;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.ComboBoxCellEditor;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.StyledCellLabelProvider;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.TreeViewerColumn;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerCell;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.RefDatabase;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;

class GitSynchronizeWizardPage extends WizardPage {

	private static final IWorkspaceRoot ROOT = ResourcesPlugin.getWorkspace()
			.getRoot();

	private boolean forceFetch;

	private boolean shouldIncludeLocal = false;

	private TreeViewer treeViewer;

	private final Map<Repository, Set<IProject>> resources = new HashMap<>();

	private final Map<Repository, String> selectedBranches = new HashMap<>();

	private final Image branchImage = UIIcons.BRANCH.createImage();

	private final Image repositoryImage = UIIcons.REPOSITORY.createImage();

	GitSynchronizeWizardPage() {
		super(GitSynchronizeWizardPage.class.getName());
		setTitle(UIText.GitBranchSynchronizeWizardPage_title);
		setMessage(UIText.GitBranchSynchronizeWizardPage_description, WARNING);
	}

	@Override
	public void createControl(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout(1, false);
		layout.marginWidth = 0;
		layout.marginHeight = 0;
		composite.setLayout(layout);

		for (IProject project : ROOT.getProjects()) {
			RepositoryMapping repositoryMapping = RepositoryMapping
					.getMapping(project);
			if (repositoryMapping == null)
				continue;
			Repository repo = repositoryMapping.getRepository();
			Set<IProject> projects = resources.get(repo);
			if (projects == null) {
				projects = new HashSet<>();
				resources.put(repo, projects);
			}
			projects.add(project);
		}

		treeViewer = new TreeViewer(composite, SWT.BORDER | SWT.V_SCROLL
				| SWT.H_SCROLL | SWT.FULL_SELECTION);
		treeViewer.getTree().setLinesVisible(true);
		treeViewer.getTree().setHeaderVisible(true);
		treeViewer.getTree().setLayoutData(
				new GridData(SWT.FILL, SWT.FILL, true, true));

		TreeViewerColumn projectsColumn = new TreeViewerColumn(treeViewer,
				SWT.LEAD);
		projectsColumn.getColumn().setText(
				UIText.GitBranchSynchronizeWizardPage_repository);
		projectsColumn.getColumn().setImage(repositoryImage);
		projectsColumn.setLabelProvider(new StyledCellLabelProvider() {
			@Override
			public void update(ViewerCell cell) {
				Object element = cell.getElement();
				if (element instanceof Repository) {
					@SuppressWarnings("resource")
					Repository repo = (Repository) element;
					String descr = ""; //$NON-NLS-1$
					try {
						descr += " [" + repo.getBranch() + "]";//$NON-NLS-1$ //$NON-NLS-2$
					} catch (IOException e) {
						Activator.logError(e.getMessage(), e);
					}

					Color decorationsColor = JFaceResources.getColorRegistry()
							.get(JFacePreferences.DECORATIONS_COLOR);

					String repoName = repo.getWorkTree().getName();
					int repoNameLen = repoName.length();
					StyleRange styleRange = new StyleRange(repoNameLen,
							repoNameLen + descr.length(), decorationsColor,
							null);

					cell.setImage(repositoryImage);
					cell.setText(repoName + descr);
					cell.setStyleRanges(new StyleRange[] { styleRange });
				}

				super.update(cell);
			}
		});

		TreeViewerColumn dstColumn = new TreeViewerColumn(treeViewer, SWT.LEAD);
		dstColumn.getColumn().setText(
				UIText.GitBranchSynchronizeWizardPage_destination);
		dstColumn.getColumn().setImage(branchImage);
		dstColumn.getColumn().setWidth(200);
		final ComboBoxCellEditor branchesEditor = new ComboBoxCellEditor(
				treeViewer.getTree(), new String[0]);
		branchesEditor
				.setActivationStyle(ComboBoxCellEditor.DROP_DOWN_ON_KEY_ACTIVATION
						| ComboBoxCellEditor.DROP_DOWN_ON_MOUSE_ACTIVATION);
		((CCombo) branchesEditor.getControl()).addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				CCombo combo = (CCombo) e.widget;
				TreeSelection sel = (TreeSelection) treeViewer.getSelection();
				int selectedIdx = combo.getSelectionIndex();
				Repository repo = (Repository) sel.getFirstElement();

				if (selectedIdx != -1) {
					selectedBranches.put(repo, combo.getItem(selectedIdx));
					setPageComplete(true);
				} else {
					selectedBranches.put(repo, null);
					setPageComplete(false);
				}
			}
		});
		dstColumn.setEditingSupport(new EditingSupport(treeViewer) {
			@Override
			protected void setValue(Object element, Object value) {
				int intValue = ((Integer) value).intValue();
				if (intValue == -1)
					return;

				CCombo combo = (CCombo) branchesEditor.getControl();
				String branch = combo.getItem(intValue);

				selectedBranches.put((Repository) element, branch);
				treeViewer.refresh(element, true);

				validatePage();
			}

			@Override
			protected Object getValue(Object element) {
				String branch = selectedBranches.get(element);
				CCombo combo = (CCombo) branchesEditor.getControl();
				int index = branch == null ? 0 : combo.indexOf(branch);
				return Integer.valueOf(index);
			}

			@Override
			protected CellEditor getCellEditor(Object element) {
				Repository repo = (Repository) element;
				List<String> refNames = new LinkedList<>();
				List<Ref> refs;
				try {
					refs = repo.getRefDatabase()
							.getRefsByPrefix(RefDatabase.ALL);
				} catch (IOException e) {
					refs = Collections.emptyList();
				}
				for (Ref ref : refs) {
					refNames.add(ref.getName());
				}

				List<Ref> additionalRefs;
				try {
					additionalRefs = repo.getRefDatabase().getAdditionalRefs();
				} catch (IOException e) {
					additionalRefs = Collections.emptyList();
				}
				for (Ref ref : additionalRefs) {
					refNames.add(ref.getName());
				}

				Collections.sort(refNames,
						CommonUtils.STRING_ASCENDING_COMPARATOR);

				branchesEditor.setItems(refNames.toArray(new String[0]));

				return branchesEditor;
			}

			@Override
			protected boolean canEdit(Object element) {
				return true;
			}
		});
		dstColumn.setLabelProvider(new ColumnLabelProvider() {
			@Override
			public String getText(Object element) {
				String branch = selectedBranches.get(element);
				return branch == null ? "" : branch; //$NON-NLS-1$
			}
		});

		treeViewer.setContentProvider(new ITreeContentProvider() {
			@Override
			public void inputChanged(Viewer viewer, Object oldInput,
					Object newInput) {
				// nothing to do
			}

			@Override
			public void dispose() {
				// nothing to do
			}

			@Override
			public Object[] getElements(Object inputElement) {
				return (Object[]) inputElement;
			}

			@Override
			public boolean hasChildren(Object element) {
				return false;
			}

			@Override
			public Object getParent(Object element) {
				return null;
			}

			@Override
			public Object[] getChildren(Object parentElement) {
				return new Object[0];
			}
		});

		List<Repository> repositoriesList = new ArrayList<>(
				resources.keySet());
		Collections.sort(repositoriesList, Comparator.comparing(
				r -> r.getWorkTree().getName(), String.CASE_INSENSITIVE_ORDER));

		treeViewer.setInput(repositoriesList
				.toArray(new Repository[0]));
		projectsColumn.getColumn().pack();

		Composite buttonsComposite = new Composite(composite, SWT.NONE);
		layout = new GridLayout(4, false);
		layout.numColumns = 1;
		layout.marginWidth = 0;
		layout.marginHeight = 0;
		buttonsComposite.setLayout(layout);
		buttonsComposite.setLayoutData(GridDataFactory.fillDefaults()
				.grab(true, false).create());

		final Button fetchChanges = new Button(buttonsComposite, SWT.CHECK);
		fetchChanges
		.setText(UIText.GitBranchSynchronizeWizardPage_fetchChangesFromRemote);
		fetchChanges.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				forceFetch = fetchChanges.getSelection();
			}
		});
		fetchChanges.setLayoutData(GridDataFactory.fillDefaults()
				.grab(true, false).create());
		fetchChanges.setSelection(Activator.getDefault().getPreferenceStore()
				.getBoolean(UIPreferences.SYNC_VIEW_FETCH_BEFORE_LAUNCH));

		final Button includeLocal = new Button(buttonsComposite, SWT.CHECK);
		includeLocal
				.setText(UIText.GitBranchSynchronizeWizardPage_includeUncommitedChanges);
		includeLocal.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				shouldIncludeLocal = includeLocal.getSelection();
			}
		});
		includeLocal.setLayoutData(GridDataFactory.fillDefaults()
				.grab(true, false).create());

		Dialog.applyDialogFont(composite);
		setPageComplete(false);
		setControl(composite);
	}

	@Override
	public void dispose() {
		if (branchImage != null)
			branchImage.dispose();

		if (repositoryImage != null)
			repositoryImage.dispose();

		super.dispose();
	}

	private void validatePage() {
		setPageComplete(!selectedBranches.isEmpty());
	}

	Map<Repository, String> getSelectedBranches() {
		return selectedBranches;
	}

	Set<IProject> getSelectedProjects() {
		Set<IProject> projects = new HashSet<>();
		for (Repository repo : selectedBranches.keySet())
			projects.addAll(resources.get(repo));

		return projects;
	}

	boolean shouldIncludeLocal() {
		return shouldIncludeLocal;
	}

	boolean forceFetch() {
		return forceFetch;
	}

}

Back to the top