Adding new commands and toolbar contributions programmatically [message #517763] |
Mon, 01 March 2010 23:35 |
Howard Burgett Messages: 12 Registered: November 2009 |
Junior Member |
|
|
Hi,
I'm attempting to create new commands and toolbar contributions at runtime using eclipse 3.5.1, but org.eclipse.jface.action.ContributionManager.insert() throws java.lang.IndexOutOfBoundsException when my plugin is loaded.
Here's the code I'm using:
public void addToolbarContribution()
{
ICommandService commandService =
(ICommandService) getDefault().getWorkbench().getService( ICommandService.class );
command = commandService.getCommand( "my.new.undefined.command" );
command.define( "New Command", "This is created Programatically!", commandService
.getCategory( "com.hmb.plugin.sample.view.command.category" ) );
IHandlerService handlerService = (IHandlerService) getDefault().getWorkbench().getService( IHandlerService.class );
handlerService.activateHandler( command.getId(), new AbstractHandler()
{
public Object execute( ExecutionEvent event ) throws ExecutionException
{
System.out.println( "Command executed !" );
return null;
}
} );
AbstractContributionFactory contribFactory =
new AbstractContributionFactory( "toolbar:org.eclipse.ui.main.toolbar?after=additions", null )
{
public void createContributionItems( IServiceLocator serviceLocator, IContributionRoot additions )
{
CommandContributionItemParameter p =
new CommandContributionItemParameter( serviceLocator, "", command.getId(),
CommandContributionItem.STYLE_PUSH );
// p.label = "Exit the application";
p.icon = Activator.getImageDescriptor( "icons/sample.gif" );
CommandContributionItem item = new CommandContributionItem( p );
item.setVisible( true );
additions.addContributionItem( item, null );
}
};
// create a dynamic toolbar
IMenuService menuService = (IMenuService) getDefault().getWorkbench().getService( IMenuService.class );
menuService.addContributionFactory( contribFactory );
}
This code gets executed during the Activator.start() thread of my plugin. It seems that whatever is wrong with the above code infuences other toolbar contributions (not all) because several other declarative (plugin.xml) toolbar contributions throw the same exception as soon as the above code is executed. When I comment out the code, all other toolbar contributions work fine.
Clearly, I'm missing something about how this should work. Any ideas? Thanks in advance.
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03536 seconds