import java.awt.*;
import javax.swing.*;
import simulate.SpeciesDisks;
import simulate.P2HardDisk;
import simulate.IntegratorRectangleRule;
import simulate.ControllerButton;
import simulate.Phase;
import simulate.Simulation2D;
import simulate.DisplayConfiguration;
import simulate.*;

/**
 * A basic extension of the javax.swing.JApplet class
 */
public class DirectIntegration1 extends JApplet implements IntegrationIntervalListener
{
	public void init()
	{
		// Take out this line if you don't use symantec.itools.net.RelativeURL or symantec.itools.awt.util.StatusScroller
		symantec.itools.lang.Context.setApplet(this);
		
		// This line prevents the "Swing: checked access to system event queue" message seen in some browsers.
		getRootPane().putClientProperty("defeatSystemEventQueueCheck", Boolean.TRUE);
		
		// This code is automatically generated by Visual Cafe when you add
		// components to the visual environment. It instantiates and initializes
		// the components. To modify the code, only use code syntax that matches
		// what Visual Cafe can generate, or Visual Cafe may be unable to back
		// parse your Java file into its visual environment.
		//{{INIT_CONTROLS
		getContentPane().setLayout(null);
		getContentPane().setBackground(java.awt.Color.white);
		setSize(688,381);
		simulation2D1.setLayout(null);
		getContentPane().add(simulation2D1);
		simulation2D1.setBounds(24,12,564,312);
		displayConfiguration1.setLayout(null);
		simulation2D1.add(displayConfiguration1);
		displayConfiguration1.setBounds(216,0,300,300);
		simulation2D1.add(P2HardDisk1);
		P2HardDisk1.setBounds(60,204,30,40);
		speciesDisks1.setNMolecules(4);
		speciesDisks1.setLayout(null);
		simulation2D1.add(speciesDisks1);
		speciesDisks1.setBounds(0,0,20,40);
		controllerButton1.setLayout(null);
		simulation2D1.add(controllerButton1);
		controllerButton1.setBounds(0,0,204,36);
		integratorRectangleRule1.setLayout(null);
		controllerButton1.add(integratorRectangleRule1);
		integratorRectangleRule1.setBounds(0,0,20,40);
		JPanel1.setLayout(new GridLayout(6,1,0,0));
		simulation2D1.add(JPanel1);
		JPanel1.setBackground(java.awt.Color.white);
		JPanel1.setBounds(0,72,204,168);
		JLabel3.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
		JLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		JLabel3.setText("Progress to completion");
		JPanel1.add(JLabel3);
		JLabel3.setBounds(0,0,204,28);
		JProgressBar1.setStringPainted(true);
		JPanel1.add(JProgressBar1);
		JProgressBar1.setBounds(0,28,204,28);
		JPanel2.setLayout(new GridLayout(1,2,0,0));
		JPanel1.add(JPanel2);
		JPanel2.setBounds(0,56,204,28);
		JLabel1.setText("Number of atoms");
		JPanel2.add(JLabel1);
		JLabel1.setBackground(java.awt.Color.white);
		JLabel1.setFont(new Font("SansSerif", Font.BOLD, 12));
		JLabel1.setBounds(0,0,102,28);
		JPanel2.add(JTextField1);
		JTextField1.setBounds(102,0,102,28);
		JPanel3.setLayout(new GridLayout(1,2,0,0));
		JPanel1.add(JPanel3);
		JPanel3.setBounds(0,84,204,28);
		JLabel2.setText("Steps/dimension");
		JPanel3.add(JLabel2);
		JLabel2.setBackground(java.awt.Color.white);
		JLabel2.setFont(new Font("SansSerif", Font.BOLD, 12));
		JLabel2.setBounds(0,0,102,28);
		JPanel3.add(JTextField2);
		JTextField2.setBounds(102,0,102,28);
		phase1.setLayout(null);
		simulation2D1.add(phase1);
		phase1.setBounds(0,0,20,40);
		getContentPane().add(JButton1);
		JButton1.setBounds(0,0,0,0);
//		JPanel4.setLayout(new FlowLayout(FlowLayout.CENTER,5,5));
//		getContentPane().add(JPanel4);
//		JPanel4.setBounds(0,0,0,0);
//		getContentPane().add(JLabel4);
//		JLabel4.setBounds(0,0,0,0);
//		getContentPane().add(JTextField4);
//		JTextField4.setBounds(0,0,0,0);
//		JButton1.setText("jbutton");
//		JPanel1.add(JButton1);
//		JButton1.setBounds(0,112,204,28);
		//}}
	
//		JPanel4.setLayout(new GridLayout(1,2,0,0));
//		JPanel1.add(JPanel4);
//		JPanel4.setBounds(0,84,204,28);
//		JLabel4.setText("Integrand");
//		JPanel4.add(JLabel4);
//		JLabel4.setBackground(new java.awt.Color(204,204,204));
//		JLabel4.setFont(new Font("SansSerif", Font.BOLD, 12));
//		JLabel4.setBounds(0,0,102,28);
//		JPanel4.add(JTextField4);
//		JTextField4.setBounds(102,0,102,28);
		JButton1.setText("jbutton");
		JPanel1.add(JButton1);
		JButton1.setBounds(0,112,204,28);

		//{{REGISTER_LISTENERS
		SymAction lSymAction = new SymAction();
		JTextField1.addActionListener(lSymAction);
		JTextField2.addActionListener(lSymAction);
		JButton1.addActionListener(lSymAction);
		//}}
		integratorRectangleRule1.addIntegrationIntervalListener(this);
		integratorRectangleRule1.setDelayed(false);
		JButton1.setText(buttonText());
		speciesDisks1.setNMolecules(4);
		JTextField1.setText(Integer.toString(phase1.atomCount()));
		JTextField2.setText(Integer.toString(integratorRectangleRule1.getN()));
//		SymText lSymText = new SymText();
//		JTextField1.addTextListener(lSymText);
//		JTextField2.addTextListener(lSymText);
		integratorRectangleRule1.initialize();
	}
	
