use of org.eclipse.jface.operation.ModalContext in project eclipse.platform.ui by eclipse-platform.
the class Workbench method runUI.
/**
* Internal method for running the workbench UI. This entails processing and
* dispatching events until the workbench is closed or restarted.
*
* @return return code {@link PlatformUI#RETURN_OK RETURN_OK}for normal exit;
* {@link PlatformUI#RETURN_RESTART RETURN_RESTART}if the workbench was
* terminated with a call to {@link IWorkbench#restart
* IWorkbench.restart}; {@link PlatformUI#RETURN_UNSTARTABLE
* RETURN_UNSTARTABLE}if the workbench could not be started; other
* values reserved for future use
* @since 3.0
*/
private int runUI() {
// $NON-NLS-1$
UIStats.start(UIStats.START_WORKBENCH, "Workbench");
// deadlock code
boolean avoidDeadlock = true;
String[] commandLineArgs = Platform.getCommandLineArgs();
for (String commandLineArg : commandLineArgs) {
if (commandLineArg.equalsIgnoreCase("-allowDeadlock")) {
// $NON-NLS-1$
avoidDeadlock = false;
}
}
final UISynchronizer synchronizer;
if (avoidDeadlock) {
UILockListener uiLockListener = new UILockListener(display);
Job.getJobManager().setLockListener(uiLockListener);
synchronizer = new UISynchronizer(display, uiLockListener);
display.setSynchronizer(synchronizer);
// declare the main thread to be a startup thread.
UISynchronizer.startupThread.set(Boolean.TRUE);
} else
synchronizer = null;
// ModalContext should not spin the event loop (there is no UI yet to block)
ModalContext.setAllowReadAndDispatch(false);
// run while starting the Workbench, log a warning.
if (WorkbenchPlugin.getDefault().isDebugging()) {
display.asyncExec(() -> {
if (isStarting()) {
WorkbenchPlugin.log(StatusUtil.newStatus(IStatus.WARNING, // $NON-NLS-1$
"Event loop should not be run while the Workbench is starting.", new RuntimeException()));
}
});
}
Listener closeListener = event -> event.doit = close();
// Initialize an exception handler.
Window.IExceptionHandler handler = ExceptionHandler.getInstance();
try {
// react to display close event by closing workbench nicely
display.addListener(SWT.Close, closeListener);
// install backstop to catch exceptions thrown out of event loop
Window.setExceptionHandler(handler);
final boolean[] initOK = new boolean[1];
// initialize workbench and restore or open one window
initOK[0] = init();
if (initOK[0] && runEventLoop) {
// Same registration as in E4Workbench
Hashtable<String, Object> properties = new Hashtable<>();
// $NON-NLS-1$
properties.put("id", getId());
workbenchService = WorkbenchPlugin.getDefault().getBundleContext().registerService(IWorkbench.class.getName(), this, properties);
e4WorkbenchService = WorkbenchPlugin.getDefault().getBundleContext().registerService(org.eclipse.e4.ui.workbench.IWorkbench.class.getName(), this, properties);
Runnable earlyStartup = () -> {
// Let the advisor run its start-up code.
// May trigger a close/restart.
advisor.postStartup();
// start eager plug-ins
startPlugins();
addStartupRegistryListener();
};
e4Context.set(PartRenderingEngine.EARLY_STARTUP_HOOK, earlyStartup);
// start workspace auto-save
final int millisecondInterval = getAutoSaveJobTime();
if (millisecondInterval > 0 && workbenchAutoSave) {
autoSaveJob = new WorkbenchJob(WORKBENCH_AUTO_SAVE_JOB) {
@Override
public IStatus runInUIThread(IProgressMonitor monitor) {
if (monitor.isCanceled()) {
return Status.CANCEL_STATUS;
}
final int nextDelay = getAutoSaveJobTime();
try {
if (applicationModelChanged) {
persist(false);
applicationModelChanged = false;
}
monitor.done();
} finally {
// repeat
if (nextDelay > 0 && workbenchAutoSave) {
this.schedule(nextDelay);
}
}
return Status.OK_STATUS;
}
};
autoSaveJob.setSystem(true);
autoSaveJob.schedule(millisecondInterval);
}
display.asyncExec(new Runnable() {
@Override
public void run() {
// $NON-NLS-1$
UIStats.end(UIStats.START_WORKBENCH, this, "Workbench");
UIStats.startupComplete();
}
});
getWorkbenchTestable().init(display, this);
// allow ModalContext to spin the event loop
ModalContext.setAllowReadAndDispatch(true);
isStarting = false;
if (synchronizer != null)
synchronizer.started();
}
returnCode = PlatformUI.RETURN_OK;
if (!initOK[0]) {
returnCode = PlatformUI.RETURN_UNSTARTABLE;
}
} catch (final Exception e) {
if (!display.isDisposed()) {
handler.handleException(e);
} else {
// $NON-NLS-1$
String msg = "Exception in Workbench.runUI after display was disposed";
WorkbenchPlugin.log(msg, new Status(IStatus.ERROR, WorkbenchPlugin.PI_WORKBENCH, 1, msg, e));
}
}
// restart or exit based on returnCode
return returnCode;
}
Aggregations