自己扩展CNF之后,导航栏的删除、复制、黏贴等快捷键失效了,在网上搜索了半天,结果最终不如自己看源码。

本篇文章的主要目的不止于解决快捷键失效,更在于如何处理类似的问题,如何利用debug快速定位。这种解决问题的思路能帮助我们更加理解他人的代码,快速熟悉一套陌生的框架。

1、理解问题的本质,为什么按键不生效?

在解决快捷键失效问题之前,我们需要理解快捷键是如何生效的。

为了更直观的理解,请使用debug模式打开一个新的Eclipse IDE,然后,对org.eclipse.jdt.internal.ui.refactoring.reorg.CopyToClipboardAction这个类的run方法加上断点。在新的EclipseIDE的Package Explorer上选中一个节点,按CTRL+C

在DEBUG视图中显示如下图所示:

我们过一眼这些类名和方法名,如果你是个有经验的程序员,那么,你应当能敏感的发现WorkbenchKeyboard就是是你需要的那个类。

我们看看executeCommand方法,关键代码标记上了红色。

    final boolean executeCommand(final Binding binding, final Event trigger)
throws CommandException {
final ParameterizedCommand parameterizedCommand = binding
.getParameterizedCommand();
if (DEBUG) {
Tracing.printTrace("KEYS", //$NON-NLS-1$
"WorkbenchKeyboard.executeCommand(commandId = '" //$NON-NLS-1$
+ parameterizedCommand.getId() + "', parameters = " //$NON-NLS-1$
+ parameterizedCommand.getParameterMap() + ')');
} // Reset the key binding state (close window, clear status line, etc.)
resetState(false); // Dispatch to the handler.
final IHandlerService handlerService = (IHandlerService) workbench
.getService(IHandlerService.class);
final Command command = parameterizedCommand.getCommand();
final boolean commandDefined = command.isDefined();
final boolean commandHandled = command.isHandled();
command.setEnabled(handlerService.getCurrentState());
final boolean commandEnabled = command.isEnabled(); if (DEBUG && DEBUG_VERBOSE) {
if (!commandDefined) {
Tracing.printTrace("KEYS", " not defined"); //$NON-NLS-1$ //$NON-NLS-2$
} else if (!commandHandled) {
Tracing.printTrace("KEYS", " not handled"); //$NON-NLS-1$ //$NON-NLS-2$
} else if (!commandEnabled) {
Tracing.printTrace("KEYS", " not enabled"); //$NON-NLS-1$ //$NON-NLS-2$
}
} try {
handlerService.executeCommand(parameterizedCommand, trigger);
} catch (final NotDefinedException e) {
// The command is not defined. Forwarded to the IExecutionListener.
} catch (final NotEnabledException e) {
// The command is not enabled. Forwarded to the IExecutionListener.
} catch (final NotHandledException e) {
// There is no handler. Forwarded to the IExecutionListener.
} /*
* Now that the command has executed (and had the opportunity to use the
* remembered state of the dialog), it is safe to delete that
* information.
*/
if (keyAssistDialog != null) {
keyAssistDialog.clearRememberedState();
} return (commandDefined && commandHandled);
}

我们发现了binding这个对象。binding的注释如下:

org.eclipse.jface.bindings.Binding

A binding is a link between user input and the triggering of a particular command. The most common example of a binding is a keyboard shortcut, but there are also mouse and gesture bindings. 

看第一段,就能明白,binding是用来做按键和command绑定的,command是最终的执行。

在debug模式下把鼠标放在binding对象上按F2,如下图所示:

相信你已经看到了关键部分,ActionHandler(CopyToClipAction);

是的,只要这里出现了ActionHandler和你的事件,就说明,绑定成功了。反之,则没有。

2、如何建立绑定关系?

