/*
 * finalGUI_0_1.java
 *
 * Created on 16 May 2005, 10:31
 */

/**
 *
 * @author  mnaylor
 */
public class finalGUI_0_1 extends javax.swing.JFrame {
    public static LomnitzAutonome world;
    public static Thread viewerThread;    
    private static String fileNameRoot;
    private static int neighbourNumber,Nx,runtimeDuration;

    /**
     * Conservation factor
     */
    private static double beta;

    private static double maxStrength, minStrength;
    private static boolean usePeriodicBC;
    private static int autonomeThreadSleepDuration;
    private static boolean isFirstTime = true;

    /** Creates new form finalGUI_0_1 */
    public finalGUI_0_1() {
        initComponents();
        setDefaultParameters();
        setWorldParameters();
   }
    
    /** 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
        java.awt.GridBagConstraints gridBagConstraints;

        autonomePane = new javax.swing.JScrollPane();
        autonomePane.setViewportView( world );
        boundaryConditionComboBox = new javax.swing.JComboBox();
        betaLabel = new javax.swing.JLabel();
        betaStringInput = new javax.swing.JTextField();
        neighbourNumberComboBox = new javax.swing.JComboBox();
        minStrengthLabel = new javax.swing.JLabel();
        maxStrengthLabel = new javax.swing.JLabel();
        maxStrengthStringInput = new javax.swing.JTextField();
        minStrengthStringInput = new javax.swing.JTextField();
        startButton = new javax.swing.JButton();
        pauseButton = new javax.swing.JButton();
        resumeButton = new javax.swing.JButton();
        saveButton = new javax.swing.JButton();
        resetButton = new javax.swing.JButton();
        relaxationRangeLabel = new javax.swing.JLabel();
        loadingNoiseLabel = new javax.swing.JLabel();
        relaxationRangeStringInput = new javax.swing.JTextField();
        loadingNoiseStringInput = new javax.swing.JTextField();
        outputFilenameStringInput = new javax.swing.JTextField();
        outputFilenameLabel = new javax.swing.JLabel();

        getContentPane().setLayout(new java.awt.GridBagLayout());

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        autonomePane.setMinimumSize(new java.awt.Dimension(300, 100));
        autonomePane.setPreferredSize(new java.awt.Dimension(1000, 380));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 8;
        gridBagConstraints.weighty = 1.0;
        getContentPane().add(autonomePane, gridBagConstraints);

        boundaryConditionComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Open BC", "Periodic BC" }));
        boundaryConditionComboBox.setMinimumSize(new java.awt.Dimension(138, 24));
        boundaryConditionComboBox.setPreferredSize(new java.awt.Dimension(138, 24));
        boundaryConditionComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boundaryConditionComboBoxActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 2;
        getContentPane().add(boundaryConditionComboBox, gridBagConstraints);

        betaLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        betaLabel.setText("Beta = ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 2, 0, 2);
        getContentPane().add(betaLabel, gridBagConstraints);

        betaStringInput.setHorizontalAlignment(javax.swing.JTextField.LEFT);
        betaStringInput.setText("1.0");
        betaStringInput.setMinimumSize(new java.awt.Dimension(35, 19));
        betaStringInput.setPreferredSize(new java.awt.Dimension(35, 19));
        betaStringInput.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                betaStringInputKeyTyped(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weighty = 1.0;
        getContentPane().add(betaStringInput, gridBagConstraints);

        neighbourNumberComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Square Lattice", "Hexagonal Lattice" }));
        neighbourNumberComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                neighbourNumberComboBoxActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 3;
        getContentPane().add(neighbourNumberComboBox, gridBagConstraints);

        minStrengthLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        minStrengthLabel.setText("Min Strength =");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 2);
        getContentPane().add(minStrengthLabel, gridBagConstraints);

        maxStrengthLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        maxStrengthLabel.setText("Max Strength =");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 2);
        getContentPane().add(maxStrengthLabel, gridBagConstraints);

        maxStrengthStringInput.setText("1.0");
        maxStrengthStringInput.setMinimumSize(new java.awt.Dimension(35, 19));
        maxStrengthStringInput.setPreferredSize(new java.awt.Dimension(35, 19));
        maxStrengthStringInput.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                maxStrengthStringInputKeyTyped(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        getContentPane().add(maxStrengthStringInput, gridBagConstraints);

        minStrengthStringInput.setText("1.0");
        minStrengthStringInput.setMinimumSize(new java.awt.Dimension(35, 19));
        minStrengthStringInput.setPreferredSize(new java.awt.Dimension(35, 19));
        minStrengthStringInput.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                minStrengthStringInputKeyTyped(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        getContentPane().add(minStrengthStringInput, gridBagConstraints);

        startButton.setText("Start");
        startButton.setMaximumSize(new java.awt.Dimension(83, 25));
        startButton.setMinimumSize(new java.awt.Dimension(83, 25));
        startButton.setPreferredSize(new java.awt.Dimension(83, 25));
        startButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                startButtonMouseClicked(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        getContentPane().add(startButton, gridBagConstraints);

        pauseButton.setText("Pause");
        pauseButton.setMaximumSize(new java.awt.Dimension(83, 25));
        pauseButton.setMinimumSize(new java.awt.Dimension(83, 25));
        pauseButton.setPreferredSize(new java.awt.Dimension(83, 25));
        pauseButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                pauseButtonMouseClicked(evt);
            }
        });

        getContentPane().add(pauseButton, new java.awt.GridBagConstraints());

        resumeButton.setText("Resume");
        resumeButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                resumeButtonMouseClicked(evt);
            }
        });

        getContentPane().add(resumeButton, new java.awt.GridBagConstraints());

        saveButton.setText("Save");
        saveButton.setMaximumSize(new java.awt.Dimension(83, 25));
        saveButton.setMinimumSize(new java.awt.Dimension(83, 25));
        saveButton.setPreferredSize(new java.awt.Dimension(83, 25));
        saveButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                saveButtonMouseClicked(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 0;
        getContentPane().add(saveButton, gridBagConstraints);

        resetButton.setText("Reset");
        resetButton.setMaximumSize(new java.awt.Dimension(83, 25));
        resetButton.setMinimumSize(new java.awt.Dimension(83, 25));
        resetButton.setPreferredSize(new java.awt.Dimension(83, 25));
        resetButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                resetButtonMouseClicked(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 0;
        getContentPane().add(resetButton, gridBagConstraints);

        relaxationRangeLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        relaxationRangeLabel.setLabelFor(relaxationRangeStringInput);
        relaxationRangeLabel.setText("Relaxation Range =");
        relaxationRangeLabel.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(0, 4, 0, 3);
        getContentPane().add(relaxationRangeLabel, gridBagConstraints);

        loadingNoiseLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        loadingNoiseLabel.setLabelFor(loadingNoiseStringInput);
        loadingNoiseLabel.setText("% Loading Noise =");
        loadingNoiseLabel.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.ipadx = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 2);
        getContentPane().add(loadingNoiseLabel, gridBagConstraints);

        relaxationRangeStringInput.setText("0.0");
        relaxationRangeStringInput.setMinimumSize(new java.awt.Dimension(35, 19));
        relaxationRangeStringInput.setPreferredSize(new java.awt.Dimension(35, 19));
        relaxationRangeStringInput.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                relaxationRangeStringInputKeyTyped(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 6;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        getContentPane().add(relaxationRangeStringInput, gridBagConstraints);

        loadingNoiseStringInput.setText("0.0");
        loadingNoiseStringInput.setDragEnabled(true);
        loadingNoiseStringInput.setMinimumSize(new java.awt.Dimension(35, 19));
        loadingNoiseStringInput.setPreferredSize(new java.awt.Dimension(35, 19));
        loadingNoiseStringInput.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                loadingNoiseStringInputKeyTyped(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 6;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        getContentPane().add(loadingNoiseStringInput, gridBagConstraints);

        outputFilenameStringInput.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        outputFilenameStringInput.setText("data");
        outputFilenameStringInput.setMinimumSize(new java.awt.Dimension(100, 19));
        outputFilenameStringInput.setPreferredSize(new java.awt.Dimension(100, 19));
        outputFilenameStringInput.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                outputFilenameStringInputKeyTyped(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 6;
        gridBagConstraints.gridy = 0;
        getContentPane().add(outputFilenameStringInput, gridBagConstraints);

        outputFilenameLabel.setText("Output Filename Root :");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 0;
        getContentPane().add(outputFilenameLabel, gridBagConstraints);

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

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        world.saveData();
        System.exit(0);
    }//GEN-LAST:event_formWindowClosing

    private void saveButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_saveButtonMouseClicked
        world.saveData();
    }//GEN-LAST:event_saveButtonMouseClicked

    private void resumeButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_resumeButtonMouseClicked
        System.out.println ("Resume");
        world.resumeAutonome();
    }//GEN-LAST:event_resumeButtonMouseClicked

    private void pauseButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_pauseButtonMouseClicked
        System.out.println ("Pause");
        world.pauseAutonome();
    }//GEN-LAST:event_pauseButtonMouseClicked

    private void startButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_startButtonMouseClicked
        if( isFirstTime ){ world.startAutonome(); isFirstTime=false; }
        else{ world.resumeAutonome(); }
    }//GEN-LAST:event_startButtonMouseClicked

    private void loadingNoiseStringInputKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_loadingNoiseStringInputKeyTyped
        loadingNoiseStringInput.setBackground(new java.awt.Color(255, 255, 0));
    }//GEN-LAST:event_loadingNoiseStringInputKeyTyped

    private void relaxationRangeStringInputKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_relaxationRangeStringInputKeyTyped
        relaxationRangeStringInput.setBackground(new java.awt.Color(255, 255, 0));
    }//GEN-LAST:event_relaxationRangeStringInputKeyTyped

    private void outputFilenameStringInputKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_outputFilenameStringInputKeyTyped
        outputFilenameStringInput.setBackground(new java.awt.Color(255, 255, 0));
    }//GEN-LAST:event_outputFilenameStringInputKeyTyped

    private void resetButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_resetButtonMouseClicked
        betaStringInput.setBackground(new java.awt.Color(255, 255, 255));
        String betaString = betaStringInput.getText();
        double newBeta = Double.parseDouble( betaString );
        world.setBeta(newBeta);
        
        outputFilenameStringInput.setBackground(new java.awt.Color(255, 255, 255));
        String outputFilename = outputFilenameStringInput.getText();
        world.setFileNameRoot(outputFilename);
        
        boundaryConditionComboBox.setBackground(new java.awt.Color(255, 255, 255));
        String bCString = (String)boundaryConditionComboBox.getSelectedItem();
        if(bCString=="Periodic BC"){ world.setPeriodicBC(true); }
        else{ world.setPeriodicBC(false); }
        
        neighbourNumberComboBox.setBackground(new java.awt.Color(255, 255, 255));
        String neighbourNumberString = (String)neighbourNumberComboBox.getSelectedItem();
        if(neighbourNumberString=="Square Lattice"){ world.setNeighbourNumber(4); }
        else{ world.setNeighbourNumber(6); }
        
        maxStrengthStringInput.setBackground(new java.awt.Color(255, 255, 255));
        String maxStrengthString = maxStrengthStringInput.getText();
        double newMaxStrength = Double.parseDouble( maxStrengthString );
        world.setMaxStrength( newMaxStrength );
        
        minStrengthStringInput.setBackground(new java.awt.Color(255, 255, 255));
        String minStrengthString = minStrengthStringInput.getText();
        double newMinStrength = Double.parseDouble( minStrengthString );
        world.setMinStrength( newMinStrength );
        
        loadingNoiseStringInput.setBackground(new java.awt.Color(255, 255, 255));
        String percentageNoiseString = loadingNoiseStringInput.getText();
        double newPercentageNoise = Double.parseDouble( percentageNoiseString );
        world.setPercentageNoise( newPercentageNoise );
        
        relaxationRangeStringInput.setBackground(new java.awt.Color(255, 255, 255));
        String stressRelaxationRangeString = relaxationRangeStringInput.getText();
        double newStressRelaxationRange = Double.parseDouble( stressRelaxationRangeString );
        world.setRelaxationStressFactor( newStressRelaxationRange );
        
        world.init();
        world.repaint();
        
    }//GEN-LAST:event_resetButtonMouseClicked

    private void maxStrengthStringInputKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_maxStrengthStringInputKeyTyped
        maxStrengthStringInput.setBackground(new java.awt.Color(255, 255, 0));
    }//GEN-LAST:event_maxStrengthStringInputKeyTyped

    private void minStrengthStringInputKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_minStrengthStringInputKeyTyped
        minStrengthStringInput.setBackground(new java.awt.Color(255, 255, 0));
    }//GEN-LAST:event_minStrengthStringInputKeyTyped

    private void betaStringInputKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_betaStringInputKeyTyped
        betaStringInput.setBackground(new java.awt.Color(255, 255, 0));
    }//GEN-LAST:event_betaStringInputKeyTyped

    private void neighbourNumberComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_neighbourNumberComboBoxActionPerformed
        neighbourNumberComboBox.setBackground(new java.awt.Color(255, 255, 0));
    }//GEN-LAST:event_neighbourNumberComboBoxActionPerformed

    private void boundaryConditionComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boundaryConditionComboBoxActionPerformed
        boundaryConditionComboBox.setBackground(new java.awt.Color(255, 255, 0));
    }//GEN-LAST:event_boundaryConditionComboBoxActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        world = new LomnitzAutonome ();
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new finalGUI_0_1().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane autonomePane;
    private javax.swing.JLabel betaLabel;
    private javax.swing.JTextField betaStringInput;
    private javax.swing.JComboBox boundaryConditionComboBox;
    private javax.swing.JLabel loadingNoiseLabel;
    private javax.swing.JTextField loadingNoiseStringInput;
    private javax.swing.JLabel maxStrengthLabel;
    private javax.swing.JTextField maxStrengthStringInput;
    private javax.swing.JLabel minStrengthLabel;
    private javax.swing.JTextField minStrengthStringInput;
    private javax.swing.JComboBox neighbourNumberComboBox;
    private javax.swing.JLabel outputFilenameLabel;
    private javax.swing.JTextField outputFilenameStringInput;
    private javax.swing.JButton pauseButton;
    private javax.swing.JLabel relaxationRangeLabel;
    private javax.swing.JTextField relaxationRangeStringInput;
    private javax.swing.JButton resetButton;
    private javax.swing.JButton resumeButton;
    private javax.swing.JButton saveButton;
    private javax.swing.JButton startButton;
    // End of variables declaration//GEN-END:variables
 
    private static void setDefaultParameters() {
            beta = 1.0;
            Nx = 100;
            neighbourNumber=4;
            maxStrength = 1;
            minStrength = 1;
            usePeriodicBC = false;
            runtimeDuration = 20000;
            autonomeThreadSleepDuration = 1000/24;
            fileNameRoot = "data";
    }
    
    private static void setWorldParameters() {
        world.setFileNameRoot(fileNameRoot);
        world.setBeta(beta);
        world.setNx(Nx);
        world.setNeighbourNumber(neighbourNumber);
        world.setMaxStrength(maxStrength);
        world.setMinStrength(minStrength);
        world.setPeriodicBC(usePeriodicBC);
        world.setRuntimeDuration(runtimeDuration);
        world.setAutonomeThreadSleepDuration(autonomeThreadSleepDuration);
        world.resetAutonome();
        
        world.setRelaxationStressFactor(0);
        world.setLoadingMechanism("constantLoading");
        world.setPercentageNoise(0);
        world.setAsymmetricFactor(1.0);
        world.resetAutonome();
    }
   
}
