package Templates.API_Support.Actions_API;

import java.awt.event.ActionEvent;
import javax.swing.*;

import org.openide.util.*;
import org.openide.util.actions.Presenter;
import org.openide.util.actions.SystemAction;

/**
 * Action which just holds a few other SystemAction's for grouping purposes.
 * @author __USER__
 */
public class __Sample_grouping__Action extends SystemAction implements Presenter.Popup {

    public void actionPerformed(ActionEvent ev) {
        assert false : "Should never be called: " + ev;
    }

    public String getName() {
        return NbBundle.getMessage(__NAME__.class, "LBL_Action");
    }

    /*
    protected String iconResource() {
        return "__PACKAGE_AND_NAME_SLASHES__Icon.gif";
    }
    */

    public HelpCtx getHelpCtx() {
        return HelpCtx.DEFAULT_HELP;
        // If you will provide context help then use:
        // return new HelpCtx(__NAME__.class);
    }

    /*
     * Perform extra initialization of this action's singleton.
     * Do not use constructors for this purpose!
    protected void initialize() {
	super.initialize();
	putProperty("someProp", value);
    }
    */

    /** List of system actions to be displayed within this one's toolbar or submenu. */
    private static final SystemAction[] grouped() {
        return new SystemAction[] {
            SystemAction.get(MyFirstAction.class),
            SystemAction.get(MySecondAction.class),
            null, // separator
            SystemAction.get(MyThirdAction.class),
        };
    }

    public JMenuItem getPopupPresenter() {
        return new LazyMenu();
    }

    // Rather than use this for menu or toolbar presenters, simpler to
    // just create subfolders in your XML layer.

    /**
     * Avoids constructing submenu until it will be needed.
     */
    private final class LazyMenu extends JMenu {

        public LazyMenu() {
            super(__NAME__.this.getName());
            /* Conventional not to set an icon here:
            setIcon(__NAME__.this.getIcon());
            */
        }

        public JPopupMenu getPopupMenu() {
            if (getItemCount() == 0) {
                SystemAction[] grouped = grouped();
                for (int i = 0; i < grouped.length; i++) {
                    SystemAction action = grouped[i];
                    if (action == null) {
                        addSeparator();
                    } else if (action instanceof Presenter.Popup) {
                        add(((Presenter.Popup)action).getPopupPresenter());
                    } else {
                        assert false : "Action had no popup presenter: " + action;
                    }
                }
            }
        }
        return super.getPopupMenu();
    }

}
