import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.gestalt.scribe.*;
public class Test1 implements ActionListener
{
    private Connector conn;
    private JFrame frame;
    private JButton runButton, quitButton;
/*
**  Demo of the app using Scribe API to produce a report and display it
**  in the window managed by the API library.
*/
    public static void main(String args[])
    {
        new Test1();
    }
    public Test1()
    {
        frame = new JFrame("Scribe API test");
        frame.addNotify();
        frame.pack();
        Container contentPane = frame.getContentPane();
        contentPane.setLayout(new FlowLayout());
        Font textFont = new Font("SansSerif", Font.PLAIN, 12);
        runButton = new JButton("Run");
        runButton.setFont(textFont);
        runButton.setSize(runButton.getPreferredSize());
        runButton.addActionListener(this);
        quitButton = new JButton("Quit");
        quitButton.setFont(textFont);
        quitButton.setSize(runButton.getPreferredSize());
        quitButton.addActionListener(this);
        contentPane.add(runButton);
        contentPane.add(quitButton);
        frame.setSize(200, 60);
        frame.validate();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent evt)
            {
                if (conn != null)
                    conn.disconnect();
                System.exit(0);
            }
        });
    }
    public void actionPerformed(ActionEvent evt)
    {
        Object source = evt.getSource();
        if (source == runButton)
        {
            connect();
        }
        else if (source == quitButton)
        {
            if (conn != null)
                conn.disconnect();
            System.exit(0);
        }
    }
    private void connect()
    {
/*
**      Connect to Scribe Server
*/
        System.out.println("Connecting...");
        try
        {
            conn = new Connector("localhost", 6541, "heidi", "hello");
        }
        catch (ServerConnectionException e)
        {
            System.out.println(e);
            System.exit(1);
        }
        System.out.println("DONE");
/*
**      Run report and display it in Connector-managed window
*/
        String reportName = "Query/Customer";
        try
        {
/*
**          The report "Query/Customer" takes one parameter - customer ID.
**          We run this report for the customer "VINET". The parameter is
**          passed to the server as a Parameter object (part of the API library)
**          stored in a Vector.
*/
            Vector parameterList = conn.getReportParameters(reportName);
            Parameter packet = (Parameter) parameterList.elementAt(0);
            packet.setValue("VINET");
            conn.displayReport(reportName, parameterList);
        }
        catch (Exception e)
        {
            System.out.println(e);
        }
    }
}