blog211210. JFace之Window
Window
JFace的Window是对Shell的封装, 便利使用. 封装的内容包括,
- shell. 可以用getShell()得到.
- default exception handler
- font change listener
- 初始定位和大小的设置. 默认的定位是屏幕居中, 尺寸是缺省值.
- layout的设置, 默认是gridLayout
- 可选的event loop
- return code的设置和读取
- close过程
- 支持关联windowManager
Window的创建分做3步,
- new Window instance
- 调用create(), 创建shell和content. 此步若省略, 则在open()中会默认自动调用.
- 调用open(), 封装有shell.open(), 至此窗口shell才真正显示出来.
若想window.open()直接进入event loop, window close后方才退出, 则可以在open()调用前调用setBlockOnOpen
windowObj.setBlockOnOpen(true);
int retCode = windowObj.open();
Window继承的methods常用的有,
- configureShell(), 完成shell显示前的设定工作. 例如再次调用setShellStyle().
- createContents(), 在此创建window的具体内容
- getLayout(), 返回shell要使用的layout.
- getInitialLocation()和getInitialSize(), 设置shell显示是的位置和尺寸.
- canHandleShellCloseEvent(), 判断是否继续/放弃close过程.
- close(), 执行shell的close.
ApplicationWindow
ApplicationWindow是Window的子类, 增强的功能包括,
- 使用定制的ApplicationWindowLayout布局内容, 支持摆放menuBar, statusLine, toolBar和coolBar.
- 支持menuManager, toolBarManager, statusLineManager, coolBarManager管理和设置.
一个例子
public class Snippet002ApplicationWindow extends ApplicationWindow {
public static ApplicationWindow app;
Action newAction;
Action openAction;
Action saveAction;
Action saveAsAction;
Action exitAction;
Action copyAction;
Action cutAction;
Action pasteAction;
Action helpAction;
Text content;
class MyAction extends Action{
public MyAction(String text, String tooltip, int keycode, String img)
{
super(text);
setToolTipText(tooltip);
if(keycode != 0)
setAccelerator(keycode);
if(img != null && !img.isEmpty())
{
ImageDescriptor desc = ImageDescriptor.createFromFile(Snippet002ApplicationWindow.class, img);
setImageDescriptor(desc);
}
}
public void run()
{
MessageBox dialog = new MessageBox(Display.getCurrent()