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());
        }

  

https://stackoverflow.com/questions/12724511/how-to-get-an-ihandlerservice-object-in-eclipse-4-rcp/17405293

 

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);    }

posted @ 2020-05-22 16:09  margo  阅读(375)  评论(0编辑  收藏  举报