Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » Adding new commands and toolbar contributions programmatically
Adding new commands and toolbar contributions programmatically [message #517763] Mon, 01 March 2010 23:35 Go to next message
Howard Burgett is currently offline 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.


Re: Adding new commands and toolbar contributions programmatically [message #517764 is a reply to message #517763] Mon, 01 March 2010 23:39 Go to previous messageGo to next message
Howard Burgett is currently offline Howard Burgett
Messages: 12
Registered: November 2009
Junior Member
BTW - the exception gets thrown at the line:

 menuService.addContributionFactory( contribFactory );



Here's the stack for the first exception:

java.lang.IndexOutOfBoundsException: inserting sed.tabletree.separator.1 at 6
at org.eclipse.jface.action.ContributionManager.insert(ContributionManager.java:312)
at org.eclipse.ui.internal.menus.WorkbenchMenuService$5.run(WorkbenchMenuService.java:612)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
at org.eclipse.ui.internal.menus.WorkbenchMenuService.processAdditions(WorkbenchMenuService.java:622)
at org.eclipse.ui.internal.menus.WorkbenchMenuService.addContributionsToManager(WorkbenchMenuService.java:683)
at org.eclipse.ui.internal.menus.WorkbenchMenuService.populateContributionManager(WorkbenchMenuService.java:669)
at org.eclipse.ui.internal.menus.WorkbenchMenuService.addContributionsToManager(WorkbenchMenuService.java:729)
at org.eclipse.ui.internal.menus.WorkbenchMenuService.addContributionFactory(WorkbenchMenuService.java:477)
at com.hmb.plugin.sample.view.Activator.addToolbarContribution(Activator.java:130)
at com.hmb.plugin.sample.view.Activator.start(Activator.java:51)
at org.eclipse.osgi.framework.internal.core.BundleContextImpl$1.run(BundleContextImpl.java:783)
at java.security.AccessController.doPrivileged(Native Method)
at org.eclipse.osgi.framework.internal.core.BundleContextImpl.startActivator(BundleContextImpl.java:774)
at org.eclipse.osgi.framework.internal.core.BundleContextImpl.start(BundleContextImpl.java:755)
at org.eclipse.osgi.framework.internal.core.BundleHost.startWorker(BundleHost.java:352)
at org.eclipse.osgi.framework.internal.core.AbstractBundle.start(AbstractBundle.java:280)
at org.eclipse.osgi.framework.util.SecureAction.start(SecureAction.java:408)
at org.eclipse.core.runtime.internal.adaptor.EclipseLazyStarter.postFindLocalClass(EclipseLazyStarter.java:111)
at org.eclipse.osgi.baseadaptor.loader.ClasspathManager.findLocalClass(ClasspathManager.java:449)
at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.findLocalClass(DefaultClassLoader.java:211)
at org.eclipse.osgi.internal.loader.BundleLoader.findLocalClass(BundleLoader.java:381)
at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:457)
at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:410)
at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:398)
at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:105)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at org.eclipse.osgi.internal.loader.BundleLoader.loadClass(BundleLoader.java:326)
at org.eclipse.osgi.framework.internal.core.BundleHost.loadClass(BundleHost.java:231)
at org.eclipse.osgi.framework.internal.core.AbstractBundle.loadClass(AbstractBundle.java:1193)
at org.eclipse.core.internal.registry.osgi.RegistryStrategyOSGI.createExecutableExtension(RegistryStrategyOSGI.java:160)
at org.eclipse.core.internal.registry.ExtensionRegistry.createExecutableExtension(ExtensionRegistry.java:874)
at org.eclipse.core.internal.registry.ConfigurationElement.createExecutableExtension(ConfigurationElement.java:243)
at org.eclipse.core.internal.registry.ConfigurationElementHandle.createExecutableExtension(ConfigurationElementHandle.java:51)
at org.eclipse.ui.internal.WorkbenchPlugin$1.run(WorkbenchPlugin.java:267)
at org.eclipse.swt.custom.BusyIndicator.showWhile(BusyIndicator.java:70)
at org.eclipse.ui.internal.WorkbenchPlugin.createExtension(WorkbenchPlugin.java:263)
at org.eclipse.ui.internal.registry.ViewDescriptor.createView(ViewDescriptor.java:63)
at org.eclipse.ui.internal.ViewReference.createPartHelper(ViewReference.java:324)
at org.eclipse.ui.internal.ViewReference.createPart(ViewReference.java:226)
at org.eclipse.ui.internal.WorkbenchPartReference.getPart(WorkbenchPartReference.java:595)
at org.eclipse.ui.internal.Perspective.showView(Perspective.java:2229)
at org.eclipse.ui.internal.WorkbenchPage.busyShowView(WorkbenchPage.java:1067)
at org.eclipse.ui.internal.WorkbenchPage$20.run(WorkbenchPage.java:3816)
at org.eclipse.swt.custom.BusyIndicator.showWhile(BusyIndicator.java:70)
at org.eclipse.ui.internal.WorkbenchPage.showView(WorkbenchPage.java:3813)
at org.eclipse.ui.internal.WorkbenchPage.showView(WorkbenchPage.java:3789)
at org.eclipse.ui.handlers.ShowViewHandler.openView(ShowViewHandler.java:165)
at org.eclipse.ui.handlers.ShowViewHandler.openOther(ShowViewHandler.java:109)
at org.eclipse.ui.handlers.ShowViewHandler.execute(ShowViewHandler.java:77)
at org.eclipse.ui.internal.handlers.HandlerProxy.execute(HandlerProxy.java:294)
at org.eclipse.core.commands.Command.executeWithChecks(Command.java:476)
at org.eclipse.core.commands.ParameterizedCommand.executeWithChecks(ParameterizedCommand.java:508)
at org.eclipse.ui.internal.handlers.HandlerService.executeCommand(HandlerService.java:169)
at org.eclipse.ui.internal.handlers.SlaveHandlerService.executeCommand(SlaveHandlerService.java:241)
at org.eclipse.ui.internal.ShowViewMenu$3.run(ShowViewMenu.java:141)
at org.eclipse.jface.action.Action.runWithEvent(Action.java:498)
at org.eclipse.jface.action.ActionContributionItem.handleWidgetSelection(ActionContributionItem.java:584)
at org.eclipse.jface.action.ActionContributionItem.access$2(ActionContributionItem.java:501)
at org.eclipse.jface.action.ActionContributionItem$5.handleEvent(ActionContributionItem.java:411)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1003)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3880)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3473)
at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2405)
at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2369)
at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2221)
at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:500)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:493)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:113)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:194)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:368)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:559)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:514)
at org.eclipse.equinox.launcher.Main.run(Main.java:1311)
at org.eclipse.equinox.launcher.Main.main(Main.java:1287)


