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);
}
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);
}
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());
}
}
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);
}
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);
}
Aggregations