2016-05-10 41 views
0

我正在開發一個NetBeans插件,它需要使用「動態」菜單,因爲目前必須在專用於某個菜單項及其操作的類中進行硬編碼。不過,我希望允許用戶通過插件提供的UI來更改菜單項的值(他們所做的 - 這將在主方法中處理變量,我不允許用戶寫入主方法本身的代碼 - 它們被稱爲和它們相關的鍵盤快捷鍵)以及添加和刪除菜單項。我的插件可能會從文件中讀取這些首選項,理想情況下只有一個類可以創建所有這些動態菜單項,並在首選項文件中定義它們的值。如何在NetBeans插件中創建動態菜單?

據我所知,有這樣做的方法,但我可以找到它的唯一真正的信息是this threadthe bugzilla page它鏈接到目前似乎是下來,所以這並沒有真正幫助我...那麼怎樣才能用動態菜單項創建一個動態菜單呢?

我有NetBeans版本8.1和JDK8。另外,當我說「動態菜單項」或類似的時候,我正在討論從菜單中提供給你的實際選項(無論插件在那裏做什麼,在我的情況下它會將某些數據複製到剪貼板時點擊其中一個選項)。

回答

1

我在我的netbeans RCP應用程序中使用動態菜單。下面是一般格式我使用添加動態菜單項,右鍵菜單,工具欄,菜單欄...

import java.awt.event.ActionEvent; 
import java.util.ArrayList; 
import java.util.List; 
import javax.swing.AbstractAction; 
import javax.swing.Action; 
import javax.swing.JComponent; 
import javax.swing.JMenu; 
import javax.swing.JMenuItem; 
import javax.swing.JPopupMenu; 
import javax.swing.JSeparator; 
import org.openide.awt.ActionID; 
import org.openide.awt.ActionReference; 
import org.openide.awt.ActionReferences; 
import org.openide.awt.ActionRegistration; 
import org.openide.awt.Actions; 
import org.openide.awt.DynamicMenuContent; 
import org.openide.util.actions.Presenter; 

/* Register your dynamic action using annotations (perferred over layer.xml entries) 
@ActionID(id = "ExampleDynamicMenu", category = "Edit") 
@ActionRegistration(lazy = false, displayName = "ExampleDynamicMenu") 
@ActionReferences({ 
    @ActionReference(path = "Loaders/folder/any/Actions", position = 666), 
    @ActionReference(path = "Loaders/content/unknown/Actions", position = 666), 
    @ActionReference(path = "Loaders/text/xml/Actions", position = 666), 
    @ActionReference(path = "Projects/Actions", position = 666), 
    @ActionReference(path = "Editors/TabActions", position = 666) 
}) 
*/ 
public class ExampleDynamicMenu extends AbstractAction implements DynamicMenuContent, Presenter.Popup { 

    @Override 
    public JMenuItem getPopupPresenter() { 
     JMenu menu = new JMenu(this); 
     JComponent[] menuItems = createMenu(); 
     for (JComponent item : menuItems) { 
      menu.add(item); 
     } 
     return menu; 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     // does nothing, this is a popup menu 
    } 

    @Override 
    public JComponent[] getMenuPresenters() { 
     return createMenu(); 
    } 

    @Override 
    public JComponent[] synchMenuPresenters(JComponent[] items) { 
     return createMenu(); 
    } 

    private JComponent[] createMenu() { 
     List<JComponent> items = new ArrayList<>(20); 

     //TODO: Load the list of actions stored in the preferences, and for each action add a menu-item to the dynamic menu. 
     // Note: You preferences can simply store the Action's category and id; then you can always get the action instances using 
     // Action action = Actions.forID(category_string, id_string); 
     List<Action> actionsList = YOUR_PREFERENCES_GETTER.getActions(); //You have to figure out how to store and retrieve user's preferred actions list yourself :) 
     for (Action action : actionsList) { 
      if (action == null) { //Assume null means add a separator 
       if (items.size() > 0 && !(items.get(items.size() - 1) instanceof JSeparator)) { //Only add separators after actions. 
        items.add(new JPopupMenu.Separator()); 
       } 
      } else { //Convert action to menu item, and build your dynamic menu 
       items.add(toMenuItem(action)); 
      } 
     } 
     return items.toArray(new JComponent[items.size()]); 
    } 

    /** 
    * Creates a menu item from an action. 
    * 
    * @param action an action 
    * @return JMenuItem 
    */ 
    private static JMenuItem toMenuItem(Action action) { 
     JMenuItem item; 
     if (action instanceof Presenter.Menu) { 
      item = ((Presenter.Menu) action).getMenuPresenter(); 
     } else if (action instanceof Presenter.Popup) { 
      item = ((Presenter.Popup) action).getPopupPresenter(); 
     } else { 
      item = new JMenuItem(); 
      Actions.connect(item, action, false); 
     } 
     return item; 
    } 
} 
+0

我看到的代碼提到了「彈出」 SA很多,唯一的彈出我會需要將是偏好面板,但我可能會做到這一點,所以我應該更改Presenter等到「菜單」或「Popup」是什麼意思,目前在做什麼? –

+0

請參閱api解釋:https://netbeans.org/download/5_5/org-openide-util/org/openide/util/actions/Presenter.Popup.html – sproger

+0

對不起,我到底會替換'Loaders/folder /任何/行動'例如與我以及以什麼格式必須提供清單與偏好的getter? –