package own.crazyit12.chapter11;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
/**
 * Description:
 * <br/>Copyright (C), 2005-2008, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee kongyeeku@163.com
 * @version  1.0
 */
public class SortTable
{
	JFrame jf = new JFrame("¼òµ¥±í¸ñ");
	//¶¨Òå¶þÎ¬Êý×é×÷Îª±í¸ñÊý¾Ý
	Object[][] tableData = 
	{
		new Object[]{"ÀîÇåÕÕ" , 29 , "Å®"},
		new Object[]{"ËÕ¸ñÀ­µ×", 56 , "ÄÐ"},
		new Object[]{"Àî°×", 35 , "ÄÐ"},
		new Object[]{"ÅªÓñ", 18 , "Å®"},
		new Object[]{"»¢Í·" , 2 , "ÄÐ"}
	};
	//¶¨ÒåÒ»Î¬Êý¾Ý×÷ÎªÁÐ±êÌâ
	Object[] columnTitle = {"ÐÕÃû" , "ÄêÁä" , "ÐÔ±ð"};

	//ÒÔ¶þÎ¬Êý×éºÍÒ»Î¬Êý×éÀ´´´½¨Ò»¸öJTable¶ÔÏó
	JTable table = new JTable(tableData , columnTitle);

	//½«Ô­±í¸ñÀïµÄmodel°ü×°³ÉÐÂµÄSortFilterModel¶ÔÏó
	SortableTableModel sorterModel = new SortableTableModel(table.getModel());

	public void init()
	{
		table.setModel(sorterModel);

		table.getTableHeader().addMouseListener(new MouseAdapter()
		{  
			public void mouseClicked(MouseEvent event)
			{
				// Èç¹ûµ¥»÷´ÎÊýÐ¡ÓÚ2£¬¼´²»ÊÇË«»÷£¬Ö±½Ó·µ»Ø
				if (event.getClickCount() < 2)
				{
					return;
				}
				//ÕÒ³öÊó±êË«»÷ÊÂ¼þËùÔÚµÄÁÐË÷Òý
				int tableColumn = table.columnAtPoint(event.getPoint());
				//½«JTableÖÐµÄÁÐË÷Òý×ª»»³É¶ÔÓ¦TableModelÖÐµÄÁÐË÷Òý
				int modelColumn = table.convertColumnIndexToModel(tableColumn);
				//¸ù¾ÝÖ¸¶¨ÁÐ½øÐÐÅÅÐò
				sorterModel.sort(modelColumn);
			}
		});

		//½«JTable¶ÔÏó·ÅÔÚJScrollPaneÖÐ£¬²¢½«¸ÃJScrollPane·ÅÔÚ´°¿ÚÖÐÏÔÊ¾³öÀ´
		jf.add(new JScrollPane(table));
		jf.pack();
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf.setVisible(true);
	}

    public static void main(String[] args) 
    {
		new SortTable().init();
    }
}
class SortableTableModel extends AbstractTableModel
{
	private TableModel model;
	private int sortColumn;
	private Row[] rows;

	//½«Ò»¸öÒÑ¾­´æÔÚTableModel¶ÔÏó°ü×°³ÉSortableTableModel¶ÔÏó
	public SortableTableModel(TableModel m)
	{
		//½«±»·â×°µÄTableModel´«Èë
		model = m;
		rows = new Row[model.getRowCount()];
		//½«Ô­TableModelÖÐµÄÃ¿ÐÐ¼ÇÂ¼µÄË÷ÒýÊ¹ÓÃRowÊý×é±£´æÆðÀ´
		for (int i = 0; i < rows.length; i++)
		{  
			rows[i] = new Row(i);
		}
	}

	//ÊµÏÖ¸ù¾ÝÖ¸¶¨ÁÐ½øÐÐÅÅÐò
	public void sort(int c)
	{  
		sortColumn = c;
		Arrays.sort(rows);
		fireTableDataChanged();
	}

	//ÏÂÃæÈý¸ö·½·¨ÐèÒª·ÃÎÊModelÖÐµÄÊý¾Ý£¬ËùÒÔÉæ¼°µ½±¾ModelÖÐÊý¾Ý
	//ºÍ±»°ü×°ModelÊý¾ÝÖÐµÄË÷Òý×ª»»£¬³ÌÐòÊ¹ÓÃrowsÊý×éÍê³ÉÕâÖÖ×ª»»¡£
	public Object getValueAt(int r, int c)
	{
		return model.getValueAt(rows[r].index, c);
	}

	public boolean isCellEditable(int r, int c) 
	{ 
		return model.isCellEditable(rows[r].index, c);
	}

	public void setValueAt(Object aValue, int r, int c) 
	{ 
		model.setValueAt(aValue, rows[r].index, c);
	}

	//ÏÂÃæ·½·¨µÄÊµÏÖ°Ñ¸ÃmodelµÄ·½·¨Î¯ÍÐÎªÔ­·â×°µÄmodelÀ´ÊµÏÖ
	public int getRowCount() 
	{
		return model.getRowCount(); 
	}
	public int getColumnCount()
	{
		return model.getColumnCount();
	}
	public String getColumnName(int c)
	{
		return model.getColumnName(c); 
	}
	public Class getColumnClass(int c) 
	{
		return model.getColumnClass(c);
	}

	//¶¨ÒåÒ»¸öRowÀà£¬¸ÃÀàÓÃÓÚ·â×°JTableÖÐµÄÒ»ÐÐ¡¢
	//Êµ¼ÊÉÏËü²¢²»·â×°ÐÐÊý¾Ý£¬ËüÖ»·â×°ÐÐË÷Òý
	private class Row implements Comparable<Row>
	{
		//¸Ãindex±£´æ×Å±»·â×°ModelÀïÃ¿ÐÐ¼ÇÂ¼µÄÐÐË÷Òý
		public int index;
		public Row(int index)
		{
			this.index = index;
		}
		//ÊµÏÖÁ½ÐÐÖ®¼äµÄ´óÐ¡±È½Ï
		public int compareTo(Row other)
		{  
			Object a = model.getValueAt(index, sortColumn);
			Object b = model.getValueAt(other.index, sortColumn);
			if (a instanceof Comparable)
			{
				return ((Comparable)a).compareTo(b);
			}
			else
			{
				return a.toString().compareTo(b.toString());
			}
		}
	}
}