Search in sources :

Example 1 with E4Workbench

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

the class E4Application method start.

@Override
public Object start(IApplicationContext applicationContext) throws Exception {
    // set the display name before the Display is
    // created to ensure the app name is used in any
    // platform menus, etc. See
    // https://bugs.eclipse.org/bugs/show_bug.cgi?id=329456#c14
    IProduct product = Platform.getProduct();
    if (product != null && product.getName() != null) {
        Display.setAppName(product.getName());
    }
    Display display = getApplicationDisplay();
    Location instanceLocation = null;
    try {
        E4Workbench workbench = createE4Workbench(applicationContext, display);
        instanceLocation = (Location) workbench.getContext().get(E4Workbench.INSTANCE_LOCATION);
        Shell shell = display.getActiveShell();
        if (shell == null) {
            shell = new Shell();
            // place it off so it's not visible
            shell.setLocation(0, 10000);
        }
        if (!checkInstanceLocation(instanceLocation, shell, workbench.getContext()))
            return EXIT_OK;
        // Create and run the UI (if any)
        workbench.createAndRunUI(workbench.getApplication());
        saveModel();
        workbench.close();
        if (lcManager != null) {
            ContextInjectionFactory.invoke(lcManager, PostWorkbenchClose.class, workbench.getContext(), null);
        }
        if (workbench.isRestart()) {
            return EXIT_RESTART;
        }
        return EXIT_OK;
    } finally {
        if (display != null)
            display.dispose();
        if (instanceLocation != null)
            instanceLocation.release();
    }
}
Also used : Shell(org.eclipse.swt.widgets.Shell) E4Workbench(org.eclipse.e4.ui.internal.workbench.E4Workbench) IProduct(org.eclipse.core.runtime.IProduct) Display(org.eclipse.swt.widgets.Display) Location(org.eclipse.osgi.service.datalocation.Location)

Example 2 with E4Workbench

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

the class E4Application method createE4Workbench.

