Search in sources :

Example 1 with ActionBarAdvisor

use of org.eclipse.ui.application.ActionBarAdvisor in project eclipse.platform.ui by eclipse-platform.

the class WorkbenchSaveRestoreStateTest method testSaveRestoreActionBarState.

/**
 * Test save/restore state lifecycle API for WorkbenchWindowAdvisor
 */
@Test
public void testSaveRestoreActionBarState() {
    final String advisorStateData = Long.toString(System.currentTimeMillis());
    // launch workbench and save some state data
    WorkbenchAdvisorObserver wa = new WorkbenchAdvisorObserver(1) {

        @Override
        public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) {
            return new WorkbenchWindowAdvisor(configurer) {

                @Override
                public ActionBarAdvisor createActionBarAdvisor(IActionBarConfigurer configurer1) {
                    return new ActionBarAdvisor(configurer1) {

                        @Override
                        public IStatus saveState(IMemento memento) {
                            assertNotNull(memento);
                            memento.putString(ACTIONBAR_ADVISOR_STATE_KEY, advisorStateData);
                            return super.saveState(memento);
                        }
                    };
                }
            };
        }

        @Override
        public void initialize(IWorkbenchConfigurer c) {
            super.initialize(c);
            c.setSaveAndRestore(true);
        }

        @Override
        public void eventLoopIdle(Display d) {
            workbenchConfig.getWorkbench().restart();
        }
    };
    int code = PlatformUI.createAndRunWorkbench(display, wa);
    assertEquals(PlatformUI.RETURN_RESTART, code);
    assertFalse(display.isDisposed());
    display.dispose();
    assertTrue(display.isDisposed());
    // restore the workbench and check for state data
    display = PlatformUI.createDisplay();
    WorkbenchAdvisorObserver wa2 = new WorkbenchAdvisorObserver(1) {

        @Override
        public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) {
            return new WorkbenchWindowAdvisor(configurer) {

                @Override
                public ActionBarAdvisor createActionBarAdvisor(IActionBarConfigurer configurer1) {
                    return new ActionBarAdvisor(configurer1) {

                        @Override
                        public IStatus restoreState(IMemento memento) {
                            assertNotNull(memento);
                            String stateData = memento.getString(ACTIONBAR_ADVISOR_STATE_KEY);
                            assertNotNull(stateData);
                            assertTrue(advisorStateData.equals(stateData));
                            return super.restoreState(memento);
                        }
                    };
                }
            };
        }

        @Override
        public void initialize(IWorkbenchConfigurer c) {
            super.initialize(c);
            c.setSaveAndRestore(true);
        }
    };
    int code2 = PlatformUI.createAndRunWorkbench(display, wa2);
    assertEquals(PlatformUI.RETURN_OK, code2);
}
Also used : IWorkbenchConfigurer(org.eclipse.ui.application.IWorkbenchConfigurer) WorkbenchWindowAdvisor(org.eclipse.ui.application.WorkbenchWindowAdvisor) WorkbenchAdvisorObserver(org.eclipse.ui.tests.rcp.util.WorkbenchAdvisorObserver) IWorkbenchWindowConfigurer(org.eclipse.ui.application.IWorkbenchWindowConfigurer) ActionBarAdvisor(org.eclipse.ui.application.ActionBarAdvisor) IMemento(org.eclipse.ui.IMemento) IActionBarConfigurer(org.eclipse.ui.application.IActionBarConfigurer) Display(org.eclipse.swt.widgets.Display) Test(org.junit.Test)

Example 2 with ActionBarAdvisor

use of org.eclipse.ui.application.ActionBarAdvisor in project eclipse.platform.ui by eclipse-platform.

the class WorkbenchWindowConfigurerTest method testThreading.

