Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8162eb07345e59e30384f7f918daeaf70d99f74c (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
/*******************************************************************************
 * Copyright (c) 2010 SAP AG.
 * 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
 *
 * Contributors:
 *    Mathias Kinzler (SAP AG) - initial implementation
 *******************************************************************************/
package org.eclipse.egit.ui.internal.repository;

import java.io.IOException;
import java.net.URISyntaxException;

import org.eclipse.egit.ui.UIText;
import org.eclipse.egit.ui.internal.components.RefSpecPage;
import org.eclipse.egit.ui.internal.components.RepositorySelectionPage;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.RepositoryConfig;
import org.eclipse.jgit.transport.RemoteConfig;
import org.eclipse.osgi.util.NLS;

/**
 * Used for "remote" configuration of a Repository
 */
class ConfigureRemoteWizard extends Wizard {

	final RepositoryConfig myConfiguration;

	final boolean pushMode;

	final String myRemoteName;

	/**
	 * @param repository
	 */
	public ConfigureRemoteWizard(Repository repository) {
		this(repository, null, false);
	}

	/**
	 *
	 * @param repository
	 * @param remoteName
	 * @param push
	 */
	public ConfigureRemoteWizard(Repository repository, String remoteName,
			boolean push) {
		myConfiguration = repository.getConfig();
		myRemoteName = remoteName;
		pushMode = push;
		if (myRemoteName == null) {
			// create mode: add remote name page and repository selection page
			addPage(new SelectRemoteNamePage());
			addPage(new RepositorySelectionPage(null));
			setWindowTitle(UIText.ConfigureRemoteWizard_WizardTitle_New);
		} else {
			// edit mode: no remote name page and pre-selected repository
			// selection page
			RepositorySelectionPage sp = new RepositorySelectionPage(
					myConfiguration.getString(RepositoriesView.REMOTE,
							myRemoteName, RepositoriesView.URL));

			addPage(sp);
			// and also the corresponding configuration page
			RefSpecPage rsp = new RefSpecPage(repository, pushMode, sp,
					myRemoteName);
			addPage(rsp);
			setWindowTitle(NLS.bind(
					UIText.ConfigureRemoteWizard_WizardTitle_Change,
					myRemoteName));
		}

	}

	/**
	 * @return the configuration
	 *
	 */
	public RepositoryConfig getConfiguration() {
		return myConfiguration;
	}

	@Override
	public boolean performFinish() {

		String actRemoteName = myRemoteName;
		if (myRemoteName == null) {
			SelectRemoteNamePage page = (SelectRemoteNamePage) getPage(SelectRemoteNamePage.class
					.getName());
			actRemoteName = page.remoteName.getText();
		}

		RepositorySelectionPage sp = (RepositorySelectionPage) getPage(RepositorySelectionPage.class
				.getName());

		String uriString = sp.getSelection().getURI().toString();

		myConfiguration.setString(RepositoriesView.REMOTE, actRemoteName,
				RepositoriesView.URL, uriString);

		if (myRemoteName != null) {

			RefSpecPage specPage = (RefSpecPage) getPage(RefSpecPage.class
					.getName());

			if (specPage.getRefSpecs().isEmpty()) {
				specPage.setVisible(true);
				specPage.setVisible(false);
			}

			RemoteConfig config;
			try {
				config = new RemoteConfig(myConfiguration, actRemoteName);
			} catch (URISyntaxException e1) {
				// TODO better Exception handling
				return false;
			}

			if (pushMode)
				config.setPushRefSpecs(specPage.getRefSpecs());
			else {
				config.setFetchRefSpecs(specPage.getRefSpecs());
				config.setTagOpt(specPage.getTagOpt());
			}
			config.update(myConfiguration);
		}

		try {
			myConfiguration.save();
			return true;
		} catch (IOException e) {
			// TODO better Exception handling
			return false;
		}
	}

}

Back to the top