// 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:   TvarkaAnalyser.java

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.StringTokenizer;
import javax.swing.*;
import javax.swing.text.JTextComponent;
import lt.ktu.gmj.Result;
import lt.ktu.gmj.ResultRepository;
import lt.ktu.gmj.analysis.ResultFrame;

public class TvarkaAnalyser extends ResultFrame
	implements ActionListener
{
	private static final String CMD_FILE = "File";
	private static final String CMD_SAVE = "Save";
	private static final String CMD_COUNT = "Count";
	private static final String CMD_REFRESH = "Refresh";
	private static final String CMD_HELP = "Help...";
	private static final String CMD_ABOUT = "About...";
	protected JLabel oLabel1;
	protected JLabel oLabel2;
	protected JLabel oLabel3;
	protected JCheckBox oFreeze;
	protected JTextField oFile;
	protected TvarkaPanel fields;
	protected JLabel label;
	private Tvarka gTvarka;

	public TvarkaAnalyser()
		throws Exception
	{
		super("TvarkaAnalyser");
		oLabel1 = new JLabel("  ");
		oLabel2 = new JLabel("  ");
		oLabel3 = new JLabel("  ");
		oFreeze = new JCheckBox("Freeze", false);
		System.out.println("TvarkaAnalyser() begin");
		setTvarka(Tvarka.getTvarka());
		if(getTvarka() == null)
			GlobalTools.showError("Error: You must create Tvarka object first.");
		getContentPane().setBackground(Color.lightGray);
		fields = new TvarkaPanel();
		fields.TvarkaModified(getTvarka());
		TvarkaPanel tvarkapanel = fields;
		JPanel jpanel = new JPanel();
		jpanel.setLayout(new GridLayout(1, 3));
		jpanel.add(oLabel1);
		jpanel.add(oLabel2);
		jpanel.add(oLabel3);
		JPanel jpanel1 = jpanel;
		jpanel = new JPanel();
		jpanel.setLayout(new GridLayout(1, 4));
		JButton jbutton = new JButton("Count");
		jbutton.addActionListener(this);
		jpanel.add(jbutton);
		jbutton = new JButton("Refresh");
		jbutton.addActionListener(this);
		jpanel.add(jbutton);
		jbutton = new JButton("Help...");
		jbutton.addActionListener(this);
		jpanel.add(jbutton);
		jbutton = new JButton("About...");
		jbutton.addActionListener(this);
		jpanel.add(jbutton);
		JPanel jpanel2 = jpanel;
		jpanel = new JPanel();
		jpanel.setLayout(new BorderLayout());
		jbutton = new JButton("Save");
		jbutton.addActionListener(this);
		jpanel.add(jbutton, "West");
		oFile = new JTextField(getTvarka().data_url + ".out");
		jpanel.add(oFile, "Center");
		jbutton = new JButton("File");
		jbutton.addActionListener(this);
		jpanel.add(jbutton, "East");
		JPanel jpanel3 = jpanel;
		jpanel = new JPanel();
		jpanel.setLayout(new BorderLayout());
		jpanel.add(oFreeze, "Center");
		JPanel jpanel4 = jpanel;
		jpanel = new JPanel();
		jpanel.setLayout(new BorderLayout());
		jpanel.add(jpanel1, "North");
		jpanel.add(jpanel3, "Center");
		jpanel.add(jpanel4, "West");
		JPanel jpanel5 = jpanel;
		getContentPane().setLayout(new BorderLayout());
		getContentPane().add(jpanel5, "North");
		getContentPane().add(tvarkapanel, "Center");
		getContentPane().add(jpanel2, "South");
	}

	public synchronized void actionPerformed(ActionEvent actionevent)
	{
		String s = actionevent.getActionCommand();
		System.out.println("actionPerformed: " + s);
		if("File".equals(s))
		{
			FileDialog filedialog = null;
			filedialog = new FileDialog(this, "Save shedule as file", 1);
			if(filedialog == null)
			{
				GlobalTools.showError("Cann't create FileDialog.");
				return;
			}
			filedialog.show();
			String s3 = filedialog.getFile();
			if(s3 == null)
			{
				return;
			} else
			{
				s3 = filedialog.getDirectory() + s3;
				oFile.setText(s3);
				return;
			}
		}
		if("Save".equals(s))
		{
			if(fields.f == null)
			{
				GlobalTools.showError("Save: No data");
				return;
			}
			String s1 = fields.FieldsToString(fields.f, fields.H, fields.W, true, true);
			boolean flag = false;
			try
			{
				FileWriter filewriter = null;
				filewriter = new FileWriter(oFile.getText());
				if(filewriter != null)
				{
					filewriter.write(s1);
					filewriter.close();
					flag = true;
				}
			}
			catch(FileNotFoundException _ex) { }
			catch(IOException _ex) { }
			catch(SecurityException _ex) { }
			if(!flag)
				GlobalTools.showError("Write failed " + oFile.getText());
			return;
		}
		if("Refresh".equals(s))
		{
			validate();
			repaint();
			return;
		}
		if("Help...".equals(s))
		{
			GlobalTools.showMsgFromUrl("Help", GlobalTools.getCodeBase() + "tvarka_help.txt");
			return;
		}
		if("About...".equals(s))
		{
			GlobalTools.showMsgFromUrl("About", GlobalTools.getCodeBase() + "tvarka_about.txt");
			return;
		}
		if("Count".equals(s))
		{
			String s2 = "X";
			StringTokenizer stringtokenizer = new StringTokenizer(fields.getText(), s2);
			int i = stringtokenizer.countTokens() - 1;
			String s4 = Integer.toString(i);
			(new ShowMessage(this, s4, "Count of gaps", true)).show();
			return;
		} else
		{
			return;
		}
	}

	public void finalResult(ResultRepository resultrepository)
	{
	}

	protected void finalize()
		throws Throwable
	{
		setTvarka(null);
		label = null;
		super.finalize();
	}

	private Tvarka getTvarka()
	{
		return gTvarka;
	}

	public void newLog(ResultRepository resultrepository)
	{
		fields.TvarkaChanged(getTvarka());
	}

	public void newProgressResult(ResultRepository resultrepository)
	{
		Result result = super.results.lastResult();
		oLabel2.setText("Progress(" + Integer.toString(result.iteration) + ")=" + Double.toString(result.value.fx()));
		if(!oFreeze.isSelected())
		{
			fields.TvarkaModified(getTvarka());
			Result result1 = result;
			oLabel3.setText("Displayed(" + Integer.toString(result1.iteration) + ")=" + Double.toString(result1.value.fx()));
		}
	}

	public void newResult(ResultRepository resultrepository)
	{
		Result result = super.results.lastResult();
		oLabel1.setText("Last(" + Integer.toString(result.iteration) + ")=" + Double.toString(result.value.fx()));
	}

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

	private void setTvarka(Tvarka tvarka)
	{
		gTvarka = tvarka;
	}
}
