/*
#=======================================================================================================
#
#                   Copyright (c) 2007 Origin Enterprise Solution LTD.
#
#=======================================================================================================
# File description:  test.java
#=======================================================================================================
#	Date		Name		  Action	  Description of Change
#	2010-1-6	tyl  	修改	
#	2010-4-25	zyn  	修改		     横向显示模式修改为纵向显示模式
#=======================================================================================================
 */

package cn.com.customization.util;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;
import java.util.Vector;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.plaf.SplitPaneUI;
import javax.swing.plaf.basic.BasicSplitPaneUI;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

import com.teamcenter.rac.util.VerticalLayout;

//import components.TableDemo;

public class QueryTaskOpinionTableHead extends JPanel {
	/**
	 * serialVersionUID:TODO（描述变量的功能）
	 * @since Ver 1.1
	 */
	private static final long	serialVersionUID	= 1L;
	private Object[][]			objArray;					// 表中的数据
	private DefaultTableModel	dm;
	private JTable				table;
	private JTextArea           textarea;
//	private String[] headers = { "流程名称","序号", "任务" , "签审决定","执行人","提交时间","注释"};
	private String[] headers = { "流程名称", "任务" , "签审决定","执行人","提交时间","注释"};
	
	public QueryTaskOpinionTableHead() {
		super(new GridLayout(1, 0));
	}

	// 设置组合表格标题
	public void setTableHeadTestModel(Vector<String> titleArrayTemp) {
		dm = new DefaultTableModel(objArray, headers);
		table = new JTable(dm);
		textarea = new JTextArea(15,35);
		textarea.setAutoscrolls(true);
		textarea.setLineWrap(true);
		textarea.setEditable(false);
		textarea.addMouseListener(new   MouseAdapter()   {
            public   void   mouseEntered(MouseEvent   mouseEvent)   {
            	textarea.setCursor(new   Cursor(Cursor.TEXT_CURSOR));   //鼠标进入Text区后变为文本输入指针
            }
            public   void   mouseExited(MouseEvent   mouseEvent)   {
            	textarea.setCursor(new   Cursor(Cursor.DEFAULT_CURSOR));   //鼠标离开Text区后恢复默认形态
            }
        });
		textarea.getCaret().addChangeListener(new   ChangeListener()   {
            public   void   stateChanged(ChangeEvent   e)   {
            	textarea.getCaret().setVisible(true);   //使textarea区的文本光标显示
            }
        });

		GroupableTableHeader header = new GroupableTableHeader(table.getColumnModel());

		table.setTableHeader(header);
//		table.setPreferredScrollableViewportSize(new Dimension(670, 150));
		table.setPreferredScrollableViewportSize(new Dimension(690, 150));
		addListener();
		
		JScrollPane scrollPane = new JScrollPane(table);

		JPanel jpane = new JPanel(new BorderLayout());
		jpane.add(new JLabel("注释:"),BorderLayout.NORTH);
		jpane.add(textarea,BorderLayout.CENTER);
		
		JSplitPane splitPane = new JSplitPane(); 
		splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
		splitPane.setLeftComponent(scrollPane);
		splitPane.setRightComponent(jpane);
		splitPane.setDividerLocation(0.6);
		splitPane.setEnabled(false);
		SplitPaneUI ui = splitPane.getUI();   
		if (ui instanceof BasicSplitPaneUI) {   
		    ((BasicSplitPaneUI) ui).getDivider().setBorder(null);//隐藏分割线
		} 
		
		add(splitPane);
	}

	public void setobjArray(Object[][] objArrayTemp) {
		objArray = objArrayTemp;
	}

	/*
	 * This method picks good column sizes. If all column heads are wider than
	 * the column's cells' contents, then you can just use
	 * column.sizeWidthToFit().
	 */
	public void initColumnSizes() {
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		// DefaultTableModel model = (DefaultTableModel) table.getModel();
		TableColumn column = null;
		Component comp = null;
		int headerWidth = 0;
		int cellWidth = 0;
		TableCellRenderer headerRenderer = table.getTableHeader().getDefaultRenderer();
		TableColumnModel colmode = table.getColumnModel();
		int count = colmode.getColumnCount();
		for (int i = 0; i < count; i++) {
			column = table.getColumnModel().getColumn(i);
			comp = headerRenderer.getTableCellRendererComponent(table, column.getHeaderValue(), false, false, 0, 0);
			headerWidth = comp.getPreferredSize().width;
//			if (i == 1) {
//				cellWidth = 50;
//			}else if (i == 2) {
//				cellWidth = 50;
//			}else if (i == 3) {
//				cellWidth = 80;
//			}else if (i == 4) {
//				cellWidth = 100;
//			}else {
//				cellWidth = 130;
//			}
			if (i == 0) {
				cellWidth = 180;
			}else if (i == 1) {
				cellWidth = 75;
			}else if (i == 2) {
				cellWidth = 75;
			}else if (i == 3) {
				cellWidth = 100;
			}else {
				cellWidth = 130;
			}
			column.setPreferredWidth(Math.max(headerWidth, cellWidth));
		}
	}

	public String getUserChoose() {
		String cellValue = null;
		int selectRows = table.getSelectedRows().length;// 取得用户所选行的行数
		if (selectRows == 1) {
			int selectedRowIndex = table.getSelectedRow(); // 取得用户所选单行
			cellValue = (String) table.getValueAt(selectedRowIndex, 5);
		}
		return cellValue;
	}
	
	/**
	 * 添加单击事件，获得注释信息
	 */
	public void addListener(){
		table.addMouseListener(new MouseListener(){

			public void mouseClicked(MouseEvent e) {
				String userComments = getUserChoose();
				String Comments = "";
				if(userComments!=null){
					int len = userComments.split("\t").length;
					for(int i = 0;i<len;i++){
						if(i!=len-1){
							Comments = Comments +userComments.split("\t")[i]+"\n";
						}else{
							Comments = Comments +userComments.split("\t")[i];
						}					
					}
					textarea.setText(Comments);
				}else{
					textarea.setText("");
				}			
			}

			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}

			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}

			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}

			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
		});
	}
}
