/*
 * Decompiled with CFR 0_132.
 */
package useful;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Stroke;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.ButtonModel;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.border.Border;
import javax.swing.plaf.ButtonUI;
import javax.swing.plaf.basic.BasicButtonUI;
import main.ChronoPanel;
import main.ChronoWindow;
import main.Main;
import useful.ButtonTabComponent;

public class TabButton
extends JButton
implements MouseListener {
    private static final long serialVersionUID = 1L;
    private ButtonTabComponent comp;

    public TabButton(ButtonTabComponent comp) {
        int size = 17;
        this.comp = comp;
        this.setPreferredSize(new Dimension(size, size));
        this.setUI(new BasicButtonUI());
        this.setContentAreaFilled(false);
        this.setFocusable(false);
        this.setBorder(BorderFactory.createEtchedBorder());
        this.setBorderPainted(false);
        this.addMouseListener(this);
        this.setRolloverEnabled(true);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g.create();
        if (this.getModel().isPressed()) {
            g2.translate(1, 1);
        }
        g2.setStroke(new BasicStroke(2.0f));
        g2.setColor(new Color(50, 50, 50));
        if (this.getModel().isRollover()) {
            g2.setColor(Color.RED);
        }
        int delta = 6;
        g2.drawLine(delta, delta, this.getWidth() - delta, this.getHeight() - delta);
        g2.drawLine(this.getWidth() - delta, delta, delta, this.getHeight() - delta);
        g2.dispose();
    }

    @Override
    public void mouseClicked(MouseEvent arg0) {
    }

    @Override
    public void mouseEntered(MouseEvent arg0) {
    }

    @Override
    public void mouseExited(MouseEvent arg0) {
    }

    @Override
    public void mousePressed(MouseEvent arg0) {
        int i = ChronoWindow.tabs.indexOfTabComponent(this.comp);
        if (i != -1) {
            if (ChronoWindow.chrono.get((int)i).modifToSave) {
                int option = JOptionPane.showConfirmDialog(null, "Save chronogram before closing ?", "Closing file", 1, 3);
                if (option != 2 && option != -1) {
                    if (option == 0) {
                        Main.window.save(false, i);
                    }
                    this.close(i);
                }
            } else {
                this.close(i);
            }
        }
    }

    public void close(int i) {
        ChronoWindow.tabs.remove(i);
        ChronoWindow.chrono.remove(i);
    }

    @Override
    public void mouseReleased(MouseEvent arg0) {
    }
}

