package com.apldbio.sds.platform.sae.audit.ui.view;


import com.jidesoft.grid.*;
import com.jidesoft.plaf.LookAndFeelFactory;
import com.jidesoft.swing.JideTabbedPane;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
import java.awt.*;

/**
 * Demoed Component: {@link com.jidesoft.grid.TableScrollPane}
 * <br>
 * Required jar files: jide-common.jar, jide-grids.jar
 * <br>
 * Required L&F: any L&F
 */
public class TableScrollPaneDemo extends AbstractDemo {

    protected MultiTableModel _totalModel;
    protected MultiTableModel _model;

    public TableScrollPaneDemo() {
    }

    public String getName() {
        return "TableScrollPane";
    }

    public String getDescription() {
        return "This is a demo of TableScrollPane. TableScrollPane is a special component which supports table row header, row footer and column footer.\n" +
                "\nIt also shows how to archive multiple line cell renderer. Try to resize \"Task\" column to make it smaller and see what happens.\n" +
                "\n" +
                "Demoed classes:\n" +
                "com.jidesoft.grid.TableScrollPane\n" +
                "com.jidesoft.grid.JideTable";
    }

    public String getProduct() {
        return PRODUCT_NAME_GRIDS;
    }

    public Component getDemoPanel() {
        JPanel panel = new JPanel();
        panel.setPreferredSize(new Dimension(800, 400));
        panel.setLayout(new BorderLayout());
        panel.add(createTabbedPane(), BorderLayout.CENTER);
        return panel;
    }

    private Component createTabbedPane() {
        JideTabbedPane tabbedPane = new JideTabbedPane();
        tabbedPane.setBoxStyleTab(true);
        tabbedPane.addTab("Timesheet Example", createTablePane());
        tabbedPane.addTab("Table in JScrollPane (for comparision)", new JScrollPane(new SortableTable(new TimeSheetTableModelEx())));
        return tabbedPane;

    }

    private TableScrollPane createTablePane() {
        _model = new TimeSheetTableModelEx();
        _totalModel = new TimeSheetTotalTableModel(_model);
        TableScrollPane pane = new TableScrollPane(_model, _totalModel, true) {
            public TableCustomizer getTableCustomizer() {
                return new TableCustomizer() {
                    public void customize(JTable table) {
// uncomment the following line to allow multipline line renderer and make row height adjust automatically
//                        ((JideTable) table).setRowAutoResizes(true);
// uncomment the following line to have a fixed row height
                        table.setRowHeight(20);
                    }
                };
            }
        };
        pane.getRowHeaderTable().getColumnModel().getColumn(0).setPreferredWidth(25);
        pane.getRowHeaderTable().getColumnModel().getColumn(1).setPreferredWidth(100);
        pane.getRowHeaderTable().getColumnModel().getColumn(2).setPreferredWidth(60);
        pane.getRowHeaderTable().getColumnModel().getColumn(3).setPreferredWidth(60);
        pane.getRowHeaderTable().getColumnModel().getColumn(4).setPreferredWidth(40);
        pane.getRowHeaderTable().getColumnModel().getColumn(0).setCellRenderer(new SortTableHeaderRenderer());
        pane.getRowHeaderTable().setBackground(new Color(255, 254, 203));
        pane.getMainTable().setBackground(new Color(255, 254, 203));
        pane.getRowFooterTable().setBackground(new Color(178, 178, 142));

        JLabel label = new JLabel("Total: ");
        label.setHorizontalAlignment(SwingConstants.TRAILING);
        label.setVerticalAlignment(SwingConstants.TOP);
        pane.setCorner(JScrollPane.LOWER_LEFT_CORNER, label);

        pane.getColumnFooterTable().setBackground(new Color(178, 178, 142));

        return pane;
    }

    public String getDemoFolder() {
        return "G10. TableScrollPane";
    }

    static public void main(String[] s) {
        LookAndFeelFactory.installDefaultLookAndFeelAndExtension();
        showAsFrame(new TableScrollPaneDemo());
    }

