I am creating a custom toolbar for my RCP application.
As shown in figure I want to have a drop down box with three other text boxes. These are basically the input box and are interdependent. Right now each of these boxes are in separate classes. I want to bring them together in one class so it is easier to create listeners for each other.
protected void fillCoolBar(ICoolBarManager coolBar) { IToolBarManager toolbar = new ToolBarManager(coolBar.getStyle()); coolBar.add(toolbar); Toolbar extraToolBar = new Toolbar("Toolbar"); toolbar.add(extraToolBar); toolbar.add(new Separator()); toolbar.add(new MyCombo("Demo Combo box")); toolbar.add(new Separator()); toolbar.add(new IPaddress("Ip")); toolbar.add(new Separator()); toolbar.add(new Mask("Mask")); toolbar.add(new Separator()); toolbar.add(new Count("Count")); } public class IPaddress extends ControlContribution { Text textBox; public IPaddress(String id) { super(id); // TODO Auto-generated constructor stub } @Override protected Control createControl(Composite parent) { textBox = new Text(parent, SWT.MULTI | SWT.BORDER | SWT.WRAP); textBox.setLayoutData(new GridData(GridData.FILL_BOTH)); textBox.addModifyListener(new ModifyListener(){ public void modifyText(ModifyEvent event) { Text text = (Text) event.widget; System.out.println(text.getText()); } }); return textBox; } }
Thus I want to create a new custom Toolbar will all the functionalities that I want and then stick it to the original. But somehow it only shows an empty bar on the left.
protected Control createControl(Composite parent) { toolBar = new ToolBar(parent, SWT.FLAT |SWT.BORDER); Device dev = toolBar.getDisplay(); try { newi = new Image(dev, "C:\\Users\\RahmanAs\\ChipcoachWorkspace\\ChipCoach\\icons\\FileClose.png"); opei = new Image(dev, "C:\\Users\\RahmanAs\\ChipcoachWorkspace\\ChipCoach\\icons\\FileOpen.png"); } catch (Exception e) { System.out.println("Cannot load images"); System.out.println(e.getMessage()); System.exit(1); } ToolItem item0 = new ToolItem (toolBar, SWT.PUSH); item0.setImage(newi); item0.setText("Hello"); ToolItem item1 = new ToolItem(toolBar, SWT.PUSH); item1.setText("Push"); ToolItem item2 = new ToolItem(toolBar, SWT.PUSH); item2.setText("Pull"); return toolBar; }
I also have run buttons, which I created in the plugin using Vogella's tutorial. But I cannot program their placements in this way. (For example if I want them in the beginning.) Is there a way to create them programmatically?
I think the reason your leftmost
ToolBar
is empty is a layout issue. In my code below, I had a similar "empty"ToolBar
problem when I did not have any buttons located outside the customToolBar
but still in the mainToolBar
. Adding in the "foo" and "bar" buttons fixed the layout issue, but I could not figure out the right calls tolayout()
orpack()
to fix it. I think this may be related to the bug here.I took a swing at creating a similar
ToolBar
and built around the "RCP Mail Template" plugin-project that you can create from the "New Plug-in Project" wizard.To address your first two concerns, I created 3 packages in the example RCP bundle (I called my project "com.bar.foo"):
ContributionControl
and wrapCombo
andText
widgets. These have nothing to do with the data model and just worry about creating widgets.ToolBar
via theorg.eclipse.ui.menus
extension point. They link the data model to theContributionControls
in the first package. The most important class here isToolBarContribution
, which effectively centralizes all of your listeners. This makes it easier for you to link the listeners for the widgets to the same model.Here's the source for the
ToolBarContribution
(note that it addresses your first two concerns because it hooks up the listeners to the model and provides its ownToolBar
to the UI):In addition to this
ToolBar
, I have two separate buttons in the main UIToolBar
, one before, and one after the customToolBar
. Their sources are in the package com.bar.foo.toolBar. Here is the first command:And here is the second one:
Since I didn't know too much about your data, I had to create my own model. The model in the package com.bar.foo.model is just one class:
Now for the com.bar.foo.actions package that contains the
ControlContributions
that go in the customToolBar
. Note that neither of these two classes have anything to do with the model, and they can be re-used elsewhere in your product.The first class just wraps a
Combo
widget. The widget can be initially customized by overriding thecontrolCreated(Combo)
method. I use that in theToolBarContribution
class to add aSelectionListener
and set theCombo
's items. Here's the class:The other class in this package wraps a
Text
widget:I didn't do this, but if you need to further customize the
Text
widget for yourToolBar
, you can override thecreateControl(Composite)
method just like I did when initializing theComboContributionItem
.Now one last thing: I used extensions to customize the
ToolBar
. However, the same logic used byToolBarContribution
applies to yourfillCoolBar(ICoolBarManager)
method or yourcreateControl(Composite)
method, depending on whichToolBar
you ultimately wish to modify.In my case, here's what I added to the end of the plugin's
plugin.xml
:The commands are hooked up so that there's a button for
FooHandler
before the customToolBar
and a button forBarHandler
after the customToolBar
. The order in which these commands are specified in the xml will be reflected in the application. Likewise, the order in which the items are added to the customToolBar
will reflect in your product.Another note on placement: You can make the menuContributions appear in different places by setting a placement in the locationURI's query, e.g.,
toolbar:org.eclipse.ui.main.toolbar?after=additions
. "before" is another placement keyword like "after". More examples of this can be found in this Eclipse help doc.