Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 596044bd566c3b8df0e6a5ac49d21c97388c5027 (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
/*******************************************************************************
 * Copyright (c) 2011, 2016 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.views.editor.pages;

import org.eclipse.core.runtime.Assert;
import org.eclipse.help.HelpSystem;
import org.eclipse.help.IContext;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.ContributionManager;
import org.eclipse.jface.action.ControlContribution;
import org.eclipse.jface.action.GroupMarker;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.viewers.ILabelDecorator;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.tcf.te.ui.forms.CustomFormToolkit;
import org.eclipse.tcf.te.ui.forms.FormLayoutFactory;
import org.eclipse.tcf.te.ui.views.activator.UIPlugin;
import org.eclipse.tcf.te.ui.views.editor.Editor;
import org.eclipse.tcf.te.ui.views.extensions.LabelProviderDelegateExtensionPointManager;
import org.eclipse.tcf.te.ui.views.interfaces.ImageConsts;
import org.eclipse.tcf.te.ui.views.nls.Messages;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.menus.IMenuService;

/**
 * Abstract details editor page implementation managing
 *                  an custom form toolkit instance.
 */
public abstract class AbstractCustomFormToolkitEditorPage extends AbstractEditorPage {
	// Reference to the form toolkit instance
	private CustomFormToolkit toolkit = null;
	// Reference to the toolbar toolBarManager to release menu contributions for
	private IToolBarManager toolBarManager = null;
	// Reference to the toolbar MenuManager to release menu contributions for
//	private IMenuManager menuManager = null;

	// The default help action class definition
	static protected class HelpAction extends Action {
		/* default */ final String helpID;

		/**
		 * Constructor.
		 *
		 * @param helpID The context help id. Must not be <code>null</code>.
		 */
		public HelpAction(String helpID) {
			super(Messages.AbstractCustomFormToolkitEditorPage_HelpAction_label, IAction.AS_PUSH_BUTTON);
			Assert.isNotNull(helpID);
			this.helpID = helpID;
			setToolTipText(Messages.AbstractCustomFormToolkitEditorPage_HelpAction_tooltip);
			setImageDescriptor(UIPlugin.getImageDescriptor(ImageConsts.HELP));
		}

		/* (non-Javadoc)
		 * @see org.eclipse.jface.action.Action#run()
		 */
		@Override
		public void run() {
			PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
				@Override
				public void run() {
					IContext context = HelpSystem.getContext(helpID);
					if (context != null) {
						PlatformUI.getWorkbench().getHelpSystem().displayHelp(context);
					}
					else {
						PlatformUI.getWorkbench().getHelpSystem().displayHelp();
					}
				}
			});
		}
	}

	/**
	 * Returns the custom form toolkit instance.
	 *
	 * @return The custom form toolkit instance or <code>null</code>.
	 */
	protected final CustomFormToolkit getFormToolkit() {
		return toolkit;
	}



	/**
	 * Sets the custom form toolkit instance.
	 *
	 * @param toolkit The custom form toolkit instance or <code>null</code>.
	 */
	protected final void setFormToolkit(CustomFormToolkit toolkit) {
		this.toolkit = toolkit;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.forms.editor.FormPage#dispose()
	 */
	@Override
	public void dispose() {
		IMenuService service = (IMenuService) getSite().getService(IMenuService.class);
		// Get the menu service and release the toolbar toolBarManager
		if (service != null) {
			if (toolBarManager instanceof ContributionManager) {
				service.releaseContributions((ContributionManager)toolBarManager);
			}
//			if (menuManager instanceof ContributionManager) {
//				service.releaseContributions((ContributionManager)menuManager);
//			}
		}
		// Dispose the custom form toolkit
		if (toolkit != null) { toolkit.dispose(); toolkit = null; }
		// Dispose all the rest
		super.dispose();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.forms.editor.FormPage#createFormContent(org.eclipse.ui.forms.IManagedForm)
	 */
	@Override
	protected void createFormContent(IManagedForm managedForm) {
		super.createFormContent(managedForm);

		Assert.isNotNull(managedForm);

		// Create the toolkit instance
		toolkit = new CustomFormToolkit(managedForm.getToolkit());

		// Configure the managed form
		configureManagedForm(managedForm);

		// Do create the content of the form now
		doCreateFormContent(managedForm.getForm().getBody(), getFormToolkit());

		// Re-arrange the controls
		managedForm.reflow(true);
	}

	/**
	 * Configure the managed form to be ready for usage.
	 *
	 * @param managedForm The managed form. Must not be <code>null</code>.
	 */
	protected void configureManagedForm(IManagedForm managedForm) {
		Assert.isNotNull(managedForm);

		// Configure main layout
		Composite body = managedForm.getForm().getBody();
		body.setLayout(FormLayoutFactory.createFormGridLayout(false, 1));

		// Set context help id
		if (getContextHelpId() != null) {
			PlatformUI.getWorkbench().getHelpSystem().setHelp(managedForm.getForm(), getContextHelpId());
		}

		// Decorate the form header
		getFormToolkit().getFormToolkit().decorateFormHeading(managedForm.getForm().getForm());

		// Set the header text
		String title = getFormTitle();
		if (title != null) setFormTitle(title);

		// Set the header image
		managedForm.getForm().getForm().setImage(getFormImage());

		// Add the toolbar items which will appear in the form header
		toolBarManager = managedForm.getForm().getForm().getToolBarManager();
		// Add the default "additions" separator
		toolBarManager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
		// Create fixed toolbar contribution items
		createToolbarContributionItems(toolBarManager);
		// Get the menu service and populate contributed toolbar actions
		IMenuService service = (IMenuService) getSite().getService(IMenuService.class);
		if (service != null && toolBarManager instanceof ContributionManager) {
			service.populateContributionManager((ContributionManager)toolBarManager, "toolbar:" + getId()); //$NON-NLS-1$
		}

//		// Add the menu items which will appear in the form header
//		menuManager = managedForm.getForm().getForm().getMenuManager();
//		// Get the menu service and populate contributed menu actions
//		if (service != null && menuManager instanceof ContributionManager) {
//			service.populateContributionManager((ContributionManager)menuManager, "menu:" + getId()); //$NON-NLS-1$
//		}
//		// Trigger an update of the menu widget
//		menuManager.update(true);

		// Trigger an update of the toolbar widget
		toolBarManager.update(true);
	}

	/**
	 * Returns the context help id to associate with the page form.
	 *
	 * @return The context help id.
	 */
	protected String getContextHelpId() {
		return null;
	}

	/**
	 * Returns the form title to set to the top form header.
	 *
	 * @return The form title or <code>null</code>.
	 */
	protected String getFormTitle() {
		return null;
	}

	/**
	 * Returns the form title state decoration.
	 *
	 * @return The form title state decoration or <code>null</code>.
	 */
	public String getFormTitleStateDecoration() {
		return null;
	}

	/**
	 * Returns the image to be set to the top form header.
	 *
	 * @return The image or <code>null</code> to use no image.
	 */
	protected Image getFormImage() {
		Image image = null;
		ILabelProvider[] delegates = LabelProviderDelegateExtensionPointManager.getInstance().getDelegates(getEditorInputNode(), false);
		if (delegates != null && delegates.length > 0) {
			image = delegates[0].getImage(getEditorInputNode());
			if (image != null && delegates[0] instanceof ILabelDecorator) {
				image = ((ILabelDecorator)delegates[0]).decorateImage(image, getEditorInputNode());
			}
		}
		return image;
	}

	/**
	 * Update the form header title.
	 *
	 * @param title The title text. Must not be <code>null</code>:
	 */
	public void setFormTitle(String title) {
		Assert.isNotNull(title);

		String fullTitle = title;
		String titleStateDecoration = getFormTitleStateDecoration();
		if (titleStateDecoration != null) fullTitle += " " + titleStateDecoration; //$NON-NLS-1$

		String oldTitle = getManagedForm().getForm().getText();
		if (!fullTitle.equals(oldTitle)) {
			getManagedForm().getForm().setText(fullTitle);
		}
	}

	/**
	 * Update the form header image.
	 *
	 * @param image The image or <code>null</code>.
	 */
	public void setFormImage(Image image) {
		ScrolledForm form = getManagedForm().getForm();
		if (form.getImage() != image) form.setImage(image);
		if (getEditor() instanceof Editor) {
			Editor editor = (Editor)getEditor();
			if (editor.getTitleImage() != image) editor.setTitleImage(image);
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.ui.views.editor.pages.AbstractEditorPage#setActive(boolean)
	 */
//	@Override
//	public void setActive(boolean active) {
//		if (active) setFormTitle(getFormTitle());
//	    super.setActive(active);
//	}

	/**
	 * Create the toolbar contribution items.
	 *
	 * @param toolBarManager The toolbar toolBarManager. Must not be <code>null</code>.
	 */
	protected void createToolbarContributionItems(IToolBarManager manager) {
		Assert.isNotNull(manager);

		manager.add(new Separator("group.connect")); //$NON-NLS-1$
		manager.add(new Separator("group.launch")); //$NON-NLS-1$
		manager.add(new GroupMarker("group.launch.rundebug")); //$NON-NLS-1$
		manager.add(new GroupMarker("group.launch.additions")); //$NON-NLS-1$
		manager.add(new Separator("group.additions")); //$NON-NLS-1$
		manager.add(new Separator("group.additions.control")); //$NON-NLS-1$
		manager.add(new Separator("group.help")); //$NON-NLS-1$
		IContributionItem linkContribution = doCreateLinkContribution(manager);
		if (linkContribution != null) manager.add(linkContribution);
		// If the page is associated with a context help id, add a default
		// help action button into the toolbar
		if (getContextHelpId() != null) {
			Action helpAction = doCreateHelpAction(getContextHelpId());
			if (helpAction != null) manager.add(helpAction);
		}

		final MenuManager mgr = new MenuManager();
		mgr.add(new Separator("group.launch")); //$NON-NLS-1$
		mgr.add(new GroupMarker("group.launch.rundebug")); //$NON-NLS-1$
		mgr.add(new Separator("group.launch.additions")); //$NON-NLS-1$
		mgr.add(new Separator("group.delete")); //$NON-NLS-1$
		mgr.add(new Separator("group.additions")); //$NON-NLS-1$
		final IMenuService service = (IMenuService) getSite().getService(IMenuService.class);
		if (service != null) {
			service.populateContributionManager(mgr, "menu:" + AbstractCustomFormToolkitEditorPage.this.getId()); //$NON-NLS-1$
		}
		if (mgr.getSize() > 5) {
			toolBarManager.add(new ControlContribution("toolbarmenu") { //$NON-NLS-1$
				@Override
				protected Control createControl(Composite parent) {
					final ToolBar tb = new ToolBar(parent, SWT.FLAT);
					final ToolItem item = new ToolItem(tb, SWT.PUSH);
					item.setImage(UIPlugin.getImage(ImageConsts.MENU));
					item.addSelectionListener(new SelectionAdapter() {
						@Override
						public void widgetSelected(SelectionEvent e) {
							MenuManager mgr = new MenuManager();
							mgr.add(new Separator("group.launch")); //$NON-NLS-1$
							mgr.add(new GroupMarker("group.launch.rundebug")); //$NON-NLS-1$
							mgr.add(new Separator("group.launch.additions")); //$NON-NLS-1$
							mgr.add(new Separator("group.delete")); //$NON-NLS-1$
							mgr.add(new Separator("group.additions")); //$NON-NLS-1$
							service.populateContributionManager(mgr, "menu:" + AbstractCustomFormToolkitEditorPage.this.getId()); //$NON-NLS-1$
							Menu menu = mgr.createContextMenu(tb);
							menu.setVisible(true);
						}
					});
					return tb;
				}
			});
		}
	}

	/**
	 * Create the help action.
	 *
	 * @param contextHelpId The context help id. Must not be <code>null</code>.
	 * @return The help action or <code>null</code>.
	 */
	protected Action doCreateHelpAction(String contextHelpId) {
		Assert.isNotNull(contextHelpId);
		return new HelpAction(contextHelpId);
	}

	protected IContributionItem doCreateLinkContribution(IToolBarManager tbManager) {
		return null;
	}

	/**
	 * Do create the managed form content.
	 *
	 * @param parent The parent composite. Must not be <code>null</code>
	 * @param toolkit The {@link CustomFormToolkit} instance. Must not be <code>null</code>.
	 */
	protected abstract void doCreateFormContent(Composite parent, CustomFormToolkit toolkit);
}

Back to the top