//******************************************************************************
// fjMenuItem.java:	
//
// written by Wiebe de Jong (August 7, 1997)
//
// Aug11/97     function hOffset added
// Aug11/97     constructor parameter p_menuStyle added
// Aug11/97     menuStyle 1 & 2 added
// Aug19/97     parameter for label added
// Aug19/97     function label() added
//
//******************************************************************************
import java.awt.*;
import java.applet.Applet;
import java.applet.AppletContext;
import java.net.URL;

//==============================================================================
// Main Class for fjMenuItem
//
//==============================================================================
public class fjMenuItem 
{
    String itemText; // button text
    URL itemUrl; // destination url
    String itemTarget; // destination frame
    String itemLabel; // item label
    int x; // starting x value of bounding box
    int y; // starting y value of bounding box
    int xd; // x width of bounding box
    int yd; // y width of bounding box
    int xf; // starting x value of text
    int yf; // starting y value of text
    Color bgColor; // background color
    Color lnColor; // line color
    Color itemColor; // item color
    Color selectColor; // selected item color
    Color fontColor; // text color
    Color fontselColor; // selected text color
    fjMenu mainMenu; // pointer to parent
    Font itemFont; // text font
    FontMetrics itemMetrics; // text metrics
    int menuStyle; // text stype

    boolean selected; // Q: is this item highlighted?

    // fjMenuItem Class Constructor
    //--------------------------------------------------------------------------
    public fjMenuItem(String p_text, String p_url, String p_target, String p_label,
        int p_x, int p_y, int p_xd, int p_yd, int p_xf, int p_yf,
        Color p_bgColor, Color p_lnColor, Color p_itemColor, Color p_selectColor, Color p_fontColor, Color p_fontselColor, 
        Font p_font, FontMetrics p_fontMetrics, int p_menuStyle, fjMenu p_mainMenu)
    {
        itemText = p_text;
        try {
            itemUrl = new URL(((Applet)p_mainMenu).getDocumentBase(), p_url);
        } catch (Exception e) {
            itemUrl = null;
        }

        itemTarget = p_target;
        itemLabel = p_label;
        x = p_x;
        y = p_y;
        xd = p_xd;
        yd = p_yd;
        xf = p_xf;
        yf = p_yf;
        bgColor = p_bgColor;
        lnColor = p_lnColor;
        itemColor = p_itemColor;
        selectColor = p_selectColor;
        fontColor = p_fontColor;
        fontselColor = p_fontselColor;
        itemFont = p_font;
        itemMetrics = p_fontMetrics;
        menuStyle = p_menuStyle;
        mainMenu = p_mainMenu;

        selected = false;
    }

    // fjMenuItem Horizontal offset
    //--------------------------------------------------------------------------
    public void xOffset(int xOff)
    {
        x = x + xOff;
        xf = xf + xOff;
    }

    // fjMenuItem Paint Handler
    //--------------------------------------------------------------------------
    public void paint(Graphics g)
    {
        // background color
        if (selected)
            g.setColor(selectColor);
        else
            g.setColor(itemColor);

        // draw background
        switch (menuStyle)
        {
            case 0: g.fillRect(x, y, xd, yd); break;
            case 1: g.fillOval(x, y, xd, yd); break;
            case 2: g.fillRoundRect(x, y, xd, yd, yd, yd); break;
            default: g.fillRect(x, y, xd, yd);
        }

        // font and font color
        g.setFont(itemFont);
        if (selected)
            g.setColor(fontselColor);
        else
            g.setColor(fontColor);

        // draw text
        switch (menuStyle)
        {
            default: g.drawString(itemText, xf, yf);
        }
    }

    // fjMenuItem: Does button contain point?
    //--------------------------------------------------------------------------
    public boolean contains(int mx, int my)
    {
        if (mx < x || mx > x + xd || my < y || my > y + yd)
            return false;
        return true;
    }

    // fjMenuItem: Select button
    //--------------------------------------------------------------------------
    public void select()
    {
        selected = true;
    }

    // fjMenuItem: Unselect button
    //--------------------------------------------------------------------------
    public void unselect()
    {
        selected = false;
    }

    // fjMenuItem: description label
    //--------------------------------------------------------------------------
    public String label()
    {
        return itemLabel;
    }

    // fjMenuItem: Activate button
    //--------------------------------------------------------------------------
    public void activate()
    {
        if (itemUrl != null)
        {
            if (itemTarget != null)
            {
                ((Applet)mainMenu).getAppletContext().showDocument(itemUrl, itemTarget);
                return;
            }
            ((Applet)mainMenu).getAppletContext().showDocument(itemUrl);
        }
    }
}

