Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 50f676331d2bb16cc46634554ec82747404ca868 (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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
/*******************************************************************************
 * Copyright (c) 2000, 2017 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Dan Rubel (dan_rubel@instantiations.com) - accessor to get context menu ids
 *******************************************************************************/
package org.eclipse.ui.internal;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.Platform;
import org.eclipse.e4.core.contexts.ContextFunction;
import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.ui.model.application.MApplication;
import org.eclipse.e4.ui.model.application.ui.MElementContainer;
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
import org.eclipse.e4.ui.model.application.ui.basic.MWindow;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IKeyBindingService;
import org.eclipse.ui.IPageService;
import org.eclipse.ui.IPartService;
import org.eclipse.ui.ISelectionService;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchPartReference;
import org.eclipse.ui.IWorkbenchPartSite;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.SubActionBars;
import org.eclipse.ui.contexts.IContextService;
import org.eclipse.ui.handlers.IHandlerService;
import org.eclipse.ui.internal.contexts.SlaveContextService;
import org.eclipse.ui.internal.expressions.ActivePartExpression;
import org.eclipse.ui.internal.handlers.LegacyHandlerService;
import org.eclipse.ui.internal.menus.SlaveMenuService;
import org.eclipse.ui.internal.progress.WorkbenchSiteProgressService;
import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
import org.eclipse.ui.internal.services.IServiceLocatorCreator;
import org.eclipse.ui.internal.services.IWorkbenchLocationService;
import org.eclipse.ui.internal.services.ServiceLocator;
import org.eclipse.ui.internal.services.WorkbenchLocationService;
import org.eclipse.ui.internal.testing.WorkbenchPartTestable;
import org.eclipse.ui.menus.IMenuService;
import org.eclipse.ui.progress.IProgressService;
import org.eclipse.ui.progress.IWorkbenchSiteProgressService;
import org.eclipse.ui.services.IServiceScopes;
import org.eclipse.ui.testing.IWorkbenchPartTestable;

/**
 * <code>PartSite</code> is the general implementation for an
 * <code>IWorkbenchPartSite</code>. A site maintains the context for a part,
 * including the part, its pane, active contributions, selection provider, etc.
 * Together, these components make up the complete behavior for a part as if it
 * was implemented by one person.
 *
 * The <code>PartSite</code> lifecycle is as follows ..
 *
 * <ol>
 * <li>a site is constructed </li>
 * <li>a part is constructed and stored in the part </li>
 * <li>the site calls part.init() </li>
 * <li>a pane is constructed and stored in the site </li>
 * <li>the action bars for a part are constructed and stored in the site </li>
 * <li>the pane is added to a presentation </li>
 * <li>the SWT widgets for the pane and part are created </li>
 * <li>the site is activated, causing the actions to become visible </li>
 * </ol>
 */
public abstract class PartSite implements IWorkbenchPartSite {

	/**
	 * This is a helper method for the register context menu functionality. It
	 * is provided so that different implementations of the
	 * <code>IWorkbenchPartSite</code> interface don't have to worry about how
	 * context menus should work.
	 *
	 * @param menuId
	 *            the menu id
	 * @param menuManager
	 *            the menu manager
	 * @param selectionProvider
	 *            the selection provider
	 * @param includeEditorInput
	 *            whether editor inputs should be included in the structured
	 *            selection when calculating contributions
	 * @param part
	 *            the part for this site
	 * @param menuExtenders
	 *            the collection of menu extenders for this site
	 * @see IWorkbenchPartSite#registerContextMenu(MenuManager,
	 *      ISelectionProvider)
	 */
	public static final void registerContextMenu(final String menuId,
			final MenuManager menuManager, final ISelectionProvider selectionProvider,
			final boolean includeEditorInput, final IWorkbenchPart part, IEclipseContext context,
			final Collection menuExtenders) {
		/*
		 * Check to see if the same menu manager and selection provider have
		 * already been used. If they have, then we can just add another menu
		 * identifier to the existing PopupMenuExtender.
		 */
		final Iterator extenderItr = menuExtenders.iterator();
		boolean foundMatch = false;
		while (extenderItr.hasNext()) {
			final PopupMenuExtender existingExtender = (PopupMenuExtender) extenderItr
					.next();
			if (existingExtender.matches(menuManager, selectionProvider, part)) {
				existingExtender.addMenuId(menuId);
				foundMatch = true;
				break;
			}
		}

		if (!foundMatch) {
			menuExtenders.add(new PopupMenuExtender(menuId, menuManager, selectionProvider, part,
					context, includeEditorInput));
		}
	}

	private IWorkbenchPartReference partReference;

	private IWorkbenchPart part;

	private ISelectionProvider selectionProvider;

	private SubActionBars actionBars;

	private KeyBindingService keyBindingService;

	private SlavePageService pageService;

	private SlavePartService partService;

	private SlaveSelectionService selectionService;

	private SlaveContextService contextService;

	private SlaveMenuService menuService;

	protected ArrayList menuExtenders;

	private WorkbenchSiteProgressService progressService;

	protected ServiceLocator serviceLocator;

	protected MPart model;

	private IConfigurationElement element;

	private IEclipseContext e4Context;

	private IWorkbenchWindow workbenchWindow;

	private String extensionId;

	/**
	 * Build the part site.
	 *
	 * @param ref
	 *            the part reference
	 * @param part
	 *            the part
	 * @param page
	 *            the page it belongs to
	 */
	public PartSite(MPart model, IWorkbenchPart part, IWorkbenchPartReference ref,
			IConfigurationElement element) {
		this.model = model;
		this.part = part;
		this.partReference = ref;
		this.element = element;

		MElementContainer<?> parent = (MElementContainer<?>) ((EObject) model).eContainer();
		while (!(parent instanceof MWindow)) {
			parent = (MElementContainer<?>) ((EObject) parent).eContainer(); // parent.getParent();
		}

		setWindow((MWindow) parent);

		e4Context = model.getContext();
		IServiceLocatorCreator slc = e4Context
				.get(IServiceLocatorCreator.class);
		IWorkbenchWindow workbenchWindow = getWorkbenchWindow();
		this.serviceLocator = (ServiceLocator) slc.createServiceLocator(workbenchWindow, null,
				() -> {
					// not sure what to do here
				}, e4Context);
		initializeDefaultServices();
	}

	void setExtensionId(String extensionId) {
		this.extensionId = extensionId;
	}

	private void setWindow(MWindow window) {
		MWindow topWindow = getTopLevelModelWindow(window);
		MApplication application = topWindow.getContext().get(MApplication.class);
		Workbench workbench = (Workbench) application.getContext().get(IWorkbench.class);

		workbenchWindow = workbench.createWorkbenchWindow(
				workbench.getDefaultPageInput(),
				workbench.getPerspectiveRegistry().findPerspectiveWithId(
						workbench.getPerspectiveRegistry().getDefaultPerspective()), topWindow,
				false);
	}

	/**
	 * Initialize the local services.
	 */
	private void initializeDefaultServices() {
		IHandlerService handlerService = new LegacyHandlerService(e4Context,
				new ActivePartExpression(part));
		e4Context.set(IHandlerService.class, handlerService);

		serviceLocator.registerService(IWorkbenchLocationService.class,
				new WorkbenchLocationService(IServiceScopes.PARTSITE_SCOPE,
						getWorkbenchWindow().getWorkbench(),
						getWorkbenchWindow(), this, null, null, 2));
		// added back for legacy reasons
		serviceLocator.registerService(IWorkbenchPartSite.class, this);
		serviceLocator.registerService(IWorkbenchPart.class, getPart());

		e4Context.set(IWorkbenchSiteProgressService.class.getName(), new ContextFunction() {
			@Override
			public Object compute(IEclipseContext context, String contextKey) {
				if (progressService == null) {
					progressService = new WorkbenchSiteProgressService(PartSite.this);
				}
				return progressService;
			}
		});
		e4Context.set(IProgressService.class.getName(), new ContextFunction() {
			@Override
			public Object compute(IEclipseContext context, String contextKey) {
				if (progressService == null) {
					progressService = new WorkbenchSiteProgressService(PartSite.this);
				}
				return progressService;
			}
		});
		e4Context.set(IKeyBindingService.class.getName(), new ContextFunction() {
			@Override
			public Object compute(IEclipseContext context, String contextKey) {
				if (keyBindingService == null) {
					keyBindingService = new KeyBindingService(PartSite.this);
				}

				return keyBindingService;
			}
		});
		e4Context.set(IPageService.class.getName(), new ContextFunction() {
			@Override
			public Object compute(IEclipseContext context, String contextKey) {
				if (pageService == null) {
					pageService = new SlavePageService(context.getParent().get(IPageService.class));
				}

				return pageService;
			}
		});
		e4Context.set(IPartService.class.getName(), new ContextFunction() {
			@Override
			public Object compute(IEclipseContext context, String contextKey) {
				if (partService == null) {
					partService = new SlavePartService(context.getParent().get(IPartService.class));
				}
				return partService;
			}
		});
		e4Context.set(ISelectionService.class.getName(), new ContextFunction() {
			@Override
			public Object compute(IEclipseContext context, String contextKey) {
				if (selectionService == null) {
					selectionService = new SlaveSelectionService(context.getParent().get(
							ISelectionService.class));
				}
				return selectionService;
			}
		});
		e4Context.set(IContextService.class.getName(), new ContextFunction() {
			@Override
			public Object compute(IEclipseContext context, String contextKey) {
				if (contextService == null) {
					contextService = new SlaveContextService(context.getParent().get(
							IContextService.class), new ActivePartExpression(part));
				}
				return contextService;
			}
		});
		e4Context.set(IMenuService.class.getName(), new ContextFunction() {
			@Override
			public Object compute(IEclipseContext context, String contextKey) {
				if (menuService == null) {
					menuService = new SlaveMenuService(context.getParent().get(IMenuService.class),
							model);
				}
				return menuService;
			}
		});
	}

	/**
	 * Dispose the contributions.
	 */
	public void dispose() {
		if (menuExtenders != null) {
			HashSet managers = new HashSet(menuExtenders.size());
			for (int i = 0; i < menuExtenders.size(); i++) {
				PopupMenuExtender ext = (PopupMenuExtender) menuExtenders.get(i);
				managers.add(ext.getManager());
				ext.dispose();
			}
			if (managers.size()>0) {
				for (Iterator iterator = managers.iterator(); iterator
						.hasNext();) {
					MenuManager mgr = (MenuManager) iterator.next();
					mgr.dispose();
				}
			}
			menuExtenders = null;
		}

		 if (keyBindingService != null) {
			keyBindingService.dispose();
			keyBindingService = null;
		 }

		if (progressService != null) {
			progressService.dispose();
			progressService = null;
		}

		if (pageService != null) {
			pageService.dispose();
			pageService = null;
		}

		if (partService != null) {
			partService.dispose();
			partService = null;
		}

		if (selectionService != null) {
			selectionService.dispose();
			selectionService = null;
		}

		if (contextService != null) {
			contextService.dispose();
			contextService = null;
		}

		if (serviceLocator != null) {
			serviceLocator.dispose();
			serviceLocator = null;
		}
		if (menuService != null) {
			menuService.dispose();
			menuService = null;
		}
		part = null;
		workbenchWindow = null;
		e4Context = null;
	}

	/**
	 * Returns the action bars for the part. If this part is a view then it has
	 * exclusive use of the action bars. If this part is an editor then the
	 * action bars are shared among this editor and other editors of the same
	 * type.
	 */
	public IActionBars getActionBars() {
		return actionBars;
	}

	@Override
	public String getId() {
		return extensionId == null ? element == null ? model.getElementId() : element
				.getAttribute(IWorkbenchRegistryConstants.ATT_ID)
				: extensionId;
	}

	@Override
	public String getPluginId() {
		return element == null ? model.getElementId() : element.getNamespaceIdentifier();
	}

	@Override
	public String getRegisteredName() {
		return element == null ? model.getLocalizedLabel() : element
				.getAttribute(IWorkbenchRegistryConstants.ATT_NAME);
	}

	/**
	 * Returns the page containing this workbench site's part.
	 *
	 * @return the page containing this part
	 */
	@Override
	public IWorkbenchPage getPage() {
		IWorkbenchWindow window = getWorkbenchWindow();
		return window == null ? null : window.getActivePage();
	}


	/**
	 * Returns the part.
	 */
	@Override
	public IWorkbenchPart getPart() {
		return part;
	}

	/**
	 * Returns the part reference.
	 */
	public IWorkbenchPartReference getPartReference() {
		return partReference;
	}

	/**
	 * Returns the selection provider for a part.
	 */
	@Override
	public ISelectionProvider getSelectionProvider() {
		return selectionProvider;
	}

	/**
	 * Returns the shell containing this part.
	 *
	 * @return the shell containing this part
	 */
	@Override
	public Shell getShell() {

		// Compatibility: This method should not be used outside the UI
		// thread... but since this condition
		// was not always in the JavaDoc, we still try to return our best guess
		// about the shell if it is
		// called from the wrong thread.
		Display currentDisplay = Display.getCurrent();
		if (currentDisplay == null) {
			// Uncomment this to locate places that try to access the shell from
			// a background thread
			// WorkbenchPlugin.log(new Exception("Error:
			// IWorkbenchSite.getShell() was called outside the UI thread. Fix
			// this code.")); //$NON-NLS-1$

			return getWorkbenchWindow().getShell();
		}

		Control control = (Control) model.getWidget();
		if (control != null && !control.isDisposed()) {
			return control.getShell();
		}

		// If someone works with disposed parts, we can't help, it is a bug
		if (e4Context == null) {
			WorkbenchPlugin.log(new IllegalStateException(
					"IWorkbenchSite.getShell() was called after part disposal: " + toString())); //$NON-NLS-1$
			return PlatformUI.getWorkbench().getDisplay().getActiveShell();
		}

		// likely means the part has been destroyed, return the parent window's
		// shell, we don't just arbitrarily return the workbench window's shell
		// because we may be in a detached window
		MWindow window = e4Context.get(MWindow.class);
		return window == null ? getWorkbenchWindow().getShell() : (Shell) window.getWidget();
	}

	private MWindow getTopLevelModelWindow(MWindow window) {
		EObject previousParent = (EObject) window;
		EObject parent = previousParent.eContainer();
		// we can't simply stop at an MWindow because the part may be in a detached window
		while (!(parent instanceof MApplication)) {
			previousParent = parent;
			parent = parent.eContainer();
		}

		return (MWindow) previousParent;
	}

	/**
	 * Returns the workbench window containing this part.
	 *
	 * @return the workbench window containing this part
	 */
	@Override
	public IWorkbenchWindow getWorkbenchWindow() {
		return workbenchWindow;
	}

	/**
	 * Register a popup menu for extension.
	 */
	@Override
	public void registerContextMenu(String menuID, MenuManager menuMgr,
			ISelectionProvider selProvider) {
		if (menuExtenders == null) {
			menuExtenders = new ArrayList(1);
		}

		registerContextMenu(menuID, menuMgr, selProvider, true, getPart(), e4Context, menuExtenders);
	}

	/**
	 * Register a popup menu with the default id for extension.
	 */
	@Override
	public void registerContextMenu(MenuManager menuMgr,
			ISelectionProvider selProvider) {
		registerContextMenu(getId(), menuMgr, selProvider);
	}

	// getContextMenuIds() added by Dan Rubel (dan_rubel@instantiations.com)
	/**
	 * Get the registered popup menu identifiers
	 */
	public String[] getContextMenuIds() {
		if (menuExtenders == null) {
			return new String[0];
		}
		ArrayList menuIds = new ArrayList(menuExtenders.size());
		for (Iterator iter = menuExtenders.iterator(); iter.hasNext();) {
			final PopupMenuExtender extender = (PopupMenuExtender) iter.next();
			menuIds.addAll(extender.getMenuIds());
		}
		return (String[]) menuIds.toArray(new String[menuIds.size()]);
	}

	/**
	 * Sets the action bars for the part.
	 */
	public void setActionBars(SubActionBars bars) {
		actionBars = bars;
	}


	/**
	 * Sets the part.
	 */
	public void setPart(IWorkbenchPart newPart) {
		part = newPart;
	}


	/**
	 * Set the selection provider for a part.
	 */
	@Override
	public void setSelectionProvider(ISelectionProvider provider) {
		selectionProvider = provider;
	}

	/*
	 * @see IWorkbenchPartSite#getKeyBindingService()
	 */
	@Override
	public IKeyBindingService getKeyBindingService() {
		return e4Context.get(IKeyBindingService.class);
	}

	protected String getInitialScopeId() {
		return null;
	}

	/**
	 * Get an adapter for this type.
	 *
	 * @param adapter
	 * @return
	 */
	@Override
	public final <T> T getAdapter(Class<T> adapter) {

		if (IWorkbenchSiteProgressService.class == adapter) {
			return adapter.cast(getService(adapter));
		}

		if (IWorkbenchPartTestable.class == adapter) {
			return adapter.cast(new WorkbenchPartTestable(this));
		}

		return Platform.getAdapterManager().getAdapter(this, adapter);
	}

	public void activateActionBars(boolean forceVisibility) {
		if (serviceLocator != null) {
			serviceLocator.activate();
		}

		if (actionBars != null) {
			actionBars.activate(forceVisibility);
		}
	}

	public void deactivateActionBars(boolean forceHide) {
		if (actionBars != null) {
			actionBars.deactivate(forceHide);
		}
		if (serviceLocator != null) {
			serviceLocator.deactivate();
		}
	}

	/**
	 * Get a progress service for the receiver.
	 *
	 * @return WorkbenchSiteProgressService
	 */
	IWorkbenchSiteProgressService getSiteProgressService() {
		return e4Context.get(IWorkbenchSiteProgressService.class);
	}

	@Override
	public final <T> T getService(final Class<T> key) {
		return serviceLocator == null ? null : serviceLocator.getService(key);
	}

	@Override
	public final boolean hasService(final Class<?> key) {
		return serviceLocator == null ? false : serviceLocator.hasService(key);
	}

	/**
	 * Prints out the identifier, the plug-in identifier and the registered
	 * name. This is for debugging purposes only.
	 *
	 * @since 3.2
	 */
	@Override
	public String toString() {
		final StringBuilder buffer = new StringBuilder();
		buffer.append("PartSite(id="); //$NON-NLS-1$
		buffer.append(getId());
		buffer.append(",pluginId="); //$NON-NLS-1$
		buffer.append(getPluginId());
		buffer.append(",registeredName="); //$NON-NLS-1$
		buffer.append(getRegisteredName());
		buffer.append(",hashCode="); //$NON-NLS-1$
		buffer.append(hashCode());
		buffer.append(')');
		return buffer.toString();
	}

	public MPart getModel() {
		return model;
	}

	public IEclipseContext getContext() {
		return e4Context;
	}
}

Back to the top