package org.kne.cloud.network.klalb.ui;

import java.awt.event.*;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
class TabPanel extends JPanel{
    private JLabel title;
    private CloseButton closebutton;
    private final JTabbedPane pane;

    public TabPanel(String s,JTabbedPane pane,Icon icon,String tip, boolean closable){
        super(new BorderLayout());
        title=new JLabel(s);
        title.setIcon(icon);
        title.setToolTipText(tip);
        setToolTipText(tip);
        this.pane=pane;
        add(title,BorderLayout.CENTER);
        if(closable) {
        closebutton=new CloseButton();
        add(closebutton,BorderLayout.EAST);
        }
        title.setBorder(BorderFactory.createEmptyBorder(0, 3, 0, 3));
        setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
        setOpaque(false);
    }

    private class CloseButton extends JButton {
        private ImageIcon icon;
        public CloseButton(){
        	//图片的位置随自身情况而定
            try {
				icon=new ImageIcon(ImageIO.read( getClass().getResource("/assets/close.png")).getScaledInstance(10, 10, Image.SCALE_SMOOTH));
            setSize(icon.getImage().getWidth(null),icon.getImage().getHeight(null));
            setIcon(icon);
			} catch (IOException e1) {
				// TODO 自动生成的 catch 块
				e1.printStackTrace();
			}
            setContentAreaFilled(false);
            setBorder(null);
            setBorderPainted(false);
            setFocusPainted(false);
            try {
				setRolloverIcon(new ImageIcon(ImageIO.read(getClass().getResource("/assets/close.png")).getScaledInstance(10, 10, Image.SCALE_SMOOTH)));
			} catch (IOException e1) {
				e1.printStackTrace();
			}
            addMouseListener(new MouseAdapter(){
                public void mouseClicked(MouseEvent e){
                    pane.remove(pane.indexOfTabComponent(TabPanel.this));
                }
            });
        }
    }

}