import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.*;
import javax.swing.JScrollPane;
import javax.swing.JFrame;
import javax.swing.*;
import javax.swing.SwingUtilities;
import javax.swing.JOptionPane;
import javax.swing.JViewport;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.sql.*;
import java.net.URL;
import java.util.StringTokenizer;
import netscape.javascript.*;
import javax.swing.event.*;

public class checklist_applet extends Applet {
	private Connection connection = null;
	public String[] header = null;
	public Object[][] datacontent = null;
	public int[] reportID = null;
	public int[] report = null;
	public String[] statusVal = null;
	private JSObject mainWindow;
 
    public void init()
	{
		mainWindow = JSObject.getWindow(this);
		checklist_applet frame = new checklist_applet();
        frame.setVisible(true);
		gettingParams();
    }

	final void readParam(String days,String row, String report, String reptype, String repid, String repdata) 
	{ 
		int	noOfDays = Integer.parseInt(days);
		int noOfRows = Integer.parseInt(row);
		header = new String[noOfDays + 2];
		datacontent = new Object[noOfRows][noOfDays+2];
		reportID = new int[noOfRows];
		statusVal = new String[noOfRows];

		header[0] = "Reports";
		header[1] = "Date";
		for (int i=1; i<=noOfDays; i++)
		{
			header[i+1] = Integer.toString(i);
		}
		StringTokenizer st;
		String param_data = null;
		int counter = 0;
		param_data = report;
		param_data = param_data.substring(0, (param_data.length() - 1));
		st = new StringTokenizer(param_data, "|"); 
		while (st.hasMoreTokens()) 
		{ 
			datacontent[counter][1] = st.nextToken();
			counter++;
		}
		counter = 0;
		param_data = reptype;
		param_data = param_data.substring(0, (param_data.length() - 1));
		st = new StringTokenizer(param_data, "|"); 
		while (st.hasMoreTokens()) 
		{ 
			datacontent[counter][0] = st.nextToken();
			counter++;
		}
		counter = 0;
		param_data = repid;
		param_data = param_data.substring(0, (param_data.length() - 1));
		st = new StringTokenizer(param_data, "|"); 
		while (st.hasMoreTokens()) 
		{ 
			reportID[counter] = Integer.parseInt(st.nextToken());
			counter++;
		}
		counter = 0;
		param_data = repdata;
		param_data = param_data.substring(0, (param_data.length() - 1));
		st = new StringTokenizer(param_data, "|"); 
		String status = null;

		while (st.hasMoreTokens()) 
		{ 
			status = st.nextToken();
			statusVal[counter] = status;
			for (int j=1; j<=noOfDays; j++)
			{
				if ((status.substring(j, j+1)).equals("0"))
					datacontent[counter][j+1] = new Boolean(false);
				else
					datacontent[counter][j+1] = new Boolean(true);
			}
			counter++;
		}
	}

	 public checklist_applet() {
		super();
	 }

	 final void gettingParams()
	 {
		String days = getParameter("header");
System.out.println("after header : " + days);
		String row = getParameter("rowno");
		String report = getParameter("reportname");
		String reptype = getParameter("reporttype");
		String repid = getParameter("reportid");
		String repdata = getParameter("data");
		String paneWidth = getParameter("width");
		String paneHeight = getParameter("height");
		readParam(days, row, report, reptype, repid, repdata);
        MyTableModel myModel = new MyTableModel();

		TableColumnModel cm = new DefaultTableColumnModel(){
			boolean first = true;
			boolean second = true;

			public void addColumn(TableColumn tc){
				if (first)
				{
					first = false;
					return;
				}
				else if (second)
				{
					second = false;
					return;
				}
				tc.setPreferredWidth(30);
				super.addColumn(tc);
			}
		};

		TableColumnModel hm = new DefaultTableColumnModel(){
			boolean first = true;
			boolean second = true;

			public void addColumn(TableColumn tc){
				if (first)
				{
					first = false;
					tc.setPreferredWidth(70);
    				tc.setCellRenderer( new CustomCellRenderer() );
					super.addColumn(tc);
					return;
				}
				else if (second)
				{
					second = false;
					tc.setPreferredWidth(200);
					tc.setCellRenderer( new CustomCellRenderer() );
					super.addColumn(tc);
					return;
				}
			}
		};

        JTable table = new JTable(myModel, cm);
        JTable header = new JTable(myModel, hm);
		
		header.setCellSelectionEnabled(false);
		table.setRowSelectionAllowed(false);
		table.setColumnSelectionAllowed(false);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.getModel().addTableModelListener(new RepaintingDataListener(table));

    	table.setRowHeight(25);
		header.setRowHeight(25);

		JTableHeader tableheader = table.getTableHeader();
        tableheader.setForeground(new Color(185,92,0));
		tableheader.setFont(new Font("arial", Font.BOLD, 14));

        table.createDefaultColumnsFromModel();
		header.createDefaultColumnsFromModel();
        table.setPreferredScrollableViewportSize(new Dimension(Integer.parseInt(paneWidth) - 300, Integer.parseInt(paneHeight) - 40));
        header.setPreferredScrollableViewportSize(new Dimension(270, 300));

		JViewport jv = new JViewport();
		jv.setView(header);

		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		header.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

		/*SelectionListener listener = new SelectionListener(table);
		table.getSelectionModel().addListSelectionListener(listener);
		table.getColumnModel().getSelectionModel().addListSelectionListener(listener);*/
    
		JScrollPane scrollPane = new JScrollPane(table);
		scrollPane.setRowHeader(jv);
		add(scrollPane, BorderLayout.CENTER);
    }

