Search in sources :

Example 1 with E4Application

use of org.eclipse.e4.ui.internal.workbench.swt.E4Application in project eclipse.platform.ui by eclipse-platform.

the class Workbench method createAndRunWorkbench.

/**
 * Creates the workbench and associates it with the the given display and
 * workbench advisor, and runs the workbench UI. This entails processing and
 * dispatching events until the workbench is closed or restarted.
 * <p>
 * This method is intended to be called by <code>PlatformUI</code>. Fails if the
 * workbench UI has already been created.
 * </p>
 * <p>
 * The display passed in must be the default display.
 * </p>
 *
 * @param display the display to be used for all UI interactions with the
 *                workbench
 * @param advisor the application-specific advisor that configures and
 *                specializes the workbench
 * @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}; other values reserved for future use
 */
public static int createAndRunWorkbench(final Display display, final WorkbenchAdvisor advisor) {
    final int[] returnCode = new int[1];
    Realm.runWithDefault(DisplayRealm.getRealm(display), () -> {
        boolean showProgress = PrefUtil.getAPIPreferenceStore().getBoolean(IWorkbenchPreferenceConstants.SHOW_PROGRESS_ON_STARTUP);
        final String nlExtensions = Platform.getNLExtensions();
        if (nlExtensions.length() > 0) {
            ULocale.setDefault(Category.FORMAT, new ULocale(ULocale.getDefault(Category.FORMAT).getBaseName() + nlExtensions));
        }
        System.setProperty(org.eclipse.e4.ui.workbench.IWorkbench.XMI_URI_ARG, // $NON-NLS-1$
        "org.eclipse.ui.workbench/LegacyIDE.e4xmi");
        Object obj = getApplication(Platform.getCommandLineArgs());
        IPreferenceStore store = WorkbenchPlugin.getDefault().getPreferenceStore();
        if (!store.isDefault(IPreferenceConstants.LAYOUT_DIRECTION)) {
            int orientation = store.getInt(IPreferenceConstants.LAYOUT_DIRECTION);
            Window.setDefaultOrientation(orientation);
        }
        if (obj instanceof E4Application) {
            E4Application e4app = (E4Application) obj;
            E4Workbench e4Workbench = e4app.createE4Workbench(getApplicationContext(), display);
            MApplication appModel = e4Workbench.getApplication();
            IEclipseContext context = e4Workbench.getContext();
            // create the workbench instance
            Workbench workbench = new Workbench(display, advisor, appModel, context);
            Dictionary<String, Object> properties = new Hashtable<>();
            properties.put(Constants.SERVICE_RANKING, Integer.valueOf(Integer.MAX_VALUE - 1));
            ServiceRegistration<?>[] registration = new ServiceRegistration[1];
            StartupMonitor startupMonitor = new StartupMonitor() {

                @Override
                public void applicationRunning() {
                    // unregister ourself
                    registration[0].unregister();
                    // fire part visibility events now that we're up
                    for (IWorkbenchWindow window : workbench.getWorkbenchWindows()) {
                        IWorkbenchPage page = window.getActivePage();
                        if (page != null) {
                            ((WorkbenchPage) page).fireInitialPartVisibilityEvents();
                        }
                    }
                }

                @Override
                public void update() {
                // do nothing - we come into the picture far too late
                // for this to be relevant
                }
            };
            registration[0] = FrameworkUtil.getBundle(WorkbenchPlugin.class).getBundleContext().registerService(StartupMonitor.class.getName(), startupMonitor, properties);
            // listener for updating the splash screen
            SynchronousBundleListener bundleListener = null;
            createSplash = WorkbenchPlugin.isSplashHandleSpecified();
            if (createSplash) {
                // prime the splash nice and early
                workbench.createSplashWrapper();
                // Bug 539376, 427393, 455162: show the splash screen after
                // the image is loaded. See IDEApplication#checkInstanceLocation
                // where the splash shell got hidden to avoid empty shell
                AbstractSplashHandler handler = getSplash();
                if (handler != null) {
                    Shell splashShell = handler.getSplash();
                    if (splashShell != null && !splashShell.isDisposed()) {
                        splashShell.setVisible(true);
                        splashShell.forceActive();
                    }
                }
                spinEventQueueToUpdateSplash(display);
                if (handler != null && showProgress) {
                    IProgressMonitor progressMonitor = SubMonitor.convert(handler.getBundleProgressMonitor());
                    bundleListener = new Workbench.StartupProgressBundleListener(progressMonitor, display);
                    WorkbenchPlugin.getDefault().addBundleListener(bundleListener);
                }
            }
            setSearchContribution(appModel, true);
            // run the legacy workbench once
            returnCode[0] = workbench.runUI();
            if (returnCode[0] == PlatformUI.RETURN_OK) {
                // run the e4 event loop and instantiate ... well, stuff
                if (bundleListener != null) {
                    WorkbenchPlugin.getDefault().removeBundleListener(bundleListener);
                }
                e4Workbench.createAndRunUI(e4Workbench.getApplication());
            }
            if (returnCode[0] != PlatformUI.RETURN_UNSTARTABLE) {
                setSearchContribution(appModel, false);
                e4app.saveModel();
            }
            // code needs to be set appropriately
            if (e4Workbench.isRestart()) {
                returnCode[0] = PlatformUI.RETURN_RESTART;
            } else {
                e4Workbench.close();
                returnCode[0] = workbench.returnCode;
            }
        }
    });
    return returnCode[0];
}
Also used : E4Workbench(org.eclipse.e4.ui.internal.workbench.E4Workbench) IWorkbench(org.eclipse.ui.IWorkbench) Shell(org.eclipse.swt.widgets.Shell) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) E4Workbench(org.eclipse.e4.ui.internal.workbench.E4Workbench) StartupMonitor(org.eclipse.osgi.service.runnable.StartupMonitor) ServiceRegistration(org.osgi.framework.ServiceRegistration) IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) ULocale(com.ibm.icu.util.ULocale) Hashtable(java.util.Hashtable) Point(org.eclipse.swt.graphics.Point) IExtensionPoint(org.eclipse.core.runtime.IExtensionPoint) AbstractSplashHandler(org.eclipse.ui.splash.AbstractSplashHandler) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) E4Application(org.eclipse.e4.ui.internal.workbench.swt.E4Application) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) EObject(org.eclipse.emf.ecore.EObject) IPreferenceStore(org.eclipse.jface.preference.IPreferenceStore) SynchronousBundleListener(org.osgi.framework.SynchronousBundleListener) MApplication(org.eclipse.e4.ui.model.application.MApplication)

