/*
 * Copyright (c) 2002 iReasoning Networks. All Rights Reserved.
 * 
 * This SOURCE CODE FILE, which has been provided by iReasoning Networks as part
 * of an iReasoning Software product for use ONLY by licensed users of the product,
 * includes CONFIDENTIAL and PROPRIETARY information of iReasoning Networks.  
 *
 * USE OF THIS SOFTWARE IS GOVERNED BY THE TERMS AND CONDITIONS 
 * OF THE LICENSE STATEMENT AND LIMITED WARRANTY FURNISHED WITH
 * THE PRODUCT.
 *
 * IN PARTICULAR, YOU WILL INDEMNIFY AND HOLD IREASONING SOFTWARE, ITS
 * RELATED COMPANIES AND ITS SUPPLIERS, HARMLESS FROM AND AGAINST ANY
 * CLAIMS OR LIABILITIES ARISING OUT OF THE USE, REPRODUCTION, OR
 * DISTRIBUTION OF YOUR PROGRAMS, INCLUDING ANY CLAIMS OR LIABILITIES
 * ARISING OUT OF OR RESULTING FROM THE USE, MODIFICATION, OR
 * DISTRIBUTION OF PROGRAMS OR FILES CREATED FROM, BASED ON, AND/OR
 * DERIVED FROM THIS SOURCE CODE FILE.
 */



package table;
import com.ultraswing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.io.File;

/**
 * This class demonstrates the usages of CTable class.
 */
public class Table
{
    private static CFrame _frame;
    private static JComponent _centerComp;

    private static CFrame createFrame()
    {
        CFrame frame = new CFrame();
        createFrame(frame);
        frame.setSize(800, 600);
        frame.centerOnScreen();
        frame.setVisible(true);
        frame.setExitOnClose();
        return frame;
    }

    private static void createFrame(CFrame frame)
    {
        frame.setTitle( "Table demo" );

        _centerComp = createMainWindow();
        frame.getContentPane().add( _centerComp, BorderLayout.CENTER );
        frame.getContentPane().add( createToolBar(), BorderLayout.NORTH);
        StatusBar status = new StatusBar( new JComponent[]
                {StatusBar.INDICATOR_STATUS,
                 StatusBar.SEPARATOR,
                 StatusBar.INDICATOR_DATE,
                 StatusBar.SEPARATOR,
                 StatusBar.INDICATOR_TIME,
                 StatusBar.SEPARATOR,
                 StatusBar.INDICATOR_JVM
                 });
        frame.addStatusBar(status);
    }

    private static JScrollPane createMainWindow()
    {
        Object[][] data = {
            {"Mary", "Campione",
             "Snowboarding", new Integer(5), new Boolean(false)},
            {"Alison", "Huml",
             "Rowing", new Integer(3), new Boolean(true)},
            {"Kathy", "Walrath",
             "Chasing toddlers", new Integer(2), new Boolean(false)},
            {"Mark", "Andrews", "Speed reading", new Integer(20), new Boolean(true)},
            {"Angela", "Lih", "Teaching high school", new Integer(4), new Boolean(false)}
        };

        String[] columnNames = {"First\nName\n",
                                "<html>Last<br>Name",
                                "Sport",
                                "# of Years",
                                "Vegetarian"};

        CTable table = new CTable(data, columnNames);
        int rowCount = table.getRowCount();
        ArrayList list = new ArrayList();
        for (int i = 0; i < rowCount ; i++)
        {
            list.add(new JLabel("rh" + i));//, getImage("new.gif"), SwingUtilities.LEFT));
        }
        table.setRowHeaderValues(list);
        table.setAlternate(new Color(233, 233, 233), Color.white, true);

        table.setRowSelectionAllowed(true);
        table.setSelectionBackground(Color.blue);
        table.setSelectionForeground(Color.white);

        JScrollPane scrollPane = new JScrollPane(table);
        table.setRowHeight(30);
        table.setRowHeight(2, 50);
        table.setRowHeight(0, 70);
        table.setRowHeaderEnabled(true);
        CTableRowHeader header = table.getCTableRowHeader();

        if(header != null)
        {
            header.setWidth(100);
            // header.setValueAt(new JLabel("test"), 1);
        }
        return scrollPane;
    }

    private static JScrollPane createMainWindow2(String driver, String url, String query)
    {
        try
        {
            ResultSetTableModel model = new ResultSetTableModel(driver, url, query);
            //"com.mysql.jdbc.Driver", "jdbc:mysql:///test?profileSql=true", "SELECT * FROM UPDATABLE ORDER BY pos1");
            CTable table = new CTable(model);

            table.setRowSelectionAllowed(true);
            table.setSelectionBackground(Color.blue);
            table.setSelectionForeground(Color.white);

            JScrollPane scrollPane = new JScrollPane(table);
            return scrollPane;
        }
        catch(Exception e)
        {
            System.out.println( e);
            return null;
        }
    }

    private static JScrollPane createMainWindow3(String fileName)
    {
        try
        {
            CTable table = new CTable(fileName, true);

            table.setRowSelectionAllowed(true);
            table.setSelectionBackground(Color.blue);
            table.setSelectionForeground(Color.white);

            JScrollPane scrollPane = new JScrollPane(table);
            return scrollPane;
        }
        catch(Exception e)
        {
            System.out.println( e);
            return null;
        }
    }

    private static CToolBar createToolBar()
    {
        CToolBar toolbar = new CToolBar();
        ToolBarButton btn = new ToolBarButton("CSV table");
        btn.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    FileDialog fdlg = new FileDialog(_frame);
                    fdlg.show();
                    String file = fdlg.getFile();
                    String dir = fdlg.getDirectory();
                    if(file != null)
                    {
                        File f = new File(dir + File.separator + file);
                        System.out.println("file=" + f.getAbsolutePath());
                        JScrollPane pane = createMainWindow3(f.getAbsolutePath());
                        _frame.getContentPane().remove(_centerComp);
                        _frame.getContentPane().add(pane, BorderLayout.CENTER);
                        _centerComp = pane;
                        _frame.validate();
                        _frame.repaint();
                    }
                }
            });
        toolbar.add(btn);
        btn = new ToolBarButton("Database table");
        btn.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    String driver = null;
                    String url = null;
                    String query = null;
                    if( (driver = JOptionPane.showInputDialog(null, "Enter JDBC driver string")) != null &&
                        (url = JOptionPane.showInputDialog(null, "Enter JDBC url string")) != null &&
                        (query = JOptionPane.showInputDialog(null, "Enter SQL query")) != null)
                    {
                        JScrollPane pane = createMainWindow2(driver, url, query);
                        _frame.getContentPane().remove(_centerComp);
                        _frame.getContentPane().add(pane, BorderLayout.CENTER);
                        _centerComp = pane;
                        _frame.validate();
                        _frame.repaint();
                    }
                }
            });
        toolbar.add(btn);
        return toolbar;
    }

    public static CFrame getFrame()
    {
        return _frame;
    }

    public static void setFrame(CFrame f)
    {
        _frame = f;
    }

    public static void main(String[] args)
    {
        try
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch(Exception e)
        {
            System.out.println( "setLookAndFeel failed ..........................." );
        }
        _frame = createFrame();
    }
}// end of class Table