Find the open eclipse dialog programmatically
It is possible to find an active page/editor in eclipse. How can we programmatically get hold of the instance of the open mo开发者_开发知识库dal dialog in eclipse.
There are no corresponding interface in Eclipse to access any current model dialog. The best approximation is Display.getActiveShell()
, which will return the shell that hosts the active dialog if one exists.
[Dialogs are implemented by having their own event loop, so it can be rather difficult to run your own code...]
EDIT: Based on your comment below, here is a small snippet I use constantly to test for the presence of specific preference pages. I guess this can be used used as the starting point for your own test... Note the use of Display.timerExec(...)
.
public void test(String pageId) {
try {
final IWorkbench workbench = PlatformUI.getWorkbench();
final Shell[] shells = workbench.getDisplay().getShells();
final ICommandService cs = (ICommandService) workbench.getService(ICommandService.class);
final ParameterizedCommand command = cs.deserialize("org.eclipse.ui.window.preferences(preferencePageId="
+ pageId + ")");
assertNotNull(command);
final IHandlerService hs = (IHandlerService) workbench.getService(IHandlerService.class);
// Have to use timerExec to get the runnable executed after the dialog is shown
workbench.getDisplay().timerExec(2000, new Runnable() {
@Override
public void run() {
assertEquals(shells.length + 1, workbench.getDisplay().getShells().length);
final Shell lastShell = findLastShell(workbench.getDisplay().getShells(), shells);
assertNotNull(lastShell);
final Object data = lastShell.getData();
assertNotNull(data);
assertTrue(data instanceof PreferenceDialog);
lastShell.close();
assertEquals(shells.length, workbench.getDisplay().getShells().length);
}
private Shell findLastShell(Shell[] currentShells, Shell[] oldShells) {
CheckNext: for (final Shell cs : currentShells) {
for (final Shell os : oldShells) {
if (os == cs) {
continue CheckNext;
}
}
return cs;
}
return null;
}
});
hs.executeCommand(command, null);
} catch (final Exception ex) {
fail(ex.getMessage());
}
}
精彩评论