// tests to ensure that all WorkbenchAdvisor API is called from the UI thread.
@Test
public void testThreading() {
    final ArrayList<String> results = new ArrayList<>();
    WorkbenchAdvisor advisor = new RCPTestWorkbenchAdvisor(1) {

        @Override
        public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) {
            return new WorkbenchWindowAdvisor(configurer) {

                private void ensureThread() {
                    if (Display.getCurrent() != null) {
                        return;
                    }
                    Exception e = new Exception();
                    e.fillInStackTrace();
                    StackTraceElement element = e.getStackTrace()[1];
                    results.add(element.getClassName() + '.' + element.getMethodName());
                }

                @Override
                public ActionBarAdvisor createActionBarAdvisor(IActionBarConfigurer configurer) {
                    ensureThread();
                    return new ActionBarAdvisor(configurer) {

                        @Override
                        public void dispose() {
                            ensureThread();
                            super.dispose();
                        }

                        @Override
                        protected void disposeAction(IAction action) {
                            ensureThread();
                            super.disposeAction(action);
                        }

                        @Override
                        protected void disposeActions() {
                            ensureThread();
                            super.disposeActions();
                        }

                        @Override
                        public void fillActionBars(int flags) {
                            ensureThread();
                            super.fillActionBars(flags);
                        }

                        @Override
                        protected void fillCoolBar(ICoolBarManager coolBar) {
                            ensureThread();
                            super.fillCoolBar(coolBar);
                        }

                        @Override
                        protected void fillMenuBar(IMenuManager menuBar) {
                            ensureThread();
                            super.fillMenuBar(menuBar);
                        }

                        @Override
                        protected void fillStatusLine(IStatusLineManager statusLine) {
                            ensureThread();
                            super.fillStatusLine(statusLine);
                        }

                        @Override
                        protected IAction getAction(String id) {
                            ensureThread();
                            return super.getAction(id);
                        }

                        @Override
                        protected IActionBarConfigurer getActionBarConfigurer() {
                            ensureThread();
                            return super.getActionBarConfigurer();
                        }

                        @Override
                        public boolean isApplicationMenu(String menuId) {
                            ensureThread();
                            return super.isApplicationMenu(menuId);
                        }

                        @Override
                        protected void makeActions(IWorkbenchWindow window) {
                            ensureThread();
                            super.makeActions(window);
                        }

                        @Override
                        protected void register(IAction action) {
                            ensureThread();
                            super.register(action);
                        }

                        @Override
                        public IStatus restoreState(IMemento memento) {
                            ensureThread();
                            return super.restoreState(memento);
                        }

                        @Override
                        public IStatus saveState(IMemento memento) {
                            ensureThread();
                            return super.saveState(memento);
                        }
                    };
                }

                @Override
                @SuppressWarnings("deprecation")
                public Control createEmptyWindowContents(Composite parent) {
                    ensureThread();
                    return super.createEmptyWindowContents(parent);
                }

                @Override
                @SuppressWarnings("deprecation")
                public void createWindowContents(Shell shell) {
                    ensureThread();
                    super.createWindowContents(shell);
                }

                @Override
                public void dispose() {
                    ensureThread();
                    super.dispose();
                }

                @Override
                protected IWorkbenchWindowConfigurer getWindowConfigurer() {
                    ensureThread();
                    return super.getWindowConfigurer();
                }

                @Override
                public void openIntro() {
                    ensureThread();
                    super.openIntro();
                }

                @Override
                public void postWindowClose() {
                    ensureThread();
                    super.postWindowClose();
                }

                @Override
                public void postWindowCreate() {
                    ensureThread();
                    super.postWindowCreate();
                }

                @Override
                public void postWindowOpen() {
                    ensureThread();
                    super.postWindowOpen();
                }

                @Override
                public void postWindowRestore() throws WorkbenchException {
                    ensureThread();
                    super.postWindowRestore();
                }

                @Override
                public void preWindowOpen() {
                    ensureThread();
                    super.preWindowOpen();
                }

                @Override
                public boolean preWindowShellClose() {
                    ensureThread();
                    return super.preWindowShellClose();
                }

                @Override
                public IStatus restoreState(IMemento memento) {
                    ensureThread();
                    return super.restoreState(memento);
                }

                @Override
                public IStatus saveState(IMemento memento) {
                    ensureThread();
                    return super.saveState(memento);
                }
            };
        }
    };
    int code = PlatformUI.createAndRunWorkbench(display, advisor);
    assertEquals(PlatformUI.RETURN_OK, code);
    if (!results.isEmpty()) {
        StringBuilder buffer = new StringBuilder("Window/action bar advisor methods called from non-UI threads:\n");
        for (String string : results) {
            buffer.append(string).append('\n');
        }
        fail(buffer.toString());
    }
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) WorkbenchAdvisor(org.eclipse.ui.application.WorkbenchAdvisor) RCPTestWorkbenchAdvisor(org.eclipse.ui.tests.harness.util.RCPTestWorkbenchAdvisor) IAction(org.eclipse.jface.action.IAction) Composite(org.eclipse.swt.widgets.Composite) IStatusLineManager(org.eclipse.jface.action.IStatusLineManager) ArrayList(java.util.ArrayList) WorkbenchException(org.eclipse.ui.WorkbenchException) IMemento(org.eclipse.ui.IMemento) IActionBarConfigurer(org.eclipse.ui.application.IActionBarConfigurer) Shell(org.eclipse.swt.widgets.Shell) RCPTestWorkbenchAdvisor(org.eclipse.ui.tests.harness.util.RCPTestWorkbenchAdvisor) WorkbenchWindowAdvisor(org.eclipse.ui.application.WorkbenchWindowAdvisor) IWorkbenchWindowConfigurer(org.eclipse.ui.application.IWorkbenchWindowConfigurer) ActionBarAdvisor(org.eclipse.ui.application.ActionBarAdvisor) IMenuManager(org.eclipse.jface.action.IMenuManager) ICoolBarManager(org.eclipse.jface.action.ICoolBarManager) Test(org.junit.Test)

Aggregations

IMemento (org.eclipse.ui.IMemento)2 ActionBarAdvisor (org.eclipse.ui.application.ActionBarAdvisor)2 IActionBarConfigurer (org.eclipse.ui.application.IActionBarConfigurer)2 IWorkbenchWindowConfigurer (org.eclipse.ui.application.IWorkbenchWindowConfigurer)2 WorkbenchWindowAdvisor (org.eclipse.ui.application.WorkbenchWindowAdvisor)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 IAction (org.eclipse.jface.action.IAction)1 ICoolBarManager (org.eclipse.jface.action.ICoolBarManager)1 IMenuManager (org.eclipse.jface.action.IMenuManager)1 IStatusLineManager (org.eclipse.jface.action.IStatusLineManager)1 Composite (org.eclipse.swt.widgets.Composite)1 Display (org.eclipse.swt.widgets.Display)1 Shell (org.eclipse.swt.widgets.Shell)1 IWorkbenchWindow (org.eclipse.ui.IWorkbenchWindow)1 WorkbenchException (org.eclipse.ui.WorkbenchException)1 IWorkbenchConfigurer (org.eclipse.ui.application.IWorkbenchConfigurer)1 WorkbenchAdvisor (org.eclipse.ui.application.WorkbenchAdvisor)1 RCPTestWorkbenchAdvisor (org.eclipse.ui.tests.harness.util.RCPTestWorkbenchAdvisor)1 WorkbenchAdvisorObserver (org.eclipse.ui.tests.rcp.util.WorkbenchAdvisorObserver)1