    static String[] HEADER = new String[]{
            "#", "Task", "Billing", "Project", "Location", "Mon 8-2", "Tue 8-3", "Wed 8-4", "Thu 8-5", "Fri 8-6", "Sat 8-7", "Sun 8-8", "Task Total"
    };

    static Object[][] ENTRIES = new Object[][]{
            new Object[]{null, "a", "Non-Billable", "Project 1", "NY", "", "2", "", "2", "", "", "", null},
            new Object[]{null, "c", "Time-off", "", "NY", "", "", "", "", "4", "", "", null},
            new Object[]{null, "g", "Time-off", "", "NY", "", "", "", "", "8", "", "", null},
            new Object[]{null, "b", "Billable", "Project 2", "NJ", "", "", "", "8", "", "", "", null},
            new Object[]{null, "Website design", "Billable", "Project 2", "NJ", "4", "", "", "", "", "", "", null},
            new Object[]{null, "Customer Support", "Billable", "Project 2", "NJ", "4", "", "", "", "", "", "", null}
    };

    // Sick, Vacation,

    class TimeSheetTableModelEx extends AbstractMultiTableModel {
        public String getColumnName(int column) {
            return HEADER[column];
        }

        public int getColumnCount() {
            return HEADER.length;
        }

        public int getRowCount() {
            return 20;
        }

        public Class getColumnClass(int columnIndex) {
            return String.class;
        }

        public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
            if (rowIndex >= ENTRIES.length) {
                // skip for now
            }
            else if (columnIndex == 0) {
                // no editable
            }
            else if (columnIndex == HEADER.length - 1) { //last column
                // no editable
            }
            else {
                ENTRIES[rowIndex][columnIndex] = aValue;
                if (columnIndex >= 5 && columnIndex <= 11) {
                    // update column total
                    ((AbstractTableModel) _totalModel).fireTableCellUpdated(0, columnIndex - 5);
                    // update row total
                    fireTableCellUpdated(rowIndex, getColumnCount() - 1);
                }
            }
        }

        public Object getValueAt(int rowIndex, int columnIndex) {
            if (columnIndex == 0) {
                return new Integer(rowIndex + 1);
            }
            else if (rowIndex >= ENTRIES.length) {
                if (columnIndex >= 5 && columnIndex <= 11) {
                    return "";
                }
                else if (columnIndex == HEADER.length - 1) {
                    return "";
                }
                else {
                    return "";
                }
            }
            else if (columnIndex == HEADER.length - 1) { //last column
                double total = 0.0;
                for (int i = 5; i <= 11; i++) {
                    try {
                        try {
                            total += Double.parseDouble((String) ENTRIES[rowIndex][i]);
                        }
                        catch (NumberFormatException e) {
                        }
                    }
                    catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                return "" + total;
            }
            else {
                return ENTRIES[rowIndex][columnIndex];
            }
        }

        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return columnIndex >= 5 && columnIndex != HEADER.length - 1; // only cells in the main table are editable.
        }

        public int getTableIndex(int columnIndex) {
            return 0;
        }

        public int getColumnType(int column) {
            if (column <= 4) {
                return HEADER_COLUMN;
            }
            else if (column == HEADER.length - 1) {
                return FOOTER_COLUMN;
            }
            else {
                return REGULAR_COLUMN;
            }
        }
    }

    class TimeSheetTotalTableModel extends AbstractMultiTableModel {
        TableModel _model;

        public TimeSheetTotalTableModel(TableModel model) {
            _model = model;
        }

        public int getColumnCount() {
            return 7;
        }

        public int getRowCount() {
            return 1;
        }

        public Class getColumnClass(int columnIndex) {
            return String.class;
        }

        public Object getValueAt(int rowIndex, int columnIndex) {
            double total = 0.0;
            for (int i = 0; i < _model.getRowCount(); i++) {
                try {
                    total += Double.parseDouble((String) _model.getValueAt(i, columnIndex + 5));
                }
                catch (NumberFormatException e) {
                }
            }
            return "" + total;
        }

        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return false;
        }

        public int getColumnType(int column) {
            return REGULAR_COLUMN;
        }

        public int getTableIndex(int columnIndex) {
            return 0;
        }
    }
}
