import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.*;
import javax.swing.*;
import com.gestalt.scribe.*; 
public class Test2 implements DrillDownReportListener
{
    private Connector conn;
/*
**  Demo of the app using Scribe API to produce a report and display it
**  in its own window. Drill-down reports are displayed in displayReport()
**  method of Test2 called by Connector object.
*/
    private JFrame frame;
    public static void main(String args[])
    {
        new Test2();
    }
    public Test2()
    {
/*
**      Connect to Scribe Server
*/
        System.out.println("Connecting...");
        try
        {
            conn = new Connector("localhost", 6541, "heidi", "hello");
            conn.setDelegate(this);
        }
        catch (ServerConnectionException e)
        {
            System.out.println(e);
            System.exit(1);
        }
        System.out.println("DONE");
/*
**      Run report
*/
        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");
            Vector lines = conn.getReport(reportName, parameterList);
/*
**          The server returns a Vector of objects that should be fed into ReportPanel
**          object (part of the API library).
*/
            frame = new JFrame("Test " + reportName);
            frame.addNotify();
            frame.pack();
            ReportPanel view = new ReportPanel(lines, reportName);
            JScrollPane scrollPane = new JScrollPane(view);
            scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
            frame.getContentPane().add(scrollPane);
            frame.setSize(628, 500);
            frame.validate();
            frame.setVisible(true);
            frame.addWindowListener(new WindowAdapter()
            {
                public void windowClosing(WindowEvent evt)
                {
                    if (conn != null)
                        conn.disconnect();
                    System.exit(0);
                }
            });
        }
        catch (Exception e)
        {
            System.out.println(e);
        }
    }
/*
**  Implementation of DrillDownReportListener interface
*/
    public void displayReport(String reportName, Vector lines)
    {
        frame = new JFrame("Test - drill down (" + reportName + ")");
        frame.addNotify();
        frame.pack();
        ReportPanel view = new ReportPanel(lines, reportName);
        JScrollPane scrollPane = new JScrollPane(view);
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        frame.getContentPane().add(scrollPane);
        frame.setSize(628, 500);
        frame.validate();
        frame.setVisible(true);
    }
}