public E4Workbench createE4Workbench(IApplicationContext applicationContext, final Display display) {
    args = (String[]) applicationContext.getArguments().get(IApplicationContext.APPLICATION_ARGS);
    IEclipseContext appContext = createDefaultContext();
    appContext.set(Display.class, display);
    appContext.set(Realm.class, DisplayRealm.getRealm(display));
    appContext.set(UISynchronize.class, new DisplayUISynchronize(display));
    appContext.set(IApplicationContext.class, applicationContext);
    // This context will be used by the injector for its
    // extended data suppliers
    ContextInjectionFactory.setDefault(appContext);
    // Get the factory to create DI instances with
    IContributionFactory factory = appContext.get(IContributionFactory.class);
    // Install the life-cycle manager for this session if there's one
    // defined
    Optional<String> lifeCycleURI = getArgValue(IWorkbench.LIFE_CYCLE_URI_ARG, applicationContext, false);
    lifeCycleURI.ifPresent(lifeCycleURIValue -> {
        lcManager = factory.create(lifeCycleURIValue, appContext);
        if (lcManager != null) {
            // Let the manager manipulate the appContext if desired
            ContextInjectionFactory.invoke(lcManager, PostContextCreate.class, appContext, null);
        }
    });
    Optional<String> forcedPerspectiveId = getArgValue(PERSPECTIVE_ARG_NAME, applicationContext, false);
    forcedPerspectiveId.ifPresent(forcedPerspectiveIdValue -> appContext.set(E4Workbench.FORCED_PERSPECTIVE_ID, forcedPerspectiveIdValue));
    String showLocation = getLocationFromCommandLine();
    if (showLocation != null) {
        appContext.set(E4Workbench.FORCED_SHOW_LOCATION, showLocation);
    }
    // Create the app model and its context
    MApplication appModel = loadApplicationModel(applicationContext, appContext);
    appModel.setContext(appContext);
    boolean isRtl = ((Window.getDefaultOrientation() & SWT.RIGHT_TO_LEFT) != 0);
    appModel.getTransientData().put(E4Workbench.RTL_MODE, isRtl);
    // context (see Workbench#getInstance())
    if (!E4Workbench.getServiceContext().containsKey(MApplication.class)) {
        // first one wins.
        E4Workbench.getServiceContext().set(MApplication.class, appModel);
    }
    // Set the app's context after adding itself
    appContext.set(MApplication.class, appModel);
    // adds basic services to the contexts
    initializeServices(appModel);
    // let the life cycle manager add to the model
    if (lcManager != null) {
        ContextInjectionFactory.invoke(lcManager, ProcessAdditions.class, appContext, null);
        ContextInjectionFactory.invoke(lcManager, ProcessRemovals.class, appContext, null);
    }
    // Create the addons
    IEclipseContext addonStaticContext = EclipseContextFactory.create();
    for (MAddon addon : appModel.getAddons()) {
        addonStaticContext.set(MAddon.class, addon);
        Object obj = factory.create(addon.getContributionURI(), appContext, addonStaticContext);
        addon.setObject(obj);
    }
    // Parse out parameters from both the command line and/or the product
    // definition (if any) and put them in the context
    Optional<String> xmiURI = getArgValue(IWorkbench.XMI_URI_ARG, applicationContext, false);
    xmiURI.ifPresent(xmiURIValue -> {
        appContext.set(IWorkbench.XMI_URI_ARG, xmiURIValue);
    });
    setCSSContextVariables(applicationContext, appContext);
    Optional<String> rendererFactoryURI = getArgValue(E4Workbench.RENDERER_FACTORY_URI, applicationContext, false);
    rendererFactoryURI.ifPresent(rendererFactoryURIValue -> {
        appContext.set(E4Workbench.RENDERER_FACTORY_URI, rendererFactoryURIValue);
    });
    // This is a default arg, if missing we use the default rendering engine
    Optional<String> presentationURI = getArgValue(IWorkbench.PRESENTATION_URI_ARG, applicationContext, false);
    appContext.set(IWorkbench.PRESENTATION_URI_ARG, presentationURI.orElse(PartRenderingEngine.engineURI));
    // 'running' the UI (if any)...
    return workbench = new E4Workbench(appModel, appContext);
}
Also used : E4Workbench(org.eclipse.e4.ui.internal.workbench.E4Workbench) DisplayUISynchronize(org.eclipse.e4.ui.workbench.swt.DisplayUISynchronize) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) EObject(org.eclipse.emf.ecore.EObject) MAddon(org.eclipse.e4.ui.model.application.MAddon) IContributionFactory(org.eclipse.e4.core.services.contributions.IContributionFactory) MApplication(org.eclipse.e4.ui.model.application.MApplication)

Example 3 with E4Workbench

use of org.eclipse.e4.ui.internal.workbench.E4Workbench 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)

Example 4 with E4Workbench

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

the class NewMWindowTest method testContextChildren.

@Test
public void testContextChildren() {
    final MWindow window = createWindowWithOneView();
    wb = new E4Workbench(window, appContext);
    Widget topWidget = (Widget) window.getWidget();
    assertTrue(topWidget instanceof Shell);
    Shell shell = (Shell) topWidget;
    assertEquals("MyWindow", shell.getText());
    // should get the window context
    IEclipseContext child = appContext.getActiveChild();
    assertNotNull(child);
    assertEquals(window.getContext(), child);
    MPart modelPart = getContributedPart(window);
    assertNotNull(modelPart);
    assertEquals(window, modelPart.getParent().getParent().getParent());
    // "activate" the part, same as (in theory) an
    // SWT.Activate event.
    AbstractPartRenderer factory = (AbstractPartRenderer) modelPart.getRenderer();
    factory.activate(modelPart);
    IEclipseContext next = child.getActiveChild();
    while (next != null) {
        child = next;
        next = child.getActiveChild();
        if (next == child) {
            fail("Cycle detected in part context");
            break;
        }
    }
    assertFalse(window.getContext() == child);
    MPart contextPart = child.get(MPart.class);
    assertNotNull(contextPart);
    assertEquals(window, contextPart.getParent().getParent().getParent());
}
Also used : Shell(org.eclipse.swt.widgets.Shell) MPart(org.eclipse.e4.ui.model.application.ui.basic.MPart) AbstractPartRenderer(org.eclipse.e4.ui.internal.workbench.swt.AbstractPartRenderer) E4Workbench(org.eclipse.e4.ui.internal.workbench.E4Workbench) Widget(org.eclipse.swt.widgets.Widget) IEclipseContext(org.eclipse.e4.core.contexts.IEclipseContext) MWindow(org.eclipse.e4.ui.model.application.ui.basic.MWindow) Test(org.junit.Test)

