package com.canoo.ulc.playground.developexamples.fcc.fixedcolumntable;

import java.awt.BorderLayout;

import com.ulcjava.base.application.AbstractApplication;
import com.ulcjava.base.application.ULCBoxPane;
import com.ulcjava.base.application.ULCFrame;
import com.ulcjava.base.application.ULCListSelectionModel;
import com.ulcjava.base.application.ULCScrollPane;
import com.ulcjava.base.application.ULCTable;
import com.ulcjava.base.application.event.IListSelectionListener;
import com.ulcjava.base.application.event.ListSelectionEvent;
import com.ulcjava.base.application.table.AbstractTableModel;
import com.ulcjava.base.application.table.DefaultTableModel;
import com.ulcjava.base.application.util.Dimension;
import com.ulcjava.base.development.DevelopmentRunner;

/***********************************************************************************************************************
 * / * Class to demo
 */
public class ULCFixedColumnExample extends AbstractApplication {

    private ULCFrame frame;

    Object[][] data;
    Object[] column;
    ULCTable fixedTable, table;

    public ULCFixedColumnExample() {
    }

    public void start() {

        frame = new ULCFrame("Fixed Column Example");
        frame.setSize(new Dimension(400, 150));

        column = new Object[] { "fixed 1", "fixed 2", "a", "b", "c", "d", "e", "f" };
        data = createData(1000, column.length);

        AbstractTableModel fixedModel = new AbstractTableModel() {
            // We have two fixed rows so report 2 columns from data
            public int getColumnCount() {
                return 2;
            }

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

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

            // As only the first two columns are reported this can stay
            // the same
            public Object getValueAt(int row, int col) {
                return data[row][col];
            }

            public void setValueAt(Object obj, int row, int col) {
                // As above
                data[row][col] = obj;
            }

            public boolean isCellEditable(int row, int col) {
                return true;
            }
        };

        AbstractTableModel model = new AbstractTableModel() {
            // This model has two less columns in the data as two are
            // fixed
            public int getColumnCount() {
                return column.length - 2;
            }

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

            public String getColumnName(int col) {
                // The columns start 2 on in the data
                return (String)column[col + 2];
            }

            public Object getValueAt(int row, int col) {
                // The first two column are for the fixed table so return
                // data after by adding 2
                return data[row][col + 2];
            }

            public void setValueAt(Object obj, int row, int col) {
                // As above
                data[row][col + 2] = obj;
            }

            public boolean isCellEditable(int row, int col) {
                return true;
            }
        };

        fixedTable = new ULCTable(fixedModel);
        fixedTable.setPreferredScrollableViewportSize(new Dimension(150, 0));
        ULCListSelectionModel lsm = fixedTable.getSelectionModel();
        fixedTable.getTableHeader().setResizingAllowed(false);
        fixedTable.setAutoResizeMode(ULCTable.AUTO_RESIZE_OFF);
        fixedTable.setSelectionMode(ULCListSelectionModel.SINGLE_SELECTION);

        table = new ULCTable(model, null, lsm);
        table.setAutoResizeMode(ULCTable.AUTO_RESIZE_OFF);
        table.setSelectionMode(ULCListSelectionModel.SINGLE_SELECTION);

        ULCScrollPane scroll = new ULCScrollPane();
        scroll.setViewPortView(table);
        scroll.setRowHeaderView(fixedTable);
        scroll.setCorner(ULCScrollPane.UPPER_LEFT_CORNER, fixedTable.getTableHeader());
        frame.getContentPane().add(scroll);
        frame.setVisible(true);
    }

    private Object[][] createData(int row, int col) {
        Object[][] data = new Object[row][col];
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                data[i][j] = Integer.toString(i) + Integer.toString(j);
            }
        }
        return data;
    }

    public static void main(String[] args) {
        DevelopmentRunner.setApplicationClass(ULCFixedColumnExample.class);
        DevelopmentRunner.run();
    }

}
