use of org.eclipse.e4.ui.bindings.keys.KeyBindingDispatcher in project eclipse.platform.ui by eclipse-platform.
the class PartRenderingEngine method run.
@Override
@Inject
@Optional
public Object run(final MApplicationElement uiRoot, final IEclipseContext runContext) {
final Display display;
if (runContext.get(Display.class) != null) {
display = runContext.get(Display.class);
} else {
display = Display.getDefault();
runContext.set(Display.class, display);
}
Realm.runWithDefault(DisplayRealm.getRealm(display), new Runnable() {
@Override
public void run() {
initializeStyling(display, runContext);
// Register an SWT resource handler
runContext.set(IResourceUtilities.class, new ResourceUtility());
// set up the keybinding manager
KeyBindingDispatcher dispatcher = ContextInjectionFactory.make(KeyBindingDispatcher.class, runContext);
runContext.set(KeyBindingDispatcher.class, dispatcher);
keyListener = dispatcher.getKeyDownFilter();
display.addFilter(SWT.KeyDown, keyListener);
display.addFilter(SWT.Traverse, keyListener);
// Show the initial UI
// Create a 'limbo' shell (used to host controls that shouldn't
// be in the current layout)
Shell limbo = getLimboShell();
runContext.set("limbo", limbo);
// HACK!! we should loop until the display gets disposed...
// ...then we listen for the last 'main' window to get disposed
// and dispose the Display
testShell = null;
theApp = null;
boolean spinOnce = true;
if (uiRoot instanceof MApplication) {
ShellActivationListener shellDialogListener = new ShellActivationListener((MApplication) uiRoot);
display.addFilter(SWT.Activate, shellDialogListener);
display.addFilter(SWT.Deactivate, shellDialogListener);
// loop until the app closes
spinOnce = false;
theApp = (MApplication) uiRoot;
// long startTime = System.currentTimeMillis();
for (MWindow window : theApp.getChildren()) {
createGui(window);
}
// long endTime = System.currentTimeMillis();
// System.out.println("Render: " + (endTime - startTime));
// tell the app context we are starting so the splash is
// torn down
IApplicationContext ac = appContext.get(IApplicationContext.class);
if (ac != null) {
ac.applicationRunning();
if (eventBroker != null) {
eventBroker.post(UIEvents.UILifeCycle.APP_STARTUP_COMPLETE, theApp);
}
}
} else if (uiRoot instanceof MUIElement) {
if (uiRoot instanceof MWindow) {
testShell = (Shell) createGui((MUIElement) uiRoot);
} else {
// Special handling for partial models (for testing...)
testShell = new Shell(display, SWT.SHELL_TRIM);
createGui((MUIElement) uiRoot, testShell, null);
}
}
// allow any early startup extensions to run
Runnable earlyStartup = (Runnable) runContext.get(EARLY_STARTUP_HOOK);
if (earlyStartup != null) {
earlyStartup.run();
}
TestableObject testableObject = runContext.get(TestableObject.class);
if (testableObject instanceof E4Testable) {
((E4Testable) testableObject).init(display, runContext.get(IWorkbench.class));
}
IEventLoopAdvisor advisor = runContext.getActiveLeaf().get(IEventLoopAdvisor.class);
if (advisor == null) {
advisor = new IEventLoopAdvisor() {
@Override
public void eventLoopIdle(Display display) {
display.sleep();
}
@Override
public void eventLoopException(Throwable exception) {
StatusReporter statusReporter = appContext.get(StatusReporter.class);
if (statusReporter != null) {
statusReporter.show(StatusReporter.ERROR, "Internal Error", exception);
} else if (logger != null) {
logger.error(exception);
}
}
};
}
final IEventLoopAdvisor finalAdvisor = advisor;
display.setErrorHandler(e -> {
// recoverable, hand it to the event loop advisor
if (e instanceof LinkageError || e instanceof AssertionError) {
handle(e, finalAdvisor);
} else {
// Otherwise, rethrow it
throw e;
}
});
display.setRuntimeExceptionHandler(e -> handle(e, finalAdvisor));
// Spin the event loop until someone disposes the display
while (((testShell != null && !testShell.isDisposed()) || (theApp != null && someAreVisible(theApp.getChildren()))) && !display.isDisposed()) {
try {
if (!display.readAndDispatch()) {
runContext.processWaiting();
if (spinOnce) {
return;
}
advisor.eventLoopIdle(display);
}
} catch (ThreadDeath th) {
throw th;
} catch (Exception | Error err) {
handle(err, advisor);
}
}
if (!spinOnce) {
cleanUp();
}
}
private void handle(Throwable ex, IEventLoopAdvisor advisor) {
try {
advisor.eventLoopException(ex);
} catch (Throwable t) {
if (t instanceof ThreadDeath) {
throw (ThreadDeath) t;
}
// couldn't handle the exception, print to console
t.printStackTrace();
}
}
});
return IApplication.EXIT_OK;
}
use of org.eclipse.e4.ui.bindings.keys.KeyBindingDispatcher in project eclipse.platform.ui by eclipse-platform.
the class KeyDispatcherTest method testExecuteOneCommand.
@Test
public void testExecuteOneCommand() {
KeyBindingDispatcher dispatcher = new KeyBindingDispatcher();
ContextInjectionFactory.inject(dispatcher, workbenchContext);
final Listener listener = dispatcher.getKeyDownFilter();
display.addFilter(SWT.KeyDown, listener);
display.addFilter(SWT.Traverse, listener);
assertFalse(handler.q2);
Shell shell = new Shell(display, SWT.NONE);
Event event = new Event();
event.type = SWT.KeyDown;
event.keyCode = SWT.CTRL;
shell.notifyListeners(SWT.KeyDown, event);
event = new Event();
event.type = SWT.KeyDown;
event.stateMask = SWT.CTRL;
event.keyCode = 'A';
shell.notifyListeners(SWT.KeyDown, event);
assertTrue(handler.q2);
}
use of org.eclipse.e4.ui.bindings.keys.KeyBindingDispatcher in project eclipse.platform.ui by eclipse-platform.
the class KeyDispatcherTest method TODOtestKeyDispatcherReset.
@Test
@Ignore
public void TODOtestKeyDispatcherReset() throws Exception {
KeyBindingDispatcher dispatcher = new KeyBindingDispatcher();
ContextInjectionFactory.inject(dispatcher, workbenchContext);
final Listener listener = dispatcher.getKeyDownFilter();
display.addFilter(SWT.KeyDown, listener);
display.addFilter(SWT.Traverse, listener);
assertFalse(twoStrokeHandler.q2);
Shell shell = new Shell(display, SWT.NONE);
Event event = new Event();
event.type = SWT.KeyDown;
event.keyCode = SWT.CTRL;
shell.notifyListeners(SWT.KeyDown, event);
event = new Event();
event.type = SWT.KeyDown;
event.stateMask = SWT.CTRL;
event.keyCode = '5';
shell.notifyListeners(SWT.KeyDown, event);
assertFalse(twoStrokeHandler.q2);
Thread.sleep(2000L);
processEvents();
event = new Event();
event.type = SWT.KeyDown;
event.keyCode = SWT.CTRL;
shell.notifyListeners(SWT.KeyDown, event);
event = new Event();
event.type = SWT.KeyDown;
event.stateMask = SWT.CTRL;
event.keyCode = 'A';
shell.notifyListeners(SWT.KeyDown, event);
processEvents();
assertFalse(twoStrokeHandler.q2);
assertTrue(handler.q2);
}
use of org.eclipse.e4.ui.bindings.keys.KeyBindingDispatcher in project eclipse.platform.ui by eclipse-platform.
the class KeyDispatcherTest method testExecuteMultiStrokeBinding.
@Test
public void testExecuteMultiStrokeBinding() {
KeyBindingDispatcher dispatcher = new KeyBindingDispatcher();
ContextInjectionFactory.inject(dispatcher, workbenchContext);
final Listener listener = dispatcher.getKeyDownFilter();
display.addFilter(SWT.KeyDown, listener);
display.addFilter(SWT.Traverse, listener);
assertFalse(twoStrokeHandler.q2);
Shell shell = new Shell(display, SWT.NONE);
Event event = new Event();
event.type = SWT.KeyDown;
event.keyCode = SWT.CTRL;
shell.notifyListeners(SWT.KeyDown, event);
event = new Event();
event.type = SWT.KeyDown;
event.stateMask = SWT.CTRL;
event.keyCode = '5';
shell.notifyListeners(SWT.KeyDown, event);
assertFalse(twoStrokeHandler.q2);
event = new Event();
event.type = SWT.KeyDown;
event.keyCode = SWT.CTRL;
shell.notifyListeners(SWT.KeyDown, event);
event = new Event();
event.type = SWT.KeyDown;
event.stateMask = SWT.CTRL;
event.keyCode = 'A';
shell.notifyListeners(SWT.KeyDown, event);
processEvents();
assertTrue(twoStrokeHandler.q2);
assertFalse(handler.q2);
}
use of org.eclipse.e4.ui.bindings.keys.KeyBindingDispatcher in project eclipse.platform.ui by eclipse-platform.
the class KeyDispatcherTest method testSendKeyStroke.
@Test
public void testSendKeyStroke() {
KeyBindingDispatcher dispatcher = ContextInjectionFactory.make(KeyBindingDispatcher.class, workbenchContext);
final Listener listener = dispatcher.getKeyDownFilter();
display.addFilter(SWT.KeyDown, listener);
display.addFilter(SWT.Traverse, listener);
Shell shell = new Shell(display, SWT.NONE);
shell.setLayout(new FillLayout());
StyledText text = new StyledText(shell, SWT.WRAP | SWT.MULTI);
shell.setBounds(100, 100, 100, 100);
shell.layout();
processEvents();
assertEquals("", text.getText());
Event event = new Event();
event.type = SWT.KeyDown;
event.stateMask = SWT.SHIFT;
event.keyCode = '(';
event.character = '(';
text.notifyListeners(SWT.KeyDown, event);
event = new Event();
event.type = SWT.KeyUp;
event.stateMask = SWT.SHIFT;
event.keyCode = '(';
event.character = '(';
text.notifyListeners(SWT.KeyUp, event);
processEvents();
assertEquals("(", text.getText());
}
Aggregations