Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0741d162402bae48d3fec1a11fb5e3dd6bb97585 (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
package org.eclipse.team.internal.ccvs.ui.wizards;

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

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.resource.ImageDescriptor;
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.Text;
import org.eclipse.team.ccvs.core.CVSTeamProvider;
import org.eclipse.team.internal.ccvs.ui.Policy;

/**
 * Wizard page for configuring a project with a CVS repository location.
 * 
 * This is a multi-purpose wizard page. Its contents are determined by a
 * bitmask of capabilities set with setStyle().
 */
public class ConfigurationWizardMainPage extends CVSWizardPage {
	public static final int CONNECTION_METHOD = 1 << 0;
	public static final int USER = 1 << 1;
	public static final int PASSWORD = 1 << 2;
	public static final int HOST = 1 << 3;
	public static final int PORT = 1 << 4;
	public static final int REPOSITORY_PATH = 1 << 5;
	public static final int MODULE_TEXT = 1 << 6;
	public static final int MODULE_RADIO = 1 << 7;
	public static final int PROJECT_NAME = 1 << 8;
	public static final int TAG = 1 << 9;
	public static final int PROJECT_LOCATION = 1 << 10;
	
	// The mode for the page
	int style;
	
	// Widgets
	
	// Connection Method
	private Combo connectionMethodCombo;
	// User
	private Combo userCombo;
	// Password
	private Text passwordText;
	// Port
	private Text portText;
	private Button useDefaultPort;
	private Button useCustomPort;
	// Host
	private Combo hostCombo;
	// Repository Path
	private Combo repositoryPathCombo;
	// Module combo
	private Combo moduleCombo;
	// Module radio
	private Button moduleIsProject;
	private Button moduleIsCustom;
	private Text moduleText;
	// Project name
	private Button projectIsModule;
	private Button projectIsCustom;
	private Text projectText;
	// Tag
	private Button checkoutHead;
	private Button checkoutTag;
	private Combo tagCombo;

	private static final int COMBO_HISTORY_LENGTH = 5;
	
	private Properties properties = new Properties();
	
	// Dialog store id constants
	private static final String STORE_USERNAME_ID =
		"ConfigurationWizardMainPage.STORE_USERNAME_ID";//$NON-NLS-1$
	private static final String STORE_HOSTNAME_ID =
		"ConfigurationWizardMainPage.STORE_HOSTNAME_ID";//$NON-NLS-1$
	private static final String STORE_PATH_ID =
		"ConfigurationWizardMainPage.STORE_PATH_ID";//$NON-NLS-1$
	private static final String STORE_MODULE_ID =
		"ConfigurationWizardMainPage.STORE_MODULE_ID";//$NON-NLS-1$
	private static final String STORE_TAG_ID =
		"ConfigurationWizardMainPage.STORE_TAG_ID";//$NON-NLS-1$
		
	/**
	 * ConfigurationWizardMainPage constructor.
	 * 
	 * @param pageName  the name of the page
	 * @param title  the title of the page
	 * @param titleImage  the image for the page
	 */
	public ConfigurationWizardMainPage(String pageName, String title, ImageDescriptor titleImage) {
		super(pageName, title, titleImage);
	}
	/**
	 * Adds an entry to a history, while taking care of duplicate history items
	 * and excessively long histories.  The assumption is made that all histories
	 * should be of length <code>ConfigurationWizardMainPage.COMBO_HISTORY_LENGTH</code>.
	 *
	 * @param history the current history
	 * @param newEntry the entry to add to the history
	 * @return the history with the new entry appended
	 */
	private String[] addToHistory(String[] history, String newEntry) {
		ArrayList l = new ArrayList(Arrays.asList(history));
		addToHistory(l, newEntry);
		String[] r = new String[l.size()];
		l.toArray(r);
		return r;
	}
	/**
	 * Adds an entry to a history, while taking care of duplicate history items
	 * and excessively long histories.  The assumption is made that all histories
	 * should be of length <code>ConfigurationWizardMainPage.COMBO_HISTORY_LENGTH</code>.
	 *
	 * @param history the current history
	 * @param newEntry the entry to add to the history
	 */
	private void addToHistory(List history, String newEntry) {
		history.remove(newEntry);
		history.add(0,newEntry);
	
		// since only one new item was added, we can be over the limit
		// by at most one item
		if (history.size() > COMBO_HISTORY_LENGTH)
			history.remove(COMBO_HISTORY_LENGTH);
	}
	/**
	 * Creates the UI part of the page.
	 * 
	 * @param parent  the parent of the created widgets
	 */
	public void createControl(Composite parent) {
		Composite composite = createComposite(parent, 2);
		// set F1 help
		// WorkbenchHelp.setHelp(composite, new DialogPageContextComputer (this, ITeamHelpContextIds.REPO_CONNECTION_MAIN_PAGE));
		
		if ((style & CONNECTION_METHOD) != 0) {
			createLabel(composite, Policy.bind("ConfigurationWizardMainPage.connection"));
			connectionMethodCombo = createCombo(composite);
		}
		if ((style & USER) != 0) {
			createLabel(composite, Policy.bind("ConfigurationWizardMainPage.userName"));
			userCombo = createEditableCombo(composite);
		}
		if ((style & PASSWORD) != 0) {
			createLabel(composite, Policy.bind("ConfigurationWizardMainPage.password"));
			passwordText = createTextField(composite);
			passwordText.setEchoChar('*');
		}
		if ((style & HOST) != 0) {
			createLabel(composite, Policy.bind("ConfigurationWizardMainPage.host"));
			hostCombo = createEditableCombo(composite);
		}
		if ((style & PORT) != 0) {
			useDefaultPort = createRadioButton(composite, Policy.bind("ConfigurationWizardMainPage.useDefaultPort"), 2);
			useCustomPort = createRadioButton(composite, Policy.bind("ConfigurationWizardMainPage.usePort"), 1);
			portText = createTextField(composite);
		}
		if ((style & REPOSITORY_PATH) != 0) {
			createLabel(composite, Policy.bind("ConfigurationWizardMainPage.repositoryPath"));
			repositoryPathCombo = createEditableCombo(composite);
		}
		if ((style & MODULE_TEXT) != 0) {
			createLabel(composite, Policy.bind("ConfigurationWizardMainPage.module"));
			moduleCombo = createEditableCombo(composite);
		} else if ((style & MODULE_RADIO) != 0) {
			Composite radioComposite = createRadioComposite(composite);
			moduleIsProject = createRadioButton(radioComposite, Policy.bind("ConfigurationWizardMainPage.moduleIsProject"), 2);
			moduleIsCustom = createRadioButton(radioComposite, Policy.bind("ConfigurationWizardMainPage.module"), 1);
			moduleText = createTextField(radioComposite);
		}
		if ((style & PROJECT_NAME) != 0) {
			Composite radioComposite = createRadioComposite(composite);
			projectIsModule = createRadioButton(radioComposite, Policy.bind("ConfigurationWizardMainPage.projectIsModule"), 2);
			projectIsCustom = createRadioButton(radioComposite, Policy.bind("ConfigurationWizardMainPage.projectName"), 1);
			projectText = createTextField(radioComposite);			
		}
		if ((style & TAG) != 0) {
			createLabel(composite, "");
			createLabel(composite, "");
			Composite radioComposite = createRadioComposite(composite);	
			checkoutHead = createRadioButton(radioComposite, Policy.bind("ConfigurationWizardMainPage.checkoutHead"), 2);
			checkoutTag = createRadioButton(radioComposite, Policy.bind("ConfigurationWizardMainPage.checkoutTag"), 1);
			tagCombo = createEditableCombo(radioComposite);
		}
		
		initializeValues();
		updateWidgetEnablements();
		if (userCombo != null) {
			userCombo.setFocus();
		}
		
		setControl(composite);
	}
	/**
	 * Utility method to create an editable combo box
	 * 
	 * @param parent  the parent of the combo box
	 * @return the created combo
	 */
	protected Combo createEditableCombo(Composite parent) {
		Combo combo = new Combo(parent, SWT.NULL);
		GridData data = new GridData(GridData.FILL_HORIZONTAL);
		data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
		combo.setLayoutData(data);
		combo.addListener(SWT.Modify, this);
		return combo;
	}
	/**
	 * Utility method to create a radio button
	 * 
	 * @param parent  the parent of the radio button
	 * @param label  the label of the radio button
	 * @param span  the number of columns to span
	 * @return the created radio button
	 */
	protected Button createRadioButton(Composite parent, String label, int span) {
		Button button = new Button(parent, SWT.RADIO);
		button.setText(label);
		button.addListener(SWT.Selection, this);
		GridData data = new GridData();
		data.horizontalSpan = span;
		button.setLayoutData(data);
		return button;
	}
	/**
	 * Utility method to create a composite for radio buttons
	 * 
	 * @param composite  the parent
	 * @return the created composite
	 */
	protected Composite createRadioComposite(Composite composite) {
		Composite comboComposite = new Composite(composite, SWT.NULL);
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		comboComposite.setLayout(layout);
		GridData data = new GridData(GridData.FILL_BOTH);
		data.horizontalSpan = 2;
		comboComposite.setLayoutData(data);
		return comboComposite;
	}
	/**
	 * @see CVSWizardPage#finish
	 */
	public boolean finish(IProgressMonitor monitor) {
		// Set the result to be the current values
		Properties result = new Properties();
		if ((style & CONNECTION_METHOD) != 0) {
			result.setProperty("connection", connectionMethodCombo.getText());
		}
		if ((style & USER) != 0) {
			result.setProperty("user", userCombo.getText());
		}
		if ((style & PASSWORD) != 0) {
			result.setProperty("password", passwordText.getText());
		}
		if ((style & HOST) != 0) {
			result.setProperty("host", hostCombo.getText());
		}
		if ((style & PORT) != 0) {
			if (useCustomPort.getSelection()) {
				result.setProperty("port", portText.getText());
			}
		}
		if ((style & REPOSITORY_PATH) != 0) {
			result.setProperty("root", repositoryPathCombo.getText());
		}
		if ((style & TAG) != 0) {
			if (checkoutTag.getSelection()) {
				result.setProperty("tag", tagCombo.getText());
			}
		}
		if ((style & PROJECT_NAME) != 0) {
			if (projectIsCustom.getSelection()) {
				result.setProperty("project", projectText.getText());
			}
		}
		if ((style & MODULE_RADIO) != 0) {
			if (moduleIsCustom.getSelection()) {
				result.setProperty("module", moduleText.getText());
			}
		} else if ((style & MODULE_TEXT) != 0) {
			result.setProperty("module", moduleCombo.getText());
		}
		this.properties = result;
		
		saveWidgetValues();
		
		return true;
	}
	/**
	 * Returns the properties for the repository connection
	 * 
	 * @return the properties or null
	 */
	public Properties getProperties() {
		return properties;
	}
	/**
	 * Initializes states of the controls.
	 */
	private void initializeValues() {
		// Set remembered values
		IDialogSettings settings = getDialogSettings();
		if (settings != null) {
			if ((style & HOST) != 0) {
				String[] hostNames = settings.getArray(STORE_HOSTNAME_ID);
				if (hostNames != null) {
					for (int i = 0; i < hostNames.length; i++) {
						hostCombo.add(hostNames[i]);
					}
				}
			}
			if ((style & REPOSITORY_PATH) != 0) {
				String[] paths = settings.getArray(STORE_PATH_ID);
				if (paths != null) {
					for (int i = 0; i < paths.length; i++) {
						repositoryPathCombo.add(paths[i]);
					}
				}
			}
			if ((style & USER) != 0) {
				String[] userNames = settings.getArray(STORE_USERNAME_ID);
				if (userNames != null) {
					for (int i = 0; i < userNames.length; i++) {
						userCombo.add(userNames[i]);
					}
				}
			}
			if ((style & MODULE_TEXT) != 0) {
				String[] moduleNames = settings.getArray(STORE_MODULE_ID);
				if (moduleNames != null) {
					for (int i = 0; i < moduleNames.length; i++) {
						moduleCombo.add(moduleNames[i]);
					}
				}
			}
			if ((style & TAG) != 0) {
				String[] tags = settings.getArray(STORE_TAG_ID);
				if (tags != null) {
					for (int i = 0; i < tags.length; i++) {
						tagCombo.add(tags[i]);
					}
				}
			}
		}
		
		// Initialize other values and widget states
		if ((style & CONNECTION_METHOD) != 0) {
			String[] methods = CVSTeamProvider.getConnectionMethods();
			for (int i = 0; i < methods.length; i++) {
				connectionMethodCombo.add(methods[i]);
			}
			String method = (String)properties.getProperty("method");
			if (method == null) {
				connectionMethodCombo.select(0);
			} else {
				connectionMethodCombo.select(connectionMethodCombo.indexOf(method));
			}
		}
		if ((style & USER) != 0) {
			String user = (String)properties.getProperty("user");
			if (user != null) {
				userCombo.setText(user);
			}
		}
		if ((style & PASSWORD) != 0) {
			String password = (String)properties.getProperty("password");
			if (password != null) {
				passwordText.setText(password);
			}
		}
		if ((style & HOST) != 0) {
			String host = (String)properties.getProperty("host");
			if (host != null) {
				hostCombo.setText(host);
			}
		}
		if ((style & PORT) != 0) {
			String port = (String)properties.getProperty("port");
			if (port == null) {
				useDefaultPort.setSelection(true);
			} else {
				useCustomPort.setSelection(true);
				portText.setText(port);
			}
		}
		if ((style & REPOSITORY_PATH) != 0) {
			String repositoryPath = (String)properties.getProperty("root");
			if (repositoryPath != null) {
				repositoryPathCombo.setText(repositoryPath);
			}
		}
		if ((style & MODULE_RADIO) != 0) {
			String module = (String)properties.getProperty("module");
			if (module == null) {
				moduleIsProject.setSelection(true);
			} else {
				moduleIsCustom.setSelection(true);
				moduleText.setText(module);
			}
		} else if ((style & MODULE_TEXT) != 0) {
			String module = (String)properties.getProperty("module");
			if (module != null) {
				moduleCombo.setText(module);
			}
		}
		if ((style & PROJECT_NAME) != 0) {
			String project = (String)properties.getProperty("project");
			if (project == null) {
				projectIsModule.setSelection(true);
			} else {
				projectIsCustom.setSelection(true);
				projectText.setText(project);
			}
		}
		if ((style & TAG) != 0) {
			String tag = (String)properties.getProperty("tag");
			if (tag == null) {
				checkoutHead.setSelection(true);
			} else {
				checkoutTag.setSelection(true);
				tagCombo.setText(tag);
			}
		}
}
	/**
	 * Saves the widget values
	 */
	private void saveWidgetValues() {
		// Update history
		IDialogSettings settings = getDialogSettings();
		if (settings != null) {
			if ((style & USER) != 0) {
				String[] userNames = settings.getArray(STORE_USERNAME_ID);
				if (userNames == null) userNames = new String[0];
				userNames = addToHistory(userNames, userCombo.getText());
				settings.put(STORE_USERNAME_ID, userNames);
			}
			if ((style & HOST) != 0) {
				String[] hostNames = settings.getArray(STORE_HOSTNAME_ID);
				if (hostNames == null) hostNames = new String[0];
				hostNames = addToHistory(hostNames, hostCombo.getText());
				settings.put(STORE_HOSTNAME_ID, hostNames);
			}
			if ((style & REPOSITORY_PATH) != 0) {
				String[] paths = settings.getArray(STORE_PATH_ID);
				if (paths == null) paths = new String[0];
				paths = addToHistory(paths, repositoryPathCombo.getText());
				settings.put(STORE_PATH_ID, paths);
			}
			if ((style & MODULE_TEXT) != 0) {
				String[] modules = settings.getArray(STORE_MODULE_ID);
				if (modules == null) modules = new String[0];
				modules = addToHistory(modules, moduleCombo.getText());
				settings.put(STORE_MODULE_ID, modules);
			}
			if ((style & TAG) != 0) {
				String tag = tagCombo.getText();
				if (!tag.equals("")) {
					String[] tags = settings.getArray(STORE_TAG_ID);
					if (tags == null) tags = new String[0];
					tags = addToHistory(tags, tag);
					settings.put(STORE_TAG_ID, tags);
				}
			}
		}
	}
	/**
	 * Set the style for the wizard page
	 * 
	 * @param style  the style for the page
	 */
	public void setStyle(int style) {
		this.style = style;
	}
	/**
	 * Sets the properties for the repository connection
	 * 
	 * @param properties  the properties or null
	 */
	public void setProperties(Properties properties) {
		this.properties = properties;
	}
	/**
	 * Updates widget enablements and sets error message if appropriate.
	 */
	protected void updateWidgetEnablements() {
		if ((style & PORT) != 0) {
			if (useDefaultPort.getSelection()) {
				portText.setEnabled(false);
			} else {
				portText.setEnabled(true);
			}
		}
		if ((style & PROJECT_NAME) != 0) {
			if (projectIsModule.getSelection()) {
				projectText.setEnabled(false);
			} else {
				projectText.setEnabled(true);
			}
		}
		if ((style & MODULE_RADIO) != 0) {
			if (moduleIsProject.getSelection()) {
				moduleText.setEnabled(false);
			} else {
				moduleText.setEnabled(true);
			}
		}
		if ((style & TAG) != 0) {
			if (checkoutHead.getSelection()) {
				tagCombo.setEnabled(false);
			} else {
				tagCombo.setEnabled(true);
			}
		}
		validateFields();
	}
	/**
	 * Validates the contents of the editable fields and set page completion 
	 * and error messages appropriately.
	 */
	private void validateFields() {
		if ((style & USER) != 0) {
			String user = userCombo.getText();
			if (user.length() == 0) {
				setErrorMessage(null);
				setPageComplete(false);
				return;
			}
			if ((user.indexOf('@') != -1) || (user.indexOf(':') != -1)) {
				setErrorMessage(Policy.bind("ConfigurationWizardMainPage.invalidUserName"));
				setPageComplete(false);
				return;
			}
		}
		if ((style & HOST) != 0) {
			String host = hostCombo.getText();
			if (host.length() == 0) {
				setErrorMessage(null);
				setPageComplete(false);
				return;
			}
			if (host.indexOf(':') != -1) {
				setErrorMessage(Policy.bind("ConfigurationWizardMainPage.invalidHostName"));
				setPageComplete(false);
				return;
			}
		}
		if ((style & PORT) != 0) {
			if (portText.isEnabled()) {
				if (portText.getText().length() == 0) {
					setErrorMessage(null);
					setPageComplete(false);
					return;
				}
				try {
					Integer.parseInt(portText.getText());
				} catch (NumberFormatException e) {
					setErrorMessage(Policy.bind("ConfigurationWizardMainPage.invalidPort"));
					setPageComplete(false);
					return;
				}
			}
		}
		if ((style & REPOSITORY_PATH) != 0) {
			if (repositoryPathCombo.getText().length() == 0) {
				setErrorMessage(null);
				setPageComplete(false);
				return;
			}
		}
		if ((style & MODULE_RADIO) != 0) {
			if (moduleIsCustom.getSelection()) {
				if (moduleText.getText().length() == 0) {
					setErrorMessage(null);
					setPageComplete(false);
					return;
				}
			}
		}
		if ((style & MODULE_TEXT) != 0) {
			if (moduleCombo.getText().length() == 0) {
				setErrorMessage(null);
				setPageComplete(false);
				return;
			}
		}
		if ((style & PROJECT_NAME) != 0) {
			if (projectIsCustom.getSelection()) {
				if (projectText.getText().length() == 0) {
					setErrorMessage(null);
					setPageComplete(false);
					return;
				}
			}
		}
		setErrorMessage(null);
		setPageComplete(true);
	}
}

Back to the top