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

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.tcf.te.ui.terminals.interfaces.IConfigurationPanel;
import org.eclipse.tcf.te.ui.terminals.interfaces.IConfigurationPanelContainer;
import org.eclipse.tcf.te.ui.terminals.panels.AbstractConfigurationPanel;
import org.eclipse.ui.forms.widgets.FormToolkit;

/**
 * Base control to deal with wizard or property page controls
 * which should share the same UI space.
 */
public class ConfigurationPanelControl implements IConfigurationPanelContainer, IMessageProvider {
	private final Map<String, IConfigurationPanel> configurationPanels = new Hashtable<String, IConfigurationPanel>();

	private String message = null;
	private int messageType = IMessageProvider.NONE;

	private boolean isGroup;

	private FormToolkit toolkit = null;

	private Composite panel;
	private StackLayout panelLayout;

	private String activeConfigurationPanelKey = null;
	private IConfigurationPanel activeConfigurationPanel = null;

	private final AbstractConfigurationPanel EMPTY_PANEL;

	/**
	 * An empty configuration panel implementation.
	 */
	private static final class EmptySettingsPanel extends AbstractConfigurationPanel {

		/**
		 * Constructor.
		 *
		 * @param container The configuration panel container or <code>null</code>.
	     */
	    public EmptySettingsPanel(IConfigurationPanelContainer container) {
		    super(container);
	    }

	    /* (non-Javadoc)
	     * @see org.eclipse.tcf.te.ui.terminals.interfaces.IConfigurationPanel#setupPanel(org.eclipse.swt.widgets.Composite, org.eclipse.ui.forms.widgets.FormToolkit)
	     */
        @Override
	    public void setupPanel(Composite parent, FormToolkit toolkit) {
	    	Composite panel = new Composite(parent, SWT.NONE);
	    	panel.setLayout(new GridLayout());
	    	panel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

	    	panel.setBackground(parent.getBackground());

	    	setControl(panel);
	    }

        /* (non-Javadoc)
         * @see org.eclipse.tcf.te.ui.terminals.panels.AbstractConfigurationPanel#isValid()
         */
	    @Override
	    public boolean isValid() {
	        return false;
	    }
	}

	/**
	 * Cleanup all resources the control might have been created.
	 */
	public void dispose() {
		EMPTY_PANEL.dispose();
	}

	/**
	 * Constructor.
	 */
	public ConfigurationPanelControl() {
		EMPTY_PANEL = new EmptySettingsPanel(this);
		clear();
		setPanelIsGroup(false);
	}

	/**
	 * Sets if or if not the controls panel is a <code>Group</code>.
	 *
	 * @param isGroup <code>True</code> if the controls panel is a group, <code>false</code> otherwise.
	 */
	public void setPanelIsGroup(boolean isGroup) {
		this.isGroup = isGroup;
	}

	/**
	 * Returns if or if not the controls panel is a <code>Group</code>.
	 *
	 * @return <code>True</code> if the controls panel is a group, <code>false</code> otherwise.
	 */
	public boolean isPanelIsGroup() {
		return isGroup;
	}

	/**
	 * Returns the controls panel.
	 *
	 * @return The controls panel or <code>null</code>.
	 */
	public Composite getPanel() {
		return panel;
	}

	/**
	 * Returns the label text to set for the group (if the panel is a group).
	 *
	 * @return The label text to apply or <code>null</code>.
	 */
	public String getGroupLabel() {
		return null;
	}

	/**
	 * Sets the form toolkit to be used for creating the control widgets.
	 *
	 * @param toolkit The form toolkit instance or <code>null</code>.
	 */
	public final void setFormToolkit(FormToolkit toolkit) {
		this.toolkit = toolkit;
	}

