/*
#=============================================================================
#																			   
#			Copyright (c) 2009 Origin Enterprise Solution LTD.		   
#																			   
#=============================================================================
# File name: ResultTableModel.java
# File description: 										   	
#=============================================================================
#	Date		Name		Action	Description of Change					   
#	2011-4-20	liqz	  	   Ini		初始化(From Shaangu)										   
#=============================================================================
 */
package cn.com.customization.util;

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;


public class ResultTableModel extends DefaultTableModel {

	/**
	 * 
	 */
	private static final long	serialVersionUID	= 1L;
	private static String[]		names				= { "*", "零组件ID", "版本", "所有者","数据集", "命名的引用" ,"原因","可重新下载"};

	/**
	 * Creates a new instance of EditBatchProcessLineTableModel.
	 * @param data
	 */
	public ResultTableModel(List<PackFailDownInfo> data) {
		super(getData(data), names);
	}

	public static Object[][] getData(List<PackFailDownInfo> data) {
		Object[][] datas = new Object[data.size()][];
		for (int i = 0; i < data.size(); i++) {
			String[] info = new String[8];
			info[0] = String.valueOf(i + 1);
			info[1] = data.get(i).getItemId();
			info[2] = data.get(i).getRev();
			info[3] = data.get(i).getOwner();
			info[4] = data.get(i).getDatasetName();
			info[5] = data.get(i).getReference_file();
			info[6] = data.get(i).getReason();
			info[7] = data.get(i).getNeedDown();
			datas[i] = info;
		}
		return datas;
	}

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

	// 根据标题大小设置列宽
	public void FitTableColumns(JTable myTable) {
		JTableHeader header = myTable.getTableHeader();
		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);
		}
	}

	// 设置列宽
	public void setTableColumnsWidth(JTable myTable) {
		myTable.getColumnModel().getColumn(0).setPreferredWidth(25);
		myTable.getColumnModel().getColumn(1).setPreferredWidth(100);
		myTable.getColumnModel().getColumn(2).setPreferredWidth(40);
		myTable.getColumnModel().getColumn(3).setPreferredWidth(100);
		myTable.getColumnModel().getColumn(4).setPreferredWidth(100);
		myTable.getColumnModel().getColumn(5).setPreferredWidth(125);
		myTable.getColumnModel().getColumn(6).setPreferredWidth(100);
		myTable.getColumnModel().getColumn(7).setPreferredWidth(80);
	}

}
