// Written by Kong Eu Tak for Swing Connection Article
// Email: konget@cheerful.com
// Homepage: http://www.singnet.com.sg/~kongeuta/

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.*;
import java.util.*;

import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;

public class Demo extends Applet implements ActionListener{
	static Demo Instance;
	JButton StartButton;

	// Application Launching
	public static void main(String Args[]){
		Instance=null;
		launch();
	}
	
	// Applet Launching
	public Demo(){
		setLayout(new BorderLayout());
		add(StartButton=new JButton("Show Demonstration"),BorderLayout.CENTER);
		StartButton.addActionListener(this);
		Instance=this;
	}

	public void actionPerformed(ActionEvent a){
		if (a.getActionCommand().equals("Show Demonstration")){
			launch();
		}
	}

	// Creates a Frame with the SortedJTable inside
	public static void launch(){
		SortedJTable Test=new SortedJTable(new TestModel());
		JFrame TestFrame=new JFrame();
		
		TestFrame.setTitle("Sorted JTable Demonstration");
		TestFrame.getContentPane().setLayout(new BorderLayout());
		TestFrame.getContentPane().add(new JScrollPane(Test,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER),BorderLayout.CENTER);
		TestFrame.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent w){
				((JFrame)w.getComponent()).setVisible(false);
				((JFrame)w.getComponent()).dispose();
			}
		});
	
		TestFrame.pack();
	
		Dimension ScreenSize=Toolkit.getDefaultToolkit().getScreenSize();
		Dimension WinSize=TestFrame.getSize();
		TestFrame.setLocation((ScreenSize.width-WinSize.width)/2,(ScreenSize.height-WinSize.height)/2);
		TestFrame.setVisible(true);
	}

	// Returns an ImageIcon object, handling both Application and Applet cases
	public static ImageIcon getImageIcon(String Filename){
		try{
			if (Instance!=null) return new ImageIcon(new URL(Instance.getCodeBase(),Filename));
			else return new ImageIcon(Filename);
		}catch(Exception e){return null;}
	}

	// A Nonsense model for Demo purposes
	static class TestModel extends AbstractTableModel implements SortedJTable.ModifiedTableModel{
		String ColNames[]={"No.","Name","BirthDay"};
		Object[][] Data={{new Integer(1),new Integer(2),new Integer(3),new Integer(4),new Integer(5),new Integer(6)},
			{"Tom","Dick","Harry","Peter","Bob","Zimmerman"},
			{new Date(80,11,11),new Date(30,11,12),new Date(23,3,3),new Date(65,4,10),new Date(95,12,7),new Date(10,12,30)}};

		public Class getColumnClass(int Col){return getValueAt(0,Col).getClass();}
		public int getColumnCount(){return Data.length;}
		public int getRowCount(){return Data[0].length;}
		public String getColumnName(int Col){return ColNames[Col];}
		public Object getValueAt(int Row,int Col){return Data[Col][Row];}
		public void fireChanged(){fireTableChanged(new TableModelEvent(this));}
	}
}