通过上面的debug追溯过程,我们发现绑定服务源自org.eclipse.ui.internal.keys.WorkbenchKeyboard.isPartialMatch方法。代码如下:

    /**
* Determines whether the key sequence partially matches on of the active
* key bindings.
*
* @param keySequence
* The key sequence to check for a partial match; must never be
* <code>null</code>.
* @return <code>true</code> if there is a partial match;
* <code>false</code> otherwise.
*/
private boolean isPartialMatch(KeySequence keySequence) {
return getBindingService().isPartialMatch(keySequence);
}

getBindingService()方法返回一个IBindingService对象,注释如下,关键部分红色粗体字标注:

Provides services related to the binding architecture (e.g., keyboard shortcuts) within the workbench. This service can be used to access the currently active bindings, as well as the current state of the binding architecture. 

This service can be acquired from your service locator: 

     IBindingService service = (IBindingService) getSite().getService(IBindingService.class);

This service is available globally. 

getSite()指的是workbenchPart.getSite(),返回的是org.eclipse.ui.IWorkbenchPartSite对象。它是视图、编辑器(工作区部件)和工作区的原生接口,用于它们之间的交互协同。

你可以在任何地方得到它。

所以,你只需要使用它来注册你需要绑定的command,即可。

常用command譬如"org.eclipse.ui.edit.delete","org.eclipse.ui.edit.copy"

3、更加推荐的方式

很多人为导航器提供了ActionProvider,在默认情况下复制、删除、黏贴等action都会被正确的触发。

如果不行,则需要理解以下几个对象:

a、org.eclipse.jface.action.Action.getActionDefinitionId()

用于匹配commandId,该ID如果和注册了快捷键的commandId匹配,该action的run会被调用。

b、org.eclipse.ui.internal.handlers.IActionCommandMappingService

如果你使用了TextActionHandler之类的工具,你会发现上述内容失效了。

这是因为TextActionHandler对CopyAction等做了多一层封装,这时,你需要使用该类,对actionId和commandId多一次处理。

用于actionId和commandId的绑定,可以通过getSite().getService(IActionCommandMappingService.class)获取