	/**
	 * Returns the form toolkit used for creating the control widgets.
	 *
	 * @return The form toolkit instance or <code>null</code>.
	 */
	public final FormToolkit getFormToolkit() {
		return toolkit;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.terminals.interfaces.IConfigurationPanelContainer#validate()
	 */
	@Override
	public void validate() {
	}

	/**
	 * To be called from the embedding control to setup the controls UI elements.
	 *
	 * @param parent The parent control. Must not be <code>null</code>!
	 * @param toolkit The form toolkit. Must not be <code>null</code>.
	 */
	public void setupPanel(Composite parent, String[] configurationPanelKeys, FormToolkit toolkit) {
		Assert.isNotNull(parent);
		Assert.isNotNull(toolkit);

		setFormToolkit(toolkit);

		if (isPanelIsGroup()) {
			panel = new Group(parent, SWT.NONE);
			if (getGroupLabel() != null) ((Group)panel).setText(getGroupLabel());
		} else {
			panel = new Composite(parent, SWT.NONE);
		}
		Assert.isNotNull(panel);
		panel.setFont(parent.getFont());
		panel.setBackground(parent.getBackground());

		panelLayout = new StackLayout();
		panel.setLayout(panelLayout);

		setupConfigurationPanels(panel, configurationPanelKeys, toolkit);
		EMPTY_PANEL.setupPanel(panel, toolkit);
	}

	/**
	 * Removes all configuration panels.
	 */
	public void clear() {
		configurationPanels.clear();
	}

	/**
	 * Returns a unsorted list of all registered configuration panel id's.
	 *
	 * @return A list of registered configuration panel id's.
	 */
	public String[] getConfigurationPanelIds() {
		return configurationPanels.keySet().toArray(new String[configurationPanels.keySet().size()]);
	}

	/**
	 * Returns the configuration panel instance registered for the given configuration panel key.
	 *
	 * @param key The key to get the configuration panel for. Must not be <code>null</code>!
	 * @return The configuration panel instance or an empty configuration panel if the key is unknown.
	 */
	public IConfigurationPanel getConfigurationPanel(String key) {
		IConfigurationPanel panel = key != null ? configurationPanels.get(key) : null;
		return panel != null ? panel : EMPTY_PANEL;
	}

	/**
	 * Returns if or if not the given configuration panel is equal to the
	 * empty configuration panel.
	 *
	 * @param panel The configuration panel or <code>null</code>.
	 * @return <code>True</code> if the configuration panel is equal to the empty configuration panel.
	 */
	public final boolean isEmptyConfigurationPanel(IConfigurationPanel panel) {
		return EMPTY_PANEL == panel;
	}

	/**
	 * Adds the given configuration panel under the given configuration panel key to the
	 * list of known panels. If the given configuration panel is <code>null</code>, any
	 * configuration panel stored under the given key is removed from the list of known panels.
	 *
	 * @param key The key to get the configuration panel for. Must not be <code>null</code>!
	 * @param panel The configuration panel instance or <code>null</code>.
	 */
	public void addConfigurationPanel(String key, IConfigurationPanel panel) {
		if (key == null) return;
		if (panel != null) {
			configurationPanels.put(key, panel);
		} else {
			configurationPanels.remove(key);
		}
	}

	/**
	 * Setup the configuration panels for being presented to the user. This method is called by the
	 * controls <code>doSetupPanel(...)</code> and initialize all possible configuration panels to show.
	 * The default implementation iterates over the given list of configuration panel keys and calls
	 * <code>setupPanel(...)</code> for each of them.
	 *
	 * @param parent The parent composite to use for the configuration panels. Must not be <code>null</code>!
	 * @param configurationPanelKeys The list of configuration panels to initialize. Might be <code>null</code> or empty!
	 * @param toolkit The form toolkit. Must not be <code>null</code>.
	 */
	public void setupConfigurationPanels(Composite parent, String[] configurationPanelKeys, FormToolkit toolkit) {
		Assert.isNotNull(parent);
		Assert.isNotNull(toolkit);

		if (configurationPanelKeys != null) {
			for (int i = 0; i < configurationPanelKeys.length; i++) {
				IConfigurationPanel configPanel = getConfigurationPanel(configurationPanelKeys[i]);
				Assert.isNotNull(configPanel);
				configPanel.setupPanel(parent, toolkit);
			}
		}
	}

	/**
	 * Make the wizard configuration panel registered under the given configuration panel key the
	 * most top configuration panel. If no configuration panel is registered under the given key,
	 * nothing will happen.
	 *
	 * @param key The key to get the wizard configuration panel for. Must not be <code>null</code>!
	 */
	public void showConfigurationPanel(String key) {
		String activeKey = getActiveConfigurationPanelKey();
		if (key != null && key.equals(activeKey) && activeConfigurationPanel != null) {
			return;
		}
		IConfigurationPanel configPanel = getActiveConfigurationPanel();
		Map<String, Object> data = new HashMap<String, Object>();
		if (configPanel != null) configPanel.extractData(data);
		configPanel = getConfigurationPanel(key);
		Assert.isNotNull(configPanel);
		if (configPanel.getControl() != null) {
			activeConfigurationPanel = configPanel;
			activeConfigurationPanelKey = key;
			panelLayout.topControl = configPanel.getControl();
			panel.layout();
			if (!data.isEmpty()) configPanel.updateData(data);
			configPanel.activate();
		}
		else {
			activeConfigurationPanelKey = key;
		}
	}

	/**
	 * Returns the currently active configuration panel.
	 *
	 * @return The active configuration panel or <code>null</code>.
	 */
	public IConfigurationPanel getActiveConfigurationPanel() {
		return activeConfigurationPanel;
	}

	/**
	 * Returns the currently active configuration panel key.
	 *
	 * @return The active configuration panel key or <code>null</code>.
	 */
	public String getActiveConfigurationPanelKey() {
		return activeConfigurationPanelKey;
	}

	/**
	 * Returns the dialog settings to use to save and restore control specific
	 * widget values.
	 *
	 * @param settings The parent dialog settings. Must not be <code>null</code>.
	 * @return The dialog settings to use.
	 */
	public final IDialogSettings getDialogSettings(IDialogSettings settings) {
		Assert.isNotNull(settings);

		// Store the settings of the control within it's own section.
		String sectionName = this.getClass().getSimpleName();
		Assert.isNotNull(sectionName);

		IDialogSettings section = settings.getSection(sectionName);
		if (section == null) {
			section = settings.addNewSection(sectionName);
		}

        return section;
	}

	/**
	 * Restore the widget values from the dialog settings store to recreate the control history.
	 * <p>
	 * <b>Note:</b>
	 * The control is saving the widget values into a section equal to the class name {@link Class#getName()}.
	 * After the sections has been created, the method calls <code>doRestoreWidgetValues</code> for restoring
	 * the single properties from the dialog settings. Subclasses may override <code>doRestoreWidgetValues</code>
	 * only to deal with the single properties only or <code>restoreWidgetValues</code> when to override the
	 * creation of the subsections.
	 *
	 * @param settings The dialog settings object instance to restore the widget values from. Must not be <code>null</code>!
	 * @param idPrefix The prefix to use for every dialog settings slot keys. If <code>null</code>, the dialog settings slot keys are not to prefix.
	 */
	public final void restoreWidgetValues(IDialogSettings settings, String idPrefix) {
		Assert.isNotNull(settings);

		// now, call the hook for actually reading the single properties from the dialog settings.
		doRestoreWidgetValues(getDialogSettings(settings), idPrefix);
	}

	/**
	 * Hook to restore the widget values finally plain from the given dialog settings. This method should
	 * not fragment the given dialog settings any further.
	 *
	 * @param settings The dialog settings to restore the widget values from. Must not be <code>null</code>!
	 * @param idPrefix The prefix to use for every dialog settings slot keys. If <code>null</code>, the dialog settings slot keys are not to prefix.
	 */
	public void doRestoreWidgetValues(IDialogSettings settings, String idPrefix) {
		Assert.isNotNull(settings);

		for (String panelKey : configurationPanels.keySet()) {
			IConfigurationPanel configPanel = getConfigurationPanel(panelKey);
			if (configPanel != null && !isEmptyConfigurationPanel(configPanel)) {
				IDialogSettings configPanelSettings = settings.getSection(panelKey);
				if (configPanelSettings == null) configPanelSettings = settings.addNewSection(panelKey);
				configPanel.doRestoreWidgetValues(configPanelSettings, idPrefix);
			}
		}
	}

	/**
	 * Saves the widget values to the dialog settings store for remembering the history. The control might
	 * be embedded within multiple pages multiple times handling different properties. Because the single
	 * controls should not mix up the history, we create subsections within the given dialog settings if
	 * they do not already exist. After the sections has been created, the method calls <code>doSaveWidgetValues</code>
	 * for saving the single properties to the dialog settings. Subclasses may override <code>doSaveWidgetValues</code>
	 * only to deal with the single properties only or <code>saveWidgetValues</code> when to override the
	 * creation of the subsections.
	 *
	 * @param settings The dialog settings object instance to save the widget values to. Must not be <code>null</code>!
	 * @param idPrefix The prefix to use for every dialog settings slot keys. If <code>null</code>, the dialog settings slot keys are not to prefix.
	 */
	public final void saveWidgetValues(IDialogSettings settings, String idPrefix) {
		Assert.isNotNull(settings);

		// now, call the hook for actually writing the single properties to the dialog settings.
		doSaveWidgetValues(getDialogSettings(settings), idPrefix);
	}

	/**
	 * Hook to save the widget values finally plain to the given dialog settings. This method should
	 * not fragment the given dialog settings any further.
	 *
	 * @param settings The dialog settings to save the widget values to. Must not be <code>null</code>!
	 * @param idPrefix The prefix to use for every dialog settings slot keys. If <code>null</code>, the dialog settings slot keys are not to prefix.
	 */
	public void doSaveWidgetValues(IDialogSettings settings, String idPrefix) {
		Assert.isNotNull(settings);

		IConfigurationPanel configPanel = getActiveConfigurationPanel();
		if (configPanel != null && !isEmptyConfigurationPanel(configPanel)) {
			String key = getActiveConfigurationPanelKey();
			IDialogSettings configPanelSettings = settings.getSection(key);
			if (configPanelSettings == null) configPanelSettings = settings.addNewSection(key);
			configPanel.doSaveWidgetValues(configPanelSettings, idPrefix);
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.IMessageProvider#getMessage()
	 */
	@Override
	public final String getMessage() {
		return message;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.IMessageProvider#getMessageType()
	 */
	@Override
	public final int getMessageType() {
		return messageType;
	}

	/**
	 * Set the message and the message type to display.
	 *
	 * @param message The message or <code>null</code>.
	 * @param messageType The message type or <code>IMessageProvider.NONE</code>.
	 */
	@Override
    public final void setMessage(String message, int messageType) {
		this.message = message;
		this.messageType = messageType;
	}
}

Back to the top