/*
 * Demos.java
 *
 * Created on February 9, 2003, 4:39 PM
 */

/**
 * Convenient way of running demos.
 *
 * @author  Dave Voorhis
 */
public class Demo extends javax.swing.JFrame {
    
    /** Creates a new instance of Demo */
    public Demo() {
        setTitle("DBAppBuilder -- Demonstration Programs");
        getContentPane().setLayout(new javax.swing.BoxLayout(getContentPane(), javax.swing.BoxLayout.Y_AXIS));
        setSize(600, 400);
        
        addDemo(Demos.DatabaseDefinitions.ConnectionManager.class);
        addDemo(Demos.DatabaseDefinitions.DataModel.class);
        addDemo(Demos.Demo01.EmployeeForm.class);
        addDemo(Demos.Demo02.Demo02.class);
        addDemo(Demos.Demo03.PropertyTableDemo.class);
        addDemo(Demos.Demo04.EnumComboBoxDemo.class);
        addDemo(Demos.Demo04.EnumListDemo.class);
        
        show();
    }

    /** Add a demo program. */
    public void addDemo(final Class runnable) {
        javax.swing.JPanel demoPanel = new javax.swing.JPanel();
        demoPanel.setLayout(new java.awt.BorderLayout());

        String description;
        try {
            java.lang.reflect.Method descriptionMethod = runnable.getMethod("getDescription", null);
            description = (String)descriptionMethod.invoke(null, null);
        } catch (Exception e) {
            description = "<i>No description</i>";
        }
        
        String htmlDescription = "<html><u>" + runnable.getName() + "</u> -- " + description + "</html>";
        
        javax.swing.JLabel descLabel = new javax.swing.JLabel(htmlDescription);
        descLabel.setFont(new java.awt.Font("Dialog", 0, 10));
        demoPanel.add(descLabel, java.awt.BorderLayout.CENTER);
        
        javax.swing.JButton runButton = new javax.swing.JButton("Run");
        runButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {
                java.lang.reflect.Method runMethod;
                try {
                    runMethod = runnable.getMethod("run", null);
                } catch (java.lang.NoSuchMethodException nsme) {
                    System.out.println("Class " + runnable.getName() + " does not have a 'public static void run()' method.");
                    return;
                }
                try {
                    runMethod.invoke(null, null);
                } catch (java.lang.IllegalAccessException iae) {
                    System.out.println("Illegal access exception: " + iae.toString());
                } catch (java.lang.reflect.InvocationTargetException ite) {
                    System.out.println("Invocation target exception: " + ite.toString() + ": " + ite.getTargetException().toString());
                }
            }
        });
        
        demoPanel.add(runButton, java.awt.BorderLayout.EAST);
        
        getContentPane().add(demoPanel);
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Demo demorunner = new Demo();
        demorunner.addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent e) {
                System.exit(0);
            }
        });
    }
    
}
