Search in sources :

Example 1 with ContextAwareAction

use of org.openide.util.ContextAwareAction in project ACS by ACS-Community.

the class ToolsAction method generate.

/** Implementation method that regenerates the items in the menu or
    * in the array.
    *
    * @param forMenu true if Presenter.Menu should be used false if Presenter.Popup
    * @param list (can be null)
    */
private static List generate(Action toolsAction, boolean forMenu) {
    ActionManager am = (ActionManager) Lookup.getDefault().lookup(ActionManager.class);
    SystemAction[] actions = am.getContextActions();
    List list = new ArrayList(actions.length);
    boolean separator = false;
    // flag to prevent adding separator before actual menu items
    boolean firstItemAdded = false;
    // Get action context.
    Lookup lookup;
    if (toolsAction instanceof Lookup.Provider) {
        lookup = ((Lookup.Provider) toolsAction).getLookup();
    } else {
        lookup = null;
    }
    for (int i = 0; i < actions.length; i++) {
        Action a;
        // Retrieve context sensitive action instance if possible.
        if (lookup != null && actions[i] instanceof ContextAwareAction) {
            a = ((ContextAwareAction) actions[i]).createContextAwareInstance(lookup);
        } else {
            a = actions[i];
        }
        if (a == null) {
            if (firstItemAdded)
                separator = true;
        } else if (forMenu) {
            if (a instanceof Presenter.Menu && a.isEnabled()) {
                if (separator) {
                    list.add(null);
                    separator = false;
                }
                JMenuItem mi = ((Presenter.Menu) a).getMenuPresenter();
                list.add(mi);
                firstItemAdded = true;
            }
        } else if (a instanceof Presenter.Popup && a.isEnabled()) {
            if (separator) {
                list.add(null);
                separator = false;
            }
            JMenuItem mi = ((Presenter.Popup) a).getPopupPresenter();
            list.add(mi);
            firstItemAdded = true;
        }
    }
    return list;
}
Also used : ContextAwareAction(org.openide.util.ContextAwareAction) ContextAwareAction(org.openide.util.ContextAwareAction) ActionManager(org.openide.actions.ActionManager) Lookup(org.openide.util.Lookup) JInlineMenu(org.openide.awt.JInlineMenu)

Example 2 with ContextAwareAction

use of org.openide.util.ContextAwareAction in project blue by kunstmusik.

the class AlignActionsPresenter method getPopupPresenter.

@Override
public JMenuItem getPopupPresenter() {
    JMenu menu = new JMenu(NbBundle.getMessage(AlignActionsPresenter.class, "CTL_AlignActionsPresenter"));
    org.openide.awt.Mnemonics.setLocalizedText(menu, menu.getText());
    ScoreTopComponent scoreTC = (ScoreTopComponent) WindowManager.getDefault().findTopComponent("ScoreTopComponent");
    for (Action action : actions) {
        Action temp = action;
        if (action instanceof ContextAwareAction) {
            temp = ((ContextAwareAction) action).createContextAwareInstance(scoreTC.getLookup());
        }
        menu.add(new JMenuItem(temp));
    }
    return menu;
}
Also used : ScoreTopComponent(blue.ui.core.score.ScoreTopComponent) ContextAwareAction(org.openide.util.ContextAwareAction) Action(javax.swing.Action) AbstractAction(javax.swing.AbstractAction) ContextAwareAction(org.openide.util.ContextAwareAction) JMenuItem(javax.swing.JMenuItem) JMenu(javax.swing.JMenu)

Aggregations

ContextAwareAction (org.openide.util.ContextAwareAction)2 ScoreTopComponent (blue.ui.core.score.ScoreTopComponent)1 AbstractAction (javax.swing.AbstractAction)1 Action (javax.swing.Action)1 JMenu (javax.swing.JMenu)1 JMenuItem (javax.swing.JMenuItem)1 ActionManager (org.openide.actions.ActionManager)1 JInlineMenu (org.openide.awt.JInlineMenu)1 Lookup (org.openide.util.Lookup)1