// Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/kpdus/jad.html
// Decompiler options: packimports(3) fieldsfirst 
// Source File Name:   cutAnalyser.java

package lt.ktu.gmj.analysis;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.PrintStream;
import java.util.Vector;
import javax.swing.*;
import lt.ktu.gmj.*;
import lt.ktu.gmj.tasks.Cut;

// Referenced classes of package lt.ktu.gmj.analysis:
//			ResultFrame, DrawingPanel, Plokste, worker

public class cutAnalyser extends ResultFrame
{

	DrawingPanel pnlMain;
	JTextField txtHeight;
	JTextField txtWidth;
	JTextField txtTime;
	JTextArea txtData;
	JScrollPane areaScrollPane;
	JPanel pnlRight;
	JPanel pnlSettings;
	JPanel pnlButtonsEXE;
	JPanel pnlButtons;
	JButton btnClear;
	JButton btnRun;
	JButton btnStop;
	JLabel lblStatus;
	Cut taskCut;
	Domain domainCut;
	int m_iRandom;
	int m_iBigest;
	int m_iSmallest;
	public Timer timer;
	static final int ONE_SECOND = 1000;
	private int myTime;

	public cutAnalyser()
		throws Exception
	{
		super("CutAnalyser");
		m_iRandom = 50;
		m_iBigest = 30;
		m_iSmallest = 20;
		getContentPane().setLayout(new BorderLayout(20, 20));
		pnlMain = new DrawingPanel();
		pnlMain.setBorder(BorderFactory.createLineBorder(Color.black));
		getContentPane().setBackground(pnlMain.getBackground());
		getContentPane().add(pnlMain, "Center");
		taskCut = Cut.getCut();
		domainCut = taskCut.domain();
		System.out.println(domainCut.defaultPoint.x[0]);
		System.out.println(domainCut.defaultPoint.x[1]);
		System.out.println(domainCut.defaultPoint.x[2]);
		m_iRandom = (int)domainCut.defaultPoint.x[0];
		m_iBigest = (int)domainCut.defaultPoint.x[1];
		m_iSmallest = (int)domainCut.defaultPoint.x[2];
		pnlRight = new JPanel();
		pnlRight.setLayout(new BoxLayout(pnlRight, 1));
		pnlSettings = new JPanel();
		pnlSettings.setLayout(new GridLayout(0, 2));
		pnlSettings.add(new JLabel("Height: "));
		txtHeight = new JTextField("100");
		pnlSettings.add(txtHeight);
		pnlSettings.add(new JLabel("Widht: "));
		txtWidth = new JTextField("100");
		pnlSettings.add(txtWidth);
		pnlSettings.add(new JLabel("Time: "));
		txtTime = new JTextField("2");
		pnlSettings.add(txtTime);
		pnlSettings.setMaximumSize(new Dimension(400, 200));
		pnlSettings.add(new JLabel(" "));
		pnlSettings.add(new JLabel(" "));
		pnlSettings.add(new JLabel("Size of patterns: "));
		pnlSettings.add(new JLabel(" xx ; yy"));
		pnlRight.add(pnlSettings, "East");
		txtData = new JTextArea();
		txtData.setLineWrap(true);
		txtData.setWrapStyleWord(true);
		areaScrollPane = new JScrollPane(txtData);
		areaScrollPane.setVerticalScrollBarPolicy(22);
		pnlRight.add(areaScrollPane, "East");
		pnlRight.add(new JLabel(" "));
		pnlButtons = new JPanel();
		pnlButtons.setLayout(new FlowLayout());
		btnClear = new JButton("Clear patterns");
		pnlButtons.add(btnClear);
		pnlButtons.setMaximumSize(new Dimension(300, 100));
		pnlRight.add(pnlButtons);
		pnlButtonsEXE = new JPanel();
		pnlButtonsEXE.setLayout(new FlowLayout());
		btnRun = new JButton("Run");
		btnRun.setForeground(Color.green);
		btnStop = new JButton("Stop");
		btnStop.setForeground(Color.red);
		pnlButtonsEXE.add(btnRun);
		pnlButtonsEXE.add(btnStop);
		pnlButtonsEXE.setMaximumSize(new Dimension(300, 100));
		pnlRight.add(pnlButtonsEXE);
		lblStatus = new JLabel("Enter Size of Patterns and press Run..");
		getContentPane().add(pnlRight, "East");
		getContentPane().add(lblStatus, "South");
		btnRun.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent actionevent)
			{
				timer.start();
				lblStatus.setText("Please wait...");
				initData();
			}

		});
		btnClear.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent actionevent)
			{
				txtData.setText("");
			}

		});
		btnStop.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent actionevent)
			{
				timer.stop();
				txtTime.setText("" + myTime);
			}

		});
		timer = new Timer(1000, new ActionListener() {

			public void actionPerformed(ActionEvent actionevent)
			{
				int i = Integer.parseInt(txtTime.getText().trim());
				txtTime.setText("" + --i);
				if(i == 0)
				{
					timer.stop();
					txtTime.setText("" + myTime);
				}
			}

		});
	}

	public void initData()
	{
		Vector vector = new Vector();
		int i = 0;
		int j = 0;
		int k = 0;
		int l = 0;
		int i1 = 0;
		boolean flag = false;
		String s = new String();
		String s1 = new String();
		myTime = Integer.parseInt(txtTime.getText().trim());
		try
		{
			String s2 = txtHeight.getText().trim();
			if(s2.compareTo("") != 0)
				k = Integer.parseInt(s2);
			if(k <= 0)
				throw new NumberFormatException();
		}
		catch(NumberFormatException numberformatexception)
		{
			JOptionPane.showMessageDialog(null, "Bad Heigth parameter!", "Error", 0);
		}
		try
		{
			String s3 = txtWidth.getText().trim();
			if(s3.compareTo("") != 0)
				l = Integer.parseInt(s3);
			if(k <= 0)
				throw new NumberFormatException();
		}
		catch(NumberFormatException numberformatexception1)
		{
			JOptionPane.showMessageDialog(null, "Bad Widht parameter!", "Error", 0);
		}
		try
		{
			String s4 = txtData.getText().trim();
			if(s4.compareTo("") != 0)
			{
				int l1;
				for(l1 = 0; l1 < s4.length(); l1++)
					if(s4.charAt(l1) != '\n')
					{
						if(s4.charAt(l1) != ';')
						{
							s = s + s4.charAt(l1);
						} else
						{
							s = s.trim();
							i1 = Integer.parseInt(s);
							s = "";
						}
					} else
					{
						s = s.trim();
						int j1 = Integer.parseInt(s);
						s = "";
						Plokste plokste = new Plokste(0, 0, i1, j1, l1, false);
						vector.addElement(plokste);
					}

				s = s.trim();
				int k1 = Integer.parseInt(s);
				s = "";
				Plokste plokste1 = new Plokste(0, 0, i1, k1, l1, false);
				vector.addElement(plokste1);
			}
		}
		catch(NumberFormatException numberformatexception2)
		{
			JOptionPane.showMessageDialog(null, "Mistake in Size of Patterns field", "Error", 0);
		}
		worker worker1 = new worker(this, i, j, l, k, vector, m_iRandom, m_iBigest, m_iSmallest);
		worker1.start();
	}

	public void finalResult(ResultRepository resultrepository1)
	{
	}

	public void newProgressResult(ResultRepository resultrepository1)
	{
	}

	public void newResult(ResultRepository resultrepository1)
	{
	}

	public void prepare(ResultRepository resultrepository)
	{
		super.prepare(resultrepository);
		results.addObserver(this);
		show();
	}

}
