2012-09-17 74 views
1

我寫了一些代碼來禁用彈出菜單動作,當我們選擇particulation動作。這些動作即將動態。但是,當我們點擊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() { 

    } 
} 

回答

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)); 
}