I wrote some code for disabling the popup menu actions when we choose the particulation action.Those actions are coming dynamically. But what happens it is disabling the actions when we click the particular action
on the popumenu. But also it keep increasing the actions. For example I have 3 actions in the popup menu. First time when I right click and choose any actions it is disabling. Next time when I right click it comes 6 actions. Third time it comes 9 actions and so on..
This is the problem I am facing. Following is my code.
package rcppopumenu;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;
public class View extends ViewPart {
public static final String ID = "RCPPOPUMENU.view";
private Action action1;
private Action action2;
private TableViewer viewer = null;
private Composite composite = null;
private Button button = null;
/**
* This is a callback that will allow us to create the viewer and initialize
* it.
*/
public void createPartControl(Composite parent) {
composite = parent;
composite.setLayout(new RowLayout());
button = new Button(composite, SWT.NONE);
button.setText("Hello");
// viewer = new TableViewer(composite);
// createActions();
createHookContextMenu();
}
private void createHookContextMenu() {
MenuManager menuMgr = new MenuManager("#PopupMenu");
menuMgr.setRemoveAllWhenShown(true);
menuMgr.addMenuListener(new IMenuListener() {
public void menuAboutToShow(IMenuManager manager) {
View.this.fillContextMenu(manager);
}
});
Menu menu = menuMgr.createContextMenu(button);
button.setMenu(menu);
getSite()
.registerContextMenu(menuMgr, getSite().getSelectionProvider());
}
private void fillContextMenu(IMenuManager manager) {
List<Action> createActions = createActions();
for (Action action : createActions) {
manager.add(action);
}
// Other plug-ins can contribute there actions here
manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
}
List<Action> list = new ArrayList<Action>();
private List<Action> createActions() {
// list.clear();
String[] array = { "a", "b", "c" };
for (final String str : array) {
Action action1 = new Action() {
public void run() {
System.out.println(str);
setEnabled(false);
}
};
action1.setText(str);
action1.setToolTipText("Action 1 tooltip");
action1.setImageDescriptor(PlatformUI.getWorkbench()
.getSharedImages()
.getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));
if (!list.contains(action1)) {
list.add(action1);
}
}
return list;
}
/**
* Passing the focus request to the viewer's control.
*/
public void setFocus() {
}
}
Your createActions() method should be called only once, and your actions list stored in your object as a member attribute – Called in the createPartControl() method for example – .