/*
#=======================================================================================================
#
#                   Copyright (c) 2007 Origin Enterprise Solution LTD.
#
#=======================================================================================================
# File description:  SearchResultTableModel.java
#=======================================================================================================
#	 Date	       time   	   Name	 Action	   Description of Change
#	2011-1-17      下午03:46:12    tyl  	Ini	            初始化文件
#=======================================================================================================
*/
package cust.com.rac.workflow;

import java.util.Enumeration;
import java.util.List;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;

import cust.com.rac.workflow.SearchResultDialog.SearchResults;

public class SearchResultTableModel extends DefaultTableModel {

	private static final long	serialVersionUID	= 1L;

	/**
	 * Creates a new instance of EditBatchProcessLineTableModel.
	 * @param data
	 */
	public SearchResultTableModel(String[] names, List<SearchResults> data, int index) {
		super(getData(data, index), names);
	}

	public static Object[][] getData(List<SearchResults> data, int index) {
		Object[][] datas = new Object[data.size()][];
		for (int i = 0; i < data.size(); i++) {
			if (index == 0) {
				String[] info = new String[15];
				info[0] = String.valueOf(i + 1);
				info[1] = data.get(i).getTask_name();
				info[2] = data.get(i).getTask_executor();
				info[3] = data.get(i).getTask_createDate();
				info[4] = data.get(i).getTask_finishDate();
				info[5] = data.get(i).getTask_status();
				info[6] = data.get(i).getTask_note();
				info[7] = data.get(i).getTask_type();
				info[8] = data.get(i).getProcess_executor();
				info[9] = data.get(i).getProcess_name();
				info[10] = data.get(i).getProcess_createDate();
				info[11] = data.get(i).getProcess_finishDate();
				info[12] = data.get(i).getProcess_lastModeUser();
				info[13] = data.get(i).getProcess_status();
				info[14] = data.get(i).getProcess_templateName();
				datas[i] = info;
			} else {
				if (index == 1) {
					String[] info = new String[8];
					info[0] = String.valueOf(i + 1);
					info[1] = data.get(i).getProcess_executor();
					info[2] = data.get(i).getProcess_name();
					info[3] = data.get(i).getTask_name();
					info[4] = data.get(i).getProcess_createDate();
					info[5] = data.get(i).getProcess_finishDate();
					info[6] = data.get(i).getProcess_lastModeUser();
					info[7] = data.get(i).getProcess_status();
					datas[i] = info;
				}
			}
		}
		return datas;
	}

	@Override
	public boolean isCellEditable(int row, int column) {
		if (column == 0) {
			return false;
		} else {
			return true;
		}
	}

	// 设置列宽
	public void setTableColumnsWidth(JTable myTable, int index) {
		if (index == 0) {
			myTable.getColumnModel().getColumn(0).setPreferredWidth(30);
			myTable.getColumnModel().getColumn(1).setPreferredWidth(80);
			myTable.getColumnModel().getColumn(2).setPreferredWidth(80);
			myTable.getColumnModel().getColumn(3).setPreferredWidth(80);
			myTable.getColumnModel().getColumn(4).setPreferredWidth(80);
			myTable.getColumnModel().getColumn(5).setPreferredWidth(60);
			myTable.getColumnModel().getColumn(6).setPreferredWidth(60);
			myTable.getColumnModel().getColumn(7).setPreferredWidth(60);
			myTable.getColumnModel().getColumn(8).setPreferredWidth(80);
			myTable.getColumnModel().getColumn(9).setPreferredWidth(80);
			myTable.getColumnModel().getColumn(10).setPreferredWidth(80);
			myTable.getColumnModel().getColumn(11).setPreferredWidth(80);
			myTable.getColumnModel().getColumn(12).setPreferredWidth(80);
			myTable.getColumnModel().getColumn(13).setPreferredWidth(50);
			myTable.getColumnModel().getColumn(14).setPreferredWidth(80);
		} else {
			if (index == 1) {
				myTable.getColumnModel().getColumn(0).setPreferredWidth(30);
				myTable.getColumnModel().getColumn(1).setPreferredWidth(100);
				myTable.getColumnModel().getColumn(2).setPreferredWidth(120);
				myTable.getColumnModel().getColumn(3).setPreferredWidth(100);
				myTable.getColumnModel().getColumn(4).setPreferredWidth(120);
				myTable.getColumnModel().getColumn(5).setPreferredWidth(120);
				myTable.getColumnModel().getColumn(6).setPreferredWidth(100);
				myTable.getColumnModel().getColumn(7).setPreferredWidth(80);
			}
		}
	}

	// 设置列宽：
	public void FitTableColumns(JTable myTable) {
		JTableHeader header = myTable.getTableHeader();
		// header.getColumnModel().getColumn(0)
		int rowCount = myTable.getRowCount();
		Enumeration<TableColumn> columns = myTable.getColumnModel().getColumns();
		while (columns.hasMoreElements()) {
			TableColumn column = (TableColumn) columns.nextElement();
			int col = header.getColumnModel().getColumnIndex(column.getIdentifier());
			int width = (int) myTable.getTableHeader().getDefaultRenderer().getTableCellRendererComponent(myTable, column.getIdentifier(), false, false, -1, col).getPreferredSize().getWidth();
			for (int row = 0; row < rowCount; row++) {
				int preferedWidth = (int) myTable.getCellRenderer(row, col).getTableCellRendererComponent(myTable, myTable.getValueAt(row, col), false, false, row, col).getPreferredSize().getWidth();
				width = Math.max(width, preferedWidth);
			}
			header.setResizingColumn(column);
			column.setWidth(width + myTable.getIntercellSpacing().width);
		}
	}
}
