Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c8887534afc702d7ccd55995baa4ada50e70834c (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
/*******************************************************************************
 * Copyright (c) 2012, 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 java.util.Map;
import java.util.UUID;

import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogPage;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
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.runtime.properties.PropertiesContainer;
import org.eclipse.tcf.te.tcf.core.interfaces.ITransportTypes;
import org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerNode;
import org.eclipse.tcf.te.tcf.locator.model.ModelManager;
import org.eclipse.tcf.te.tcf.ui.controls.CustomTransportPanel;
import org.eclipse.tcf.te.tcf.ui.controls.PeerAttributesTablePart;
import org.eclipse.tcf.te.tcf.ui.controls.PeerNameControl;
import org.eclipse.tcf.te.tcf.ui.controls.PipeTransportPanel;
import org.eclipse.tcf.te.tcf.ui.controls.TcpTransportPanel;
import org.eclipse.tcf.te.tcf.ui.controls.TransportTypeControl;
import org.eclipse.tcf.te.tcf.ui.controls.TransportTypePanelControl;
import org.eclipse.tcf.te.tcf.ui.help.IContextHelpIds;
import org.eclipse.tcf.te.tcf.ui.nls.Messages;
import org.eclipse.tcf.te.ui.controls.interfaces.IWizardConfigurationPanel;
import org.eclipse.tcf.te.ui.forms.FormLayoutFactory;
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.AbstractValidatingWizardPage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.forms.widgets.ExpandableComposite;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;

/**
 * Wizard page implementation querying all information needed
 * to create the different TCF peer types.
 */
public class NewTargetWizardPage extends AbstractValidatingWizardPage implements IDataExchangeNode {
	private PeerNameControl peerNameControl;
	TransportTypeControl transportTypeControl;
	TransportTypePanelControl transportTypePanelControl;
	private PeerAttributesTablePart tablePart;

	private FormToolkit toolkit = null;

	// The UUID of the new peer to create
	private final UUID uuid = UUID.randomUUID();

	// 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>();

	/**
	 * Local transport type control implementation.
	 */
	private class MyTransportTypeControl extends TransportTypeControl {

		/**
		 * 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 MyTransportTypeControl(IDialogPage parentPage) {
			super(parentPage);
		}

		/* (non-Javadoc)
		 * @see org.eclipse.tcf.te.ui.controls.BaseEditBrowseTextControl#widgetSelected(org.eclipse.swt.events.SelectionEvent)
		 */
		@Override
		public void widgetSelected(SelectionEvent e) {
			if (transportTypePanelControl != null) {
				transportTypePanelControl.showConfigurationPanel(getSelectedTransportType());
				validate();
			}
		}
	}

	/**
	 * Local transport type panel control implementation.
	 */
	private class MyTransportTypePanelControl extends TransportTypePanelControl {

		/**
		 * 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 MyTransportTypePanelControl(IDialogPage parentPage) {
			super(parentPage);
		}

		/* (non-Javadoc)
		 * @see org.eclipse.tcf.te.ui.controls.BaseControl#isValid()
		 */
		@Override
		public boolean isValid() {
			boolean valid = super.isValid();
			if (!valid) return false;

			// Get the currently selected transport type
			if (transportTypeControl != null) {
				String transportType = transportTypeControl.getSelectedTransportType();
				if (transportType != null) {
					// get the panel for the transport type and validate the panel
					IWizardConfigurationPanel panel = getConfigurationPanel(transportType);

					if (panel != null) {
						valid = panel.isValid();
						setMessage(panel.getMessage(), panel.getMessageType());
					}
				}
			}

			return valid;
		}
	}

	/**
	 * Constructor.
	 */
	public NewTargetWizardPage() {
		this(NewTargetWizardPage.class.getName());
	}

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

	/* (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.DialogPage#dispose()
	 */
	@Override
	public void dispose() {
		if (peerNameControl != null) { peerNameControl.dispose(); peerNameControl = null; }
		if (transportTypeControl != null) { transportTypeControl.dispose(); transportTypeControl = null; }
		if (transportTypePanelControl != null) { transportTypePanelControl.dispose(); transportTypePanelControl = null; }
		if (tablePart != null) { tablePart.dispose(); tablePart = null; }
		if (toolkit != null) { toolkit.dispose(); toolkit = null; }

	    super.dispose();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	public void createControl(Composite parent) {
		// Setup title and description
		setTitle(Messages.NewTargetWizardPage_title);
		setDescription(Messages.NewTargetWizardPage_description);

		// Create the forms toolkit
		toolkit = new FormToolkit(parent.getDisplay());

		// Create the main panel
		Composite mainPanel = toolkit.createComposite(parent);
		mainPanel.setLayout(FormLayoutFactory.createClearGridLayout(false, 1));
		mainPanel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		mainPanel.setBackground(parent.getBackground());

		setControl(mainPanel);

		// Setup the help
		PlatformUI.getWorkbench().getHelpSystem().setHelp(mainPanel, IContextHelpIds.NEW_TARGET_WIZARD_PAGE);

		// Do not validate the page while creating the controls
		boolean changed = setValidationInProgress(true);
		// Create the main panel sub controls
		createMainPanelControls(mainPanel, toolkit);
		// Reset the validation in progress state
		if (changed) setValidationInProgress(false);

		// Adjust the font
		Dialog.applyDialogFont(mainPanel);

		// Validate the page for the first time
		validate();
	}

	/**
	 * Creates the main panel sub controls.
	 *
	 * @param parent The parent main panel composite. Must not be <code>null</code>.
	 * @param toolkit The form toolkit. Must not be <code>null</code>.
	 */
	protected void createMainPanelControls(Composite parent, FormToolkit toolkit) {
		Assert.isNotNull(parent);
		Assert.isNotNull(toolkit);

		// Create the client composite
		Composite client = toolkit.createComposite(parent);
		client.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, 2));
		client.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		client.setBackground(parent.getBackground());

		// Add the controls
		peerNameControl = new PeerNameControl(this) {
			@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 NewTargetWizardPage) {
						valid = !((NewTargetWizardPage)getParentPage()).usedNames.contains(name.trim().toUpperCase());
						if (!valid) {
							setMessage(Messages.NewTargetWizardPage_error_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;
			}
		};
		peerNameControl.setFormToolkit(toolkit);
		peerNameControl.setParentControlIsInnerPanel(true);
		peerNameControl.setupPanel(client);
		peerNameControl.getEditFieldControl().setFocus();

		createEmptySpace(client, 5, 2, toolkit);

		// Create and configure the transport type section
		Section transportTypeSection = toolkit.createSection(client, ExpandableComposite.TITLE_BAR);
		transportTypeSection.setText(Messages.NewTargetWizardPage_section_transportType);
		transportTypeSection.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, 2));
		GridData layoutData = new GridData(SWT.FILL, SWT.CENTER, true, false);
		layoutData.horizontalSpan = 2;
		transportTypeSection.setLayoutData(layoutData);
		transportTypeSection.setBackground(client.getBackground());

		Composite transportTypeClient = toolkit.createComposite(transportTypeSection);
		transportTypeClient.setLayout(new GridLayout());
		transportTypeClient.setBackground(transportTypeSection.getBackground());
		transportTypeSection.setClient(transportTypeClient);

		// Create the transport type control
		transportTypeControl = new MyTransportTypeControl(this);
		transportTypeControl.setFormToolkit(toolkit);
		transportTypeControl.setupPanel(transportTypeClient);

		// The transport type specific controls are placed into a stack
		transportTypePanelControl = new MyTransportTypePanelControl(this);

		// Create and add the panels
		TcpTransportPanel tcpTransportPanel = new TcpTransportPanel(transportTypePanelControl);
		transportTypePanelControl.addConfigurationPanel(ITransportTypes.TRANSPORT_TYPE_TCP, tcpTransportPanel);
		transportTypePanelControl.addConfigurationPanel(ITransportTypes.TRANSPORT_TYPE_SSL, tcpTransportPanel);
		transportTypePanelControl.addConfigurationPanel(ITransportTypes.TRANSPORT_TYPE_PIPE, new PipeTransportPanel(transportTypePanelControl));
		transportTypePanelControl.addConfigurationPanel(ITransportTypes.TRANSPORT_TYPE_CUSTOM, new CustomTransportPanel(transportTypePanelControl));

		// Setup the panel control
		transportTypePanelControl.setupPanel(transportTypeClient, transportTypeControl.getTransportTypes(), toolkit);
		layoutData = new GridData(SWT.FILL, SWT.FILL, true, true);
		layoutData.horizontalSpan = 2;
		transportTypePanelControl.getPanel().setLayoutData(layoutData);
		toolkit.adapt(transportTypePanelControl.getPanel());

		transportTypePanelControl.showConfigurationPanel(transportTypeControl.getSelectedTransportType());

		// Create the advanced peer properties table
		createPeerAttributesTableControl(client, toolkit);

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

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

	/**
	 * Creates the peer attributes table controls.
	 *
	 * @param parent The parent composite. Must not be <code>null</code>.
	 * @param toolkit The form toolkit. Must not be <code>null</code>.
	 */
	protected void createPeerAttributesTableControl(Composite parent, FormToolkit toolkit) {
		Assert.isNotNull(parent);

		createEmptySpace(parent, 5, 2, toolkit);

		// Create and configure the advanced attributes section
		Section attributesSection = toolkit.createSection(parent, ExpandableComposite.TITLE_BAR);
		attributesSection.setText(Messages.NewTargetWizardPage_section_attributes);
		attributesSection.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, 2));
		GridData layoutData = new GridData(SWT.FILL, SWT.CENTER, true, false);
		layoutData.horizontalSpan = 2;
		attributesSection.setLayoutData(layoutData);
		attributesSection.setBackground(parent.getBackground());

		Composite client = toolkit.createComposite(attributesSection);
		client.setLayout(new GridLayout(2, false));
		client.setBackground(attributesSection.getBackground());
		attributesSection.setClient(client);

		tablePart = new PeerAttributesTablePart();
		tablePart.setMinSize(SWTControlUtil.convertWidthInCharsToPixels(client, 20), SWTControlUtil.convertHeightInCharsToPixels(client, 6));
		tablePart.setBannedNames(new String[] { IPeer.ATTR_ID, IPeer.ATTR_AGENT_ID, IPeer.ATTR_SERVICE_MANGER_ID, IPeer.ATTR_NAME, IPeer.ATTR_TRANSPORT_NAME, IPeer.ATTR_IP_HOST, IPeer.ATTR_IP_PORT, "PipeName" }); //$NON-NLS-1$
		tablePart.createControl(client, SWT.SINGLE | SWT.FULL_SELECTION, 2, toolkit);
	}

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

		boolean valid = true;

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

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

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

		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) {

		if (data.containsKey(IPeer.ATTR_NAME) && peerNameControl != null) {
			String name = data.getStringProperty(IPeer.ATTR_NAME);
			int i = 1;
			while (usedNames.contains(name.toUpperCase())) {
				name = data.getStringProperty(IPeer.ATTR_NAME) + " (" + i + ")"; //$NON-NLS-1$ //$NON-NLS-2$
				i++;
			}
			peerNameControl.setEditFieldControlText(name);
		}

		String transportType = data.getStringProperty(IPeer.ATTR_TRANSPORT_NAME);
		if (transportType != null) {
			if (transportTypeControl != null) {
				transportTypeControl.setSelectedTransportType(transportType);
				if (!transportTypeControl.getSelectedTransportType().equals(transportType)) {
					transportTypeControl.setSelectedTransportType(ITransportTypes.TRANSPORT_TYPE_CUSTOM);
				}
				transportTypePanelControl.showConfigurationPanel(transportTypeControl.getSelectedTransportType());
			}
			IWizardConfigurationPanel panel = transportTypePanelControl != null ? transportTypePanelControl.getActiveConfigurationPanel() : null;
			if (panel instanceof IDataExchangeNode) {
				((IDataExchangeNode)panel).setupData(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;

		peerAttributes.setProperty(IPeer.ATTR_ID, uuid.toString());

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

		value = transportTypeControl != null ? transportTypeControl.getSelectedTransportType() : null;
		if (value != null && !"".equals(value) && !ITransportTypes.TRANSPORT_TYPE_CUSTOM.equals(value)) { //$NON-NLS-1$
			peerAttributes.setProperty(IPeer.ATTR_TRANSPORT_NAME, value);
		}

		IWizardConfigurationPanel panel = transportTypePanelControl != null ? transportTypePanelControl.getConfigurationPanel(value) : null;
		if (panel instanceof IDataExchangeNode) {
			IPropertiesContainer data = new PropertiesContainer();
			((IDataExchangeNode)panel).extractData(data);

			// Copy all string properties to the peer attributes map
			for (String key : data.getProperties().keySet()) {
				value = data.getStringProperty(key);
				if (value != null && !"".equals(value)) peerAttributes.setProperty(key, value); //$NON-NLS-1$
			}
		}

		Map<String, String> additionalAttributes = tablePart != null ? tablePart.getAttributes() : null;
		if (additionalAttributes != null && !additionalAttributes.isEmpty()) {
			peerAttributes.addProperties(additionalAttributes);
		}
	}

	/* (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() {
		IDialogSettings settings = getDialogSettings();
		if (settings != null) {
			if (peerNameControl != null) peerNameControl.saveWidgetValues(settings, null);
			if (transportTypeControl != null) transportTypeControl.saveWidgetValues(settings, null);
			if (transportTypePanelControl != null) transportTypePanelControl.saveWidgetValues(settings, null);
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.wizards.pages.AbstractWizardPage#restoreWidgetValues()
	 */
	@Override
	public void restoreWidgetValues() {
		IDialogSettings settings = getDialogSettings();
		if (settings != null) {
			if (peerNameControl != null) peerNameControl.restoreWidgetValues(settings, null);
			if (transportTypeControl != null) transportTypeControl.restoreWidgetValues(settings, null);
			if (transportTypePanelControl != null) transportTypePanelControl.restoreWidgetValues(settings, null);
		}
	}

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

		Runnable runnable = new Runnable() {
			@Override
			public void run() {
				// Get all peer model objects
				IPeerNode[] peers = ModelManager.getPeerModel().getPeerNodes();
				// Loop them and find the ones which are of our handled types
				for (IPeerNode peerNode : peers) {
						String name = peerNode.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);
	}
}

Back to the top