eclipse rcp 中获取到定义的handler
想在任何view中调用这个handler事件,方法如下:
/** * Execute extension. * @param commandId */ private void executeExtension(final String commandId, Object data) { ISafeRunnable runnable = new ISafeRunnable() { @Override public void handleException(Throwable e) { log.error(e.getMessage(), e); } @Override public void run() throws Exception { ICommandService commandService = SqlInspectionView.this.getSite().getService(ICommandService.class); Command command = commandService.getCommand(commandId); Event trigger = new Event(); trigger.data = data; // EvaluationContext context = new EvaluationContext(null, ""); IHandlerService handlerService = ((IHandlerService) SqlInspectionView.this.getSite().getService(IHandlerService.class)); ExecutionEvent executionEvent = handlerService.createExecutionEvent(command, trigger); // command.executeWithChecks(executionEvent); // 这种方式调用不会传递数据过去 handlerService.executeCommand("org.jkiss.dbeaver.core.sqlinspection.sql.doubleclick.edit", trigger); // 通过trigger把data数据传递过去 } }; SafeRunner.run(runnable); }
获取一个扩展点下指定的handler类型:
/** * Gets the extension registry. * @return the extension registry */ private IExtensionRegistry getExtensionRegistry() { return Platform.getExtensionRegistry(); }
IConfigurationElement[] config = this.getExtensionRegistry().getConfigurationElementsFor("org.eclipse.ui.handlers"); try { for (IConfigurationElement e : config) { System.out.println("Evaluating extension " + e.getName()); final Object object = e.createExecutableExtension("class"); if (object instanceof DoubleClickEditDialogHandler) { // handler = (IHandler) object; //executeExtension(object); } } } catch (CoreException ex) { System.err.println(ex.getMessage()); }
handler传递数据 https://wiki.eclipse.org/Platform_Command_Framework
/** * Execute extension. * @param commandId */ private void executeExtension(final String commandId, Object data) { ISafeRunnable runnable = new ISafeRunnable() { @Override public void handleException(Throwable e) { log.error(e.getMessage(), e); }
@Override public void run() throws Exception { ICommandService commandService = SqlInspectionView.this.getSite().getService(ICommandService.class); Command command = commandService.getCommand(commandId); Event trigger = new Event(); trigger.data = data;// EvaluationContext context = new EvaluationContext(null, ""); IHandlerService handlerService = ((IHandlerService) SqlInspectionView.this.getSite().getService(IHandlerService.class)); ExecutionEvent executionEvent = handlerService.createExecutionEvent(command, trigger);// command.executeWithChecks(executionEvent); // 这种方式调用不会传递数据过去 handlerService.executeCommand("org.jkiss.dbeaver.core.sqlinspection.sql.doubleclick.edit", trigger); // 通过trigger把data数据传递过去 } }; SafeRunner.run(runnable); }
本文来自博客园,作者:margo,转载请注明原文链接:https://www.cnblogs.com/ZMargo/articles/12937798.html