package co.dc.aptiv.utils;

import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;

public class ResetJButton extends JButton {
    private static final long serialVersionUID = 39082560987930759L;
    /**
     * 按钮类型-
     * 1：切换状态，样式伴随不同动作切换
     * 2：锁定状态，锁定在按压状态样式
     */
    private int type = 1;
    private String prepressedUrl = "", prenormalUrl = "", preenterUrl = "";

    public ResetJButton(final String normalUrl, final String enterUrl, final String pressedUrl) {
        setFont(new Font("system", Font.PLAIN, 12));
        setBorderPainted(false);
        setFocusPainted(false);
        setContentAreaFilled(false);
        prepressedUrl = pressedUrl;
        prenormalUrl = normalUrl;
        preenterUrl = enterUrl;
        setIcon(new ImageIcon(getClass().getResource(prenormalUrl)));
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                //划过
                if (type == 1) {
                    setIcon(new ImageIcon(getClass().getResource(preenterUrl)));
                }
            }

            @Override
            public void mousePressed(MouseEvent e) {
                //按压
                if (type == 1) {
                    setIcon(new ImageIcon(getClass().getResource(prepressedUrl)));
                }
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                if (type == 1) {
                    setIcon(new ImageIcon(getClass().getResource(prenormalUrl)));
                }
            }

            @Override
            public void mouseExited(MouseEvent e) {
                if (type == 1) {
                    setIcon(new ImageIcon(getClass().getResource(prenormalUrl)));
                }
            }
        });
    }

    public ResetJButton(String normalUrl, String enterUrl, String pressedUrl, int type2) {
        setFont(new Font("system", Font.PLAIN, 12));
        setBorderPainted(false);
        setFocusPainted(false);
        setContentAreaFilled(false);
        prepressedUrl = pressedUrl;
        prenormalUrl = normalUrl;
        preenterUrl = enterUrl;
        setIcon(new ImageIcon(getClass().getResource(prenormalUrl)));
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                //划过
                if (type == 1) {
                    setIcon(new ImageIcon(getClass().getResource(preenterUrl)));
                }
            }

            @Override
            public void mousePressed(MouseEvent e) {
                //按压
                if (type == 1) {
                    setIcon(new ImageIcon(getClass().getResource(prepressedUrl)));
                }
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                if (type == 1) {
                    setIcon(new ImageIcon(getClass().getResource(prenormalUrl)));
                }
            }

            @Override
            public void mouseExited(MouseEvent e) {
                if (type == 1) {
                    setIcon(new ImageIcon(getClass().getResource(prenormalUrl)));
                }
            }
        });
    }

    /**
     * 切换成锁定状态的样式
     */
    public void settype() {
        type = 2;
        setIcon(new ImageIcon(getClass().getResource(prepressedUrl)));
    }

    /**
     * 释放锁定状态
     */
    public void resettype() {
        type = 1;
        setIcon(new ImageIcon(getClass().getResource(prenormalUrl)));
    }

    /**
     * 更换常态下图片显示
     *
     * @param normalUrl
     */
    public void setnormal(String normalUrl) {
        prenormalUrl = normalUrl;
        setIcon(new ImageIcon(getClass().getResource(normalUrl)));
    }

    /**
     * 更换划过状态下图片显示
     *
     * @param normalUrl
     */
    public void setenter(String enterlUrl) {
        preenterUrl = enterlUrl;
    }

    /**
     * 更换anya状态下图片显示
     *
     * @param normalUrl
     */
    public void setpress(String pressurl) {
        prepressedUrl = pressurl;
    }
}