	/*public class SelectionListener implements ListSelectionListener {
        JTable table;
    
        // It is necessary to keep the table since it is not possible
        // to determine the table from the event's source
        SelectionListener(JTable table) {
            this.table = table;
        }
       public void valueChanged(ListSelectionEvent e) {
	        if (e.getValueIsAdjusting()) {
System.out.println("inside mouse pressed");
                // The mouse button has not yet been released
            }
        }
    }*/

	class RepaintingDataListener implements TableModelListener
	{
	   private JTable mTable;

	   public RepaintingDataListener(JTable pTable)
	   {
		  this.mTable = pTable;
	   }
	   public void tableChanged(TableModelEvent e)
	   {
		  //mTable.repaint();
	   }
	}


	class CustomCellRenderer extends DefaultTableCellRenderer 
	{
		public CustomCellRenderer()
		{
		}

		public Component getTableCellRendererComponent( JTable table,Object value, boolean isSelected, boolean hasFocus, int row, int column )
		{
			if (column == 0 || column == 1)
			{
				setBackground(new Color(64,128,128));
				setForeground(Color.white);
				setFont(new Font("arial", Font.BOLD, 13));
			}

			super.getTableCellRendererComponent(table,value, isSelected, hasFocus, row, column );

			if (column == 0 || column == 1)
				setFont(new Font("arial", Font.BOLD, 13));

			return this;
		}

		/*public void paint( Graphics g )
		{
			g.setColor( new Color(64,128,128) );

			super.paint( g );
		}*/

	}

	class MyTableModel extends AbstractTableModel {
        final String[] columnNames = header;
        final Object[][] data = datacontent;

        public int getColumnCount() {
            return columnNames.length;
        }
        
        public int getRowCount() {
            return data.length;
        }

        public String getColumnName(int col) {
            return columnNames[col];
        }

        public Object getValueAt(int row, int col) {
            return data[row][col];
        }

        /*
         * JTable uses this method to determine the default renderer/
         * editor for each cell.  If we didn't implement this method,
         * then the columns would contain text ("true"/"false"),
         * rather than a check box.
         */
        public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }

        /*
         * Don't need to implement this method unless your table's
         * editable.
         */
        public boolean isCellEditable(int row, int col) {
            //Note that the data/cell address is constant,
            //no matter where the cell appears onscreen.
            if (col < 2) { 
                return false;
            } else {
                return true;
            }
        }

        /*
         * Don't need to implement this method unless your table's
         * data can change.
         */
        public void setValueAt(Object value, int row, int col) 
		{
			data[row][col] = value;
            //fireTableCellUpdated(row, col);
			updateStatus(row, col, value);
        }

        private void updateStatus(int row, int col, Object value) 
		{
			String status = null;
			String [] stringArgs = new String[2];
			
			status = statusVal[row];
			if (((Boolean)value).booleanValue() == true)
			{
				status = status.substring(0,col-1) + "1" + status.substring(col);
			}
			else
			{
				status = status.substring(0,col-1) + "0" + status.substring(col);
			}
			statusVal[row] = status;
			stringArgs[0] = String.valueOf(row);
			stringArgs[1] = status;
			mainWindow.call("setStatus", stringArgs);
		}
	}
}