英文:
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>显示的源代码相反(EventManager
← AbstractAction
← Action
),它不公开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 New → Project... → 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, "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 );
}
}
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 (EventManager
← AbstractAction
← Action
) it doesn't expose EventManager
s 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论