Java Code Examples for org.eclipse.e4.ui.model.application.ui.basic.MPart#getParent()

The following examples show how to use org.eclipse.e4.ui.model.application.ui.basic.MPart#getParent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: E4WindowCmd.java    From e4macs with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * The parent of the apart is typically a PartStack, but it could be something under an MCompositePart, so look up
 * @param apart the original selection
 * @return the MPart and PartStack
 */
protected PartAndStack getParentStack(MPart apart) {
	MPartSashContainerElement part = apart;
	MElementContainer<MUIElement> stack = apart.getParent();
	try {
		while (!((MPartSashContainerElement)stack instanceof MPartStack)) {
			if ((MPartSashContainerElement)stack instanceof MArea) {
				// some unexpected structure
				stack = null;
				part = apart;
				break;
			} else {
				part = (MPartSashContainerElement)stack;
				stack = stack.getParent();
			}
		}
	} catch (Exception e) {
		// Bail on anything unexpected - will just make the command a noop
		stack = null;
		part = apart;
	}
	return new PartAndStack((MPart)part, stack);
}
 
Example 2
Source File: UIHelper.java    From tlaplus with MIT License 4 votes vote down vote up
/**
 * Parts (Editors and Views) can be stacked if the user drags them on top of
 * each other. This method returns true, if the given part (first parameter)
 * and the second part (identified by its Id) are indeed stacked.
 * 
 * @param part
 *            The one part (not null) for which to check if the second part
 *            (otherId) are on the same part stack.
 * @param otherId
 *            The second part's id (not null).
 * @return true iff both parts are stacked on top of each other, false
 *         otherwise.
 */
public static boolean isInSameStack(final IWorkbenchPart part, final String otherId) {
	Assert.isNotNull(part);
	Assert.isNotNull(otherId);
	
	// The implementation below uses Eclipse's new UI model (e4) that is the
	// default starting with the 4.x series. It does *not* exist on any 3.x
	// release of Eclipse. Thus, if the Toolbox ever has to run on 3.x
	// again, just remove the implementation below and return false. The
	// only place where this method is used at the time of writing is to
	// prevent a stack overflow when the ModelEditor and TLCErrorView are
	// stacked. This isn't possible with 3.x anyway as Views (TLCErrorView)
	// and Editors (ModelEditor) could not be stacked upon each other (this
	// was actually an artificial restriction and missed by many). That is
	// also the very reason, why the Eclipse 3.x API does not support
	// finding out if an editor and a view are stacked
	// (IWorkbenchPage#getViewRefererences() and
	// IWorkbenchPage#getEditorReferences() are mutually exclusive).

	// Obtain the e4's part context IEclipseContext (each logical UI element
	// (parts, perspectives, application windows... have a corresponding
	// context).
	final IEclipseContext ctx = (IEclipseContext) part.getSite().getService(IEclipseContext.class);
	if (ctx == null) {
		// I don't see in which case the ctx is null, but lets be on the safe side.
		return false;
	}
	// The ctx also has the e4 UI model counterpart (MPart) to the technical
	// IWorkbenchPart instance (the UI model is a logical representation of
	// the UI state of the Toolbox).
	final MPart e4ModelPart = ctx.get(MPart.class);
	// e4 Model elements have references to their respective parents. In
	// case of our MPart/IWorkbenchPart, it is a stack if it happens to be
	// rendered in one at the moment.
	final MElementContainer<MUIElement> stack = e4ModelPart.getParent();
	if (stack == null) {
		// The part has no parent, e.g when it's not stacked
		return false;
	}
	// A MPartStack's children are the MParts currently rendered in it
	// (visible or not). Since it is the MPartStack of the IWorkbenchPart,
	// we just check if another child has the Id of the other part we are
	// interested in. If this is the case, both parts are indeed shown in
	// the same part stack on top of each other.
	final List<MUIElement> children = stack.getChildren();
	for (final MUIElement muiElement : children) {
		final String elementId = muiElement.getElementId();
		if (elementId != null && elementId.equals(otherId)) {
			return true;
		}
	}
	return false;
}