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?
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 );
}
}
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 F3 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.