Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 49f5963a991f9305ba2796291b93304fbd685c44 (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
/*******************************************************************************
 * 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.forms.parts;

import java.lang.reflect.Field;
import java.security.AccessController;
import java.security.PrivilegedAction;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.tcf.te.ui.forms.FormLayoutFactory;
import org.eclipse.tcf.te.ui.jface.interfaces.IValidatable;
import org.eclipse.tcf.te.ui.swt.SWTControlUtil;
import org.eclipse.ui.forms.AbstractFormPart;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.SectionPart;
import org.eclipse.ui.forms.widgets.ExpandableComposite;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;

/**
 * Abstract section implementation.
 */
public abstract class AbstractSection extends SectionPart implements IAdaptable, IValidatable {
	// The message text
	private String message = null;
	// The message type. See IMessageProvider
	private int messageType = NONE;

	// Flag to mark if the controls of the sections are currently updated.
	// While the control are updated, validation and similar logic should
	// not be processed.
	// <p>
	// <b>Note:</b> This flag default to <code>true</code> on instantiation.
	private boolean updating = true;

	/**
	 * Constructor.
	 *
	 * @param form The parent managed form. Must not be <code>null</code>.
	 * @param parent The parent composite. Must not be <code>null</code>.
	 * @param style The section style.
	 */
	public AbstractSection(IManagedForm form, Composite parent, int style) {
		this(form, parent, style, true);
	}

	/**
	 * Constructor.
	 *
	 * @param form The parent managed form. Must not be <code>null</code>.
	 * @param parent The parent composite. Must not be <code>null</code>.
	 * @param style The section style.
	 * @param titleBar If <code>true</code>, the title bar style bit is added to <code>style</code>.
	 */
	public AbstractSection(IManagedForm form, Composite parent, int style, boolean titleBar) {
		super(parent, form.getToolkit(), titleBar ? (ExpandableComposite.TITLE_BAR | style) : style);
		initialize(form);
		configureSection(getSection());
    }

	/**
	 * Configure the section.
	 *
	 * @param section The section. Must not be <code>null</code>.
	 */
	protected void configureSection(Section section) {
		Assert.isNotNull(section);

		section.clientVerticalSpacing = FormLayoutFactory.SECTION_HEADER_VERTICAL_SPACING;
		section.setData("part", this); //$NON-NLS-1$

		// Adjust the background
		Color bg = section.getParent().getBackground();
		if (bg != null && !bg.equals(section.getBackground())) {
			section.setBackground(bg);
		}
	}

	/**
	 * Creates the section client.
	 *
	 * @param section The parent section. Must not be <code>null</code>.
	 * @param toolkit The form toolkit. Must not be <code>null</code>.
	 */
	protected abstract void createClient(Section section, FormToolkit toolkit);

	/**
	 * Creates the client container composite.
	 *
	 * @param parent The parent composite. Must not be <code>null</code>.
	 * @param numColumns The number of columns.
	 * @param toolkit The form toolkit or <code>null</code>.
	 *
	 * @return The client container composite.
	 */
	protected Composite createClientContainer(Composite parent, int numColumns, FormToolkit toolkit) {
		Composite container = toolkit != null ? toolkit.createComposite(parent) : new Composite(parent, SWT.NONE);
		container.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, numColumns));

		// Adjust the background
		Color bg = parent.getBackground();
		if (bg != null && !bg.equals(container.getBackground())) {
			container.setBackground(bg);
		}

		return container;
	}

	/**
	 * Convenience method to create a "invisible" label for creating an
	 * empty space between controls.
	 *
	 * @param parent The parent composite. Must not be <code>null</code>.
	 * @param span The horizontal span.
	 * @param toolkit The form toolkit or <code>null</code>.
	 *
	 * @return
	 */
	protected Label createEmptySpace(Composite parent, int span, FormToolkit toolkit) {
		Assert.isNotNull(parent);

		Label emptySpace = toolkit != null ? toolkit.createLabel(parent, null) : new Label(parent, SWT.NONE);

		GridData layoutData = new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false);
		layoutData.horizontalSpan = span;
		layoutData.widthHint = 0; layoutData.heightHint = SWTControlUtil.convertHeightInCharsToPixels(emptySpace, 1) / 2;

		emptySpace.setLayoutData(layoutData);

		return emptySpace;
	}

	/**
	 * Convenience method to create a section toolbar.
	 *
	 * @param section The section. Must not be <code>null</code>.
	 * @param toolkit The form toolkit or <code>null</code>.
	 */
	protected void createSectionToolbar(Section section, FormToolkit toolkit) {
		Assert.isNotNull(section);

		// Create the toolbar manager and the toolbar control
		ToolBarManager tlbMgr = new ToolBarManager(SWT.FLAT);
		ToolBar tlb = tlbMgr.createControl(section);

		// If the user moves over the toolbar area, change the cursor to become a hand
		final Cursor cursor = new Cursor(Display.getCurrent(), SWT.CURSOR_HAND);
		tlb.setCursor(cursor);

		// Cursor needs to be explicitly disposed
		tlb.addDisposeListener(new DisposeListener() {
			@Override
            public void widgetDisposed(DisposeEvent e) {
				if (cursor.isDisposed() == false) {
					cursor.dispose();
				}
			}
		});

		// Create the toolbar items
		createSectionToolbarItems(section, toolkit, tlbMgr);

		// Update the toolbar manager
		tlbMgr.update(true);
		// Associate the toolbar control with the section
		section.setTextClient(tlb);
	}

	/**
	 * Convenience method to create section toolbar items.
	 * <p>
	 * This method is called from {@link #createSectionToolbar(Section, FormToolkit)}.
	 *
	 * @param section The section. Must not be <code>null</code>.
	 * @param toolkit The form toolkit or <code>null</code>.
	 * @param tlbMgr The toolbar manager. Must not be <code>null</code>.
	 */
	protected void createSectionToolbarItems(Section section, FormToolkit toolkit, ToolBarManager tlbMgr) {
		Assert.isNotNull(section);
		Assert.isNotNull(tlbMgr);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
	 */
	@Override
	public Object getAdapter(Class adapter) {
		return Platform.getAdapterManager().getAdapter(this, adapter);
	}

	/**
	 * Marks the section dirty or reset the dirty state.
	 *
	 * @param dirty <code>True</code> to mark the section dirty, <code>false</code> otherwise.
	 */
	public final void markDirty(boolean dirty) {
		if (dirty) markDirty();
		else {
			// For now, there is no direct way to reset the dirty state,
			// and the refresh() method is setting back both flags (stale and dirty).
			// Plus, refresh() might be overwritten to refresh the widget content
			// from the data itself, what will trigger an stack overflow after all.
			try {
				final Field f = AbstractFormPart.class.getDeclaredField("dirty"); //$NON-NLS-1$
				AccessController.doPrivileged(new PrivilegedAction<Object>() {
					@Override
					public Object run() {
						f.setAccessible(true);
						return null;
					}
				});
				f.setBoolean(this, dirty);
				if (getManagedForm() != null) getManagedForm().dirtyStateChanged();
			} catch (Exception e) { /* ignored on purpose */ }
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.forms.AbstractFormPart#commit(boolean)
	 */
	@Override
	public void commit(boolean onSave) {
		// commit is reseting the dirty state
		boolean hasBeenDirty = isDirty();
	    super.commit(onSave);
	    if (hasBeenDirty) getManagedForm().dirtyStateChanged();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.forms.AbstractFormPart#refresh()
	 */
	@Override
	public void refresh() {
		// refresh is reseting both the stale and the dirty state
		boolean hasBeenStale = isStale();
		boolean hasBeenDirty = isDirty();
	    super.refresh();
	    if (hasBeenStale) getManagedForm().staleStateChanged();
	    if (hasBeenDirty) getManagedForm().dirtyStateChanged();
	}

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

	/**
	 * Sets the message text and type.
	 *
	 * @param message The message or <code>null</code>.
	 * @param messageType The message type. See {@link IMessageProvider}.
	 */
	protected final void setMessage(String message, int messageType) {
		this.message = message;
		this.messageType = messageType;
	}

	/* (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;
	}

	/**
	 * Marks if or if not the controls of the section are currently updated.
	 */
	protected final void setIsUpdating(boolean updating) {
		this.updating = updating;
	}

	/**
	 * Returns if or if not the controls of the section are currently updated.
	 *
	 * @return <code>True</code> if the controls are currently updated, <code>false</code> otherwise.
	 */
	protected final boolean isUpdating() {
		return updating;
	}
}

Back to the top