GEF KeyHandler

第一段代码: 注册action
		this.actionRegistry = this.getActionRegistry();

		IAction action = null;

		action = new UndoAction(this.page.getEditor());
		actionRegistry.registerAction(action);

这段代码的意义是: 往ActionRegistry中注册声明的action,当然只是注册,具体怎么用还要看右键菜单或Editor的actionbar怎么用了.

第二段代码: 给GraphicalViewer区域生成右键菜单
		ContextMenuProvider provider = new GefAreaContextMenuProvider(getGraphicalViewer(), actionRegistry);
		getGraphicalViewer().setContextMenu(provider);

每次在GraphicalViewer上点击右键的时候会生成右键菜单,具体怎么生成的就需要看ContextMenuProvider.buildContextMenu是怎么写的了.


第三段代码: 初始化一个KeyHandler.
private KeyHandler sharedKeyHandler;
	protected KeyHandler getCommonKeyHandler() {
		if (sharedKeyHandler == null) {
			sharedKeyHandler = new KeyHandler();
			sharedKeyHandler.put(KeyStroke.getPressed(SWT.DEL, 127, 0), getActionRegistry().getAction(ActionFactory.DELETE.getId()));
		}
		return sharedKeyHandler;
	}

这段代码是指:创建一个KeyHandler,表示,当用户按下Delete键的时候,调用getActionRegistry().getAction(ActionFactory.DELETE.getId()));这个action. 注:这步和Delete键绑定的action不能为空. 也就是在上面这段代码中,ActionFactory.DELETE必须已经在ActionRegistry中注册过了. 也就是第一段代码必须在这段代码前面.[当然,你也可以绑定自己的action,不一定非要在actionRegistry中注册]


第四段代码: 给GraphicalViewer添加KeyHandler
viewer.setKeyHandler(new GraphicalViewerKeyHandler(viewer).setParent(getCommonKeyHandler()));

这步:就是给GraphicalViewer真正添加上面定义的KeyHandler.
setParent().. 这个的意思就是除了GraphicalViewerKeyHandler中自己截获的按钮事件外,还需要监听额外的keyCode按钮事件. 执行过程会先在GraphicalViewerKeyHandler中查找,当找不到,再在parent中查找是否对当前的key进行了监听.

流水帐,别介意. 哈哈

你可能感兴趣的:(gef keyhandler)