/*
 * LVeverything.java
 *
 * Created on December 10, 2004, 7:47 PM
 */

package LVeverything;
import LotkaVolterra.*;
import java.awt.FileDialog;
import java.io.*;

/**
 *
 * @author  default
 */
public class LVeverything extends javax.swing.JFrame {
    
    /** Creates new form LVeverything */
    public LVeverything() {
        initComponents();
    }
    
    /** 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
        jTextArea1 = new javax.swing.JTextArea();
        menuBar = new javax.swing.JMenuBar();
        fileMenu = new javax.swing.JMenu();
        openMenuItem = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JSeparator();
        exitMenuItem = new javax.swing.JMenuItem();
        infoMenu = new javax.swing.JMenu();
        calcLLEMenuItem = new javax.swing.JMenuItem();
        connMenuItem = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Lotka Volterra");
        jTextArea1.setMinimumSize(new java.awt.Dimension(200, 300));
        jTextArea1.setPreferredSize(new java.awt.Dimension(300, 300));
        getContentPane().add(jTextArea1, java.awt.BorderLayout.CENTER);

        fileMenu.setText("File");
        openMenuItem.setText("Open");
        openMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openMenuItemActionPerformed(evt);
            }
        });

        fileMenu.add(openMenuItem);

        fileMenu.add(jSeparator1);

        exitMenuItem.setText("Exit");
        exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitMenuItemActionPerformed(evt);
            }
        });

        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        infoMenu.setText("Info");
        calcLLEMenuItem.setText("Largest LE");
        calcLLEMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                calcLLEMenuItemActionPerformed(evt);
            }
        });

        infoMenu.add(calcLLEMenuItem);

        connMenuItem.setText("Connectivity");
        connMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                connMenuItemActionPerformed(evt);
            }
        });

        infoMenu.add(connMenuItem);

        menuBar.add(infoMenu);

        setJMenuBar(menuBar);

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

    private void connMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_connMenuItemActionPerformed
        if (animalsLoaded) {
            if (LVmatrix.isConnected(animal)) {
                jTextArea1.setText(jTextArea1.getText() + "\n\n It is Totally Connected.");
            } else {
                jTextArea1.setText(jTextArea1.getText() + "\n\n It is NOT Totally Connected.");
            }
        } else {
            jTextArea1.setText("No LV model system loaded.");
        }
    }//GEN-LAST:event_connMenuItemActionPerformed

    private void calcLLEMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_calcLLEMenuItemActionPerformed
        if (animalsLoaded) {
            LVroutines.evolveThruTime(animal, 4500, 0.01);
            jTextArea1.setText(jTextArea1.getText() + "\n\n Largest LE = " + LVroutines.calculateLE(animal, 10000, 0.01));
        } else {
            jTextArea1.setText("No LV model system loaded.");
        }
    }//GEN-LAST:event_calcLLEMenuItemActionPerformed

    private void openMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openMenuItemActionPerformed
        FileDialog fileDialog = new FileDialog(this, "Open...", FileDialog.LOAD);
        fileDialog.setFile("*.dat");
        fileDialog.show();
        if (fileDialog.getFile() == null)
            return;
        fileName = fileDialog.getDirectory() + File.separator + fileDialog.getFile();
        
        
        
        //Now show information about this stuff      
        animal = LVroutines.createAnimals(fileName);
        animalsLoaded = true;
        
        int numberOfSpecies = animal.length;
        int totalLinks = LVroutines.totalLinks(animal);
        
        String information = fileName;
        information = information + "\n\n N = " + numberOfSpecies;
        information = information + "\n Total Links = " + totalLinks;
        information = information + "\n <k> = " + ((double)totalLinks / numberOfSpecies);
        information = information + "\n <Connection Stength> = " + LVroutines.aveConnectionStength(animal);
        information = information + "\n Max k = " + LVroutines.maxLinks(animal);
        information = information + "\n Min k = " + LVroutines.minLinks(animal);
        
        jTextArea1.setText(information);
    }//GEN-LAST:event_openMenuItemActionPerformed
    
    private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed
        System.exit(0);
    }//GEN-LAST:event_exitMenuItemActionPerformed
    
    
    private String fileName = "";
    private boolean animalsLoaded = false;
    private Animal[] animal;
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        new LVeverything().show();
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem calcLLEMenuItem;
    private javax.swing.JMenuItem connMenuItem;
    private javax.swing.JMenuItem exitMenuItem;
    private javax.swing.JMenu fileMenu;
    private javax.swing.JMenu infoMenu;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem openMenuItem;
    // End of variables declaration//GEN-END:variables
    
}
