/*
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
 */
package com.sun.glass.ui;

import com.sun.glass.events.KeyEvent;
import com.sun.glass.ui.delegate.MenuItemDelegate;

public class MenuItem {
    public interface Callback {
        public void action();
    }

    public static final MenuItem Separator = null;

    private final MenuItemDelegate delegate;
    private String title;
    private Callback callback;
    private boolean enabled;
    private boolean checked;
    private int shortcutKey;
    private int shortcutModifiers;

    protected MenuItem(String title) {
        this(title, null);
    }

    protected MenuItem(String title, Callback callback) {
        this(title, callback, KeyEvent.VK_UNDEFINED, KeyEvent.MODIFIER_NONE);
    }

    protected MenuItem(String title, Callback callback,
            int shortcutKey, int shortcutModifiers) {
        this(title, callback, shortcutKey, shortcutModifiers, null);
    }

    protected MenuItem(String title, Callback callback,
            int shortcutKey, int shortcutModifiers, Pixels pixels) {
        this.title = title;
        this.callback = callback;
        this.shortcutKey = shortcutKey;
        this.shortcutModifiers = shortcutModifiers;
        enabled = true;
        checked = false;
        delegate = PlatformFactory.getPlatformFactory().createMenuItemDelegate(this);
        if (!delegate.createMenuItem(title, callback,
                shortcutKey, shortcutModifiers, pixels, enabled, checked)) {
            throw new RuntimeException("MenuItem creation error.");
        }
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        if (delegate.setTitle(title)) {
            this.title = title;
        }
    }

    public Callback getCallback() {
        return callback;
    }

    public void setCallback(Callback callback) {
        if (delegate.setCallback(callback)) {
            this.callback = callback;
        }
    }

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        if (delegate.setEnabled(enabled)) {
            this.enabled = enabled;
        }
    }

    public boolean isChecked() {
        return checked;
    }

    public void setChecked(boolean checked) {
        if (delegate.setChecked(checked)) {
            this.checked = checked;
        }
    }

    /**
     * returns KeyEvent.VK_UNDEFINED if the shortcut is not assigned
     */
    public int getShortcutKey() {
        return shortcutKey;
    }

    /**
     * returns KeyEvent.MODIFIER_NONE if the shortcut is not assigned
     */
    public int getShortcutModifiers() {
        return shortcutModifiers;
    }

    public void setShortcut(int shortcutKey, int shortcutModifiers) {
        if (delegate.setShortcut(shortcutKey, shortcutModifiers)) {
            this.shortcutKey = shortcutKey;
            this.shortcutModifiers = shortcutModifiers;
        }
    }

    public boolean setPixels(Pixels pixels) {
        return (delegate.setPixels(pixels));
    }

    // package private
    MenuItemDelegate getDelegate() {
        return delegate;
    }
}
