在Eclipse插件中选择外部上下文菜单时执行额外功能。

huangapple go评论78阅读模式
英文:

Execute extra functionality when selecting an external context menu in Eclipse plugin

问题

I'm creating an Eclipse plugin where I want to add some pre and post processing tasks that need to be executed automatically when a context menu is executed, this context menu is provided by a third-party plugin so it is not possible for me to modify it, does Eclipse have a mechanism that I can use to intercept the call to a context menu to execute some tasks before and after the actual context menu is executed?

英文:

I'm creating an Eclipse plugin where I want to add some pre and post processing tasks that need to be executed automatically when a context menu is executed, this context menu is provided by a third-party plugin so it is not possible for me to modify it, does Eclipse have a mechanism that I can use to intercept the call to a context menu to execute some tasks before and after the actual context menu is executed?

答案1

得分: 1

以下是您要翻译的内容:

"似乎使用EventManager,正如我在问题的评论中所提到的,它不再起作用。

我创建了一个_New_ → Project...Plugin-Project → ... → Template: Menu contribution using 4.x API 并适应了 HelloWorldHandler

  @Execute
  public void execute( @Named( IServiceConstants.ACTIVE_SHELL ) final Shell s ) {

	//MessageDialog.openInformation(s, "E4 Information Dialog", "Hello world from a pure Eclipse 4 plug-in");

	final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
	if ( window instanceof WorkbenchWindow ) {

	  final MenuManager menu = ( (WorkbenchWindow) window ).getMenuManager();
	  final Set<IContributionItem> result = new HashSet<>();
	  collectContributions( menu, result );

	  result.stream()
		  .filter( ci -> ci.getId().equals( "about" ) )
		  .forEach( ci -> {
			final IAction a = ( (ActionContributionItem) ci ).getAction();
			System.out.println( a.getDescription() );
			//((Action)a).addListenerObject(null);
			// The method addListenerObject(Object) from the type EventManager is not visible
		  } );
	}
  }

  private void collectContributions( final MenuManager menu, final Set<IContributionItem> result ) {

	final IContributionItem[] items = menu.getItems();
	for ( final IContributionItem item2 : items ) {

	  IContributionItem item = item2;

	  if ( item instanceof SubContributionItem )
		item = ( (SubContributionItem) item ).getInnerItem();

	  if ( item instanceof MenuManager )
		collectContributions( (MenuManager) item, result );
	  else if ( item instanceof ActionContributionItem && item.isEnabled() )
		result.add( item );
	}
  }

输出

关于 Eclipse 平台

所以,我可以通过其ID获取Action,但与Action文档和在Eclipse中使用<kbd>F3</kbd>显示的源代码相反(EventManagerAbstractActionAction),它不公开EventManager的方法(“EventManager类的addListenerObject(Object)` 方法不可见”)。可能是因为后者的文档如下所示:

> 警告: 不要使用此类!直接使用ListenerList。请参见bug 486067

在我看来,如果第三方插件的供应商不使用ListenerList并在其插件中提供addListener(),我们就没有运气。

英文:

It seems that with EventManager, as mentioned in my comments to the question, it's not working any longer.

I created a NewProject...Plugin-Project → ... → Template: Menu contribution using 4.x API and adapted HelloWorldHandler:

  @Execute
  public void execute( @Named( IServiceConstants.ACTIVE_SHELL ) final Shell s ) {

	//MessageDialog.openInformation(s, &quot;E4 Information Dialog&quot;, &quot;Hello world from a pure Eclipse 4 plug-in&quot;);

	final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
	if ( window instanceof WorkbenchWindow ) {

	  final MenuManager menu = ( (WorkbenchWindow) window ).getMenuManager();
	  final Set&lt;IContributionItem&gt; result = new HashSet&lt;&gt;();
	  collectContributions( menu, result );

	  result.stream()
		  .filter( ci -&gt; ci.getId().equals( &quot;about&quot; ) )
		  .forEach( ci -&gt; {
			final IAction a = ( (ActionContributionItem) ci ).getAction();
			System.out.println( a.getDescription() );
			//((Action)a).addListenerObject(null);
			// The method addListenerObject(Object) from the type EventManager is not visible
		  } );
	}
  }

  private void collectContributions( final MenuManager menu, final Set&lt;IContributionItem&gt; result ) {

	final IContributionItem[] items = menu.getItems();
	for ( final IContributionItem item2 : items ) {

	  IContributionItem item = item2;

	  if ( item instanceof SubContributionItem )
		item = ( (SubContributionItem) item ).getInnerItem();

	  if ( item instanceof MenuManager )
		collectContributions( (MenuManager) item, result );
	  else if ( item instanceof ActionContributionItem &amp;&amp; item.isEnabled() )
		result.add( item );
	}
  }

Output

About Eclipse Platform

So, I can get an Action by its ID but contrary to the doc of Action and contrary to the source displayed with <kbd>F3</kbd> in Eclipse (EventManagerAbstractActionAction) it doesn't expose EventManagers methods ("The method addListenerObject(Object) from the type EventManager is not visible"). Probably since the doc of the latter reads:

> Warning: Do not use this class! Use ListenerList directly. See bug 486067.

AFAICS if the supplier of the third-party plugin doesn't use this ListenerList and offer a addListener() in his plugin we're out of luck.

huangapple
  • 本文由 发表于 2020年8月6日 04:42:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/63273203.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定