当前位置: 首页 > 工具软件 > JFace > 使用案例 >

blog211210. JFace之Window

唐元青
2023-12-01

Window

JFace的Window是对Shell的封装, 便利使用. 封装的内容包括,

  • shell. 可以用getShell()得到.
  • default exception handler
  • font change listener
  • 初始定位和大小的设置. 默认的定位是屏幕居中, 尺寸是缺省值.
  • layout的设置, 默认是gridLayout
  • 可选的event loop
  • return code的设置和读取
  • close过程
  • 支持关联windowManager

Window的创建分做3步,

  1. new Window instance
  2. 调用create(), 创建shell和content. 此步若省略, 则在open()中会默认自动调用.
  3. 调用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().getActiveShell(), SWT.ICON_WARNING | SWT.OK);
			dialog.setText("Action");
			dialog.setMessage("do "+getText());
			dialog.open();
		}
	}
	
	public Snippet002ApplicationWindow() {
		super(null);
		
		app = this;
		
		newAction = new MyAction("&New", "new file", SWT.CTRL  + 'N', "file_new.png");
		openAction = new MyAction("&Open", "open file", SWT.CTRL  + 'O', "file_open.png");
		saveAction = new MyAction("&Save", "save file", SWT.CTRL  + 'S', "file_save.png");
		saveAsAction = new MyAction("Save &as", "save as file", SWT.CTRL  + 'A', "file_saveas.png");
		exitAction = new MyAction("E&xit", "exit app", 0, null);
		copyAction = new MyAction("&Copy", "copy to clipboard", SWT.CTRL  + 'C', "edit_copy.png");
		cutAction = new MyAction("Cut(&X)", "cut", SWT.CTRL  + 'X', "edit_cut.png");
		pasteAction = new MyAction("&Paste", "paste from clipboard", SWT.CTRL  + 'P', "edit_paste.png");
		helpAction = new MyAction("&Help", "help", 0, "help.png");
		
		
		addMenuBar();
		addToolBar(SWT.FLAT);
		addStatusLine();
		
		//创建menu
		
		MenuManager menuBar = getMenuBarManager();
		
		MenuManager fileMenu = new MenuManager("文件(&F)");
		MenuManager editMenu = new MenuManager("编辑(&E)");
		MenuManager helpMenu = new MenuManager("帮助(&H)");
		
		menuBar.add(fileMenu);
		menuBar.add(editMenu);
		menuBar.add(helpMenu);
		
		fileMenu.add(newAction);
		fileMenu.add(openAction);
		fileMenu.add(new Separator());
		fileMenu.add(saveAction);
		fileMenu.add(saveAsAction);
		fileMenu.add(new Separator());
		fileMenu.add(exitAction);
		
		editMenu.add(copyAction);
		editMenu.add(cutAction);
		editMenu.add(pasteAction);
		
		helpMenu.add(helpAction);
		
		//创建toolbar

		ToolBarManager toolbarMgr = getToolBarManager();
		
		toolbarMgr.add(new GroupMarker("file"));
		toolbarMgr.add(new GroupMarker("edit"));
		toolbarMgr.add(new GroupMarker("help"));
		
		
		toolbarMgr.appendToGroup("file", newAction);
		toolbarMgr.appendToGroup("file", openAction);
		toolbarMgr.appendToGroup("file", saveAction);
		toolbarMgr.appendToGroup("file", saveAsAction);
		
		toolbarMgr.appendToGroup("edit", new Separator());
		toolbarMgr.appendToGroup("edit", copyAction);
		toolbarMgr.appendToGroup("edit", cutAction);
		toolbarMgr.appendToGroup("edit", pasteAction);		
		
		toolbarMgr.appendToGroup("help", new Separator());
		toolbarMgr.appendToGroup("help", helpAction);

		//statusLine initialization
		
		setStatus("Ready");

		
	}

	@Override
	protected void configureShell(Shell shell) {
		super.configureShell(shell);		
		shell.setText("Snippet002ApplicationWindow");
		shell.setMaximized(true);				
	}

	@Override
	protected Point getInitialSize() {
		//return super.getInitialSize();
		return new Point(300,200);
	}

	@Override
	protected Control createContents(Composite parent) {
		Composite comp = new Composite(parent, SWT.BORDER);
		comp.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		comp.setBackground(new Color(200, 200, 0));
		comp.setLayout(new GridLayout());
		Label label = new Label(comp, SWT.PUSH);
		label.setText("new window");
		label.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));
		
		return comp;
	}
	
	public static void main(String[] args) {
		Display display = new Display();		//the default display, Display.getCurrent()
		
		Snippet002ApplicationWindow appWin = new Snippet002ApplicationWindow();
		appWin.setBlockOnOpen(true);
		app.open();
		
		display.dispose();
	}

}

Window相关的class和interface

class Window implements IShellProvider;
    class ApplicationWindow;
    class Dialog;
    class PopupDialog;

驽马一架 一花一世界 2021/12/10

 类似资料: