我在选择粒子集动作时编写了一些用于禁用弹出菜单操作的代码。这些动作正在动态发生。但是,当我们点击特定操作时,会发生什么情况会禁用操作 在popumenu上。但它也不断增加行动。例如,我在弹出菜单中有3个动作。我第一次右键单击并选择任何禁用的操作。下次我右键单击时会有6个动作。第三次它来了9个动作等等.. 这是我面临的问题。以下是我的代码。
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() {
}
}
答案 0 :(得分:1)
您的createActions()方法只应调用一次,并且您的操作列表作为成员属性存储在对象中 - 例如,在createPartControl()方法中调用 - 。
private List<Action> actionsList;
/**
* 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);
actionsList = createActions();
createHookContextMenu();
}
private void fillContextMenu(IMenuManager manager) {
for (Action action : actionsList) {
manager.add(action);
}
// Other plug-ins can contribute there actions here
manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
}