Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a146a9514ca93b3a9bb446ee5ddac76ab1c6692e (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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
/*******************************************************************************
 * Copyright (C) 2017 Thomas Wolf <thomas.wolf@paranor.ch>
 *
 * 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
 *******************************************************************************/
package org.eclipse.egit.ui.internal.dialogs;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.egit.ui.Activator;
import org.eclipse.egit.ui.internal.ActionUtils;
import org.eclipse.egit.ui.internal.UIIcons;
import org.eclipse.egit.ui.internal.UIText;
import org.eclipse.egit.ui.internal.components.TitleAndImageDialog;
import org.eclipse.egit.ui.internal.gerrit.GerritDialogSettings;
import org.eclipse.egit.ui.internal.push.RefSpecDialog;
import org.eclipse.egit.ui.internal.push.RefSpecWizard;
import org.eclipse.egit.ui.internal.repository.SelectUriWizard;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.window.Window;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.lib.ConfigConstants;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.StoredConfig;
import org.eclipse.jgit.transport.RefSpec;
import org.eclipse.jgit.transport.RemoteConfig;
import org.eclipse.jgit.transport.URIish;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.ActionFactory;

/**
 * Common super class for fetch and push remote configuration dialogs. The
 * dialog has, besides the usual OK and CANCEL buttons, three more buttons:
 * {@link #DRY_RUN} to do a dry-run of the operation, {@link #SAVE_ONLY} to save
 * the {@link RemoteConfig} without performing the operation, and
 * {@link #REVERT} to undo all configuration changes and re-load the
 * {@link RemoteConfig} from the {@link Repository}. The OK button saves the
 * {@link RemoteConfig} <em>and</em> performs the operation.
 * <p>
 * Provides UI for the common URI and changing it, and also for adding,
 * changing, and deleting {@link RefSpec}s. Can be extended via
 * {@link #createAdditionalUriArea(Composite)} to provide more UI components.
 * </p>
 */
public abstract class AbstractConfigureRemoteDialog
		extends TitleAndImageDialog {

	/** Button ID for the dry-run button. */
	protected static final int DRY_RUN = 98;

	/** Button ID for the "save only" button. */
	protected static final int SAVE_ONLY = 97;

	/** Button ID for the revert button. */
	protected static final int REVERT = 96;

	private final boolean isPush;

	private final Repository repository;

	private RemoteConfig config;

	private final boolean showBranchInfo;

	// UI components

	/** A {@link Text} field for the URI. */
	protected Text commonUriText;

	/** A {@link TableViewer} showing {@link RefSpec}s. */
	protected TableViewer specViewer;

	/** An {@link IAction} that allows changing the common URI. */
	protected IAction changeCommonUriAction;

	/** An {@link IAction} that allows deleting the common URI. */
	protected IAction deleteCommonUriAction;

	/**
	 * An {@link IAction} to add a new {@link RefSpec} to the
	 * {@link #specViewer}.
	 */
	protected IAction addRefSpecAction;

	/**
	 * An {@link IAction} to change an existing {@link RefSpec} in the
	 * {@link #specViewer}.
	 */
	protected IAction changeRefSpecAction;

	/**
	 * An {@link IAction} opening an advanced {@link RefSpec} dialog.
	 */
	protected IAction addRefSpecAdvancedAction;

	/**
	 * Create a new {@link AbstractConfigureRemoteDialog}.
	 *
	 * @param parent
	 *            SWT {@link Shell} to parent the dialog on
	 * @param repository
	 *            the remote belongs to
	 * @param config
	 *            of the remote to be configured
	 * @param showBranchInfo
	 *            whether to show additional branch information
	 * @param isPush
	 *            whether this dialog is for configuring push
	 */
	protected AbstractConfigureRemoteDialog(Shell parent, Repository repository,
			RemoteConfig config, boolean showBranchInfo, boolean isPush) {
		super(parent, isPush ? UIIcons.WIZBAN_PUSH : UIIcons.WIZBAN_FETCH);
		setHelpAvailable(false);
		setShellStyle(getShellStyle() | SWT.SHELL_TRIM);
		this.repository = repository;
		this.config = config;
		this.showBranchInfo = showBranchInfo;
		this.isPush = isPush;
	}

	/**
	 * Retrieves the {@link Repository} for which the remote is to be
	 * configured.
	 *
	 * @return the {@link Repository}
	 */
	protected Repository getRepository() {
		return repository;
	}

	/**
	 * Retrieves the {@link RemoteConfig} as configured currently.
	 *
	 * @return the {@link RemoteConfig}
	 */
	protected RemoteConfig getConfig() {
		return config;
	}

	/**
	 * Performs a dry-run of the operation.
	 *
	 * @param monitor
	 *            for progress reporting and cancellation, never {@code null}
	 */
	protected abstract void dryRun(IProgressMonitor monitor);

	/**
	 * Performs the operation for real. Invoked in the UI thread; lengthy
	 * operations should be performed in a background job.
	 */
	protected abstract void performOperation();

	/**
	 * Creates the OK button.
	 *
	 * @param parent
	 *            {@link Composite} containing the buttons
	 */
	protected abstract void createOkButton(Composite parent);

	/**
	 * Asks the user for a new {@link RefSpec} to be added to the current
	 * {@link RemoteConfig}.
	 *
	 * @return the new {@link RefSpec}, or {@code null} if none.
	 */
	protected abstract RefSpec getNewRefSpec();

	@Override
	protected Control createDialogArea(Composite parent) {
		final Composite main = new Composite(parent, SWT.NONE);
		GridLayoutFactory.fillDefaults().applyTo(main);
		GridDataFactory.fillDefaults().grab(true, true)
				.minSize(SWT.DEFAULT, SWT.DEFAULT).applyTo(main);

		if (showBranchInfo) {
			Composite branchArea = new Composite(main, SWT.NONE);
			GridLayoutFactory.swtDefaults().numColumns(2).equalWidth(false)
					.applyTo(branchArea);
			GridDataFactory.fillDefaults().grab(true, false)
					.applyTo(branchArea);

			Label branchLabel = new Label(branchArea, SWT.NONE);
			branchLabel.setText(UIText.AbstractConfigureRemoteDialog_BranchLabel);
			String branch;
			try {
				branch = getRepository().getBranch();
			} catch (IOException e) {
				branch = null;
			}
			if (branch == null || ObjectId.isId(branch)) {
				branch = UIText.AbstractConfigureRemoteDialog_DetachedHeadMessage;
			}
			Text branchText = new Text(branchArea, SWT.BORDER | SWT.READ_ONLY);
			GridDataFactory.fillDefaults().grab(true, false)
					.applyTo(branchText);
			branchText.setText(branch);

			addDefaultOriginWarning(main);

		}

		final Composite sameUriDetails = new Composite(main, SWT.NONE);
		GridLayoutFactory.fillDefaults().numColumns(4).equalWidth(false)
				.applyTo(sameUriDetails);
		GridDataFactory.fillDefaults().grab(true, false)
				.applyTo(sameUriDetails);
		Label commonUriLabel = new Label(sameUriDetails, SWT.NONE);
		commonUriLabel.setText(UIText.AbstractConfigureRemoteDialog_UriLabel);
		commonUriText = new Text(sameUriDetails, SWT.BORDER | SWT.READ_ONLY);
		GridDataFactory.fillDefaults().grab(true, false).applyTo(commonUriText);
		changeCommonUriAction = new Action(
				UIText.AbstractConfigureRemoteDialog_ChangeUriLabel) {

			@Override
			public void run() {
				SelectUriWizard wiz;
				if (!commonUriText.getText().isEmpty()) {
					wiz = new SelectUriWizard(true, commonUriText.getText());
				} else {
					wiz = new SelectUriWizard(true);
				}
				if (new WizardDialog(getShell(), wiz).open() == Window.OK) {
					if (!commonUriText.getText().isEmpty()) {
						try {
							getConfig().removeURI(
									new URIish(commonUriText.getText()));
						} catch (URISyntaxException ex) {
							Activator.handleError(ex.getMessage(), ex, true);
						}
					}
					getConfig().addURI(wiz.getUri());
					updateControls();
				}
			}
		};
		deleteCommonUriAction = new Action(
				UIText.AbstractConfigureRemoteDialog_DeleteUriLabel) {

			@Override
			public void run() {
				getConfig().removeURI(getConfig().getURIs().get(0));
				updateControls();
			}
		};
		createActionButton(sameUriDetails, SWT.PUSH, changeCommonUriAction);
		createActionButton(sameUriDetails, SWT.PUSH, deleteCommonUriAction)
				.setEnabled(false);

		commonUriText.addModifyListener(event -> deleteCommonUriAction
				.setEnabled(!commonUriText.getText().isEmpty()));

		createAdditionalUriArea(main);

		final Group refSpecGroup = new Group(main, SWT.SHADOW_ETCHED_IN);
		GridDataFactory.fillDefaults().grab(true, true)
				.minSize(SWT.DEFAULT, SWT.DEFAULT).applyTo(refSpecGroup);
		refSpecGroup.setText(UIText.AbstractConfigureRemoteDialog_RefMappingGroup);
		GridLayoutFactory.fillDefaults().numColumns(2).applyTo(refSpecGroup);

		specViewer = new TableViewer(refSpecGroup, SWT.BORDER | SWT.MULTI);
		specViewer.setContentProvider(ArrayContentProvider.getInstance());
		GridDataFactory.fillDefaults().hint(SWT.DEFAULT, 150)
				.minSize(SWT.DEFAULT, 30).grab(true, true)
				.applyTo(specViewer.getTable());

		addRefSpecAction = new Action(
				UIText.AbstractConfigureRemoteDialog_AddRefSpecLabel) {

			@Override
			public void run() {
				doAddRefSpec();
			}
		};
		changeRefSpecAction = new Action(
				UIText.AbstractConfigureRemoteDialog_ChangeRefSpecLabel) {

			@Override
			public void run() {
				doChangeRefSpec();
			}
		};
		addRefSpecAdvancedAction = new Action(
				UIText.AbstractConfigureRemoteDialog_EditAdvancedLabel) {

			@Override
			public void run() {
				doAdvanced();
			}
		};
		IAction deleteRefSpecAction = ActionUtils.createGlobalAction(
				ActionFactory.DELETE, () -> doDeleteRefSpecs());
		IAction copyRefSpecAction = ActionUtils
				.createGlobalAction(ActionFactory.COPY, () -> doCopy());
		IAction pasteRefSpecAction = ActionUtils
				.createGlobalAction(ActionFactory.PASTE, () -> doPaste());
		IAction selectAllRefSpecsAction = ActionUtils.createGlobalAction(
				ActionFactory.SELECT_ALL,
				() -> {
					specViewer.getTable().selectAll();
					// selectAll doesn't fire a "selection changed" event
					specViewer.setSelection(specViewer.getSelection());
				});

		Composite buttonArea = new Composite(refSpecGroup, SWT.NONE);
		GridLayoutFactory.fillDefaults().applyTo(buttonArea);
		GridDataFactory.fillDefaults().grab(false, true)
				.minSize(SWT.DEFAULT, SWT.DEFAULT).applyTo(buttonArea);

		createActionButton(buttonArea, SWT.PUSH, addRefSpecAction);
		createActionButton(buttonArea, SWT.PUSH, changeRefSpecAction);
		createActionButton(buttonArea, SWT.PUSH, deleteRefSpecAction);
		createActionButton(buttonArea, SWT.PUSH, copyRefSpecAction);
		createActionButton(buttonArea, SWT.PUSH, pasteRefSpecAction);
		createActionButton(buttonArea, SWT.PUSH, addRefSpecAdvancedAction);

		MenuManager contextMenu = new MenuManager();
		contextMenu.setRemoveAllWhenShown(true);
		contextMenu.addMenuListener(manager -> {
			specViewer.getTable().setFocus();
			if (addRefSpecAction.isEnabled()) {
				manager.add(addRefSpecAction);
			}
			if (changeRefSpecAction.isEnabled()) {
				manager.add(changeRefSpecAction);
			}
			if (deleteRefSpecAction.isEnabled()) {
				manager.add(deleteRefSpecAction);
			}
			manager.add(new Separator());
			manager.add(copyRefSpecAction);
			manager.add(pasteRefSpecAction);
			manager.add(selectAllRefSpecsAction);
		});
		specViewer.getTable()
				.setMenu(contextMenu.createContextMenu(specViewer.getTable()));
		ActionUtils.setGlobalActions(specViewer.getTable(), deleteRefSpecAction,
				copyRefSpecAction, pasteRefSpecAction, selectAllRefSpecsAction);

		specViewer.addSelectionChangedListener(new ISelectionChangedListener() {

			@Override
			public void selectionChanged(SelectionChangedEvent event) {
				IStructuredSelection sel = (IStructuredSelection) specViewer
						.getSelection();
				copyRefSpecAction.setEnabled(sel.size() == 1);
				changeRefSpecAction.setEnabled(sel.size() == 1);
				deleteRefSpecAction.setEnabled(!sel.isEmpty());
				selectAllRefSpecsAction.setEnabled(specViewer.getTable()
						.getItemCount() > 0
						&& sel.size() != specViewer.getTable().getItemCount());
			}
		});

		specViewer.addDoubleClickListener(event -> doChangeRefSpec());

		// Initial action enablement (no selection in the specViewer):
		copyRefSpecAction.setEnabled(false);
		changeRefSpecAction.setEnabled(false);
		deleteRefSpecAction.setEnabled(false);

		applyDialogFont(main);
		return main;
	}

	/**
	 * Add a warning about this remote being used by other branches
	 *
	 * @param parent
	 */
	private void addDefaultOriginWarning(Composite parent) {
		List<String> otherBranches = new ArrayList<>();
		String currentBranch;
		try {
			currentBranch = getRepository().getBranch();
		} catch (IOException e) {
			// just don't show this warning
			return;
		}
		String currentRemote = getConfig().getName();
		Config repositoryConfig = getRepository().getConfig();
		Set<String> branches = repositoryConfig
				.getSubsections(ConfigConstants.CONFIG_BRANCH_SECTION);
		for (String branch : branches) {
			if (branch.equals(currentBranch)) {
				continue;
			}
			String remote = repositoryConfig.getString(
					ConfigConstants.CONFIG_BRANCH_SECTION, branch,
					ConfigConstants.CONFIG_KEY_REMOTE);
			if ((remote == null
					&& currentRemote.equals(Constants.DEFAULT_REMOTE_NAME))
					|| (remote != null && remote.equals(currentRemote))) {
				otherBranches.add(branch);
			}
		}
		if (otherBranches.isEmpty()) {
			return;
		}
		Composite warningAboutOrigin = new Composite(parent, SWT.NONE);
		GridLayoutFactory.fillDefaults().numColumns(2)
				.applyTo(warningAboutOrigin);
		Label warningLabel = new Label(warningAboutOrigin, SWT.NONE);
		warningLabel.setImage(PlatformUI.getWorkbench().getSharedImages()
				.getImage(ISharedImages.IMG_OBJS_WARN_TSK));
		Text warningText = new Text(warningAboutOrigin, SWT.READ_ONLY);
		warningText.setText(NLS.bind(
				UIText.AbstractConfigureRemoteDialog_ReusedRemoteWarning,
				getConfig().getName(), Integer.valueOf(otherBranches.size())));
		warningText.setToolTipText(otherBranches.toString());
		GridDataFactory.fillDefaults().grab(true, false).applyTo(warningLabel);
	}

	private void addRefSpec(RefSpec spec) {
		if (isPush) {
			getConfig().addPushRefSpec(spec);
		} else {
			getConfig().addFetchRefSpec(spec);
		}
	}

	private void removeRefSpec(RefSpec spec) {
		if (isPush) {
			getConfig().removePushRefSpec(spec);
		} else {
			getConfig().removeFetchRefSpec(spec);
		}
	}

	/**
	 * Hook method to create an additional area between the URI and the
	 * {@link RefSpec} viewer. This default implementation does nothing.
	 *
	 * @param parent
	 *            {@link Composite} the additional area shall use as parent
	 * @return the {@link Control}, or {@code null} if none
	 */
	protected Control createAdditionalUriArea(Composite parent) {
		return null;
	}

	/**
	 * Validate and enable/disable controls depending on the current state.
	 */
	protected abstract void updateControls();

	@Override
	protected final void createButtonsForButtonBar(Composite parent) {
		createOkButton(parent);
		createButton(parent, SAVE_ONLY,
				UIText.AbstractConfigureRemoteDialog_SaveButton, false);
		createButton(parent, DRY_RUN,
				UIText.AbstractConfigureRemoteDialog_DryRunButton, false);
		createButton(parent, REVERT,
				UIText.AbstractConfigureRemoteDialog_RevertButton, false);
		createButton(parent, IDialogConstants.CANCEL_ID,
				IDialogConstants.CANCEL_LABEL, false);
	}

	@Override
	protected final void buttonPressed(int buttonId) {
		switch (buttonId) {
		case DRY_RUN:
			try {
				new ProgressMonitorDialog(getShell()).run(true, true,
						(monitor) -> dryRun(monitor));
			} catch (InvocationTargetException e) {
				Activator.showError(e.getMessage(), e);
			} catch (InterruptedException e1) {
				// Ignore cancellation here
			}
			return;
		case REVERT:
			try {
				config = new RemoteConfig(repository.getConfig(),
						config.getName());
				updateControls();
			} catch (URISyntaxException e) {
				Activator.handleError(e.getMessage(), e, true);
			}
			return;
		case OK:
		case SAVE_ONLY:
			StoredConfig repoConfig = getRepository().getConfig();
			boolean saved = false;
			try {
				config.update(repoConfig);
				repoConfig.save();
				saved = true;
			} catch (IOException e) {
				Activator.handleError(e.getMessage(), e, true);
			}
			if (saved) {
				GerritDialogSettings.updateRemoteConfig(repository, config);
			}
			if (buttonId == OK) {
				performOperation();
			}
			okPressed();
			return;
		default:
			break;
		}
		super.buttonPressed(buttonId);
	}

	private void doPaste() {
		Clipboard clipboard = new Clipboard(getShell().getDisplay());
		try {
			String content = (String) clipboard
					.getContents(TextTransfer.getInstance());
			if (content == null) {
				MessageDialog.openConfirm(getShell(),
						UIText.AbstractConfigureRemoteDialog_EmptyClipboardDialogTitle,
						UIText.AbstractConfigureRemoteDialog_EmptyClipboardDialogMessage);
			}
			try {
				RefSpec spec = new RefSpec(content);
				Ref source;
				try {
					// TODO better checks for wild-cards and such
					source = getRepository().findRef(isPush ? spec.getSource()
							: spec.getDestination());
				} catch (IOException e) {
					source = null;
				}
				if (source != null || MessageDialog.openQuestion(getShell(),
						UIText.AbstractConfigureRemoteDialog_InvalidRefDialogTitle,
						NLS.bind(
								UIText.AbstractConfigureRemoteDialog_InvalidRefDialogMessage,
								spec.toString()))) {
					addRefSpec(spec);
				}
				updateControls();
			} catch (IllegalArgumentException e) {
				MessageDialog.openError(getShell(),
						UIText.AbstractConfigureRemoteDialog_NoRefSpecDialogTitle,
						UIText.AbstractConfigureRemoteDialog_NoRefSpecDialogMessage);
			}
		} finally {
			clipboard.dispose();
		}
	}

	private void doCopy() {
		String toCopy = ((IStructuredSelection) specViewer.getSelection())
				.getFirstElement().toString();
		Clipboard clipboard = new Clipboard(getShell().getDisplay());
		try {
			clipboard.setContents(new String[] { toCopy },
					new TextTransfer[] { TextTransfer.getInstance() });
		} finally {
			clipboard.dispose();
		}
	}

	private void doAddRefSpec() {
		RefSpec spec = getNewRefSpec();
		if (spec != null) {
			addRefSpec(spec);
			updateControls();
		}
	}

	private void doChangeRefSpec() {
		RefSpec oldSpec = (RefSpec) ((IStructuredSelection) specViewer
				.getSelection()).getFirstElement();
		RefSpecDialog dlg = new RefSpecDialog(getShell(), getRepository(),
				getConfig(), oldSpec, isPush);
		if (dlg.open() == Window.OK) {
			removeRefSpec(oldSpec);
			addRefSpec(dlg.getSpec());
		}
		updateControls();
	}

	private void doDeleteRefSpecs() {
		for (Object spec : ((IStructuredSelection) specViewer.getSelection())
				.toArray()) {
			removeRefSpec((RefSpec) spec);
		}
		updateControls();
	}

	private void doAdvanced() {
		RefSpecWizard wizard = new RefSpecWizard(getRepository(), getConfig(),
				isPush);
		if (new WizardDialog(getShell(), wizard).open() == Window.OK) {
			updateControls();
		}
	}

	private Button createActionButton(Composite parent, int style,
			IAction action) {
		Button button = new Button(parent, style);
		button.setText(action.getText());
		button.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				action.run();
			}
		});
		IPropertyChangeListener listener = event -> {
			if (IAction.ENABLED.equals(event.getProperty())) {
				if (!button.isDisposed()) {
					if (Display.getCurrent() == null) {
						button.getShell().getDisplay().syncExec(() -> {
							if (!button.isDisposed()) {
								button.setEnabled(action.isEnabled());
							}
						});
					} else {
						button.setEnabled(action.isEnabled());
					}
				}
			}
		};
		button.addDisposeListener(
				event -> action.removePropertyChangeListener(listener));
		action.addPropertyChangeListener(listener);
		GridDataFactory.fillDefaults().applyTo(button);
		return button;
	}
}

Back to the top