Aggregations

ULocale (com.ibm.icu.util.ULocale)1 Hashtable (java.util.Hashtable)1 IExtensionPoint (org.eclipse.core.runtime.IExtensionPoint)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 IEclipseContext (org.eclipse.e4.core.contexts.IEclipseContext)1 E4Workbench (org.eclipse.e4.ui.internal.workbench.E4Workbench)1 E4Application (org.eclipse.e4.ui.internal.workbench.swt.E4Application)1 MApplication (org.eclipse.e4.ui.model.application.MApplication)1 EObject (org.eclipse.emf.ecore.EObject)1 IPreferenceStore (org.eclipse.jface.preference.IPreferenceStore)1 StartupMonitor (org.eclipse.osgi.service.runnable.StartupMonitor)1 Point (org.eclipse.swt.graphics.Point)1 Shell (org.eclipse.swt.widgets.Shell)1 IWorkbench (org.eclipse.ui.IWorkbench)1 IWorkbenchPage (org.eclipse.ui.IWorkbenchPage)1 IWorkbenchWindow (org.eclipse.ui.IWorkbenchWindow)1 AbstractSplashHandler (org.eclipse.ui.splash.AbstractSplashHandler)1 ServiceRegistration (org.osgi.framework.ServiceRegistration)1 SynchronousBundleListener (org.osgi.framework.SynchronousBundleListener)1