/*
 * -------------------------------------------------------------------------
 *      $Id: Driver.java,v 1.13 2005/12/30 16:57:07 estewart Exp $
 * -------------------------------------------------------------------------
 *      Copyright (c) 1999 Visual Numerics Inc. All Rights Reserved.
 *
 *      This software is confidential information which is proprietary to
 *      and a trade secret of Visual Numerics, Inc.  Use, duplication or
 *      disclosure is subject to the terms of an appropriate license
 *      agreement.
 *
 *      VISUAL NUMERICS MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
 *      SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING
 *      BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY,
 *      FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. VISUAL
 *      NUMERICS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 *      AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR
 *      ITS DERIVATIVES.
 *--------------------------------------------------------------------------
 */

package com.imsl.demo.gallery;
import javax.swing.JButton;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.List;
import java.util.jar.*;

/**
 *
 * @author  brophy
 * @created February 6, 2002
 */
public class Driver extends javax.swing.JFrame {
    private class Item {
        int     group;
        String  title;

        Item (int group, String title) {
            this.group = group;
            this.title = title;
        }

        public String toString() {
            return title;
        }
    }

    private final Demo  demoList[];
    private Describe    describe;
    private java.awt.Color vniBlueDark = new java.awt.Color(0, 51, 102);
    private java.awt.Color vniBlue = new java.awt.Color(51, 102, 153);
    private java.awt.Color vniBlueLight = new java.awt.Color(102, 153, 204);

    /** Creates new form Driver */
    public Driver() {
        demoList = Binding.getDemos();
        initComponents();
        jComboBoxCategory.addItem(new Item(-1,"All"));
        jComboBoxCategory.addItem(new Item(Demo.GROUP_MATH,"Math"));
        jComboBoxCategory.addItem(new Item(Demo.GROUP_STAT,"Statistics"));
        jComboBoxCategory.addItem(new Item(Demo.GROUP_FINANCE,"Finance"));
        jComboBoxCategory.addItem(new Item(Demo.GROUP_CHART,"Chart"));
        jComboBoxCategory.addItem(new Item(Demo.GROUP_QS,"Quick Start"));
        createButtons(-1);
        pack();

        describe = new Describe(this);
        java.awt.Dimension size = getSize();
        describe.setLocation(size.width,0);
        describe.show();

    }

    private void createButtons(int group) {
        jPanelButtons.removeAll();
        for (int k = 0;  k < demoList.length;  k++) {
            if ((group&demoList[k].group) != 0) {
                JButton b = new JButton(demoList[k].title);
                b.setForeground(vniBlueDark);
                b.setFont(new java.awt.Font("Verdana", 1, 11));
                b.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
                b.setPreferredSize(new java.awt.Dimension(185,25));
                final String main = demoList[k].mainClass;
                final String page = demoList[k].page;
                final List classpath = demoList[k].classpath;
                b.addActionListener(new java.awt.event.ActionListener() {
                    public void actionPerformed(java.awt.event.ActionEvent evt) {
                        run(main, classpath, page);
                    }
                });
                jPanelButtons.add(b);
            }
        }
        pack();
        repaint();
    }


    private String findMainClass(String jarFilename) {
        try {
            JarFile jf = new JarFile(jarFilename);
            Manifest manifest = jf.getManifest();
            if (manifest != null) {
                Attributes attr = manifest.getMainAttributes();
                String attrname = "Main-Class";
                return attr.getValue("Main-Class");
            }
        } catch (java.io.IOException e) {
        }
        return null;
    }


