package com.langtech.plm.form;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map.Entry;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.kernel.TCComponentForm;
import com.teamcenter.rac.kernel.TCComponentItem;
import com.teamcenter.rac.kernel.TCComponentItemRevision;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.rac.util.MessageBox;
import com.teamcenter.rac.util.PropertyLayout;

public class ChooseDialog extends JFrame implements ActionListener {

	protected DefaultTableModel tm_part;
	private JComboBox<String> objectComboBox = new JComboBox<String>();
	private JButton okButton = new JButton("确定");
	private JButton concelButton = new JButton("取消");
	private String[] strings;
	private int row;
	private int column;

	public ChooseDialog(String[] strings,int row,int column,DefaultTableModel tm_part) {
		// TODO Auto-generated constructor stub
		this.strings = strings;
		this.row = row;
		this.column = column;
		this.tm_part = tm_part;
		initUI();
	}

	
	
	public void initUI() {
		// TODO Auto-generated method stub
		try {
			this.setTitle("选择人员");
			this.setLayout(new BorderLayout());

			JPanel topPanel = getTopPanel();

			JPanel btnPanel = getBtnPanel();

			this.add(topPanel, BorderLayout.NORTH);
			// this.add(pane,BorderLayout.CENTER);
			this.add(btnPanel, BorderLayout.SOUTH);
			this.setPreferredSize(new Dimension(350, 150));
			Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); // 获取屏幕尺寸
			int screenWidth = screenSize.width; // 获取屏幕宽度
			int screenHeight = screenSize.height; // 获取屏幕高度
			int x = (screenWidth - 350) / 2; // 计算Frame的左上角x坐标
			int y = (screenHeight - 150) / 2; // 计算Frame的左上角y坐标
			this.setLocation(x, y); // 设置Frame的位置

			// this.setLocationRelativeTo(null);
			this.createActionEvent();
			this.pack();

			// this.validate();
			this.setVisible(true);

//			this.setAlwaysOnTop(true);

			//设置下拉值
			for (int i = 0; i < strings.length; i++) {
				objectComboBox.addItem(strings[i]);
			}

		} catch (Exception e) {
			e.printStackTrace();
			return;
		}
	}

	// 添加监听
	public void createActionEvent() {

		this.okButton.addActionListener(this);
		this.concelButton.addActionListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub

		Object source = e.getSource();
		System.out.println("source==>+" + source);
		if (this.okButton.equals(source)) {
			String selectedItem = (String) objectComboBox.getSelectedItem();
			tm_part.setValueAt(selectedItem, row, column);
			this.dispose();
		} else if (this.concelButton.equals(source)) {
			this.dispose();
		}

	}

	private JPanel getBtnPanel() {
		JPanel topPanel = new JPanel();
		topPanel.setLayout(new PropertyLayout());
		topPanel.add("1.1.center", new JLabel(""));
		topPanel.add("2.1.center", new JLabel("                       "));
		topPanel.add("2.2.center", okButton);
		topPanel.add("2.3.center", new JLabel(""));
		topPanel.add("2.4.center", concelButton);
		topPanel.add("3.1.center", new JLabel(""));
		topPanel.add("4.1.center", new JLabel(""));
		return topPanel;
	}

	// 查询部分
	private JPanel getTopPanel() {
		// TODO Auto-generated method stub
		JPanel topPanel = new JPanel();
		topPanel.setLayout(new PropertyLayout());
		topPanel.setBorder(new EmptyBorder(5, 5, 5, 5));

		topPanel.add("1.1.left.center", new JLabel(""));
		topPanel.add("2.1.left.center", new JLabel(""));
		topPanel.add("3.1.left.center", new JLabel(""));
		topPanel.add("4.1.left.center", new JLabel(""));

		topPanel.add("5.1.left.center", new JLabel("            选择人员："));
		topPanel.add("5.2.left.center", objectComboBox);

		return topPanel;
	}

}
