/*
 * Copyright (C) The avalon Software Foundation. All rights reserved.
 *
 * This software is published under the terms of the avalon Software License
 * version 1.1, a copy of which has been included with this distribution in
 * the LICENSE file.
 */
package org.apache.avalon.db.test;

import javax.swing.*;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Driver;
import java.sql.ResultSet;    
import java.sql.Statement;
import java.sql.SQLException;
    
import java.awt.*;
import java.awt.event.*;

import org.apache.avalon.db.driver.AvalonDBDriver;

/**
 * TestGUI for testing the AvalonDB.
 *
 * @author Gerhard Froehlich <a href="mailto:g-froehlich@gmx.de">g-froehlich@gmx.de</a>
 */
public class TestGUI {

    private Connection con = null;
    private final JButton execute = new JButton("Execute");
    private final JButton connect = new JButton("Connect");
    private final JButton test = new JButton("test");
    private final JTextArea inputArea = new JTextArea();
    private final JTextArea outputArea = new JTextArea();
    private final JTextField connectField = new JTextField();
    private final JScrollPane inputAreaScrollPane = new JScrollPane();
    private final JScrollPane outputAreaScrollPane = new JScrollPane();

    public TestGUI() {
        setupDBDriver();
    }

    public Component createComponents() {
        
        outputArea.setEditable(false);
        inputAreaScrollPane.setViewportView(inputArea);
        outputAreaScrollPane.setViewportView(outputArea);
        
        connect.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    if(!connectField.getText().equals("")) {
                        System.out.println("Fired JDBC connect!");
                        con = DriverManager.getConnection(connectField.getText(),null);
                        outputArea.setText("Connection established!");
                    } else {
                        outputArea.setText("Please insert JDBC URL!");
                    }
                } catch (Exception ex) {
                    handleException(ex);
                }
            }
        });
    

        execute.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(con != null) {
                    try {
                        Statement statement = con.createStatement();
                        ResultSet rs = statement.executeQuery(inputArea.getText());
                    } catch (SQLException slqe) {
                        handleException(slqe);
                    }
                    //TODO rs processing
                } else {
                    outputArea.setText("No connection!");
                }
            }
        });

        JPanel pane = new JPanel();
        pane.setBorder(BorderFactory.createEmptyBorder(
                                        5, //top
                                        5, //left
                                        5, //bottom
                                        5) //right
                       
                                 );
        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        pane.setLayout(gridbag);
        c.fill = GridBagConstraints.HORIZONTAL; 
        
        //Connect Text Field
        c.gridx = 0;
        c.gridy = 0;
        c.ipadx = 250;
        c.ipady = 8;
        gridbag.setConstraints(connectField, c);
        pane.add(connectField);

        c.gridx = 1;
        c.gridy = 0;
        c.ipadx = 0;
        c.ipady = 0;
        gridbag.setConstraints(connect, c);
        pane.add(connect);

        c.gridx = 2;
        c.gridy = 0;
        c.ipadx = 0;
        c.ipady = 0;
        gridbag.setConstraints(execute, c);
        pane.add(execute);

        c.gridx = 0;
        c.gridy = 1;
        c.gridwidth = 3;
        c.ipadx = 300;
        c.ipady = 100;
        c.insets = new Insets(2,0,0,0);
        gridbag.setConstraints(inputAreaScrollPane, c);
        pane.add(inputAreaScrollPane);

        c.gridx = 0;
        c.gridy = 2;
        c.gridwidth = 3;
        c.insets = new Insets(2,0,0,0);
        c.ipadx = 300;
        c.ipady = 100;
        gridbag.setConstraints(outputAreaScrollPane, c);
        pane.add(outputAreaScrollPane);
        return pane;
    }

    public static void main(String args[]) {
        //Set the Look and Feel
        try {
            UIManager.setLookAndFeel(
                UIManager.getCrossPlatformLookAndFeelClassName());
        } catch (Exception e) { }
    
        JFrame frame = new JFrame("TestGUI");
        TestGUI app = new TestGUI();
        Component contents = app.createComponents();
        frame.getContentPane().add(contents, BorderLayout.CENTER);
    
        //Finish setting up the frame, and show it.
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        frame.pack();
        frame.setSize(450, 330);
        frame.setVisible(true);
        frame.setResizable(false);
    }

    private void setupDBDriver() {
        try {
            Driver driver = (Driver)Class.forName("org.apache.avalon.db.driver.AvalonDBDriver").newInstance();
            DriverManager.registerDriver(driver);
        } catch (Exception e) {
            handleException(e);
        }   
    }

    private void handleException(Throwable exception) {
        System.out.println("--------- UNCAUGHT EXCEPTION ---------");
    	exception.printStackTrace(System.out);
        outputArea.setText("--------- UNCAUGHT EXCEPTION ---------" + "\n" 
                                  + exception
                                  + "\n" + exception.getMessage());
    }
}