Re: Adding new commands and toolbar contributions programmatically [message #517793 is a reply to message #517764] Tue, 02 March 2010 07:32 Go to previous messageGo to next message
Prakash G.R. is currently offline Prakash G.R.
Messages: 621
Registered: July 2009
Senior Member
On 02/03/10 5:09 AM, Howard Burgett wrote:
> BTW - the exception gets thrown at the line:
>
>
> menuService.addContributionFactory( contribFactory );
>
>
>
> Here's the stack for the first exception:
>
>
> java.lang.IndexOutOfBoundsException: inserting sed.tabletree.separator.1
> at 6


https://bugs.eclipse.org/bugs/show_bug.cgi?id=205747


- Prakash
Platform UI Team, IBM

www.eclipse-tips.com
Re: Adding new commands and toolbar contributions programmatically [message #518036 is a reply to message #517763] Tue, 02 March 2010 20:07 Go to previous messageGo to next message
Paul Webster is currently offline Paul Webster
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

Howard Burgett wrote:
> 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.

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

In addition to what Prakash mentioned, you have to return a ToolBar for
the uri toolbar:org.eclipse.ui.main.toolbar?after=additions.

Or you can create a toolbar using your plugin.xml with a known ID and
contribute commands to that.

PW




--
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Menu_Contributions
http://wiki.eclipse.org/Menus_Extension_Mapping
http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse .platform.doc.isv/guide/workbench.htm


Re: Adding new commands and toolbar contributions programmatically [message #518056 is a reply to message #517763] Tue, 02 March 2010 21:46 Go to previous message
Howard Burgett is currently offline Howard Burgett
Messages: 12
Registered: November 2009
Junior Member
Thanks for the great responses!

Using the same code with a toolbar created in plugin.xml for a view that I have works fine. I'll look forward to the bug fix that allows me to do the same with main toolbar.
Previous Topic:Global Code-Templates
Next Topic:Can not set ContributionItem size for dynamic toolbar contributions
Goto Forum:
  


Current Time: Mon Sep 22 12:15:30 GMT 2014

Powered by FUDForum. Page generated in 0.11517 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software