Search in sources :

Example 1 with Presenter

use of org.openide.util.actions.Presenter in project netbeans-rcp-lite by outersky.

the class Utilities method actionsToPopup.

/**
 * Builds a popup menu from actions for provided context specified by
 * <code>Lookup</code>.
 * Takes list of actions and for actions whic are instances of
 * <code>ContextAwareAction</code> creates and uses the context aware instance.
 * Then gets the action presenter or simple menu item for the action to the
 * popup menu for each action (or separator for each 'lonely' null array member).
 *
 * @param actions array of actions to build menu for. Can contain null
 *   elements, they will be replaced by separators
 * @param context the context for which the popup is build
 * @return the constructed popup menu
 * @see ContextAwareAction
 * @since 3.29
 */
public static JPopupMenu actionsToPopup(Action[] actions, Lookup context) {
    // keeps actions for which was menu item created already (do not add them twice)
    Set<Action> counted = new HashSet<Action>();
    // components to be added (separators are null)
    List<Component> components = new ArrayList<Component>();
    for (Action action : actions) {
        if (action != null && counted.add(action)) {
            // switch to replacement action if there is some
            if (action instanceof ContextAwareAction) {
                Action contextAwareAction = ((ContextAwareAction) action).createContextAwareInstance(context);
                if (contextAwareAction == null) {
                    Logger.getLogger(Utilities.class.getName()).log(Level.WARNING, "ContextAwareAction.createContextAwareInstance(context) returns null. That is illegal!" + " action={0}, context={1}", new Object[] { action, context });
                } else {
                    action = contextAwareAction;
                }
            }
            JMenuItem item;
            if (action instanceof Presenter.Popup) {
                item = ((Presenter.Popup) action).getPopupPresenter();
                if (item == null) {
                    Logger.getLogger(Utilities.class.getName()).log(Level.WARNING, "findContextMenuImpl, getPopupPresenter returning null for {0}", action);
                    continue;
                }
            } else {
                // We need to correctly handle mnemonics with '&' etc.
                item = ActionPresenterProvider.getDefault().createPopupPresenter(action);
            }
            for (Component c : ActionPresenterProvider.getDefault().convertComponents(item)) {
                if (c instanceof JSeparator) {
                    components.add(null);
                } else {
                    components.add(c);
                }
            }
        } else {
            components.add(null);
        }
    }
    // Now create actual menu. Strip adjacent, leading, and trailing separators.
    JPopupMenu menu = ActionPresenterProvider.getDefault().createEmptyPopup();
    // has anything been added yet?
    boolean nonempty = false;
    // should there be a separator before any following item?
    boolean pendingSep = false;
    for (Component c : components) {
        try {
            if (c == null) {
                pendingSep = nonempty;
            } else {
                nonempty = true;
                if (pendingSep) {
                    pendingSep = false;
                    menu.addSeparator();
                }
                menu.add(c);
            }
        } catch (RuntimeException ex) {
            // NOI18N
            Exceptions.attachMessage(ex, "Current component: " + c);
            // NOI18N
            Exceptions.attachMessage(ex, "List of components: " + components);
            // NOI18N
            Exceptions.attachMessage(ex, "List of actions: " + Arrays.asList(actions));
            Exceptions.printStackTrace(ex);
        }
    }
    return menu;
}
Also used : Action(javax.swing.Action) ArrayList(java.util.ArrayList) JSeparator(javax.swing.JSeparator) JPopupMenu(javax.swing.JPopupMenu) Presenter(org.openide.util.actions.Presenter) Component(java.awt.Component) JMenuItem(javax.swing.JMenuItem) HashSet(java.util.HashSet)

Example 2 with Presenter

use of org.openide.util.actions.Presenter in project netbeans-rcp-lite by outersky.

the class ToolsAction method generate.

/**
 * Implementation method that regenerates the items in the menu or
 * in the array.
 *
 * @param forMenu true if will be presented in menu or false if presented in popup
 * @param list (can be null)
 */
private static List<JMenuItem> generate(Action toolsAction, boolean forMenu) {
    List<Action> actions = getToolActions();
    List<JMenuItem> list = new ArrayList<JMenuItem>(actions.size());
    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 (Action a : actions) {
        // Retrieve context sensitive action instance if possible.
        if (lookup != null && a instanceof ContextAwareAction) {
            a = ((ContextAwareAction) a).createContextAwareInstance(lookup);
        }
        if (a == null) {
            if (firstItemAdded) {
                separator = true;
            }
        } else {
            boolean isPopup = (a instanceof Presenter.Popup);
            boolean isMenu = (a instanceof Presenter.Menu);
            if (!((forMenu && isMenu) || (!forMenu && isPopup)) && (isMenu || isPopup)) {
                // do not call isEnabled on action that is only popup presenter when building menu (i18nPopupAction)
                continue;
            }
            if (a.isEnabled()) {
                JMenuItem mi;
                if (forMenu && isMenu) {
                    mi = ((Presenter.Menu) a).getMenuPresenter();
                } else if (!forMenu && isPopup) {
                    mi = ((Presenter.Popup) a).getPopupPresenter();
                } else if (!isMenu && !isPopup) {
                    // Generic Swing action.
                    mi = new JMenuItem();
                    Actions.connect(mi, a, !forMenu);
                } else {
                    // Should not be here.
                    continue;
                }
                if (separator) {
                    list.add(null);
                    separator = false;
                }
                list.add(mi);
                firstItemAdded = true;
            }
        }
    }
    return list;
}
Also used : NodeAction(org.openide.util.actions.NodeAction) ContextAwareAction(org.openide.util.ContextAwareAction) Action(javax.swing.Action) SystemAction(org.openide.util.actions.SystemAction) CookieAction(org.openide.util.actions.CookieAction) ArrayList(java.util.ArrayList) ContextAwareAction(org.openide.util.ContextAwareAction) Presenter(org.openide.util.actions.Presenter) Lookup(org.openide.util.Lookup) JMenuItem(javax.swing.JMenuItem)

Aggregations

ArrayList (java.util.ArrayList)2 Action (javax.swing.Action)2 JMenuItem (javax.swing.JMenuItem)2 Presenter (org.openide.util.actions.Presenter)2 Component (java.awt.Component)1 HashSet (java.util.HashSet)1 JPopupMenu (javax.swing.JPopupMenu)1 JSeparator (javax.swing.JSeparator)1 ContextAwareAction (org.openide.util.ContextAwareAction)1 Lookup (org.openide.util.Lookup)1 CookieAction (org.openide.util.actions.CookieAction)1 NodeAction (org.openide.util.actions.NodeAction)1 SystemAction (org.openide.util.actions.SystemAction)1