Search in sources :

Example 1 with WorkbenchWindowAdvisor

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

the class WorkbenchSaveRestoreStateTest method testSaveRestoreWindowState.

/**
 * Test save/restore state lifecycle API for WorkbenchWindowAdvisor
 */
@Test
public void testSaveRestoreWindowState() {
    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 IStatus saveState(IMemento memento) {
                    assertNotNull(memento);
                    memento.putString(WINDOW_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 IStatus restoreState(IMemento memento) {
                    assertNotNull(memento);
                    String stateData = memento.getString(WINDOW_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) IMemento(org.eclipse.ui.IMemento) Display(org.eclipse.swt.widgets.Display) Test(org.junit.Test)

Example 2 with WorkbenchWindowAdvisor

use of org.eclipse.ui.application.WorkbenchWindowAdvisor 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 3 with WorkbenchWindowAdvisor

use of org.eclipse.ui.application.WorkbenchWindowAdvisor 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)

Example 4 with WorkbenchWindowAdvisor

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

the class WorkbenchWindowConfigurerTest method doTest104558.

private void doTest104558(final boolean showPerspectiveBar, final boolean showCoolBar) {
    WorkbenchAdvisor wa = new WorkbenchAdvisorObserver(1) {

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

                @Override
                public void preWindowOpen() {
                    super.preWindowOpen();
                    getWindowConfigurer().setShowCoolBar(showCoolBar);
                    getWindowConfigurer().setShowPerspectiveBar(showPerspectiveBar);
                }

                @Override
                @SuppressWarnings("deprecation")
                public void createWindowContents(Shell shell) {
                    IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
                    configurer.createPageComposite(shell);
                }
            };
        }

        @Override
        public void eventLoopIdle(Display disp) {
            IWorkbenchWindow activeWorkbenchWindow = getWorkbenchConfigurer().getWorkbench().getActiveWorkbenchWindow();
            assertEquals("testing showCoolBar=" + showCoolBar, showCoolBar, ((WorkbenchWindow) activeWorkbenchWindow).getCoolBarVisible());
            assertEquals("testing showPerspectiveBar=" + showPerspectiveBar, showPerspectiveBar, ((WorkbenchWindow) activeWorkbenchWindow).getPerspectiveBarVisible());
            super.eventLoopIdle(disp);
        }

        @Override
        public void eventLoopException(Throwable exception) {
            throw new RuntimeException(exception);
        }
    };
    int code = PlatformUI.createAndRunWorkbench(display, wa);
    assertEquals(PlatformUI.RETURN_OK, code);
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) Shell(org.eclipse.swt.widgets.Shell) WorkbenchAdvisor(org.eclipse.ui.application.WorkbenchAdvisor) RCPTestWorkbenchAdvisor(org.eclipse.ui.tests.harness.util.RCPTestWorkbenchAdvisor) WorkbenchWindowAdvisor(org.eclipse.ui.application.WorkbenchWindowAdvisor) WorkbenchAdvisorObserver(org.eclipse.ui.tests.rcp.util.WorkbenchAdvisorObserver) IWorkbenchWindowConfigurer(org.eclipse.ui.application.IWorkbenchWindowConfigurer) Display(org.eclipse.swt.widgets.Display)

Example 5 with WorkbenchWindowAdvisor

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

the class WorkbenchSaveRestoreStateTest method testOnDemandSaveRestoreState.

/**
 * Test on-demand save/restore state API
 */
@Ignore
@Test
public void testOnDemandSaveRestoreState() {
    // save some window state on demand
    WorkbenchAdvisorObserver wa = new WorkbenchAdvisorObserver(1) {

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

        @Override
        public void eventLoopIdle(Display d) {
            workbenchConfig.getWorkbench().restart();
        }

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

                @Override
                public void postWindowOpen() {
                    File stateLocation = getStateFileLocation();
                    ensureDirectoryExists(stateLocation);
                    String stateFileName = stateLocation.getPath() + File.separator + "testOnDemandSaveRestoreState.xml";
                    OutputStreamWriter writer = null;
                    try {
                        writer = new OutputStreamWriter(new FileOutputStream(stateFileName), StandardCharsets.UTF_8);
                    } catch (FileNotFoundException e1) {
                    // creating a new file, won't happen unless the path
                    // eclipse
                    // specifies is totally wrong, or its read-only
                    }
                    XMLMemento xmlm = XMLMemento.createWriteRoot("TestState");
                    saveState(xmlm);
                    try {
                        xmlm.save(writer);
                        writer.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            };
        }
    };
    int code = PlatformUI.createAndRunWorkbench(display, wa);
    assertEquals(PlatformUI.RETURN_RESTART, code);
    assertFalse(display.isDisposed());
    display.dispose();
    assertTrue(display.isDisposed());
    // restore the workbench and restore a window
    // with state data on demand
    display = PlatformUI.createDisplay();
    WorkbenchAdvisorObserver wa2 = new WorkbenchAdvisorObserver(1) {

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

        @Override
        public boolean openWindows() {
            File stateLocation = getStateFileLocation();
            String stateFileName = "testOnDemandSaveRestoreState.xml";
            File stateFile = new File(stateLocation.getPath() + File.separator + stateFileName);
            assertTrue(stateFile.exists());
            IMemento memento = null;
            try {
                memento = XMLMemento.createReadRoot(new InputStreamReader(new FileInputStream(stateFile), StandardCharsets.UTF_8));
            } catch (WorkbenchException e) {
                e.printStackTrace();
            } catch (FileNotFoundException e) {
            // won't happen because we already checked it exists
            }
            assertNotNull(memento);
            IWorkbenchWindowConfigurer window = null;
            try {
                window = getWorkbenchConfigurer().restoreWorkbenchWindow(memento);
            } catch (WorkbenchException e) {
                e.printStackTrace();
            }
            assertNotNull(window);
            return true;
        }

        @Override
        public void postWindowRestore(IWorkbenchWindowConfigurer configurer) throws WorkbenchException {
            // TODO Auto-generated method stub
            super.postWindowRestore(configurer);
        }
    };
    int code2 = PlatformUI.createAndRunWorkbench(display, wa2);
    assertEquals(PlatformUI.RETURN_OK, code2);
}
Also used : IWorkbenchConfigurer(org.eclipse.ui.application.IWorkbenchConfigurer) InputStreamReader(java.io.InputStreamReader) FileNotFoundException(java.io.FileNotFoundException) WorkbenchAdvisorObserver(org.eclipse.ui.tests.rcp.util.WorkbenchAdvisorObserver) IOException(java.io.IOException) WorkbenchException(org.eclipse.ui.WorkbenchException) IMemento(org.eclipse.ui.IMemento) FileInputStream(java.io.FileInputStream) XMLMemento(org.eclipse.ui.XMLMemento) WorkbenchWindowAdvisor(org.eclipse.ui.application.WorkbenchWindowAdvisor) FileOutputStream(java.io.FileOutputStream) IWorkbenchWindowConfigurer(org.eclipse.ui.application.IWorkbenchWindowConfigurer) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) Display(org.eclipse.swt.widgets.Display) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

IWorkbenchWindowConfigurer (org.eclipse.ui.application.IWorkbenchWindowConfigurer)5 WorkbenchWindowAdvisor (org.eclipse.ui.application.WorkbenchWindowAdvisor)5 Display (org.eclipse.swt.widgets.Display)4 IMemento (org.eclipse.ui.IMemento)4 WorkbenchAdvisorObserver (org.eclipse.ui.tests.rcp.util.WorkbenchAdvisorObserver)4 Test (org.junit.Test)4 IWorkbenchConfigurer (org.eclipse.ui.application.IWorkbenchConfigurer)3 Shell (org.eclipse.swt.widgets.Shell)2 IWorkbenchWindow (org.eclipse.ui.IWorkbenchWindow)2 WorkbenchException (org.eclipse.ui.WorkbenchException)2 ActionBarAdvisor (org.eclipse.ui.application.ActionBarAdvisor)2 IActionBarConfigurer (org.eclipse.ui.application.IActionBarConfigurer)2 WorkbenchAdvisor (org.eclipse.ui.application.WorkbenchAdvisor)2 RCPTestWorkbenchAdvisor (org.eclipse.ui.tests.harness.util.RCPTestWorkbenchAdvisor)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1