/*
 * Created on 28.01.2004
 *
 */

package CH.ifa.draw.util;

import java.awt.Toolkit;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

import de.renew.draw.ui.ontology.AbstractCommand;
import de.renew.propertymanagement.prop.ConfigurablePropertyManager;
import de.renew.propertymanagement.prop.HotkeyType;


/**
 * This class represents a JMenuItem that knows a Command to be triggered
 * if the item is selected by the user.
 *
 * @author Jörn Schumacher
 */
public class CommandMenuItem extends JMenuItem {

    /**
     * Holds the reference to the associated <code>Command</code>.
     **/
    private AbstractCommand _cmd;

    /**
     * Creates a new <code>CommandMenuItem</code> associated with
     * the given command.
     *
     * @param cmd  the associated <code>Command</code>
     **/
    public CommandMenuItem(AbstractCommand cmd) {
        super(cmd.getCommandName());
        this._cmd = cmd;
    }

    /**
     * Creates a new <code>CommandMenuItem</code> associated with
     * the given command, using the given accelerator key.
     * @deprecated this is not to be used, please use {@link CommandMenuItem#CommandMenuItem(AbstractCommand,int,String)}
     *
     * @param cmd  the associated <code>Command</code>
     * @param ms   the accelerator key to use as shortcut for this command.
     **/
    @Deprecated
    public CommandMenuItem(AbstractCommand cmd, int ms) {
        super(cmd.getCommandName());
        KeyStroke ks =
            KeyStroke.getKeyStroke(ms, Toolkit.getDefaultToolkit().getMenuShortcutKeyMaskEx());
        setAccelerator(ks);
        this._cmd = cmd;
        ConfigurablePropertyManager.getInstance().addConfigurableHotkeyProperty(
            cmd.getCommandName(), ks, HotkeyType.COMMAND_HOTKEY, "Uncategorized",
            cmd.getCommandName());
    }

    /**
     * Creates a new <code>CommandMenuItem</code> associated with
     * the given command, using the given accelerator key.
     *
     * @param cmd  the associated <code>Command</code>
     * @param ms   the accelerator key to use as shortcut for this command.
     * @param pluginName the name of the plugin the CommandMenuItem belongs to.
     **/
    public CommandMenuItem(AbstractCommand cmd, int ms, String pluginName) {
        super(cmd.getCommandName());
        KeyStroke ks =
            KeyStroke.getKeyStroke(ms, Toolkit.getDefaultToolkit().getMenuShortcutKeyMaskEx());
        setAccelerator(ks);
        this._cmd = cmd;
        ConfigurablePropertyManager.getInstance().addConfigurableHotkeyProperty(
            cmd.getCommandName(), ks, HotkeyType.COMMAND_HOTKEY, pluginName, cmd.getCommandName());
    }

    /**
     * Creates a new <code>CommandMenuItem</code> associated with
     * the given command, using the given accelerator key.
     * @deprecated this is not to be used, please use {@link CommandMenuItem#CommandMenuItem(AbstractCommand,int,int,String)}
     *
     * @param cmd  the associated <code>Command</code>
     * @param ms   the accelerator key to use as shortcut for this command.
     * @param modifier the modifier that determines the modifying key(s)
     **/
    @Deprecated
    public CommandMenuItem(AbstractCommand cmd, int ms, int modifier) {
        super(cmd.getCommandName());
        KeyStroke ks = KeyStroke.getKeyStroke(ms, modifier);
        setAccelerator(ks);
        this._cmd = cmd;
        ConfigurablePropertyManager.getInstance().addConfigurableHotkeyProperty(
            cmd.getCommandName(), ks, HotkeyType.COMMAND_HOTKEY, "Uncategorized",
            cmd.getCommandName());
    }

    /**
     * Creates a new <code>CommandMenuItem</code> associated with
     * the given command, using the given accelerator key.
     *
     * @param cmd  the associated <code>Command</code>
     * @param ms   the accelerator key to use as shortcut for this command.
     * @param modifier the modifier that determines the modifying key(s)
     * @param pluginName the name of the plugin the CommandMenuItem belongs to.
     **/
    public CommandMenuItem(AbstractCommand cmd, int ms, int modifier, String pluginName) {
        super(cmd.getCommandName());
        KeyStroke ks = KeyStroke.getKeyStroke(ms, modifier);
        setAccelerator(ks);
        this._cmd = cmd;
        ConfigurablePropertyManager.getInstance().addConfigurableHotkeyProperty(
            cmd.getCommandName(), ks, HotkeyType.COMMAND_HOTKEY, pluginName, cmd.getCommandName());
    }

    /**
     * Creates a new CommandMenuItem associated with
     * the given command, using the given KeyStroke as a hotkey
     * @deprecated this is not to be used, please use {@link CommandMenuItem#CommandMenuItem(AbstractCommand,KeyStroke,String)}
     *
     * @param cmd the associated AbstractCommand
     * @param hotkey the KeyStroke used as a hotkey
     */
    @Deprecated
    public CommandMenuItem(AbstractCommand cmd, KeyStroke hotkey) {
        super(cmd.getCommandName());
        this._cmd = cmd;
        if (hotkey != null && hotkey.getKeyCode() != 65535) {
            setAccelerator(hotkey);
        }
    }

    /**
     * Creates a new CommandMenuItem associated with
     * the given command, using the given KeyStroke as a hotkey
     *
     * @param cmd the associated AbstractCommand
     * @param hotkey the KeyStroke used as a hotkey
     * @param pluginName the name of the plugin the CommandMenuItem belongs to.
     */
    public CommandMenuItem(AbstractCommand cmd, KeyStroke hotkey, String pluginName) {
        super(cmd.getCommandName());
        this._cmd = cmd;
        if (hotkey != null) {
            setAccelerator(hotkey);
        }
        ConfigurablePropertyManager.getInstance().addConfigurableHotkeyProperty(
            cmd.getCommandName(), hotkey, HotkeyType.COMMAND_HOTKEY, pluginName,
            cmd.getCommandName());
    }

    /**
     * Gets the command of the CommandMenuItem.
     *
     * @return the command associated with the CommandMenuItem
     */
    public AbstractCommand getCommand() {
        return this._cmd;
    }

    /**
     * Sets the command for this CommandMenuItem.
     *
     * @param c the command to be associated with the CommandMenuItem
     */
    public void setCommand(AbstractCommand c) {
        this._cmd = c;
    }

    /**
     * Disposes the item, its command and uninstalls the ui.
     */
    public void dispose() {
        _cmd = null;
        this.ui.uninstallUI(this);
    }

}