Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: be515d53ad2f03e5a914364f5d2771343af741d2 (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
/*******************************************************************************
 * Copyright (c) 2011, 2012 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.ui.terminals.panels;

import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.dialogs.IInputValidator;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
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.Label;
import org.eclipse.tcf.te.runtime.services.ServiceManager;
import org.eclipse.tcf.te.runtime.services.interfaces.IPropertiesAccessService;
import org.eclipse.tcf.te.runtime.services.interfaces.constants.IPropertiesAccessServiceConstants;
import org.eclipse.tcf.te.ui.controls.BaseDialogPageControl;
import org.eclipse.tcf.te.ui.controls.panels.AbstractWizardConfigurationPanel;
import org.eclipse.tcf.te.ui.swt.SWTControlUtil;
import org.eclipse.tcf.te.ui.terminals.activator.UIPlugin;
import org.eclipse.tcf.te.ui.terminals.interfaces.IConfigurationPanel;
import org.eclipse.tcf.te.ui.terminals.nls.Messages;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.WorkbenchEncoding;
import org.eclipse.ui.ide.IDEEncoding;

/**
 * Abstract terminal configuration panel implementation.
 */
public abstract class AbstractConfigurationPanel extends AbstractWizardConfigurationPanel implements IConfigurationPanel {
	private static final String LAST_HOST_TAG = "lastHost";//$NON-NLS-1$
	private static final String HOSTS_TAG = "hosts";//$NON-NLS-1$
	private static final String ENCODINGS_TAG = "encodings"; //$NON-NLS-1$

	// The sub-controls
	/* default */ Combo hostCombo;
	private Button deleteHostButton;
	/* default */ Combo encodingCombo;

	// The last selected encoding
	/* default */ String lastSelectedEncoding;
	// The last entered custom encodings
	/* default */ final List<String> encodingHistory = new ArrayList<String>();

	// The selection
	private ISelection selection;
	// A map containing the settings per host
	protected final Map<String, Map<String, String>> hostSettingsMap = new HashMap<String, Map<String, String>>();

	/**
	 * Constructor.
	 *
	 * @param parentControl The parent control. Must not be <code>null</code>!
	 */
	public AbstractConfigurationPanel(BaseDialogPageControl parentControl) {
		super(parentControl);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.terminals.interfaces.IConfigurationPanel#setSelection(org.eclipse.jface.viewers.ISelection)
	 */
	@Override
	public void setSelection(ISelection selection) {
		this.selection = selection;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.terminals.interfaces.IConfigurationPanel#getSelection()
	 */
	@Override
	public ISelection getSelection() {
		return selection;
	}

	/**
	 * Returns the host name or IP from the current selection.
	 *
	 * @return The host name or IP, or <code>null</code>.
	 */
	public String getSelectionHost() {
		ISelection selection = getSelection();
		if (selection instanceof IStructuredSelection && !selection.isEmpty()) {
			Object element = ((IStructuredSelection) selection).getFirstElement();
			IPropertiesAccessService service = ServiceManager.getInstance().getService(element, IPropertiesAccessService.class);
			if (service != null) {
				Map<String, String> props = service.getTargetAddress(element);
				if (props != null && props.containsKey(IPropertiesAccessServiceConstants.PROP_ADDRESS)) {
					return props.get(IPropertiesAccessServiceConstants.PROP_ADDRESS);
				}
			}
		}

		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.controls.panels.AbstractWizardConfigurationPanel#doRestoreWidgetValues(org.eclipse.jface.dialogs.IDialogSettings, java.lang.String)
	 */
	@Override
	public void doRestoreWidgetValues(IDialogSettings settings, String idPrefix) {
		Assert.isNotNull(settings);

		String[] hosts = settings.getArray(HOSTS_TAG);
		if (hosts != null) {
			for (int i = 0; i < hosts.length; i++) {
				String hostEntry = hosts[i];
				String[] hostString = hostEntry.split("\\|");//$NON-NLS-1$
				String hostName = hostString[0];
				if (hostString.length == 2) {
					HashMap<String, String> attr = deSerialize(hostString[1]);
					hostSettingsMap.put(hostName, attr);
				}
				else {
					hostSettingsMap.put(hostName, new HashMap<String, String>());
				}
			}
		}

		if (!isWithoutSelection()) {
			String host = getSelectionHost();
			if (host != null) {
				fillSettingsForHost(host);
			}
		}
		else {
			if (hostCombo != null) {
				fillHostCombo();
				String lastHost = settings.get(LAST_HOST_TAG);
				if (lastHost != null) {
					int index = hostCombo.indexOf(lastHost);
					if (index != -1) {
						hostCombo.select(index);
					}
					else {
						hostCombo.select(0);
					}
				}
				else {
					hostCombo.select(0);
				}
				fillSettingsForHost(hostCombo.getText());
			}
		}

		encodingHistory.clear();
		String[] encodings = settings.getArray(ENCODINGS_TAG);
		if (encodings != null && encodings.length > 0) {
			encodingHistory.addAll(Arrays.asList(encodings));
			for (String encoding : encodingHistory) {
				SWTControlUtil.add(encodingCombo, encoding, SWTControlUtil.getItemCount(encodingCombo) - 1);
			}
		}
	}

	/**
	 * Decode the host settings from the given string.
	 *
	 * @param hostString The encoded host settings. Must not be <code>null</code>.
	 * @return The decoded host settings.
	 */
	private HashMap<String, String> deSerialize(String hostString) {
		Assert.isNotNull(hostString);
		HashMap<String, String> attr = new HashMap<String, String>();

		if (hostString.length() != 0) {
			String[] hostAttrs = hostString.split("\\:");//$NON-NLS-1$
			for (int j = 0; j < hostAttrs.length; j = j + 2) {
				String key = hostAttrs[j];
				String value = hostAttrs[j + 1];
				attr.put(key, value);
			}
		}
		return attr;
	}

	/**
	 * Encode the host settings to a string.
	 *
	 * @param hostEntry The host settings. Must not be <code>null</code>.
	 * @param hostString The host string to encode to. Must not be <code>null</code>.
	 */
	private void serialize(Map<String, String> hostEntry, StringBuilder hostString) {
		Assert.isNotNull(hostEntry);
		Assert.isNotNull(hostString);

		if (hostEntry.keySet().size() != 0) {
			Iterator<Entry<String, String>> nextHostAttr = hostEntry.entrySet().iterator();
			while (nextHostAttr.hasNext()) {
				Entry<String, String> entry = nextHostAttr.next();
				String attrKey = entry.getKey();
				String attrValue = entry.getValue();
				hostString.append(attrKey + ":" + attrValue + ":");//$NON-NLS-1$ //$NON-NLS-2$
			}
			hostString.deleteCharAt(hostString.length() - 1);
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.controls.panels.AbstractWizardConfigurationPanel#doSaveWidgetValues(org.eclipse.jface.dialogs.IDialogSettings, java.lang.String)
	 */
	@Override
	public void doSaveWidgetValues(IDialogSettings settings, String idPrefix) {
		Iterator<String> nextHost = hostSettingsMap.keySet().iterator();
		String[] hosts = new String[hostSettingsMap.keySet().size()];
		int i = 0;
		while (nextHost.hasNext()) {
			StringBuilder hostString = new StringBuilder();
			String host = nextHost.next();
			hostString.append(host + "|");//$NON-NLS-1$
			Map<String, String> hostEntry = hostSettingsMap.get(host);
			serialize(hostEntry, hostString);
			hosts[i] = hostString.toString();
			i = i + 1;
		}
		settings.put(HOSTS_TAG, hosts);
		if (isWithoutSelection()) {
			if (hostCombo != null) {
				String host = getHostFromSettings();
				if (host != null) settings.put(LAST_HOST_TAG, host);
			}
		}

		if (!encodingHistory.isEmpty()) {
			settings.put(ENCODINGS_TAG, encodingHistory.toArray(new String[encodingHistory.size()]));
		}
	}

	protected abstract void saveSettingsForHost(boolean add);

	protected abstract void fillSettingsForHost(String host);

	protected abstract String getHostFromSettings();

	protected void removeSecurePassword(String host) {
		// noop by default
	}

	/**
	 * Returns the selected host from the hosts combo widget.
	 *
	 * @return The selected host or <code>null</code>.
	 */
	protected final String getHostFromCombo() {
		return SWTControlUtil.getText(hostCombo);
	}

	protected void removeSettingsForHost(String host) {
		if (hostSettingsMap.containsKey(host)) {
			hostSettingsMap.remove(host);
		}
	}

	/**
	 * Returns the list of host names of the persisted hosts.
	 *
	 * @return The list of host names.
	 */
	private List<String> getHostList() {
		List<String> hostList = new ArrayList<String>();
		hostList.addAll(hostSettingsMap.keySet());
		return hostList;
	}

	/**
	 * Fill the host combo with the stored per host setting names.
	 */
	protected void fillHostCombo() {
		if (hostCombo != null) {
			hostCombo.removeAll();
			List<String> hostList = getHostList();
			Collections.sort(hostList);
			Iterator<String> nextHost = hostList.iterator();
			while (nextHost.hasNext()) {
				String host = nextHost.next();
				hostCombo.add(host);
			}
			if (hostList.size() <= 1) {
				hostCombo.setEnabled(false);
			}
			else {
				hostCombo.setEnabled(true);

			}
			if (deleteHostButton != null) {
				if (hostList.size() == 0) {
					deleteHostButton.setEnabled(false);
				}
				else {
					deleteHostButton.setEnabled(true);
				}
			}
		}
	}

	public boolean isWithoutSelection() {
		ISelection selection = getSelection();
		if (selection == null) {
			return true;
		}
		if (selection instanceof IStructuredSelection && selection.isEmpty()) {
			return true;
		}
		return false;
	}

	public boolean isWithHostList() {
		return true;
	}

	/**
	 * Create the host selection combo.
	 *
	 * @param parent The parent composite. Must not be <code>null</code>.
	 * @param separator If <code>true</code>, a separator will be added after the controls.
	 */
	protected void createHostsUI(Composite parent, boolean separator) {
		Assert.isNotNull(parent);

		if (isWithoutSelection() && isWithHostList()) {
			Composite comboComposite = new Composite(parent, SWT.NONE);
			GridLayout layout = new GridLayout(3, false);
			layout.marginHeight = 0;
			layout.marginWidth = 0;
			comboComposite.setLayout(layout);
			comboComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

			Label label = new Label(comboComposite, SWT.HORIZONTAL);
			label.setText(Messages.AbstractConfigurationPanel_hosts);

			hostCombo = new Combo(comboComposite, SWT.READ_ONLY);
			hostCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
			hostCombo.addSelectionListener(new SelectionListener() {

				@Override
				public void widgetSelected(SelectionEvent e) {
					String host = SWTControlUtil.getText(hostCombo);
					fillSettingsForHost(host);
				}

				@Override
				public void widgetDefaultSelected(SelectionEvent e) {
					widgetSelected(e);
				}
			});

			deleteHostButton = new Button(comboComposite, SWT.NONE);
			// deleteHostButton.setText(Messages.AbstractConfigurationPanel_delete);

			ISharedImages workbenchImages = UIPlugin.getDefault().getWorkbench().getSharedImages();
			deleteHostButton.setImage(workbenchImages.getImageDescriptor(ISharedImages.IMG_TOOL_DELETE).createImage());

			deleteHostButton.setToolTipText(Messages.AbstractConfigurationPanel_deleteButtonTooltip);
			deleteHostButton.addSelectionListener(new SelectionListener() {

				@Override
				public void widgetSelected(SelectionEvent e) {
					String host = getHostFromCombo();
					if (host != null && host.length() != 0) {
						removeSettingsForHost(host);
						removeSecurePassword(host);
						fillHostCombo();
						SWTControlUtil.select(hostCombo, 0);
						host = getHostFromCombo();
						if (host != null && host.length() != 0) {
							fillSettingsForHost(host);
						}
					}
				}

				@Override
				public void widgetDefaultSelected(SelectionEvent e) {
					widgetSelected(e);
				}
			});

			if (separator) {
				Label sep = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL);
				sep.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
			}
		}
	}

	/**
	 * Create the encoding selection combo.
	 *
	 * @param parent The parent composite. Must not be <code>null</code>.
	 * @param separator If <code>true</code>, a separator will be added before the controls.
	 */
	protected void createEncodingUI(final Composite parent, boolean separator) {
		Assert.isNotNull(parent);

		if (separator) {
			Label sep = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL);
			sep.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		}

		Composite panel = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout(2, false);
		layout.marginHeight = 0; layout.marginWidth = 0;
		panel.setLayout(layout);
		panel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

		Label label = new Label(panel, SWT.HORIZONTAL);
		label.setText(Messages.AbstractConfigurationPanel_encoding);

		encodingCombo = new Combo(panel, SWT.READ_ONLY);
		encodingCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		encodingCombo.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (Messages.AbstractConfigurationPanel_encoding_custom.equals(SWTControlUtil.getText(encodingCombo))) {
					InputDialog dialog = new InputDialog(parent.getShell(),
														 Messages.AbstractConfigurationPanel_encoding_custom_title,
														 Messages.AbstractConfigurationPanel_encoding_custom_message,
														 null,
														 new IInputValidator() {
															@Override
															public String isValid(String newText) {
																boolean valid = false;
																try {
																	if (newText != null && !"".equals(newText)) { //$NON-NLS-1$
																		valid = Charset.isSupported(newText);
																	}
																} catch (IllegalCharsetNameException e) { /* ignored on purpose */ }

																if (!valid) {
																	return newText != null && !"".equals(newText) ? Messages.AbstractConfigurationPanel_encoding_custom_error : ""; //$NON-NLS-1$ //$NON-NLS-2$
																}
																return null;
															}
														});
					if (dialog.open() == Window.OK) {
						String encoding = dialog.getValue();
						SWTControlUtil.add(encodingCombo, encoding, SWTControlUtil.getItemCount(encodingCombo) - 1);
						SWTControlUtil.select(encodingCombo, SWTControlUtil.indexOf(encodingCombo, encoding));
						lastSelectedEncoding = SWTControlUtil.getText(encodingCombo);

						// Remember the last 5 custom encodings entered
						if (!encodingHistory.contains(encoding)) {
							if (encodingHistory.size() == 5) encodingHistory.remove(4);
							encodingHistory.add(encoding);
						}

					} else {
						SWTControlUtil.select(encodingCombo, SWTControlUtil.indexOf(encodingCombo, lastSelectedEncoding));
					}
				}
			}
		});

		fillEncodingCombo();
	}

	/**
	 * Fill the encoding combo.
	 */
	protected void fillEncodingCombo() {
		if (encodingCombo != null) {
			List<String> encodings = new ArrayList<String>();

			// Some hard-coded encodings
			encodings.add("Default (ISO-8859-1)"); //$NON-NLS-1$
			encodings.add("UTF-8"); //$NON-NLS-1$

			// The currently selected IDE encoding from the preferences
			String ideEncoding = IDEEncoding.getResourceEncoding();
			if (ideEncoding != null && !encodings.contains(ideEncoding)) encodings.add(ideEncoding);

			// The default Eclipse Workbench encoding (configured in the preferences)
			String eclipseEncoding = WorkbenchEncoding.getWorkbenchDefaultEncoding();
			if (eclipseEncoding != null && !encodings.contains(eclipseEncoding)) encodings.add(eclipseEncoding);

			// The default host (Java VM) encoding
			//
			// Note: We do not use Charset.defaultCharset().displayName() here as it returns the bit
			//       unusual name "windows-1252" on Windows. As there is no access to the "historical"
			//       name "Cp1252" stored in MS1252.class, stick to the older way of retrieving an encoding.
			String hostEncoding = new java.io.InputStreamReader(new java.io.ByteArrayInputStream(new byte[0])).getEncoding();
			if (!encodings.contains(hostEncoding)) encodings.add(hostEncoding);

			// The "Other..." encoding
			encodings.add(Messages.AbstractConfigurationPanel_encoding_custom);

			SWTControlUtil.setItems(encodingCombo, encodings.toArray(new String[encodings.size()]));
			SWTControlUtil.select(encodingCombo, 0);

			lastSelectedEncoding = SWTControlUtil.getText(encodingCombo);
		}
	}

	/**
	 * Select the encoding.
	 *
	 * @param encoding The encoding. Must not be <code>null</code>.
	 */
	protected void setEncoding(String encoding) {
		Assert.isNotNull(encoding);

		int index = SWTControlUtil.indexOf(encodingCombo, "ISO-8859-1".equals(encoding) ? "Default (ISO-8859-1)" : encoding); //$NON-NLS-1$ //$NON-NLS-2$
		if (index != -1) SWTControlUtil.select(encodingCombo, index);
		else SWTControlUtil.setText(encodingCombo, encoding);
	}

	/**
	 * Returns the selected encoding.
	 *
	 * @return The selected encoding or <code>null</code>.
	 */
	protected String getEncoding() {
		String encoding = SWTControlUtil.getText(encodingCombo);
		return encoding != null && encoding.startsWith("Default") ? null : encoding; //$NON-NLS-1$
	}

	/**
	 * Returns if or if not the selected encoding is supported.
	 *
	 * @return <code>True</code> if the selected encoding is supported.
	 */
	protected boolean isEncodingValid() {
		try {
			String encoding = getEncoding();
			return Charset.isSupported(encoding != null ? encoding : "ISO-8859-1"); //$NON-NLS-1$
		} catch (IllegalCharsetNameException e) {
			return false;
		}
	}
}

Back to the top