我们可以看以下代码,org.eclipse.ui.SubActionBars.setGlobalActionHandler(String actionID, IAction handler),关键部分已标注红色:

    public void setGlobalActionHandler(String actionID, IAction handler) {
if (actionID == null) {
/*
* Bug 124061. It used to be invalid to pass null as an action id,
* but some people still did it. Handle this case by trapping the
* exception and logging it.
*/
WorkbenchPlugin
.log("Cannot set the global action handler for a null action id"); //$NON-NLS-1$
return;
} if (handler instanceof CommandLegacyActionWrapper) {
// this is a registration of a fake action for an already
// registered handler
WorkbenchPlugin
.log("Cannot feed a CommandLegacyActionWrapper back into the system"); //$NON-NLS-1$
return;
} if (handler instanceof CommandAction) {
// we unfortunately had to allow these out into the wild, but they
// still must not feed back into the system
return;
} if (handler != null) {
// Update the action handlers.
if (actionHandlers == null) {
actionHandlers = new HashMap(11);
}
actionHandlers.put(actionID, handler); // Add a mapping from this action id to the command id.
if (serviceLocator != null) {
String commandId = null;
final IActionCommandMappingService mappingService = (IActionCommandMappingService) serviceLocator
.getService(IActionCommandMappingService.class);
if (mappingService != null) {
commandId = mappingService.getCommandId(actionID);
}
if (commandId == null) {
commandId = handler.getActionDefinitionId();
} // Update the handler activations.
final IHandlerService service = (IHandlerService) serviceLocator
.getService(IHandlerService.class);
Map activationsByActionId = null;
if (activationsByActionIdByServiceLocator == null) {
activationsByActionIdByServiceLocator = new WeakHashMap();
activationsByActionId = new HashMap();
activationsByActionIdByServiceLocator.put(serviceLocator,
activationsByActionId);
} else {
activationsByActionId = (Map) activationsByActionIdByServiceLocator
.get(serviceLocator);
if (activationsByActionId == null) {
activationsByActionId = new HashMap();
activationsByActionIdByServiceLocator.put(
serviceLocator, activationsByActionId);
} else if (activationsByActionId.containsKey(actionID)) {
final Object value = activationsByActionId
.remove(actionID);
if (value instanceof IHandlerActivation) {
final IHandlerActivation activation = (IHandlerActivation) value;
actionIdByCommandId.remove(activation.getCommandId());
if (service != null) {
service.deactivateHandler(activation);
}
activation.getHandler().dispose();
}
} else if (commandId != null
&& actionIdByCommandId.containsKey(commandId)) {
final Object value = activationsByActionId
.remove(actionIdByCommandId.remove(commandId));
if (value instanceof IHandlerActivation) {
final IHandlerActivation activation = (IHandlerActivation) value;
if (service != null) {
service.deactivateHandler(activation);
}
activation.getHandler().dispose();
}
}
} if (commandId != null) {
actionIdByCommandId.put(commandId, actionID);
// Register this as a handler with the given definition id.
// the expression gives the setGlobalActionHandler() a
// priority.
final IHandler actionHandler = new ActionHandler(handler);
Expression handlerExpression = EXPRESSION;
//XXX add new API in next release to avoid down-casting (bug 137091)
if (this instanceof EditorActionBars) {
handlerExpression = ((EditorActionBars)this).getHandlerExpression();
}
if (service != null) {
final IHandlerActivation activation = service
.activateHandler(commandId, actionHandler,
handlerExpression);
activationsByActionId.put(actionID, activation);
}
}
} } else {
if (actionHandlers != null) {
actionHandlers.remove(actionID);
} // Remove the handler activation.
if (serviceLocator != null) {
final IHandlerService service = (IHandlerService) serviceLocator
.getService(IHandlerService.class);
if (activationsByActionIdByServiceLocator != null) {
final Map activationsByActionId = (Map) activationsByActionIdByServiceLocator
.get(serviceLocator);
if ((activationsByActionId != null)
&& (activationsByActionId.containsKey(actionID))) {
final Object value = activationsByActionId
.remove(actionID);
if (value instanceof IHandlerActivation) {
final IHandlerActivation activation = (IHandlerActivation) value;
actionIdByCommandId.remove(activation.getCommandId());
service.deactivateHandler(activation);
activation.getHandler().dispose();
}
}
}
}
}
actionHandlersChanged = true;
}

可以看到,commandId的来源,一是从IActionCommandMappingService,再是从action的getActionDefinitionId()。

如果都不行,再是其他处理(暂不表)。

该方式代码示例如下:

IActionCommandMappingService acms = (IActionCommandMappingService) getViewSite()
.getWorkbenchWindow().getService(
IActionCommandMappingService.class);
String deleteId = acms.getCommandId("delete");
if (deleteId == null)
acms.map("delete", "org.eclipse.ui.edit.delete");

