use of org.eclipse.e4.ui.di.Focus in project eclipse.platform.ui by eclipse-platform.
the class EPartServiceTest method testSwitchPerspective06.
/**
* Test to ensure that the method annotated with the {@link Focus}
* annotation is invoked when switching between perspectives.
*/
@Test
public void testSwitchPerspective06() {
MWindow window = ems.createModelElement(MWindow.class);
application.getChildren().add(window);
application.setSelectedElement(window);
MPart partA = ems.createModelElement(MPart.class);
partA.setContributionURI("bundleclass://org.eclipse.e4.ui.tests/org.eclipse.e4.ui.tests.application.ClientEditor");
window.getSharedElements().add(partA);
MPart partB = ems.createModelElement(MPart.class);
partB.setContributionURI("bundleclass://org.eclipse.e4.ui.tests/org.eclipse.e4.ui.tests.application.ClientEditor");
window.getSharedElements().add(partB);
MPerspectiveStack perspectiveStack = ems.createModelElement(MPerspectiveStack.class);
window.getChildren().add(perspectiveStack);
window.setSelectedElement(perspectiveStack);
MPerspective perspectiveA = ems.createModelElement(MPerspective.class);
perspectiveStack.getChildren().add(perspectiveA);
perspectiveStack.setSelectedElement(perspectiveA);
MPlaceholder placeholderA = ems.createModelElement(MPlaceholder.class);
placeholderA.setRef(partA);
partA.setCurSharedRef(placeholderA);
perspectiveA.getChildren().add(placeholderA);
perspectiveA.setSelectedElement(placeholderA);
MPerspective perspectiveB = ems.createModelElement(MPerspective.class);
perspectiveStack.getChildren().add(perspectiveB);
MPlaceholder placeholderB = ems.createModelElement(MPlaceholder.class);
placeholderB.setRef(partB);
perspectiveB.getChildren().add(placeholderB);
perspectiveB.setSelectedElement(placeholderB);
initialize();
getEngine().createGui(window);
EPartService partService = window.getContext().get(EPartService.class);
partService.switchPerspective(perspectiveB);
partService.switchPerspective(perspectiveA);
ClientEditor editorA = (ClientEditor) partA.getObject();
ClientEditor editorB = (ClientEditor) partB.getObject();
editorA.focusCalled = false;
editorB.focusCalled = false;
partService.switchPerspective(perspectiveB);
assertFalse(editorA.focusCalled);
assertTrue(editorB.focusCalled);
editorB.focusCalled = false;
partService.switchPerspective(perspectiveA);
assertTrue(editorA.focusCalled);
assertFalse(editorB.focusCalled);
}
use of org.eclipse.e4.ui.di.Focus in project eclipse.platform.ui by eclipse-platform.
the class ContributedPartRenderer method createWidget.
@Override
public Object createWidget(final MUIElement element, Object parent) {
if (!(element instanceof MPart) || !(parent instanceof Composite)) {
return null;
}
// retrieve context for this part
final MPart part = (MPart) element;
IEclipseContext localContext = part.getContext();
Widget parentWidget = (Widget) parent;
// retrieve existing Composite, e.g., for the e4 compatibility case
Composite partComposite = localContext.getLocal(Composite.class);
// does the part already have a composite in its contexts?
if (partComposite == null) {
final Composite newComposite = new Composite((Composite) parentWidget, SWT.NONE) {
/**
* Field to determine whether we are currently in the midst of
* granting focus to the part.
*/
private boolean beingFocused = false;
@Override
public boolean setFocus() {
if (!beingFocused) {
try {
// we are currently asking the part to take focus
beingFocused = true;
// delegate an attempt to set the focus here to the
// part's implementation (if there is one)
Object object = part.getObject();
if (object != null && isEnabled()) {
IPresentationEngine pe = part.getContext().get(IPresentationEngine.class);
pe.focusGui(part);
return true;
}
return super.setFocus();
} finally {
// we are done, unset our flag
beingFocused = false;
}
}
// just return
return true;
}
};
newComposite.setLayout(new FillLayout(SWT.VERTICAL));
partComposite = newComposite;
}
bindWidget(element, partComposite);
localContext.set(Composite.class, partComposite);
IContributionFactory contributionFactory = localContext.get(IContributionFactory.class);
Object newPart = contributionFactory.create(part.getContributionURI(), localContext);
part.setObject(newPart);
return partComposite;
}
use of org.eclipse.e4.ui.di.Focus in project eclipse.platform.ui by eclipse-platform.
the class PartRenderingEngine method focusGui.
@Override
public void focusGui(MUIElement element) {
AbstractPartRenderer renderer = (AbstractPartRenderer) element.getRenderer();
if (renderer == null || element.getWidget() == null) {
if (Policy.DEBUG_FOCUS) {
WorkbenchSWTActivator.trace(Policy.DEBUG_FOCUS_FLAG, "Trying to focus GUI on element without renderer or widget: " + element, // $NON-NLS-1$
new Exception());
}
return;
}
Object implementation = element instanceof MContribution ? ((MContribution) element).getObject() : null;
// If there is no class to call @Focus on then revert to the default
if (implementation == null) {
renderer.forceFocus(element);
return;
}
try {
IEclipseContext context = getContext(element);
Object defaultValue = new Object();
Object returnValue = ContextInjectionFactory.invoke(implementation, Focus.class, context, defaultValue);
if (returnValue == defaultValue) {
// No @Focus method, force the focus
renderer.forceFocus(element);
} else if (Policy.DEBUG_FOCUS) {
// $NON-NLS-1$
WorkbenchSWTActivator.trace(Policy.DEBUG_FOCUS_FLAG, "Focused GUI on element: " + element, null);
}
} catch (InjectionException e) {
log(// $NON-NLS-1$ //$NON-NLS-2$
"Failed to grant focus to element", // $NON-NLS-1$ //$NON-NLS-2$
"Failed to grant focus to element ({0})", element.getElementId(), e);
} catch (RuntimeException e) {
log(// $NON-NLS-1$
"Failed to grant focus to element via DI", "Failed to grant focus via DI to element ({0})", element.getElementId(), // $NON-NLS-1$
e);
}
}
use of org.eclipse.e4.ui.di.Focus in project eclipse.platform.ui by eclipse-platform.
the class WorkbenchWindow method busyClose.
/**
* Close the window.
*
* Assumes that busy cursor is active.
*/
private boolean busyClose(boolean remove) {
/*
* Warning: Intricate flow of control and re-entrant invocations of this method:
*
* - busyClose(true) is called from WorkbenchWindow#close() when the user closes
* a workbench window.
*
* - busyClose(false) is called from Workbench#close(int, boolean). This happens
* on File > Exit/Restart, [Mac] Quit Eclipse, AND ... tadaa ... from
* busyClose(true) when the user closes the last window => [Case A]
*
* Additional complication: busyClose(true) can also be called again when
* someone runs an event loop during the shutdown sequence. In that case, the
* nested busyClose(true) should be dropped (bug 381555) => [Case B]
*/
if (closing) {
// [Case A] Window is already closing.
return false;
}
if (updateDisabled && remove) {
// window again.
return false;
}
// Whether the window was actually closed or not
boolean windowClosed = false;
// Setup internal flags to indicate window is in
// progress of closing and no update should be done.
updateDisabled = true;
try {
// Tag the currently active part so we can restore focus on startup
IWorkbenchPage activePage = getActivePage();
if (activePage != null) {
WorkbenchPartReference ref = (WorkbenchPartReference) activePage.getActivePartReference();
if (ref != null) {
ref.getModel().getTags().add(EPartService.ACTIVE_ON_CLOSE_TAG);
}
}
// Only do the check if it is OK to close if we are not closing
// via the workbench as the workbench will check this itself.
Workbench workbench = getWorkbenchImpl();
int count = workbench.getWorkbenchWindowCount();
// then we'll need to open a default window.
if (!workbench.isStarting() && !workbench.isClosing() && count <= 1 && workbench.getWorkbenchConfigurer().getExitOnLastWindowClose()) {
windowClosed = workbench.close();
} else if (okToClose()) {
closing = true;
windowClosed = hardClose(remove);
}
} finally {
if (!windowClosed) {
// Reset the internal flags if window was not closed.
closing = false;
updateDisabled = false;
}
}
if (windowClosed && tracker != null) {
tracker.close();
}
return windowClosed;
}
Aggregations