Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 854648e8722c730e904c80eb1f13c1c187ad4059 (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
/*******************************************************************************
 * Copyright (c) 2013, 2015 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.dialogs;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.tcf.te.runtime.interfaces.properties.IPropertiesContainer;
import org.eclipse.tcf.te.runtime.properties.PropertiesContainer;
import org.eclipse.tcf.te.runtime.utils.Host;
import org.eclipse.tcf.te.ui.activator.UIPlugin;
import org.eclipse.tcf.te.ui.forms.CustomFormToolkit;
import org.eclipse.tcf.te.ui.forms.FormLayoutFactory;
import org.eclipse.tcf.te.ui.forms.parts.AbstractSection;
import org.eclipse.tcf.te.ui.interfaces.IDataExchangeDialog;
import org.eclipse.tcf.te.ui.interfaces.data.IDataExchangeNode;
import org.eclipse.tcf.te.ui.interfaces.data.IUpdatable;
import org.eclipse.tcf.te.ui.jface.dialogs.CustomTitleAreaDialog;
import org.eclipse.tcf.te.ui.jface.interfaces.IValidatingContainer;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.ManagedForm;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;

/**
 * Section dialog implementation.
 */
public abstract class AbstractSectionDialog extends CustomTitleAreaDialog implements IValidatingContainer, IDataExchangeDialog {
	// References to the sections
	private AbstractSection[] sections = null;

	private IPropertiesContainer data = null;
	private final boolean readOnly;
	private final String dialogTitle;
	private final String title;
	private final String message;

	/**
	 * Constructor.
	 *
	 * @param shell The active shell.
	 * @param dialogTitle The dialog window title.
	 * @param title The title area title.
	 * @param message The title aerea message.
	 * @param readOnly <code>True</code> to open the dialog in read-only mode, <code>false</code> otherwise.
	 * @param contextHelpId The context help id or <code>null</code>.
	 */
	public AbstractSectionDialog(Shell shell, String dialogTitle, String title, String message, boolean readOnly, String contextHelpId) {
		super(shell, contextHelpId);

		this.dialogTitle = dialogTitle;
		this.title = title;
		this.message = message;
		this.readOnly = readOnly;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.jface.dialogs.CustomTrayDialog#doGetDialogSettingsToInitialize()
	 */
	@Override
	protected IDialogSettings doGetDialogSettingsToInitialize() {
		return UIPlugin.getDefault().getDialogSettings();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.Dialog#getDialogBoundsSettings()
	 */
	@Override
	protected IDialogSettings getDialogBoundsSettings() {
		return super.getDialogSettings();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.Dialog#isResizable()
	 */
	@Override
	protected boolean isResizable() {
		return true;
	}

	/**
	 * Create the sections for this dialog.
	 * @param form The managed form.
	 * @param parent The parent composite.
	 * @return The sections.
	 */
	protected abstract AbstractSection[] createSections(IManagedForm form, Composite parent);

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.jface.dialogs.CustomTitleAreaDialog#createDialogAreaContent(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	protected void createDialogAreaContent(Composite parent) {
		super.createDialogAreaContent(parent);

		setDialogTitle(dialogTitle);
		setTitle(title);
		setMessage(message);

		FormToolkit toolkit = new FormToolkit(getShell().getDisplay());
		ScrolledForm scrolledForm = new CustomFormToolkit(toolkit).createScrolledForm(parent, null, true);
		scrolledForm.setLayoutData(getSectionAreaLayoutData());
		scrolledForm.getBody().setLayout(FormLayoutFactory.createClearGridLayout(false, 1));

		IManagedForm form = new ManagedForm(toolkit, scrolledForm) {
			@Override
			public void dirtyStateChanged() {
				updateSections();
				validate();
			}
			@Override
			public void staleStateChanged() {
				validate();
			}
		};

		sections = createSections(form, scrolledForm.getBody());
		if (sections != null) {
			for (AbstractSection section : sections) {
				section.setReadOnly(readOnly);
				form.addPart(section);
			}
		}

		restoreWidgetValues();
		setupData(data);

		applyDialogFont(scrolledForm.getBody());
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.jface.dialogs.CustomTrayDialog#getDialogSettingsSectionName()
	 */
	@Override
	public String getDialogSettingsSectionName() {
		return getDialogSettingsSection(getClass());
	}

	protected String getDialogSettingsSection(Class<?> clazz) {
		String name = clazz.getSimpleName();
		Class<?> enclosing = clazz.getEnclosingClass();
		while ((name == null || name.trim().length() == 0) && enclosing != null) {
			name = enclosing.getSimpleName();
			if (name != null && name.trim().length() > 0) {
				name = name + "." + AbstractSectionDialog.class.getSimpleName(); //$NON-NLS-1$
			}
			enclosing = enclosing.getEnclosingClass();
		}

		return (name != null && name.trim().length() > 0) ? name : "SectionDialog"; //$NON-NLS-1$
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.TitleAreaDialog#createContents(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	protected Control createContents(Composite parent) {
		Control control = super.createContents(parent);
		validate();
		return control;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.TitleAreaDialog#getInitialSize()
	 */
	@Override
	protected final Point getInitialSize() {
		return adjustInitialSize(super.getInitialSize());
	}

	/**
	 * Adjust the initial size of the dialog.
	 *
	 * @param size The current initial size. Must not be <code>null</code>.
	 * @return The new initial size.
	 */
	protected Point adjustInitialSize(Point size) {
		Assert.isNotNull(size);
    	if (Host.isLinuxHost()) {
    		size = new Point(size.x, size.y+5);
    	}
		return size;
	}

	/* (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) {
		Assert.isNotNull(data);

		this.data = new PropertiesContainer();
		this.data.setProperties(data.getProperties());

		if (sections != null) {
			for (AbstractSection section : sections) {
				if (section instanceof IDataExchangeNode) {
					((IDataExchangeNode)section).setupData(data);
				}
				else if (Platform.inDebugMode()){
					Platform.getLog(UIPlugin.getDefault().getBundle()).log(new Status(IStatus.WARNING,
									UIPlugin.getUniqueIdentifier(),
									"Section "+section.getClass().getName()+" does not implement IDataExchangeNode!")); //$NON-NLS-1$ //$NON-NLS-2$
				}
			}
			updateSections();
		}
	}

	public void updateSections() {
		IPropertiesContainer workingData = new PropertiesContainer();
		workingData.setProperties(data.getProperties());
		if (sections != null) {
			// get working data
			internalExtractData(workingData);
			// update sections
			for (AbstractSection section : sections) {
				if (section instanceof IUpdatable) {
					((IUpdatable)section).updateData(workingData);
				}
			}
		}

	}

	/* (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);
		data.setProperties(this.data.getProperties());
	}

	protected void internalExtractData(IPropertiesContainer data) {
		for (AbstractSection section : sections) {
			if (section instanceof IDataExchangeNode) {
				((IDataExchangeNode)section).extractData(data);
			}
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.jface.dialogs.CustomTitleAreaDialog#dispose()
	 */
	@Override
	protected void dispose() {
	    super.dispose();
		if (sections != null) {
			// get working data
			for (AbstractSection section : sections) {
				section.dispose();
			}
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.jface.dialogs.CustomTrayDialog#okPressed()
	 */
	@Override
	protected void okPressed() {
		// Extract the properties
		if (data == null) {
			data = new PropertiesContainer();
		}
		if (sections != null) {
			for (AbstractSection section : sections) {
				if (section instanceof IDataExchangeNode) {
					((IDataExchangeNode)section).extractData(data);
				}
				else if (Platform.inDebugMode()){
					Platform.getLog(UIPlugin.getDefault().getBundle()).log(new Status(IStatus.WARNING,
									UIPlugin.getUniqueIdentifier(),
									"Section "+section.getClass().getName()+" does not implement IDataExchangeNode!")); //$NON-NLS-1$ //$NON-NLS-2$
				}
			}
		}

		super.okPressed();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.jface.dialogs.CustomTrayDialog#restoreWidgetValues()
	 */
	@Override
	protected void restoreWidgetValues() {
		super.restoreWidgetValues();
		if (sections != null) {
			for (AbstractSection section : sections) {
				section.restoreWidgetValues(getDialogSettings());
			}
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.jface.dialogs.CustomTrayDialog#saveWidgetValues()
	 */
	@Override
	protected void saveWidgetValues() {
		super.saveWidgetValues();
		if (sections != null) {
			for (AbstractSection section : sections) {
				section.saveWidgetValues(getDialogSettings());
			}
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.jface.interfaces.IValidatingContainer#validate()
	 */
	@Override
	public void validate() {
		boolean valid = true;
		if (sections != null) {
			ValidationResult result = new ValidationResult();
			for (AbstractSection section : sections) {
				valid &= section.isValid();
				result.setResult(section);
			}

			valid &= doAdditionalValidation(result);

			setMessage(result.getMessage(), result.getMessageType());
			if (!isMessageSet()) {
				setMessage(message);
			}
		}
		if (getButton(IDialogConstants.OK_ID) != null) {
			getButton(IDialogConstants.OK_ID).setEnabled(!readOnly && valid);
		}
	}

	protected boolean doAdditionalValidation(ValidationResult result) {
		return true;
	}

	protected GridData getSectionAreaLayoutData() {
		return new GridData(SWT.FILL, SWT.CENTER, true, false);
	}
}

Back to the top