Reputation: 35
I wanted to make invisible some action added to the view toolbar based on perspective change. Following is my code for making a particular action invisible. This code does not give me any effect. The code executes fine, but it does not disappear from the toolbar.
IContributionItem[] items = view.getToolbar().getItems();
for (IContributionItem item : items) {
if (item instanceof ActionContributionItem) {
ActionContributionItem actionItem = (ActionContributionItem) item;
if(actionItem.getAction().getId().equals("actionid")){
//view.getToolbar().remove("actionid");
actionItem.setVisible(false);
}
}
}
Upvotes: 1
Views: 538
Reputation: 922
You need to call the update()
method on the toolbar manager.
See this snippet where an item gets invisible after 5 seconds:
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
final ToolBarManager toolBarManager = new ToolBarManager();
final Action action1 = new Action("1") {
};
action1.setId("1");
toolBarManager.add(action1);
final Action action2 = new Action("2") {
};
action2.setId("2");
toolBarManager.add(action2);
toolBarManager.createControl(shell);
display.timerExec(5000, new Runnable() {
@Override
public void run() {
final IContributionItem[] items = toolBarManager.getItems();
for (IContributionItem item : items) {
if (item.getId().equals("1")){
item.setVisible(false);
}
}
toolBarManager.update(true);
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
Upvotes: 1