package com.fr.design.gui.frpane;


import com.fr.design.gui.ilable.UILabel;
import com.fr.design.utils.gui.GUICoreUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;

/**
 * Created by IntelliJ IDEA.
 * Author : Richer
 * Version: 7.0.3
 * Date: 12-12-29
 * Time: 上午9:26
 */

public class UIExtensionPane extends JPanel implements MouseListener {
	private boolean expand;// 是否展开
	private String title;// 标题
	private Component contentPane;// 主面板
	private ImageIcon up_icon, down_icon;// 图标
	private JPanel currentLabel;
	private UILabel iconLabel;
	private UILabel titleLabel;

	// ----各种构造函数---------------------------------------
	public UIExtensionPane() {
		this("", null, true);
	}

	public UIExtensionPane(Component c) {
		this("", c, true);
	}


	public UIExtensionPane(String title, Component c) {
		this(title, c, true);
	}


	public UIExtensionPane(String title, Component c, boolean expand) {
		this.title = title;
		this.expand = expand;
		this.contentPane = GUICoreUtils.createBorderLayoutPane(c, BorderLayout.CENTER, new UILabel("    "), BorderLayout.WEST);
		init();
	}

	// ----各种方法---------------------------------------

    /**
     * 返回面板伸缩的状态
     * @return 返回面板伸缩的状态
     */
	public boolean isExpand() {
		return expand;
	}

	/**
	 * 设置面板的伸缩
	 *
	 * @param true  展开
	 * @param false 收缩
	 */
	public void setExpand(boolean expand) {
		this.expand = expand;
		if (this.expand) {
			this.iconLabel.setIcon(down_icon);
			if (null != this.contentPane) {
				contentPane.setVisible(true);
			}
		} else {
			this.iconLabel.setIcon(up_icon);
			if (null != this.contentPane) {
				contentPane.setVisible(false);
			}
		}
		this.updateUI();
		this.updateUI();
	}

	/**
	 * 返回面板标题
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * 设置面板标题
	 *
	 * @param panel 面板标题
	 */
	public void setTitle(String title) {
		this.title = title;
		this.titleLabel.setText(title);
		this.updateUI();
	}

	/**
	 * 创建标题指示图标
	 */
	private void createImages() {
		int w = this.getPreferredSize().width, h = this.getPreferredSize().height;
		BufferedImage open = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB), close = new BufferedImage(
				w, h, BufferedImage.TYPE_INT_RGB);
		// 展开的图标
		Graphics2D g2 = open.createGraphics();
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		g2.setPaint(this.getBackground());
		g2.fillRect(0, 0, w, h);
		int[] x = {w - h, w, w - h / 2};
		int[] y = {0, 0, h};
		Polygon p = new Polygon(x, y, 3);
		g2.setPaint(Color.GRAY);
		g2.draw(p);
		g2.drawLine(0, h / 2, w - h, h / 2);
		g2.dispose();

		down_icon = new ImageIcon(open);
		// 收缩的的图标
		g2 = close.createGraphics();
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		g2.setPaint(getBackground());
		g2.fillRect(0, 0, w, h);
		x = new int[]{w - h, w - h, w};
		y = new int[]{0, h, h / 2};
		p = new Polygon(x, y, 3);
		g2.setPaint(Color.GRAY);
		g2.draw(p);
		g2.drawLine(0, h / 2, w - h, h / 2);
		g2.dispose();
		up_icon = new ImageIcon(close);
	}

	/**
	 * 初始化工作
	 */
	private void init() {
		this.createImages();// 获得图标
		this.setLayout(new BorderLayout());
		if (this.expand) {
			this.currentLabel = GUICoreUtils.createBorderLayoutPane(
					iconLabel = new UILabel(down_icon), BorderLayout.EAST,
					new LineLabel(), BorderLayout.CENTER,
					titleLabel = new UILabel(title), BorderLayout.WEST);
			if (this.contentPane != null) {
				this.add(this.contentPane, BorderLayout.CENTER);
			}
		} else {
			this.currentLabel = GUICoreUtils.createBorderLayoutPane(
					iconLabel = new UILabel(up_icon), BorderLayout.EAST,
					new LineLabel(), BorderLayout.CENTER,
					titleLabel = new UILabel(title), BorderLayout.WEST);
			if (this.currentLabel != null) {
				this.add(currentLabel, BorderLayout.CENTER);
			}
			if (this.contentPane != null) {
				this.add(contentPane, BorderLayout.CENTER);
				contentPane.setVisible(false);
			}
		}
		this.add(currentLabel, BorderLayout.NORTH);

		this.currentLabel.addMouseListener(this);
	}

    /**
     * 鼠标点击
     * @param e 事件
     */
	public void mouseClicked(MouseEvent e) {
		if (e.getSource() == currentLabel && this.isEnabled()) {
			expand = !expand;
			if (this.expand) {
				this.iconLabel.setIcon(down_icon);
				if (null != this.contentPane) {
					contentPane.setVisible(true);
				}
			} else {
				this.iconLabel.setIcon(up_icon);
				if (null != this.contentPane) {
					contentPane.setVisible(false);
				}
			}
			this.updateUI();
		}
	}

    /**
     * 鼠标进入
     * @param e 事件
     */
	public void mouseEntered(MouseEvent e) {
	}

    /**
     * 鼠标移出
     * @param e 事件
     */
	public void mouseExited(MouseEvent e) {
	}

    /**
     * 鼠标按压
     * @param e 事件
     */
	public void mousePressed(MouseEvent e) {
	}

    /**
     * 鼠标释放
     * @param e 事件
     */
	public void mouseReleased(MouseEvent e) {
	}

	private class LineLabel extends UILabel {
		public LineLabel() {
			super();
		}

		public void paintComponent(Graphics g) {
			super.paintComponent(g);
			Rectangle bounds = getBounds();
			if (bounds != null) {
				Graphics2D g2d = (Graphics2D) g;
				g2d.setPaint(Color.GRAY);
				g2d.drawLine(0, bounds.height / 2, bounds.width, bounds.height / 2);
			}
		}
	}

    /**
     * 测试的入口方法
     * @param args 参数
     */
	public static void main(String[] args) {
		JFrame f = new JFrame();
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Container c = f.getContentPane();
		c.setLayout(new BoxLayout(c, BoxLayout.Y_AXIS));

		c.add(new UIExtensionPane("test1", new JButton("test1"), false));
		c.add(new UIExtensionPane("test2", new JButton("test2"), false));
		f.setSize(360, 500);
		f.setLocation(200, 100);
		f.setVisible(true);
	}
}