Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8ff94547ccffb1b7febbd1e11096fdda6e9d067c (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
/*******************************************************************************
 * Copyright (c) 2011 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.controls;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.PlatformObject;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.tcf.te.ui.jface.interfaces.IValidatable;

/**
 * Base implementation of a common UI control.
 * <p>
 * The control can be embedded into any UI container like dialogs,
 * wizard pages or preference pages.
 */
public class BaseControl extends PlatformObject implements IValidatable {

	/**
	 * Reference to the parent control.
	 */
	private Composite parentControl;

	/**
	 * A message associated with the control.
	 */
	private String message = null;

	/**
	 * The message type of the associated message.
	 * @see IMessageProvider
	 */
	private int messageType = IMessageProvider.NONE;

	/**
	 * Flag to remember the controls enabled state
	 */
	private boolean enabled = true;

	/**
	 * Constructor.
	 */
	public BaseControl() {
		super();
	}

	/**
	 * Returns if the <code>setupPanel(...)</code> method has been called at least once with
	 * a non-null parent control.
	 *
	 * @return <code>true</code> if the associated parent control is not <code>null</code>, <code>false</code> otherwise.
	 */
	public final boolean isControlCreated() {
		return (parentControl != null);
	}

	/**
	 * Returns the parent control of the control.
	 *
	 * @return The parent control or <code>null</code>.
	 */
	public final Composite getParentControl() {
		return parentControl;
	}

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

	/**
	 * Creates the controls UI elements.
	 *
	 * @param parent The parent control. Must not be <code>null</code>!
	 */
	public void setupPanel(Composite parent) {
		Assert.isNotNull(parent);
		parentControl = parent;
	}

	/**
	 * Enables or disables all UI elements belonging to this control. The
	 * control remembers the last set enabled state to allow easy enabled checks.
	 *
	 * @param enabled <code>True</code> to enable the UI elements, <code>false</code> otherwise.
	 */
	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	/**
	 * Returns the control is enabled or not.
	 */
	public final boolean isEnabled() {
		return enabled;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.interfaces.validator.IValidatable#isValid()
	 */
	@Override
    public boolean isValid() {
		setMessage(null, IMessageProvider.NONE);
		return true;
	}

	/**
	 * Validates the given subcontrol and bitwise "AND" the subcontrols valid
	 * state to the passed in current valid state. If the subcontrol validation
	 * results into a message which has a higher message type than the currently
	 * set message, the message from the subcontrol is applied to the control itself.
	 * <p>
	 * <b>Note:</b> If the given subcontrol is <code>null</code>, the current validation
	 * state is returned unchanged.
	 *
	 * @param subControl The subcontrol instance or <code>null</code>.
	 * @param currentValidationState The current control validation state before the subcontrol is validated.
	 *
	 * @return The new controls validation state after the subcontrol has been validated.
	 */
	protected final boolean isSubControlValid(BaseControl subControl, boolean currentValidationState) {
		if (subControl == null) return currentValidationState;

		// Validate the subcontrol and bitwise "AND" the result to the current validation state
		currentValidationState &= subControl.isValid();
		// Check if the subcontrol has set a message which has a higher message
		// type than the currently set message.
		if (subControl.getMessageType() > getMessageType()) {
			// Apply the message from the subcontrol to the control
			setMessage(subControl.getMessage(), subControl.getMessageType());
		}

		// Returns the resulting validation state.
		return currentValidationState;
	}

	/* (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 this control wants to display in
	 * the outer control or panel.
	 *
	 * @param message The message from this control.
	 * @param messageType The type o the message (NONE, INFORMATION, WARNING, ERROR).
	 */
	protected final void setMessage(String message, int messageType) {
		// Check if we should apply the default message instead.
		if (message == null && getDefaultMessage() != null) {
			message = getDefaultMessage();
			messageType = getDefaultMessageType();
		}
		// Set the message and message type
		this.message = message;
		this.messageType = messageType;
	}

	/**
	 * Returns the controls default message or <code>null</code> if none.
	 *
	 * @return The controls default message or <code>null</code>.
	 */
	public String getDefaultMessage() {
		return null;
	}

	/**
	 * Returns the controls default message type or {@link IMessageProvider#NONE} if none.
	 *
	 * @return The controls default message type.
	 */
	public int getDefaultMessageType() {
		return IMessageProvider.INFORMATION;
	}

	/**
	 * Returns the correctly prefixed dialog settings slot id. In case the given id
	 * suffix is <code>null</code> or empty, <code>id</code> is returned as is.
	 *
	 * @param settingsSlotId The dialog settings slot id to prefix.
	 * @param prefix The prefix.
	 * @return The correctly prefixed dialog settings slot id.
	 */
	public final String prefixDialogSettingsSlotId(String settingsSlotId, String prefix) {
		if (settingsSlotId != null && prefix != null && prefix.trim().length() > 0) {
			settingsSlotId = prefix + "." + settingsSlotId; //$NON-NLS-1$
		}
		return settingsSlotId;
	}

	/**
	 * Returns the parent section for the control dialog settings. The default implementation
	 * returns the passed in dialog settings instance unmodified. Overwrite to create additional
	 * subsections within the given dialog settings instance.
	 *
	 * @param settings The dialog settings instance. Must not be <code>null</code>.
	 *
	 * @return The parent section for the control dialog settings. Must never be <code>null</code>.
	 */
	protected IDialogSettings doGetParentSection(IDialogSettings settings) {
		Assert.isNotNull(settings);
		return settings;
	}

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

		// Get the parent section for the control dialog settings.
		IDialogSettings parentSection = doGetParentSection(settings);
		Assert.isNotNull(parentSection);

		// Store the settings of the control within it's own section.
		IDialogSettings section = parentSection.getSection(this.getClass().getName());
		if (section == null) {
			section = parentSection.addNewSection(this.getClass().getName());
		}

		// now, call the hook for actually reading the single properties from the dialog settings.
		doRestoreWidgetValues(section, 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);
	}

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

		// Get the parent section for the control dialog settings.
		IDialogSettings parentSection = doGetParentSection(settings);
		Assert.isNotNull(parentSection);

		// Store the settings of the control within it's own section.
		IDialogSettings section = parentSection.getSection(this.getClass().getName());
		if (section == null) {
			section = parentSection.addNewSection(this.getClass().getName());
		}

		// now, call the hook for actually writing the single properties to the dialog settings.
		doSaveWidgetValues(section, 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);
	}
}

Back to the top