/*
 * ViewXML.java
 *
 * Created on May 10, 2002, 4:17 PM
 */

package com.imsl.demo.xml;
import java.io.*;
import java.util.*;
import javax.swing.JOptionPane;

/**
 *
 * @author  brophy
 */
public class Viewer extends javax.swing.JFrame {
    private XMLPanel    xmlPanel;
    private boolean     exitOnClose;
    
    /** Creates new form ViewXML */
    public Viewer(boolean exitOnClose) {
        this.exitOnClose = exitOnClose;
        initComponents();
        xmlPanel = new XMLPanel();
        
        try {
            InputStream is = getClass().getResourceAsStream("list.txt");
            Reader isr = new InputStreamReader(is);
            LineNumberReader r = new LineNumberReader(isr);
            while (true) {
                String line = r.readLine();
                if (line == null)  break;
                StringTokenizer st = new StringTokenizer(line, "=");
                String key = st.nextToken();
                String title = st.nextToken();
                jComboBox.addItem(new Entry(key,title));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        jPanelMain.add(xmlPanel, java.awt.BorderLayout.CENTER);
        pack();
    }
    
    /** 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
        jPanelMain = new javax.swing.JPanel();
        jPanelControls = new javax.swing.JPanel();
        jComboBox = new javax.swing.JComboBox();
        jButtonBack = new javax.swing.JButton();
        Next = new javax.swing.JButton();

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

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

        jPanelControls.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        jComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxActionPerformed(evt);
            }
        });

        jPanelControls.add(jComboBox);

        jButtonBack.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/imsl/demo/xml//Back16.gif")));
        jButtonBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonBackActionPerformed(evt);
            }
        });

        jPanelControls.add(jButtonBack);

        Next.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/imsl/demo/xml/Forward16.gif")));
        Next.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NextActionPerformed(evt);
            }
        });

        jPanelControls.add(Next);

        jPanelMain.add(jPanelControls, java.awt.BorderLayout.NORTH);

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

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

    private void jButtonBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonBackActionPerformed
        int index = jComboBox.getSelectedIndex() - 1;
        if (index < 0)  index = jComboBox.getItemCount() - 1;
        jComboBox.setSelectedIndex(index);
    }//GEN-LAST:event_jButtonBackActionPerformed

    private void NextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NextActionPerformed
        int index = jComboBox.getSelectedIndex() + 1;
        if (index == jComboBox.getItemCount())  index = 0;
        jComboBox.setSelectedIndex(index);
    }//GEN-LAST:event_NextActionPerformed

    private void jComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxActionPerformed
        try {
            Entry entry = (Entry)jComboBox.getSelectedItem();
            if (entry != null)  xmlPanel.setInputStream(getClass().getResourceAsStream(entry.key+".xml"));;
        } catch (java.io.IOException e) {
            JOptionPane.showMessageDialog(this, e.toString(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jComboBoxActionPerformed
    
    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
        if (exitOnClose)  System.exit(0);
    }//GEN-LAST:event_exitForm
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        boolean exitOnClose = true;
        if (args.length > 0  && args[0].equals("-noexit"))  exitOnClose = false;
        new Viewer(exitOnClose).show();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanelMain;
    private javax.swing.JPanel jPanelControls;
    private javax.swing.JComboBox jComboBox;
    private javax.swing.JButton Next;
    private javax.swing.JButton jButtonBack;
    // End of variables declaration//GEN-END:variables
    
        
    static class Entry {
        String  key;
        String  title;
        
        Entry(String key, String title) {
            this.key = key;
            this.title = title;
        }
        
        public String toString() {
            return title;
        }
    }
    
}