Ganesh H
Ganesh H

Reputation: 1787

How to Add Two toolbar Manager into Coolbar or any other alternatives?

I have two views View1 and View2 set1 toolbar items are common for both the views and set2 toolbar items should be displayed only in view2 along with set1.

I am using menu contribution feature of eclipse. I have created two location URIs toolbar:com.omg.views.Objects.custom --> which has common items toolbar:com.omg.views.Objects.custom1 --> which is needed only for View2.

I tried following:

CoolBar coolBar = new CoolBar(toolBarComposite, SWT.NONE);
GridData gridData1 = new GridData(GridData.FILL_BOTH);
coolBar.setLayoutData(gridData1);
CoolBarManager coolManager = new CoolBarManager(SWT.DEFAULT);


IMenuService menuService = (IMenuService)                             
PlatformUI.getWorkbench().getActiveWorkbenchWindow()
        .getService(IMenuService.class);
manager = new ToolBarManager(SWT.FLAT);
menuService.populateContributionManager(manager,
        "toolbar:com.omg.views.Objects.custom"); //$NON-NLS-1$
manager.createControl(coolBar);
manager.getControl().setLayoutData(new GridData(SWT.END, SWT.CENTER,    
true, false));

manager1 = new ToolBarManager(SWT.FLAT);
menuService.populateContributionManager(manager1,
        "toolbar:com.omg.views.Objects.custom1"); //$NON-NLS-1$
manager1.createControl(coolBar);
manager1.getControl().setLayoutData(new GridData(SWT.END, 
 SWT.CENTER, true, false));

coolManager.add(manager);
coolManager.add(manager1);
coolManager.createControl(toolBarComposite);
coolManager.getControl().setLayoutData(new GridData(SWT.END, 
SWT.CENTER, true, false));

unfortunately above code did not work for me, It would be great if I get some suggestion.

Upvotes: 1

Views: 178

Answers (1)

Teodor d
Teodor d

Reputation: 56

I used to have something similar in my application, but when I had to update to Eclipse 4.27 from 3.8.2, I did not find any way of configuring toolbar items programatically through CoolBarManager anymore, the old approach no longer worked.

I managed to get the same result by configuring these toolbar items with actionSets and viewActions in plugin.xml. The first toolbar (toolbar:com.omg.views.Objects.custom) can be configured with an actionSet as you want it to appear globally and the second one (toolbar:com.omg.views.Objects.custom1) can be configured with a viewAction, so that it is only enabled when View1 is visible. You will need to have classes that implement IWorkbenchWindowActionDelegate to do the actions. These plugin items are not complete, some extra fields may need to be completed.

      <extension
         point="org.eclipse.ui.actionSets">
      <actionSet
            id="com.package.actions"
            label="Label1"
            visible="true">
         <menu
               id="com.package.menu1"
               label="Label2"
               path="additions">
         </menu>
         <action
               class="class implementing the action"
               id="com.package.id.Class1"
               label="some label"
               state="false"
               style="push"
               toolbarPath="com.omg.views.Objects.custom"
               tooltip="some label">
            <enablement>
               <systemProperty
                     name="someProperty"
                     value="1"/>
            </enablement>
         </action>
      </actionSet>
   </extension>


   <extension
         point="org.eclipse.ui.viewActions">
      <viewContribution
            targetID="The id for View1"
            id="com.package.id.ViewContribution">
         <menu
               id="com.package.menu2"
               label="Label3"
         </menu>
         <action
               class="class implementing the action"
               enablesFor="1"
               icon="icon.gif"
               id="com.package.id.Class2"
               label="some label"
               style="push"
               toolbarPath="com.omg.views.Objects.custom1"
               tooltip="some label">
            <enablement>
               <systemProperty
                     name="someProperty"
                     value="1"/>
            </enablement>
         </action>
      </actionSet>
   </extension>

Upvotes: 0

Related Questions