How to add customizable toolbar in eclipse RCP project
For creating the toolbar dynamic you need to provide a ContributionProvider extension point in the plugin.xml file, lets create a extension point which will look like..
<extension
point="org.eclipse.ui.menus">
<menuContribution
class="com.demo.dynamic.toolbar.ToolbarContributionFactory"
locationURI="toolbar:org.eclipse.ui.main.toolbar">
</menuContribution>
</extension>
you can directly copy paste this extension or you can create by your own..
now create this toolbar contribution factory at the declared location
that class should look like
public class ToolbarContributionFactory extends ExtensionContributionFactory
{
@Override
public void createContributionItems( IServiceLocator serviceLocator, IContributionRoot additions )
{
// list all the elements here for which you want to create toolbar buttons
List elementList = ElementHelper.getInstance().getSelectedElements();
ToolBarManager toolBarManager = new ToolBarManager();
for ( Object object : elementList )
{
CommandContributionItemParameter p = new CommandContributionItemParameter( serviceLocator, "", "com.eclipse.ui.commands.addItem", SWT.PUSH );
p.label = "Test Item";
p.icon = Activator.getImageDescriptor("path of the image file");
CommandContributionItem item = new CommandContributionItem( p );
item.setVisible( true );
toolBarManager.add( item );
}
}
}
so in this way we can write custom java code for populating dynamic toolbar. and when you think toolbar should get updated then call below method.
public class UpdateToolbarHelper
{
public static void run()
{
WorkbenchPage page = (WorkbenchPage)PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IActionBars2 bars = (IActionBars2) page.getActionBars();
ICoolBarManager coolBarManager = bars.getCoolBarManager();
WorkbenchWindow window = (WorkbenchWindow)PlatformUI.getWorkbench().getActiveWorkbenchWindow();
window.fillActionBars( ActionBarAdvisor.FILL_COOL_BAR );
}
}
<extension
point="org.eclipse.ui.menus">
<menuContribution
class="com.demo.dynamic.toolbar.ToolbarContributionFactory"
locationURI="toolbar:org.eclipse.ui.main.toolbar">
</menuContribution>
</extension>
you can directly copy paste this extension or you can create by your own..
now create this toolbar contribution factory at the declared location
that class should look like
public class ToolbarContributionFactory extends ExtensionContributionFactory
{
@Override
public void createContributionItems( IServiceLocator serviceLocator, IContributionRoot additions )
{
// list all the elements here for which you want to create toolbar buttons
List elementList = ElementHelper.getInstance().getSelectedElements();
ToolBarManager toolBarManager = new ToolBarManager();
for ( Object object : elementList )
{
CommandContributionItemParameter p = new CommandContributionItemParameter( serviceLocator, "", "com.eclipse.ui.commands.addItem", SWT.PUSH );
p.label = "Test Item";
p.icon = Activator.getImageDescriptor("path of the image file");
CommandContributionItem item = new CommandContributionItem( p );
item.setVisible( true );
toolBarManager.add( item );
}
}
}
so in this way we can write custom java code for populating dynamic toolbar. and when you think toolbar should get updated then call below method.
public class UpdateToolbarHelper
{
public static void run()
{
WorkbenchPage page = (WorkbenchPage)PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IActionBars2 bars = (IActionBars2) page.getActionBars();
ICoolBarManager coolBarManager = bars.getCoolBarManager();
WorkbenchWindow window = (WorkbenchWindow)PlatformUI.getWorkbench().getActiveWorkbenchWindow();
window.fillActionBars( ActionBarAdvisor.FILL_COOL_BAR );
}
}
Have a happy coding......
No comments:
Post a Comment