package cnp.ew.button;

import cnp.ew.displayer.*;
import cnp.ew.converter.*;
import cnp.ew.layout.*;
import cnp.ew.util.*;
import java.awt.*;
import java.util.*;
import cnp.ew.properties.*;

public class CpLabeledImageTwoStateButtonLc extends CpLabeledImageButtonLc implements CpEditor
{
    public static final int IMAGE_TRUE=0;
    public static final int IMAGE_FALSE=1;
    public static final int IMAGE_TRUE_PRESSING=2;
    public static final int IMAGE_FALSE_PRESSING=3;

    static Color highlightColor = new Color(240, 240, 240);

    boolean state;
    boolean depressedWhenOn=false;
    boolean highlightWhenOn=true;

    public CpLabeledImageTwoStateButtonLc()
    {
        super();
        setHasBorder(false);
        setState(false);
    }

    public CpLabeledImageTwoStateButtonLc(Object labelObjectOrImage)
    {
        super(labelObjectOrImage);
        setHasBorder(false);
        setState(false);
    }

    public CpLabeledImageTwoStateButtonLc(Object labelObject, Image image)
    {
        super(labelObject, image);
        setHasBorder(false);
        setState(false);
    }

    public void setDepressedWhenOn(boolean depressedWhenOn)
    {
        this.depressedWhenOn = depressedWhenOn;
    }

    public boolean getDepressedWhenOn()
    {
        return depressedWhenOn;
    }

    public void setHighlightWhenOn(boolean highlightWhenOn)
    {
        this.highlightWhenOn = highlightWhenOn;
    }

    public boolean getHighlightWhenOn()
    {
        return highlightWhenOn;
    }


    // To Support CpEditor interface; we may add support for this later.
    public int getVerticalAlignment()
    {
        return ALIGN_TOP;
    }
    public void setVerticalAlignment(int verticalAlignment)
    {
    }
    public int getHorizontalAlignment()
    {
        return ALIGN_LEFT;
    }
    public void setHorizontalAlignment(int horizontalAlignment)
    {
    }

    public boolean getState()
    {
        return state;
    }

    public void setState(boolean newState)
    {
        // Ted added test 10/23
        if (state == newState) {
            return;
        }
        state = newState;
        redisplayButton();
    }

    boolean shouldHaveBorder()
    {
        return false;
    }


    int getDefaultLabelOrientation()
    {
        return LABEL_RIGHT;
    }

    void buttonClicked()
    {
        state = !state;
        Boolean bool = new Boolean(state);
        notifyObservers(OBJECT_CHANGING, bool);
        notifyObservers(OBJECT_CHANGED, bool);
    }

    void basicSetImage(Image image, int imageType)
    {
        switch (imageType) {
        case IMAGE_TRUE:
            imageLc.notPressedOnImage = image;
            break;
        case IMAGE_FALSE:
            imageLc.notPressedOffImage = image;
            break;
        case IMAGE_TRUE_PRESSING:
            imageLc.pressedOnImage = image;
            break;
        case IMAGE_FALSE_PRESSING:
            imageLc.pressedOffImage = image;
            break;
        default:
            throw new IllegalArgumentException("Invalid image type");
        }
    }

    void reconfigureImage()
    {
        if (isDisabled()) {
            imageLc.currentState = state ? CpButtonImageLc.STATE_DISABLED_ON : CpButtonImageLc.STATE_DISABLED_OFF;
        } else if (visiblyPressedIn()) {
            imageLc.currentState = state ? CpButtonImageLc.STATE_PRESSED_ON : CpButtonImageLc.STATE_PRESSED_OFF;
        } else {
            imageLc.currentState = state ? CpButtonImageLc.STATE_NOT_PRESSED_ON : CpButtonImageLc.STATE_NOT_PRESSED_OFF;
        }
    }

    void reconfigureLabel()
    {
        labelLc.setText(converter.convert(labelObject));
        labelLc.setShouldDisplayDisabled(isDisabled());
        if (visiblyPressedIn() && shouldShiftWhenPressed) {
            labelLc.setBorderMargin(new Insets(2, 2, -2, -2));
        } else {
            if (state && shouldShiftWhenPressed) {
                labelLc.setBorderMargin(new Insets(1, 1, -1, -1));
            } else {
                labelLc.setBorderMargin(new Insets(0, 0, 0, 0));
            }
        }
    }

    void reconfigure()
    {
        super.reconfigure();
        if (highlightWhenOn && state && labelLc == null) {
            setFillColor(highlightColor);
        } else {
            setFillColor(null);
        }
    }

    public Object getObject()
    {
        return new Boolean(getState());
    }

    public void setObject(Object anObject)
    {
        setState(((Boolean)anObject).booleanValue());
    }

    boolean shouldDisplayBorderRaised()
    {
        if (depressedWhenOn && state) {
            return false;
        } else {
            return super.shouldDisplayBorderRaised();
        }
    }
}

