package co.dc.aptiv.utils;

import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Enumeration;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.Popup;
import javax.swing.PopupFactory;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.plaf.FontUIResource;
 
/**
 * 
 * @author lyy
 *
 */
public class KeyboardUtil extends JPanel {
 
	private static final long serialVersionUID = 4529266044762990227L;
 
	private JPanel monthPanel;
	private boolean isShow = false;
	private Popup pop;
 
	private JComponent showDate;
	
	public boolean isAt = false;
 
	/**
	 * 静态初始化
	 * @return
	 */
	public static KeyboardUtil getInstance() {
		return new KeyboardUtil();
	}

	/**
	 * new此可建立工具对象
	 */
	public KeyboardUtil() {
		initPanel();
	}
	public KeyboardUtil(final JComponent showDate){
		initPanel();
		register(showDate);
	}
	public KeyboardUtil(final JComponent showDate,boolean isat){
		initPanel();
		register(showDate,isat);
	}
 
	/**
	 * 初始化面板  
	 */
	private void initPanel() {
		monthPanel = new JPanel(new BorderLayout());
		/*
		 * 统一所有子窗口设置字体样式
		 */
		 FontUIResource fontRes = new FontUIResource(new Font("微软雅黑",Font.PLAIN,16));  
		  for (Enumeration<Object> keys = UIManager.getDefaults().keys();keys.hasMoreElements();) {  
		  Object key = keys.nextElement();  
		  Object value = UIManager.get(key);  
		  if (value instanceof FontUIResource) {  
		  UIManager.put(key, fontRes);  
		  }
		  }
		  
		monthPanel.setBorder(BorderFactory.createLineBorder(Color.BLUE));
		monthPanel.add(new JP3(), BorderLayout.CENTER);
	}
 
