解决RCP中CNF(navigator)配置后delete\copy\past快捷键失效

这两天在配置一个CNF导航视图时候发现快捷键delete、past、copy等都失效了,折腾良久,搞清楚了;


1.快捷键要想能在菜单右边显示出来:

deleteAction.setActionDefinitionId(IWorkbenchCommandConstants.EDIT_DELETE);

2.要想生效必须绑定handler:

@Override
	public void fillActionBars(final IActionBars actionBars) {

		if (textActionHandler == null) {
			textActionHandler = new TextActionHandler(actionBars); // hook
																	// handlers
		}
		textActionHandler.setCopyAction(copyAction);
		textActionHandler.setPasteAction(pasteAction);
		textActionHandler.setDeleteAction(deleteAction);
		// renameAction.setTextActionHandler(textActionHandler);
		updateActionBars();

		textActionHandler.updateActionBars();
	}

public void updateActionBars() {
		actionBars.setGlobalActionHandler(ActionFactory.CUT.getId(),
                textCutAction);
		actionBars.setGlobalActionHandler(ActionFactory.COPY.getId(),
                textCopyAction);
actionBars.setGlobalActionHandler(ActionFactory.PASTE.getId(),
                textPasteAction);
		actionBars.setGlobalActionHandler(ActionFactory.SELECT_ALL.getId(),
                textSelectAllAction);
		actionBars.setGlobalActionHandler(ActionFactory.DELETE.getId(),
                textDeleteAction);
	}

setGlobalActionHandler把id和action绑定到一块;

这里你发现绑定的action并不是自己那个action,是texthandler中的action;

如果想强制生效可以直接把这个action换成我们那个action;


3.推荐的解决方法:

之所以不生效,是因为系统找不到action对应的commandid,我们可以绑定:

protected void makeActions() {
		clipboard = new Clipboard(shell.getDisplay());
		...
		deleteAction.setActionDefinitionId(IWorkbenchCommandConstants.EDIT_DELETE);

		initActionCommandMappingService();
	}

	/**
	 * 快捷键绑定actionBars.setGlobalActionHandler();
	 * 这里使用了textActionHandler.updateActionBars();所以绑定的是textActionHandler中text*Action,而不是这里的action;
	 * 方法一:重新设置setGlobalActionHandler为这里的action;
	 * 方法二:ActionCommandMappingService中添加这里的action映射WorkbenchCommandConstants.EDIT_*
	 */
	private void initActionCommandMappingService() {
		final IActionCommandMappingService actionCommandMappingService = (IActionCommandMappingService) CommonUIPlugin.getDefault().getWorkbench()
				.getActiveWorkbenchWindow().getService(IActionCommandMappingService.class);
		final String idDelete = actionCommandMappingService.getCommandId(ActionFactory.DELETE.getId());
		if (idDelete == null) {
			actionCommandMappingService.map(ActionFactory.DELETE.getId(), IWorkbenchCommandConstants.EDIT_DELETE);
		}
		final String idCopy = actionCommandMappingService.getCommandId(ActionFactory.COPY.getId());
		if (idCopy == null) {
			actionCommandMappingService.map(ActionFactory.COPY.getId(), IWorkbenchCommandConstants.EDIT_COPY);
		}
		final String idPast = actionCommandMappingService.getCommandId(ActionFactory.PASTE.getId());
		if (idPast == null) {
			actionCommandMappingService.map(ActionFactory.PASTE.getId(), IWorkbenchCommandConstants.EDIT_PASTE);
		}
	}

这样问题就解决了






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值