Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c72d6154b3635c7b7e2b9a40c104b7e43d6c4aed (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
/*******************************************************************************
 * Copyright (C) 2011, Mathias Kinzler <mathias.kinzler@sap.com>
 *
 * 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.fetch;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.egit.ui.Activator;
import org.eclipse.egit.ui.UIUtils;
import org.eclipse.egit.ui.internal.UIText;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.transport.RemoteConfig;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

/**
 * Obtain the Fetch Source, i.e. the {@link Ref} on the remote
 * {@link Repository}
 */
public class FetchDestinationPage extends WizardPage {
	private final Repository repository;

	private final RemoteConfig config;

	private Text sourceText;

	private Text destinationText;

	private Button force;

	private List<Ref> trackingBranches;

	/**
	 * Default constructor
	 *
	 * @param repository
	 * @param config
	 */
	public FetchDestinationPage(Repository repository, RemoteConfig config) {
		super(FetchDestinationPage.class.getName());
		this.repository = repository;
		this.config = config;
		setTitle(UIText.FetchDestinationPage_PageTitle);
	}

	@Override
	public void createControl(Composite parent) {
		Composite main = new Composite(parent, SWT.NONE);
		main.setLayout(new GridLayout(2, false));
		GridDataFactory.fillDefaults().grab(true, true).applyTo(main);

		Label repositoryLabel = new Label(main, SWT.NONE);
		repositoryLabel.setText(UIText.FetchDestinationPage_RepositoryLabel);
		Text repositoryText = new Text(main, SWT.READ_ONLY | SWT.BORDER);
		repositoryText.setText(Activator.getDefault().getRepositoryUtil()
				.getRepositoryName(repository));
		GridDataFactory.fillDefaults().grab(true, false)
				.applyTo(repositoryText);

		Label sourceLabel = new Label(main, SWT.NONE);
		sourceLabel.setText(UIText.FetchDestinationPage_SourceLabel);

		sourceText = new Text(main, SWT.READ_ONLY | SWT.BORDER);
		GridDataFactory.fillDefaults().grab(true, false).applyTo(sourceText);

		Label destinationLabel = new Label(main, SWT.NONE);
		destinationLabel.setText(UIText.FetchDestinationPage_DestinationLabel);
		destinationText = new Text(main, SWT.BORDER);
		destinationText.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent e) {
				checkPage();
			}
		});
		GridDataFactory.fillDefaults().grab(true, false).applyTo(
				destinationText);
		UIUtils.addRefContentProposalToText(sourceText, repository,
				() -> getRemoteRefs(), true);

		force = new Button(main, SWT.CHECK);
		force.setText(UIText.FetchDestinationPage_ForceCheckbox);
		GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(
				force);

		checkPage();
		destinationText.setFocus();
		setControl(main);
	}

	@Override
	public void setVisible(boolean visible) {
		super.setVisible(visible);
		if (visible) {
			FetchSourcePage fsp = (FetchSourcePage) getWizard()
					.getPreviousPage(this);
			String sourceString = fsp.getSource();
			sourceText.setText(sourceString);
			if (sourceString.length() > 0) {
				destinationText.setText(Constants.R_REMOTES + config.getName()
						+ '/' + Repository.shortenRefName(sourceString));
			}
			destinationText.setFocus();
		}
	}

	/**
	 * @return the source
	 */
	public String getDestination() {
		return destinationText.getText();
	}

	/**
	 * @return if force is checked
	 */
	public boolean isForce() {
		return force.getSelection();
	}

	private List<Ref> getRemoteRefs() {
		if (this.trackingBranches == null) {
			List<Ref> proposals = new ArrayList<>();
			try {
				for (Ref ref : repository.getRefDatabase()
						.getRefsByPrefix(Constants.R_REMOTES)) {
					proposals.add(ref);
				}
				this.trackingBranches = proposals;
			} catch (IOException e) {
				setErrorMessage(UIText.FetchDestinationPage_CouldNotGetBranchesMessage);
			}
		}
		return this.trackingBranches;
	}

	private void checkPage() {
		setMessage(null, IMessageProvider.WARNING);
		setErrorMessage(null);
		setMessage(UIText.FetchDestinationPage_PageMessage);
		if (destinationText.getText().length() == 0) {
			setPageComplete(false);
			return;
		}
		boolean found = false;
		for (Ref ref : getRemoteRefs()) {
			if (ref.getName().equals(destinationText.getText()))
				found = true;
		}
		if (!found)
			setMessage(NLS.bind(
					UIText.FetchDestinationPage_TrackingBranchNotFoundMessage,
					destinationText.getText()), IMessageProvider.WARNING);
		setPageComplete(true);
	}
}

Back to the top