Skip to main content
summaryrefslogtreecommitdiffstats
blob: 298df261ff51af86ee6614a221116fbda3da4029 (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
package org.eclipse.team.internal.ccvs.ui;

/*
 * (c) Copyright IBM Corp. 2000, 2002.
 * All Rights Reserved.
 */

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;
import org.eclipse.team.ccvs.core.CVSProviderPlugin;
import org.eclipse.team.ccvs.core.CVSTag;
import org.eclipse.team.ccvs.core.CVSTeamProvider;
import org.eclipse.team.ccvs.core.ICVSFolder;
import org.eclipse.team.ccvs.core.ICVSRemoteResource;
import org.eclipse.team.ccvs.core.ICVSRepositoryLocation;
import org.eclipse.team.ccvs.core.IUserInfo;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.core.TeamPlugin;
import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;
import org.eclipse.team.internal.ccvs.ui.wizards.UpdateWizard;
import org.eclipse.ui.dialogs.PropertyPage;

public class CVSPropertiesPage extends PropertyPage {
	IProject project;
	
	// Widgets
	Text userText;
	Text passwordText;
	Combo methodType;
	Label hostLabel;
	Label pathLabel;
	Label moduleLabel;
	Label portLabel;
	Label tagLabel;
	
	boolean passwordChanged;
	boolean connectionInfoChanged;

	IUserInfo info;
	CVSTeamProvider provider;
		
	/*
	 * @see PreferencesPage#createContents
	 */
	protected Control createContents(Composite parent) {
		initialize();
		
		Composite composite = new Composite(parent, SWT.NULL);
		composite.setLayoutData(new GridData(GridData.FILL_BOTH));
		GridLayout layout = new GridLayout();
		layout.numColumns = 3;
		composite.setLayout(layout);
		
		Label label = createLabel(composite, Policy.bind("CVSPropertiesPage.connectionType"));
		methodType = createCombo(composite);
		GridData data = new GridData(GridData.FILL_HORIZONTAL);
		data.horizontalSpan = 2;
		methodType.setLayoutData(data);
		
		label = createLabel(composite, Policy.bind("CVSPropertiesPage.user"));
		userText = createTextField(composite);
		data = new GridData(GridData.FILL_HORIZONTAL);
		data.horizontalSpan = 2;
		userText.setLayoutData(data);
		
		label = createLabel(composite, Policy.bind("CVSPropertiesPage.password"));
		passwordText = createTextField(composite);
		passwordText.setEchoChar('*');
		data = new GridData(GridData.FILL_HORIZONTAL);
		data.horizontalSpan = 2;
		passwordText.setLayoutData(data);
		
		label = createLabel(composite, Policy.bind("CVSPropertiesPage.host"));
		hostLabel = createLabel(composite, "");
		data = new GridData();
		data.horizontalSpan = 2;
		hostLabel.setLayoutData(data);
		
		label = createLabel(composite, Policy.bind("CVSPropertiesPage.port"));
		portLabel = createLabel(composite, "");
		data = new GridData();
		data.horizontalSpan = 2;
		portLabel.setLayoutData(data);
		
		label = createLabel(composite, Policy.bind("CVSPropertiesPage.path"));
		pathLabel = createLabel(composite, "");
		data = new GridData();
		data.horizontalSpan = 2;
		pathLabel.setLayoutData(data);
		
		label = createLabel(composite, Policy.bind("CVSPropertiesPage.module"));
		moduleLabel = createLabel(composite, "");
		data = new GridData();
		data.horizontalSpan = 2;
		moduleLabel.setLayoutData(data);
		
		label = createLabel(composite, Policy.bind("CVSPropertiesPage.tag"));
		tagLabel = createLabel(composite, "");
		data = new GridData(GridData.FILL_HORIZONTAL);
		tagLabel.setLayoutData(data);
		
		Button changeTag = new Button(composite, SWT.PUSH);
		data = new GridData(GridData.HORIZONTAL_ALIGN_END);
		changeTag.setLayoutData(data);
		changeTag.setText(Policy.bind("CVSPropertiesPage.update"));
		changeTag.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				UpdateWizard wizard = new UpdateWizard();
				wizard.setProject(project);
				WizardDialog dialog = new WizardDialog(getShell(), wizard);
				dialog.open();
				initializeTag();
			}
		});
		
		initializeValues();
		passwordText.addListener(SWT.Modify, new Listener() {
			public void handleEvent(Event event) {
				passwordChanged = true;
				connectionInfoChanged = true;
			}
		});
		userText.addListener(SWT.Modify, new Listener() {
			public void handleEvent(Event event) {
				connectionInfoChanged = true;
			}
		});
		methodType.addListener(SWT.Modify, new Listener() {
			public void handleEvent(Event event) {
				connectionInfoChanged = true;
			}
		});
		return composite;
	}
	/**
	 * Utility method that creates a combo box
	 *
	 * @param parent  the parent for the new label
	 * @return the new widget
	 */
	protected Combo createCombo(Composite parent) {
		Combo combo = new Combo(parent, SWT.READ_ONLY);
		GridData data = new GridData(GridData.FILL_HORIZONTAL);
		data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
		combo.setLayoutData(data);
		return combo;
	}
	/**
	 * Utility method that creates a label instance
	 * and sets the default layout data.
	 *
	 * @param parent  the parent for the new label
	 * @param text  the text for the new label
	 * @return the new label
	 */
	protected Label createLabel(Composite parent, String text) {
		Label label = new Label(parent, SWT.LEFT);
		label.setText(text);
		GridData data = new GridData();
		data.horizontalSpan = 1;
		data.horizontalAlignment = GridData.FILL;
		label.setLayoutData(data);
		return label;
	}
	/**
	 * Create a text field specific for this application
	 *
	 * @param parent  the parent of the new text field
	 * @return the new text field
	 */
	protected Text createTextField(Composite parent) {
		Text text = new Text(parent, SWT.SINGLE | SWT.BORDER);
		GridData data = new GridData(GridData.FILL_HORIZONTAL);
		data.verticalAlignment = GridData.CENTER;
		data.grabExcessVerticalSpace = false;
		data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
		text.setLayoutData(data);
		return text;
	}
	/**
	 * Initializes the page
	 */
	private void initialize() {
		// Get the project that is the source of this property page
		project = null;
		IAdaptable element = getElement();
		if (element instanceof IProject) {
			project = (IProject)element;
		} else {
			Object adapter = element.getAdapter(IProject.class);
			if (adapter instanceof IProject) {
				project = (IProject)adapter;
			}
		}
	}
	/**
	 * Set the initial values of the widgets
	 */
	private void initializeValues() {
		passwordChanged = false;
		
		provider = (CVSTeamProvider)TeamPlugin.getManager().getProvider(project);
		if (provider == null) return;
		
		CVSWorkspaceRoot cvsRoot = provider.getCVSWorkspaceRoot();
		String[] methods = CVSProviderPlugin.getProvider().getSupportedConnectionMethods();
		for (int i = 0; i < methods.length; i++) {
			methodType.add(methods[i]);
		}
		try {
			ICVSRepositoryLocation location = cvsRoot.getRemoteLocation();
			String method = location.getMethod().getName();
			methodType.select(methodType.indexOf(method));
		
			info = location.getUserInfo(true);
			userText.setText(info.getUsername());
		} catch (TeamException e) {
			handle(e);
		}
		passwordText.setText("*********");
		
		try {
			ICVSRemoteResource resource = cvsRoot.getRemoteResourceFor(project);
			ICVSRepositoryLocation location = resource.getRepository();
			hostLabel.setText(location.getHost());
			int port = location.getPort();
			if (port == ICVSRepositoryLocation.USE_DEFAULT_PORT) {
				portLabel.setText(Policy.bind("CVSPropertiesPage.defaultPort"));
			} else {
				portLabel.setText("" + port);
			}
			pathLabel.setText(location.getRootDirectory());
			moduleLabel.setText(resource.getRepositoryRelativePath());
		} catch (TeamException e) {
			handle(e);
		}
		
		initializeTag();
	}
	
	private void initializeTag() {
		provider = (CVSTeamProvider)TeamPlugin.getManager().getProvider(project);
		if (provider == null) return;
		CVSWorkspaceRoot cvsRoot = provider.getCVSWorkspaceRoot();
		try {
			ICVSFolder local = cvsRoot.getCVSFolderFor(project);
			CVSTag tag = local.getFolderSyncInfo().getTag();
			String tagName;
			if (tag == null) {
				tagName = "HEAD";
			} else {
				tagName = tag.getName();
			}
			tagLabel.setText(tagName);
		} catch (TeamException e) {
			handle(e);
		}
	}
	/*
	 * @see PreferencesPage#performOk
	 */
	public boolean performOk() {
		if ( ! connectionInfoChanged) {
			return true;
		}
		info.setUsername(userText.getText());
		if (passwordChanged) {
			info.setPassword(passwordText.getText());
		}
		final String type = methodType.getText();
		try {
			new ProgressMonitorDialog(getShell()).run(true, false, new IRunnableWithProgress() {
				public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
					try {
						provider.setConnectionInfo(project, type, info, monitor);
					} catch (TeamException e) {
						throw new InvocationTargetException(e);
					}
				}
			});
		} catch (InvocationTargetException e) {
			Throwable t = e.getTargetException();
			if (t instanceof TeamException) {
				handle((TeamException)t);
			} else if (t instanceof CoreException) {
				handle(((CoreException)t).getStatus());
			} else {
				IStatus status = new Status(IStatus.ERROR, CVSUIPlugin.ID, 1, "Internal error occured", t);
				handle(status);
				CVSUIPlugin.log(status);
			}
		} catch (InterruptedException e) {
		}
					
		return true;
	}
	/**
	 * Shows the given errors to the user.
	 */
	protected void handle(TeamException e) {
		handle(e.getStatus());
	}
	
	protected void handle(IStatus status) {
		if (!status.isOK()) {
			IStatus toShow = status;
			if (status.isMultiStatus()) {
				IStatus[] children = status.getChildren();
				if (children.length == 1) {
					toShow = children[0];
				}
			}
			ErrorDialog.openError(getShell(), status.getMessage(), null, toShow);
		}
	}
}

Back to the top