RCP:解决Navigator快捷键不生效的问题的更多相关文章

  1. 解决eclipse快捷键Ctrl+Alt+Down冲突问题办法

    解决eclipse快捷键Ctrl+Alt+Down冲突问题办法 时间:2016-01-18 21:11:08      阅读:376      评论:0      收藏:0      [点我收藏+] ...

  2. linux下jdk环境变量配置深度分析----解决环境变量不生效的问题

    1.linux下jdk环境变量配置 是否需要配置环境变量,主要看java -version 显示的版本是否为你期望的版本 1.1 不需要配置环境变量的情况 使用java -version查看,版本显示 ...

  3. 解决IDEA快捷键 Alt+Insert 失效的问题

    现象 IDEA快捷键 Alt+Inser 失效,单击右键也不出现[Generate]. 这个问题经常出现在重新安装IDEA后. 原因 缺少2个插件 解决办法 在setting中启用这2个插件即可.这2 ...

  4. 【ssh信任关系】解决信任关系不生效问题

    配置的时候遇见点问题,发现即便将id_rsa.pub拷贝到了另一台机器上,信任也没有建立起来. 原因是另外一台机器上目录权限不对,可以通过su root后观察/var/log/message里的日志信 ...

  5. 解决Eclipse快捷键被其他软件占用

    做为一个java攻城狮,eclipse是我最常用的攻城设备,eclipse快捷键 极大的提高了我的开发效率!!!! 前段时间升级了一下我的战斗装备——给电脑的系统盘换成了一个固态硬盘,因此需要重装系统 ...

  6. 19.3.20 解决pycharm快捷键无法使用问题和熟悉git与码云操作流程

    problem:由于Vim插件导致快捷键无法使用: answer:settings→Plugins→搜索到ideaVim→取消选中→apply→重启pycharm: git:创建仓库→生成公钥(ssh ...

  7. Javascript复制内容到剪贴板,解决navigator.clipboard Cannot read property 'writeText' of undefined

    起因 最近帮同事实现了一个小功能--复制文本到剪贴板,主要参考了前端大神阮一峰的博客,根据 navigator.clipboard 返回的 Clipboard 对象的方法 writeText() 写文 ...

  8. php5.6解决curl扩展不生效的问题

    最近在本机安装PHP环境,遇到一个奇粑问题,本地安装的php5.2.php5.3.php5.4都需要做常规设置,即可正常使用.安装php5.5.php5.6时php_curl按各种方法进行配制,都无法 ...

  9. 解决sublime快捷键回车换行问题

    鼠标右键sublime 以管理员身份运行 打开首选项里面的按键绑定用户 将下面的代码粘贴复制 { "keys": ["enter"], "comman ...

随机推荐

  1. [译]关于iOS和OS X废弃的API你需要知道的一切

    原文: Everything You Need to Know about iOS and OS X Deprecated APIs 如你所知,已废弃(Deprecated)的API指的是那些已经过时 ...

  2. 长轮询(long polling)

    HTTP请求不是持续的连接,你请求一次,服务器响应一次,然后就完了.长轮训是一种利用HTTP模拟持续连接的技巧.具体来说,只要页面载入了,不管你需不需要服务器给你响应信息,你都会给服务器发一个Ajax ...

  3. 一个Email

    Email 1.接受表单数据并用单独变量保存起来,判断用户协议,这个是必须同意的.2.判断验证码,利用验证码类Captcha.3.判断用户名,密码,邮箱规则,利用Verify类验证.4.判断唯一性,邮 ...

  4. AC算法 及python实现

    零 导言 软件安全课上,老师讲了AC算法,写个博客,记一下吧. 那么AC算法是干啥的呢? ——是为了解决多模式匹配问题.换句话说,就是在大字符串S中,看看小字符串s1, s2,...有没有出现. AC ...

  5. PHP获取当前服务器信息的基本语句

    下面是PHP获取当前服务器信息的基本语句. PHP程式版本: <?PHP echo PHP_VERSION; ?> ZEND版本: <?PHP echo zend_version() ...

  6. error C2664

    error C2664: “int CWnd::MessageBoxW(LPCTSTR,LPCTSTR,UINT)”: 无法将参数 1 从“const char [19]”转换为“LPCTSTR” n ...

  7. Google高级搜索语法

    Google高级搜索语法   Google搜索果真是一个强悍的不得了的搜索引擎,今天转了一些 google的高级搜索语法 希望能帮助到大家. 一.allinanchor: anchor是一处说明性的文 ...

  8. javascript总结

    javascript:它是一种script脚本语言           脚本语言:就是可以和HTML混合在一起使用的语言,可以用来在IE的客                    户端进行程序编制,从 ...

  9. mysql数据表操作&库操作

    首先登陆mysql:mysql -uroot -proot -P3306 -h127.0.0.1 查看所有的库:show databases; 进入一个库:use database; 显示所在的库:s ...

  10. 解决Android SDK Manager更新、下载速度慢

    hosts文件里面原来的内容不做修改,只是添加内容 方法/步骤 先看看如何加快更新速度,再说如何更新. 首先更新host文件,如图,打开目录 C:\Windows\System32\drivers\e ...