	/**
	 * 绑定某控件1
	 * @param showDate
	 */
	public void register(final JComponent showDate) {
		this.showDate = showDate;
		showDate.setRequestFocusEnabled(true);
		showDate.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent me) {
				showDate.requestFocusInWindow();
			}
		});
		this.add(showDate, BorderLayout.CENTER);
		this.setPreferredSize(new Dimension(90, 25));
		this.setBorder(BorderFactory.createLineBorder(Color.GRAY));
		showDate.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent me) {
				if (showDate.isEnabled()) {
					showDate.setForeground(Color.BLACK);
					if (isShow) {
						hidePanel();
					} else {
						showPanel(showDate);
					}
				}
			}
		});
	}
 
	/**
	 * 绑定某控件2
	 * @param showDate
	 */
	public void register(final JComponent showDate,boolean isat) {
		this.isAt = isat;
		this.showDate = showDate;
		showDate.setRequestFocusEnabled(true);
		showDate.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent me) {
				showDate.requestFocusInWindow();
			}
		});
		this.add(showDate, BorderLayout.CENTER);
		this.setPreferredSize(new Dimension(90, 25));
		this.setBorder(BorderFactory.createLineBorder(Color.GRAY));
		showDate.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent me) {
				if (showDate.isEnabled()) {
					showDate.setForeground(Color.BLACK);
					if (isShow) {
						hidePanel();
					} else {
						showPanel(showDate);
					}
				}
			}
		});
	}
	
	//隐藏选择面板  
	public void hidePanel() {
		if (pop != null) {
			isShow = false;
			pop.hide();
			pop = null;
		}
	}
 
	//显示选择面板  
	private void showPanel(Component owner) {
		if (pop != null) {
			pop.hide();
		}
		Point show = new Point(0, showDate.getHeight());
		SwingUtilities.convertPointToScreen(show, showDate);
		Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
		int x = show.x;
		int y = show.y;
		if (x < 0) {
			x = 0;
		}
		if (x > size.width - 600) {
			x = size.width - 600;
		}
		if (y < size.height - 340) {
			
		} else {
			y -= (340+owner.getHeight());
		}
		
		pop = PopupFactory.getSharedInstance().getPopup(owner, monthPanel, x, y);
		pop.show();
		isShow = true;
	}
 
	private class JP3 extends JPanel implements ActionListener {
		private static final long serialVersionUID = 43157272447522985L;
		JButton button_1 = new JButton("1");
		JButton button_2 = new JButton("2");
		JButton button_3 = new JButton("3");
		JButton button_4 = new JButton("4");
		JButton button_5 = new JButton("5");
		JButton button_del = new JButton("删除(<--)");
		JButton button_6 = new JButton("6");
		JButton button_7 = new JButton("7");
		JButton button_8 = new JButton("8");
		JButton button_9 = new JButton("9");
		JButton button_0 = new JButton("0");
		JButton button_clear = new JButton("清  空");
		JButton button_a = new JButton("a");
		JButton button_b = new JButton("b");
		JButton button_c = new JButton("c");
		JButton button_d = new JButton("d");
		JButton button_e = new JButton("e");
		JButton button_f = new JButton("f");
		JButton button_g = new JButton("g");
		JButton button_h = new JButton("h");
		JButton button_i = new JButton("i");
		JButton button_j = new JButton("j");
		JButton button_k = new JButton("k");
		JButton button_l = new JButton("l");
		JButton button_m = new JButton("m");
		JButton button_n = new JButton("n");
		JButton button_o = new JButton("o");
		JButton button_p = new JButton("p");
		JButton button_q = new JButton("q");
		JButton button_r = new JButton("r");
		JButton button_s = new JButton("s");
		JButton button_t = new JButton("t");
		JButton button_u = new JButton("u");
		JButton button_v = new JButton("v");
		JButton button_w = new JButton("w");
		JButton button_x = new JButton("x");
		JButton button_y = new JButton("y");
		JButton button_z = new JButton("z");
		JButton button_cancel = new JButton("O K");
		
		public JP3() {
			super();
			setLayout(null);
			this.setPreferredSize(new Dimension(600, 340));
			updateDate();
		}
 
		public void updateDate() {
			/*
			 * 添加button+事件
			 */
			button_1.setBounds(0, 0, 85, 56);
			button_1.addActionListener(this);
			button_2.setBounds(85, 0, 85, 56);
			button_2.addActionListener(this);
			button_3.setBounds(170, 0, 85, 56);
			button_3.addActionListener(this);
			button_4.setBounds(255, 0, 85, 56);
			button_4.addActionListener(this);
			button_5.setBounds(340, 0, 85, 56);
			button_5.addActionListener(this);
			button_del.setBounds(430, 0, 170, 56);
			button_del.addActionListener(this);
			button_6.setBounds(0, 56, 85, 56);
			button_6.addActionListener(this);
			button_7.setBounds(85, 56, 85, 56);
			button_7.addActionListener(this);
			button_8.setBounds(170, 56, 85, 56);
			button_8.addActionListener(this);
			button_9.setBounds(255, 56, 85, 56);
			button_9.addActionListener(this);
			button_0.setBounds(340, 56, 85, 56);
			button_0.addActionListener(this);
			button_clear.setBounds(430, 56, 170, 56);
			button_clear.addActionListener(this);
			button_a.setBounds(0, 116, 85, 56);
			button_a.addActionListener(this);
			button_b.setBounds(85, 116, 85, 56);
			button_b.addActionListener(this);
			button_c.setBounds(170, 116, 85, 56);
			button_c.addActionListener(this);
			button_d.setBounds(255, 116, 85, 56);
			button_d.addActionListener(this);
			button_e.setBounds(340, 116, 85, 56);
			button_e.addActionListener(this);
			button_f.setBounds(425, 116, 87, 56);
			button_f.addActionListener(this);
			button_g.setBounds(512, 116, 88, 56);
			button_g.addActionListener(this);
			button_h.setBounds(0, 172, 85, 56);
			button_h.addActionListener(this);
			button_i.setBounds(85, 172, 85, 56);
			button_i.addActionListener(this);
			button_j.setBounds(170, 172, 85, 56);
			button_j.addActionListener(this);
			button_k.setBounds(255, 172, 85, 56);
			button_k.addActionListener(this);
			button_l.setBounds(340, 172, 85, 56);
			button_l.addActionListener(this);
			button_m.setBounds(425, 172, 87, 56);
			button_m.addActionListener(this);
			button_n.setBounds(512, 172, 88, 56);
			button_n.addActionListener(this);
			button_o.setBounds(0, 228, 85, 56);
			button_o.addActionListener(this);
			button_p.setBounds(85, 228, 85, 56);
			button_p.addActionListener(this);
			button_q.setBounds(170, 228, 85, 56);
			button_q.addActionListener(this);
			button_r.setBounds(255, 228, 85, 56);
			button_r.addActionListener(this);
			button_s.setBounds(340, 228, 85, 56);
			button_s.addActionListener(this);
			button_t.setBounds(425, 228, 87, 56);
			button_t.addActionListener(this);
			button_u.setBounds(512, 228, 88, 56);
			button_u.addActionListener(this);
			button_v.setBounds(0, 284, 85, 56);
			button_v.addActionListener(this);
			button_w.setBounds(85, 284, 85, 56);
			button_w.addActionListener(this);
			button_x.setBounds(170, 284, 85, 56);
			button_x.addActionListener(this);
			button_y.setBounds(255, 284, 85, 56);
			button_y.addActionListener(this);
			button_z.setBounds(340, 284, 85, 56);
			button_z.addActionListener(this);
			button_cancel.setBounds(426, 285, 174, 55);
			button_cancel.setFont(new Font("微软雅黑",Font.BOLD,20));
			button_cancel.addActionListener(this);
			
			add(button_1);
			add(button_2);
			add(button_3);
			add(button_4);
			add(button_5);
			add(button_del);
			add(button_6);
			add(button_7);
			add(button_8);
			add(button_9);
			add(button_0);
			add(button_clear);
			add(button_a);
			add(button_b);
			add(button_c);
			add(button_d);
			add(button_e);
			add(button_f);
			add(button_g);
			add(button_h);
			add(button_i);
			add(button_j);
			add(button_k);
			add(button_l);
			add(button_m);
			add(button_n);
			add(button_o);
			add(button_p);
			add(button_q);
			add(button_r);
			add(button_s);
			add(button_t);
			add(button_u);
			add(button_v);
			add(button_w);
			add(button_x);
			add(button_y);
			add(button_z);
			add(button_cancel);
			
		}
		
		public void actionPerformed(ActionEvent e) {
			if(e.getSource()==button_1){
				uploadData("1");
			}
			if(e.getSource()==button_2){
				uploadData("2");
			}
			if(e.getSource()==button_3){
				uploadData("3");
			}
			if(e.getSource()==button_4){
				uploadData("4");
			}
			if(e.getSource()==button_5){
				uploadData("5");
			}
			if(e.getSource()==button_del){
				delData();
			}
			if(e.getSource()==button_6){
				uploadData("6");
			}
			if(e.getSource()==button_7){
				uploadData("7");
			}
			if(e.getSource()==button_8){
				uploadData("8");
			}
			if(e.getSource()==button_9){
				uploadData("9");
			}
			if(e.getSource()==button_0){
				uploadData("0");
			}
			if(e.getSource()==button_clear){
				clearData();
			}
			if(e.getSource()==button_a){
				uploadData("a");
			}
			if(e.getSource()==button_b){
				uploadData("b");
			}
			if(e.getSource()==button_c){
				uploadData("c");
			}
			if(e.getSource()==button_d){
				uploadData("d");
			}
			if(e.getSource()==button_e){
				uploadData("e");
			}
			if(e.getSource()==button_f){
				uploadData("f");
			}
			if(e.getSource()==button_g){
				uploadData("g");
			}
			if(e.getSource()==button_h){
				uploadData("h");
			}
			if(e.getSource()==button_i){
				uploadData("i");
			}
			if(e.getSource()==button_j){
				uploadData("j");
			}
			if(e.getSource()==button_k){
				uploadData("k");
			}
			if(e.getSource()==button_l){
				uploadData("l");
			}
			if(e.getSource()==button_m){
				uploadData("m");
			}
			if(e.getSource()==button_n){
				uploadData("n");
			}
			if(e.getSource()==button_o){
				uploadData("o");
			}
			if(e.getSource()==button_p){
				uploadData("p");
			}
			if(e.getSource()==button_q){
				uploadData("q");
			}
			if(e.getSource()==button_r){
				uploadData("r");
			}
			if(e.getSource()==button_s){
				uploadData("s");
			}
			if(e.getSource()==button_t){
				uploadData("t");
			}
			if(e.getSource()==button_u){
				uploadData("u");
			}
			if(e.getSource()==button_v){
				uploadData("v");
			}
			if(e.getSource()==button_w){
				uploadData("w");
			}
			if(e.getSource()==button_x){
				uploadData("x");
			}
			if(e.getSource()==button_y){
				uploadData("y");
			}
			if(e.getSource()==button_z){
				uploadData("z");
			}
			if(e.getSource()==button_cancel){
				if(isAt){
//					JTextField jt = (JTextField)showDate;
//					jt.setText(jt.getText()+KeyEvent.VK_ENTER);
//					jt.keyDown(null, KeyEvent.VK_ENTER);
					//注册回车事件
					try {
						Robot r = new Robot();
						r.keyPress(KeyEvent.VK_ENTER);
					} catch (AWTException e1) {
						e1.printStackTrace();
					}
				}
				hidePanel();
			}
		}
		
		/**
		 * 更新控件内容
		 * @param data
		 */
		@SuppressWarnings("unused")
		public void uploadData(String data){
			if (showDate instanceof JTextField) {
				JTextField jt = (JTextField) showDate;
				jt.setText(jt.getText()+data);
			}else if (showDate instanceof JLabel) {
				JLabel jl = (JLabel) showDate;
				jl.setText(jl.getText()+data);
			}
		}
		/**
		 * 清空控件内容
		 */
		@SuppressWarnings("unused")
		public void clearData(){
			if (showDate instanceof JTextField){
				((JTextField) showDate).setText("");
			}else if (showDate instanceof JLabel){
				((JLabel) showDate).setText("");
			}
		}
		/**
		 * 删除控件内容
		 */
		@SuppressWarnings("unused")
		public void delData(){
			if (showDate instanceof JTextField) {
				JTextField jt = (JTextField) showDate;
				if(jt.getText().length()>0){
					jt.setText(jt.getText().substring(0, jt.getText().length()-1));
				}else{
					jt.setText("");
				}
			}else if (showDate instanceof JLabel) {
				JLabel jl = (JLabel) showDate;
				if(jl.getText().length()>0){
					jl.setText(jl.getText().substring(0, jl.getText().length()-1));
				}else{
					jl.setText("");
				}
			}
		}
	}
 
	public static void main(String[] args) {
		/*
		 * 以下三个步骤，顺序不可颠倒，初始化和注册的步骤必须紧跟着建立好的控件后面
		 * 其他面板引用此插件的方式只需要以下三步即可
		 */
		JTextField showDate1 = new JTextField();//第一步：建立输入框或者label
		KeyboardUtil dateChooser1 = KeyboardUtil.getInstance();//第二步：初始化插件
		dateChooser1.register(showDate1,true);//第三步：注册控件
 
		JFrame jf = new JFrame("测试键盘选择器");
		jf.add(showDate1, BorderLayout.NORTH);
		jf.pack();
		jf.setLocationRelativeTo(null);
		jf.setVisible(true);
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}