Issue
The subject topic seems easy enough in other areas of Eclipse plugin development such as ones own toolbars on dialogs. In this instance, I've added a drop-down to the main toolbar within my perspective, via the plugin.xml as follows
<menuContribution
locationURI="toolbar:org.eclipse.ui.main.toolbar">
<toolbar
id="com.company.ui.toolbar2">
// other items removed for clarity
// following is a drop-down
<command
commandId="com.company.ui.mydropdown"
label="%config.label.default"
icon="icons/myIcon.png"
style="pulldown">
<visibleWhen checkEnabled="false">
<reference definitionId="com.company.ui.visibilityTester"/>
</visibleWhen>
</command>
</toolbar>
</menuContribution>
I've got the command defined elsewhere and a handler in the plugin.xml file which currently works as it would for a button.
However, the dropdown is unpopulated. I need a way to dynamically populate for the following reasons:
- Dropdown list content comes from an API
- One of the list items (or clicking the list like a button) launches a dialog that can edit the list
- Closing the dialog means the list needs to be refreshed (rebuilt) and the selected/active item re-evaluated
I also need to be able to hook a user selecting a different list item and calling my API.
I've noticed other examples online try and create the dropdown differently, but I have knowledge gaps about how these wire up. For example, where a Contribution is used, it is not always clear how this links back to the particular definition in the plugin.xml file. I have not seen an example that shows this clearly.
I need to support Eclipse 4.4.2 and later, so contemporary Eclipse 4 examples may not be apt.
Any help appreciated.
Solution
The answer is to add a control to the toolbar section of the menu contributions like:
<menuContribution locationURI="toolbar:org.eclipse.ui.main.toolbar">
<toolbar id="com.company.ui.customToolbar">
<control
class="com.company.ui.MyDropDownContribution"
icon="icons/yourIconHere.png"
label="%config.label.default">
id="theDropdown">
<visibleWhen checkEnabled="false">
<reference definitionId="com.company.ui.isMyTestCaseSatisfied"/>
</visibleWhen>
</control>
</toolbar>
</menuContribution>
And then to implement a contribution class extending WorkbenchWindowControlContribution. My simplified code, which includes the means to launch an EXE from one of the list items (and could be re-used to do something else), essentially is:
public class MyDropDownContribution extends
WorkbenchWindowControlContribution {
private Combo dropdownlist;
private boolean listenerAdded = false;
private String currentlySelected = "Item 1";
private String[] listOfItems = new String[]{ "Item 1", "Item 2", "Item 3", "Launch my EXE" };
private final SelectionAdapter selectionListener = new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
if(e.widget instanceof Combo) {
// if the selection is currentlySelected, do nothing
if(dropdownlist.getText().equals(currentlySelected)) {
return;
}
//if the selection is to launch 'something', do that
if(dropdownlist.getText().equals("Launch my EXE")) {
launchExecutable();
} else {
// otherwise, a new item has been selected, do what you need to
}
}
}
};
private void launchExecutable() {
// place your code to launch your EXE here - you could do some other special case, not just launch EXE
}
@Override
protected Control createControl(Composite parent) {
Composite container = new Composite(parent, SWT.NONE);
GridLayout glContainer = new GridLayout(1, false);
glContainer.marginTop = 0;
glContainer.marginHeight = 0;
glContainer.marginWidth = 0;
container.setLayout(glContainer);
GridData glReader = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1);
glReader.widthHint = 150;
dropdownlist = new Combo(container, SWT.BORDER | SWT.READ_ONLY
| SWT.DROP_DOWN);
dropdownlist.setLayoutData(glReader);
populateDropdown();
toggleComboListener();
return container;
}
private void toggleComboListener() {
if(listenerAdded) {
dropdownlist.removeSelectionListener(selectionListener);
} else {
dropdownlist.addSelectionListener(selectionListener);
}
listenerAdded = !listenerAdded;
}
public void populateDropdown() {
PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {
@Override
public void run() {
dropdownlist.setItems(new String[]{});
for(String itemName : listOfItems) {
dropdownlist.add(itemName);
}
dropdownlist.setText(currentlySelected);
}
});
}
}
Answered By - majixin