1

当我们选择分词动作时,我编写了一些代码来禁用弹出菜单动作。这些动作是动态的。但是,当我们单击弹出菜单上的特定操作时,会发生什么禁用操作。但它也不断增加动作。例如,我在弹出菜单中有 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() {

    }
}
4

1 回答 1

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));
}
于 2012-09-17T11:37:36.720 回答