Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b201c8ee6623b0ca5979105129448e83fd620b1f (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
/*******************************************************************************
 * Copyright (c) 2013 Wind River Systems, Inc. and others. 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:
 * Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.te.tcf.ui.wizards.pages;

import java.util.ArrayList;

import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.dialogs.IDialogPage;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.tcf.protocol.IPeer;
import org.eclipse.tcf.protocol.Protocol;
import org.eclipse.tcf.te.runtime.interfaces.properties.IPropertiesContainer;
import org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerModel;
import org.eclipse.tcf.te.tcf.locator.model.Model;
import org.eclipse.tcf.te.tcf.ui.controls.PeerNameControl;
import org.eclipse.tcf.te.tcf.ui.nls.Messages;
import org.eclipse.tcf.te.ui.controls.validator.RegexValidator;
import org.eclipse.tcf.te.ui.controls.validator.Validator;
import org.eclipse.tcf.te.ui.forms.CustomFormToolkit;
import org.eclipse.tcf.te.ui.forms.parts.AbstractSection;
import org.eclipse.tcf.te.ui.interfaces.data.IDataExchangeNode;
import org.eclipse.tcf.te.ui.swt.SWTControlUtil;
import org.eclipse.tcf.te.ui.wizards.pages.AbstractFormsWizardPage;
import org.eclipse.ui.forms.IManagedForm;

/**
 * Abstract new configuration wizard page implementation.
 */
public abstract class AbstractConfigWizardPage extends AbstractFormsWizardPage implements IDataExchangeNode {
	private ConfigNameControl configName = null;
	private AbstractSection selectorSection = null;
	private AbstractSection detailsSection = null;
	/* default */ Button launchDbg = null;
	private Button advancedButton = null;

	/* default */ boolean autoLaunchDbg = false;

	// The list of existing configuration names. Used to generate a unique name
	// and validate the wizard
	/* default */ final java.util.List<String> usedNames = new ArrayList<String>();

	/**
	 * Internal configuration name control implementation.
	 */
	protected static class ConfigNameControl extends PeerNameControl {

		/**
		 * Constructor.
		 * @param parentPage The parent dialog page this control is embedded in.
		 *                   Might be <code>null</code> if the control is not associated with a page.
		 */
		public ConfigNameControl(IDialogPage parentPage) {
			super(parentPage);

			setEditFieldLabel(Messages.AbstractConfigWizardPage_configName_label);
		}

		/* (non-Javadoc)
		 * @see org.eclipse.tcf.te.ui.controls.BaseEditBrowseTextControl#doCreateEditFieldValidator()
		 */
		@Override
		protected Validator doCreateEditFieldValidator() {
			return new RegexValidator(Validator.ATTR_MANDATORY, ".*"); //$NON-NLS-1$
		}

		/* (non-Javadoc)
		 * @see org.eclipse.tcf.te.ui.controls.BaseEditBrowseTextControl#configureEditFieldValidator(org.eclipse.tcf.te.ui.controls.validator.Validator)
		 */
		@Override
		protected void configureEditFieldValidator(Validator validator) {
			if (validator instanceof RegexValidator) {
				validator.setMessageText(RegexValidator.INFO_MISSING_VALUE, Messages.AbstractConfigWizardPage_configName_infoMissingValue);
			}
		}

		/* (non-Javadoc)
		 * @see org.eclipse.tcf.te.ui.controls.BaseEditBrowseTextControl#isValid()
		 */
		@Override
		public boolean isValid() {
			boolean valid = true;

			String name = getEditFieldControlTextForValidation();
			if (!"".equals(name)) { //$NON-NLS-1$
				// Name is not empty -> check against the list of used names
				if (getParentPage() instanceof AbstractConfigWizardPage) {
					valid = !((AbstractConfigWizardPage)getParentPage()).usedNames.contains(name.trim().toUpperCase());
					if (!valid) {
						setMessage(Messages.AbstractConfigWizardPage_configName_nameInUse, IMessageProvider.ERROR);
					}
				}
			}

			if (!valid && getControlDecoration() != null) {
				// Setup and show the control decoration if necessary
				if (isEnabled()) {
					// Update the control decorator
					updateControlDecoration(getMessage(), getMessageType());
				}
			}

			return valid ? super.isValid() : false;
		}
	}

	/**
	 * Constructor.
	 *
	 * @param pageName The page name. Must not be <code>null</code>.
	 */
	public AbstractConfigWizardPage(String pageName) {
		super(pageName);

	}

	/**
	 * Constructor.
	 *
	 * @param pageName The page name. Must not be <code>null</code>.
	 * @param title The wizard page title or <code>null</code>.
	 * @param titleImage The wizard page title image or <code>null</code>.
	 */
	public AbstractConfigWizardPage(String pageName, String title, ImageDescriptor titleImage) {
		super(pageName, title, titleImage);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.DialogPage#dispose()
	 */
	@Override
	public void dispose() {
		if (configName != null) { configName.dispose(); configName = null; }
		if (selectorSection != null) { selectorSection.dispose(); selectorSection = null; }
		if (detailsSection != null) { detailsSection.dispose(); detailsSection = null; }

		super.dispose();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.wizards.pages.AbstractFormsWizardPage#doCreateFormContent(org.eclipse.swt.widgets.Composite, org.eclipse.tcf.te.ui.forms.CustomFormToolkit)
	 */
	@Override
	protected void doCreateFormContent(Composite parent, CustomFormToolkit toolkit) {
		Assert.isNotNull(parent);
		Assert.isNotNull(toolkit);

		// Add the controls
		configName = new ConfigNameControl(this);
		configName.setupPanel(parent);

		Label label = new Label(parent, SWT.HORIZONTAL | SWT.SEPARATOR);
		label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

		selectorSection = doCreateSelectorSection(getManagedForm(), parent);
		if (selectorSection != null) {
			selectorSection.getSection().setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));
		}

		detailsSection = doCreateDetailsSection(getManagedForm(), parent);
		if (detailsSection != null) {
			detailsSection.getSection().setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));
		}

		launchDbg = new Button(parent, SWT.CHECK);
		launchDbg.setText(Messages.AbstractConfigWizardPage_launchDbg_label);
		launchDbg.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		launchDbg.setSelection(autoLaunchDbg);
		launchDbg.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				autoLaunchDbg = SWTControlUtil.getSelection(launchDbg);
			}
		});

		if (hasAdvancedButton()) {
			advancedButton = new Button(parent, SWT.PUSH);
			advancedButton.setText("  " + Messages.AbstractConfigWizardPage_advancedButton_label + "  "); //$NON-NLS-1$ //$NON-NLS-2$
			advancedButton.setLayoutData(new GridData(SWT.END, SWT.CENTER, true, false));
			advancedButton.addSelectionListener(new SelectionAdapter() {
				@Override
				public void widgetSelected(SelectionEvent e) {
					onAdvancedButtonSelected();
				}
			});
		}

		// restore the widget values from the history
		restoreWidgetValues();

		// Initialize the used configuration name list
		initializeUsedNameList();
	}

	/**
	 * Creates the selector section.
	 *
	 * @param form The managed form instance. Must not be <code>null</code>.
	 * @param parent The parent composite. Must not be <code>null</code>.
	 *
	 * @return The selector section instance or <code>null</code>.
	 */
	protected abstract AbstractSection doCreateSelectorSection(IManagedForm form, Composite parent);

	/**
	 * Returns the selector section instance.
	 *
	 * @return The selector section instance or <code>null</code> if not yet created.
	 */
	protected final AbstractSection getSelectorSection() {
		return selectorSection;
	}

	/**
	 * Creates the details section.
	 *
	 * @param form The managed form instance. Must not be <code>null</code>.
	 * @param parent The parent composite. Must not be <code>null</code>.
	 *
	 * @return The details section instance or <code>null</code>.
	 */
	protected abstract AbstractSection doCreateDetailsSection(IManagedForm form, Composite parent);

	/**
	 * Returns the details section instance.
	 *
	 * @return The details section instance or <code>null</code> if not yet created.
	 */
	protected final AbstractSection getDetailsSection() {
		return detailsSection;
	}

	/**
	 * Returns if or if not the page should show an advanced button.
	 *
	 * @return <code>True</code> if the page has an advanced button, <code>false</code> if not.
	 */
	protected boolean hasAdvancedButton() {
		return false;
	}

	/**
	 * Called once the user pressed the advanced button. Subclasses
	 * needs to overwrite this method to provide the logic behind the
	 * advanced button if {@link #hasAdvancedButton()} returned <code>true</code>.
	 */
	protected void onAdvancedButtonSelected() {
	}

	/**
	 * Returns the peer type.
	 *
	 * @return The peer type. Never <code>null</code>.
	 */
	protected abstract String getPeerType();

	/**
	 * Returns the default configuration name template.
	 *
	 * @return The default configuration name template. Never <code>null</code>.
	 */
	protected abstract String getDefaultConfigNameTemplate();

	/**
	 * Return the pattern that matches a configuration name with
	 * the default name. The pattern is used to decide if the user
	 * modified the configuration name or not.
	 *
	 * @param The default configuration name template. Must not be <code>null</code>.
	 * @return The default configuration name pattern. Never <code>null</code>.
	 */
	protected abstract String getDefaultConfigNamePattern(String template);

	/**
	 * Initialize the used name list.
	 */
	protected void initializeUsedNameList() {
		usedNames.clear();

		Runnable runnable = new Runnable() {
			@Override
			public void run() {
				// Get all peer model objects
				IPeerModel[] peers = Model.getModel().getPeers();
				// Loop them and find the ones which are of our handled types
				for (IPeerModel peerModel : peers) {
					if (peerModel.isStatic()) {
						String name = peerModel.getPeer().getName();
						Assert.isNotNull(name);
						if (!"".equals(name) && !usedNames.contains(name)) { //$NON-NLS-1$
							usedNames.add(name.trim().toUpperCase());
						}
					}
				}
			}
		};

		Assert.isTrue(!Protocol.isDispatchThread());
		Protocol.invokeAndWait(runnable);
	}

	/**
	 * Auto-generate a configuration name.
	 *
	 * @param customID The custom ID to bind with the default configuration name template, or <code>null</code>.
	 */
	public void autoGenerateConfigurationName(String customID) {
		// Get the default configuration name template
		String template = getDefaultConfigNameTemplate();
		Assert.isNotNull(template);
		// If the user modified the configuration name, we have to leave it alone.
		String pattern = getDefaultConfigNamePattern(template);
		Assert.isNotNull(pattern);
		String currentName = configName != null ? configName.getEditFieldControlText().trim() : null;
		if (currentName != null && !"".equals(currentName.trim()) && !currentName.matches(pattern)) { //$NON-NLS-1$
			return;
		}
		// Generate a new proposal
		final String origProposedName = NLS.bind(template, customID != null ? customID : ""); //$NON-NLS-1$
		String proposedName = origProposedName;

		// Unify the proposed name to avoid duplicated configuration names
		int count = 0;
		while (usedNames.contains(proposedName.trim().toUpperCase())) {
			count++;
			proposedName = origProposedName.trim() + " (" + count + ")"; //$NON-NLS-1$ //$NON-NLS-2$
		}

		// Set the configuration name
		if (configName != null) {
			configName.setEditFieldControlText(proposedName.trim());
		}
	}

	/**
	 * Returns if or if not to auto-start the debugger after the configuration got created.
	 *
	 * @return <code>True</code> to auto-start the debugger, <code>false</code> if not.
	 */
	public final boolean isAutoStartDebugger() {
		return autoLaunchDbg;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.wizards.pages.AbstractValidatingWizardPage#doValidate()
	 */
	@Override
	protected ValidationResult doValidate() {
		ValidationResult result = new ValidationResult();

		boolean valid = true;

		if (configName != null) {
			valid &= configName.isValid();
			result.setResult(configName);
		}

		if (selectorSection != null) {
			valid &= selectorSection.isValid();
			result.setResult(selectorSection);
		}

		if (detailsSection != null) {
			valid &= detailsSection.isValid();
			result.setResult(detailsSection);
		}

		result.setValid(valid);

		return result;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.interfaces.data.IDataExchangeNode#setupData(org.eclipse.tcf.te.runtime.interfaces.properties.IPropertiesContainer)
	 */
	@Override
	public void setupData(IPropertiesContainer data) {
	}

	/**
	 * Updates the given attributes properties container with the current control content.
	 *
	 * @param peerAttributes The peer attributes. Must not be <code>null</code>.
	 */
	protected void updatePeerAttributes(IPropertiesContainer peerAttributes) {
		Assert.isNotNull(peerAttributes);

		// If the page has been never shown, we are done here
		if (getControl() == null) {
			return;
		}

		String value = configName != null ? configName.getEditFieldControlText() : null;
		if (value != null && !"".equals(value)) { //$NON-NLS-1$
			peerAttributes.setProperty(IPeer.ATTR_NAME, value);
		}

		if (isAutoStartDebugger()) {
			peerAttributes.setProperty(org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerModelProperties.PROP_AUTO_START_DEBUGGER, true);
		}

		if (selectorSection != null) {
			updateAttribute(selectorSection, peerAttributes);
		}
		if (detailsSection != null) {
			updateAttribute(detailsSection, peerAttributes);
		}
	}

	/**
	 * Update the given attributes properties container with the section
	 * attributes.
	 *
	 * @param section The section. Must not be <code>null</code>.
	 * @param peerAttributes The peer attributes. Must not be <code>null</code>.
	 */
	protected abstract void updateAttribute(AbstractSection section, IPropertiesContainer peerAttributes);

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.interfaces.data.IDataExchangeNode#extractData(org.eclipse.tcf.te.runtime.interfaces.properties.IPropertiesContainer)
	 */
	@Override
	public void extractData(IPropertiesContainer data) {
		Assert.isNotNull(data);
		// Update with the current control content
		updatePeerAttributes(data);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.wizards.pages.AbstractWizardPage#saveWidgetValues()
	 */
	@Override
	public void saveWidgetValues() {
		super.saveWidgetValues();

		IDialogSettings settings = getDialogSettings();
		if (settings != null) {
			if (selectorSection != null) {
				selectorSection.saveWidgetValues(settings);
			}
			if (detailsSection != null) {
				detailsSection.saveWidgetValues(settings);
			}
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.wizards.pages.AbstractWizardPage#restoreWidgetValues()
	 */
	@Override
	public void restoreWidgetValues() {
		super.restoreWidgetValues();

		IDialogSettings settings = getDialogSettings();
		if (settings != null) {
			if (selectorSection != null) {
				selectorSection.restoreWidgetValues(settings);
			}
			if (detailsSection != null) {
				detailsSection.restoreWidgetValues(settings);
			}
		}
	}
}

Back to the top