    private void run(String main, List listClasspath, String page) {
        try {
            //describe.setVisible(page != null);
            //if (page != null)  describe.setPage(page, true);
            describe.setVisible(false);

            Class classMain = null;
            if (listClasspath != null  && listClasspath.size() > 0) {
                String sClasspath[] = new String[0];
                sClasspath = (String[])listClasspath.toArray(sClasspath);
                if (main == null) {
                    for (int k = 0;  k < sClasspath.length;  k++) {
                        main = findMainClass(sClasspath[k]);
                        if (main != null)  break;
                    }
                }

                URL url[]  = new URL[sClasspath.length];
                for (int k = 0;  k < url.length;  k++) {
                    try {
                        url[k] = new URL(sClasspath[k]);
                    } catch (java.net.MalformedURLException e) {
                        try {
                            url[k] = new URL("file:"+sClasspath[k]);
                        } catch (java.net.MalformedURLException e2) {
                            e2.printStackTrace();
                        }
                    }
                }
                classMain = new URLClassLoader(url).loadClass(main);
            }
            if (classMain == null) classMain = Class.forName(main);
            String argsMain[] = {"-noexit"};
            Method methodMain = classMain.getMethod("main", new Class[]{argsMain.getClass()});
            methodMain.invoke(null, new Object[]{argsMain});
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel(new javax.swing.ImageIcon(getClass().getResource("/com/imsl/demo/gallery/blue_logo.gif")));
        jComboBoxCategory = new javax.swing.JComboBox();
        jScrollPaneButtons = new javax.swing.JScrollPane();
        jPanelButtons = new javax.swing.JPanel();

        setTitle("JMSL Demos");
        setBackground(vniBlue);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        jPanel2.setLayout(new java.awt.BorderLayout());

        jPanel2.setBackground(vniBlueDark);
        jPanel2.setForeground(vniBlueLight);
        jPanel2.setMaximumSize(new java.awt.Dimension(0, 0));
        jPanel2.setPreferredSize(new java.awt.Dimension(200,(int)(25+getToolkit().getScreenSize().height/6)));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("                  ");
        jLabel1.setPreferredSize(new java.awt.Dimension(200,(int)(25+getToolkit().getScreenSize().height/6)));
        jLabel1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jPanel2.add(jLabel1, java.awt.BorderLayout.CENTER);

        jComboBoxCategory.setFont(new java.awt.Font("Verdana", 1, 11));
        jComboBoxCategory.setForeground(vniBlueDark);
        jComboBoxCategory.setMaximumSize(new java.awt.Dimension(0, 0));
        jComboBoxCategory.setMinimumSize(new java.awt.Dimension(0, 0));
        jComboBoxCategory.setPreferredSize(new java.awt.Dimension(200, 25));
        jComboBoxCategory.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxCategoryActionPerformed(evt);
            }
        });

        jPanel2.add(jComboBoxCategory, java.awt.BorderLayout.SOUTH);

        getContentPane().add(jPanel2, java.awt.BorderLayout.NORTH);

        jScrollPaneButtons.setBackground(vniBlue);
        jScrollPaneButtons.setForeground(vniBlueLight);
        jScrollPaneButtons.setHorizontalScrollBarPolicy(javax.swing.JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        jScrollPaneButtons.setVerticalScrollBarPolicy(javax.swing.JScrollPane.VERTICAL_SCROLLBAR_NEVER);
        jScrollPaneButtons.setMaximumSize(new java.awt.Dimension(0, 0));
        jScrollPaneButtons.setMinimumSize(new java.awt.Dimension(0, 0));
        jScrollPaneButtons.setPreferredSize(new java.awt.Dimension(200, 543));
        jPanelButtons.setLayout(new java.awt.GridLayout(22, 1));

        jPanelButtons.setForeground(new java.awt.Color(0, 0, 50));
        jPanelButtons.setMaximumSize(new java.awt.Dimension(0, 0));
        jPanelButtons.setPreferredSize(new java.awt.Dimension(185, 750));
        jScrollPaneButtons.setViewportView(jPanelButtons);

        getContentPane().add(jScrollPaneButtons, java.awt.BorderLayout.CENTER);

        pack();
    }//GEN-END:initComponents

    private void jComboBoxCategoryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxCategoryActionPerformed
        createButtons(((Item)jComboBoxCategory.getSelectedItem()).group);
    }//GEN-LAST:event_jComboBoxCategoryActionPerformed

    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
        System.exit(0);
    }//GEN-LAST:event_exitForm

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        try {
            new Driver().show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JComboBox jComboBoxCategory;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanelButtons;
    private javax.swing.JScrollPane jScrollPaneButtons;
    // End of variables declaration//GEN-END:variables
}