	private String buttonText() {
	    return integratorRectangleRule1.isDelayed() ? "Speed up" : "Slow down";
	}

	//{{DECLARE_CONTROLS
	simulate.Simulation2D simulation2D1 = new simulate.Simulation2D();
	simulate.DisplayConfiguration displayConfiguration1 = new simulate.DisplayConfiguration();
	simulate.P2HardDisk P2HardDisk1 = new simulate.P2HardDisk();
	simulate.Phase phase1 = new simulate.Phase();
	simulate.SpeciesDisks speciesDisks1 = new simulate.SpeciesDisks();
	simulate.ControllerButton controllerButton1 = new simulate.ControllerButton();
	simulate.IntegratorRectangleRule integratorRectangleRule1 = new simulate.IntegratorRectangleRule();
	javax.swing.JPanel JPanel1 = new javax.swing.JPanel();
	javax.swing.JLabel JLabel3 = new javax.swing.JLabel();
	javax.swing.JProgressBar JProgressBar1 = new javax.swing.JProgressBar();
	javax.swing.JPanel JPanel2 = new javax.swing.JPanel();
	javax.swing.JLabel JLabel1 = new javax.swing.JLabel();
	javax.swing.JTextField JTextField1 = new javax.swing.JTextField();
	javax.swing.JPanel JPanel3 = new javax.swing.JPanel();
	javax.swing.JLabel JLabel2 = new javax.swing.JLabel();
	javax.swing.JTextField JTextField2 = new javax.swing.JTextField();
	javax.swing.JButton JButton1 = new javax.swing.JButton();
	//}}

	class SymAction implements java.awt.event.ActionListener
	{
		public void actionPerformed(java.awt.event.ActionEvent event)
		{
			Object object = event.getSource();
			if (object == JTextField1)
				JTextField1_actionPerformed(event);
			else if (object == JTextField2)
				JTextField2_actionPerformed(event);
			if (object == JButton1)
				JButton1_actionPerformed(event);
		}
	}

	class SymText implements java.awt.event.TextListener
	{
		public void textValueChanged(java.awt.event.TextEvent event)
		{
			Object object = event.getSource();
//			if (object == JTextField1)
//				JTextField1_actionPerformed(event);
//			else if (object == JTextField2)
//				JTextField2_actionPerformed(event);
		}
	}

	void JTextField1_actionPerformed(java.awt.event.ActionEvent event)
	{
		// to do: code goes here.
			 
		JTextField1_actionPerformed_Interaction1(event);
	}

	void JTextField1_actionPerformed_Interaction1(java.awt.event.ActionEvent event)
	{
        controllerButton1.clickForPause();
		try {
			// convert class java.lang.String->int; JTextField1 Get the JTextField's text
			speciesDisks1.getAgent(phase1).setNMolecules(java.lang.Integer.parseInt(JTextField1.getText()));
		} catch (java.lang.Exception e) {
		}
		integratorRectangleRule1.initialize();
		displayConfiguration1.repaint();
	}
	
	int iCount = 0;
	double sum = 0.0;
	public void integrationIntervalAction(IntegrationIntervalEvent evt) {
        int value = integratorRectangleRule1.percentComplete();
        JProgressBar1.setValue(value);
//        iCount++;
//        sum += integratorRectangleRule1.current;
//        if(iCount > 100) {
//            JTextField4.setText(Long.toString((long)(sum/(double)iCount)));
 //           iCount = 0;
//            sum = 0.0;
//        }
	}

	void JTextField2_actionPerformed(java.awt.event.ActionEvent event)
	{
		// to do: code goes here.
			 
		JTextField2_actionPerformed_Interaction1(event);
	}

	void JTextField2_actionPerformed_Interaction1(java.awt.event.ActionEvent event)
	{   int n =3;
        controllerButton1.clickForPause();
		try {
			// convert class java.lang.String->int; JTextField2 Get the JTextField's text
			n = java.lang.Integer.parseInt(JTextField2.getText());
		} catch (java.lang.Exception e) {System.out.println("exception!");
		}
		integratorRectangleRule1.setN(n);
		integratorRectangleRule1.initialize();
		System.out.println(n);
		System.out.println(integratorRectangleRule1.getN());
//		integratorRectangleRule1.initialize();
		displayConfiguration1.repaint();
	}

	void JButton1_actionPerformed(java.awt.event.ActionEvent event)
	{
		// to do: code goes here.
			 
		JButton1_actionPerformed_Interaction1(event);
	}

	void JButton1_actionPerformed_Interaction1(java.awt.event.ActionEvent event)
	{
		try {
			// convert class java.lang.String->boolean; JButton1 Get the JButton's text
			integratorRectangleRule1.setDelayed(!integratorRectangleRule1.isDelayed());
		} catch (java.lang.Exception e) {
		}
		JButton1.setText(buttonText());
	}
}