Example 5 with E4Workbench

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

the class NewMWindowTest method testCreateMenu.

@Test
public void testCreateMenu() {
    final MWindow window = createWindowWithOneViewAndMenu();
    wb = new E4Workbench(window, appContext);
    Widget topWidget = (Widget) window.getWidget();
    assertTrue(topWidget instanceof Shell);
    Shell shell = (Shell) topWidget;
    final Menu menuBar = shell.getMenuBar();
    assertNotNull(menuBar);
    assertEquals(1, menuBar.getItemCount());
    final MenuItem fileItem = menuBar.getItem(0);
    assertEquals("File", fileItem.getText());
    final Menu fileMenu = fileItem.getMenu();
    fileMenu.notifyListeners(SWT.Show, null);
    assertEquals(2, fileMenu.getItemCount());
    fileMenu.notifyListeners(SWT.Hide, null);
    MMenu mainMenu = window.getMainMenu();
    MMenu modelFileMenu = (MMenu) mainMenu.getChildren().get(0);
    final MMenuItem item2Model = (MMenuItem) modelFileMenu.getChildren().get(0);
    item2Model.setToBeRendered(false);
    fileMenu.notifyListeners(SWT.Show, null);
    assertEquals(1, fileMenu.getItemCount());
    fileMenu.notifyListeners(SWT.Hide, null);
    item2Model.setToBeRendered(true);
    fileMenu.notifyListeners(SWT.Show, null);
    assertEquals(2, fileMenu.getItemCount());
    fileMenu.notifyListeners(SWT.Hide, null);
}
Also used : MMenuItem(org.eclipse.e4.ui.model.application.ui.menu.MMenuItem) Shell(org.eclipse.swt.widgets.Shell) E4Workbench(org.eclipse.e4.ui.internal.workbench.E4Workbench) Widget(org.eclipse.swt.widgets.Widget) MMenuItem(org.eclipse.e4.ui.model.application.ui.menu.MMenuItem) MDirectMenuItem(org.eclipse.e4.ui.model.application.ui.menu.MDirectMenuItem) MenuItem(org.eclipse.swt.widgets.MenuItem) MMenu(org.eclipse.e4.ui.model.application.ui.menu.MMenu) Menu(org.eclipse.swt.widgets.Menu) MWindow(org.eclipse.e4.ui.model.application.ui.basic.MWindow) MMenu(org.eclipse.e4.ui.model.application.ui.menu.MMenu) Test(org.junit.Test)

Aggregations

E4Workbench (org.eclipse.e4.ui.internal.workbench.E4Workbench)15 MWindow (org.eclipse.e4.ui.model.application.ui.basic.MWindow)12 MApplication (org.eclipse.e4.ui.model.application.MApplication)10 MPart (org.eclipse.e4.ui.model.application.ui.basic.MPart)9 Test (org.junit.Test)9 Shell (org.eclipse.swt.widgets.Shell)8 MPartStack (org.eclipse.e4.ui.model.application.ui.basic.MPartStack)5 Widget (org.eclipse.swt.widgets.Widget)5 IEclipseContext (org.eclipse.e4.core.contexts.IEclipseContext)3 MPartSashContainer (org.eclipse.e4.ui.model.application.ui.basic.MPartSashContainer)3 CTabFolder (org.eclipse.swt.custom.CTabFolder)3 ULocale (com.ibm.icu.util.ULocale)2 Hashtable (java.util.Hashtable)2 EPartService (org.eclipse.e4.ui.workbench.modeling.EPartService)2 EObject (org.eclipse.emf.ecore.EObject)2 Category (com.ibm.icu.util.ULocale.Category)1 BufferedInputStream (java.io.BufferedInputStream)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1