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

package lt.ktu.gmj.ui;

import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import lt.ktu.gmj.*;
import lt.ktu.gmj.propertySheet.*;

public class ResultSheet extends PropertySheet
	implements ResultObserver
{

	protected ResultRepository set;
	protected JLabel iteration;
	protected JLabel ProgressIteration;
	protected JLabel value;
	protected JLabel dimensions[];

	public void logStarted(ResultRepository resultrepository)
	{
		set = resultrepository;
		SwingUtilities.invokeLater(new Runnable()
	{

		public void run()
		{
			setup();
		}
	});
	}

	public void logDisposed(ResultRepository resultrepository)
	{
		resultrepository.removeObserver(this);
		SwingUtilities.invokeLater(new Runnable()
	{

		public void run()
		{
			removeAll();
		}
	});
	}

	/*
		paprasto rezultato atveju
	*/
	public void newResult(ResultRepository resultrepository)
	{
		final Result r = resultrepository.lastResult();
		SwingUtilities.invokeLater(new Runnable()
	{

		public void run()
		{
			iteration.setText(String.valueOf(r.iteration+1));
		}
	});
	}

	private String format(double d)
	{
		String s = String.valueOf((double)Math.round(d * 1000D) / 1000D);
		if(s.length() > 7)
			s = s.substring(0, 7);
		return s;
	}

	private String format(int i)
	{
		return String.valueOf(i);
	}

	// gautas naujas progresas
	public void newProgressResult(ResultRepository resultrepository)
	{
		final Result r = resultrepository.lastResult();
		SwingUtilities.invokeLater(new Runnable()
	{

		public void run()
		{
			value.setText(format(r.value.fx()));
			for(int i = 0; i < r.point.x.length; i++)
				dimensions[i].setText(format(r.point.x[i]));
			ProgressIteration.setText( String.valueOf(r.iteration+1) );
		}
	});
	}

	public void finalResult(ResultRepository resultrepository)
	{
	}

	protected void setup()
	{
		removeAll();
		add(new Header("Property", "Value"));
		
		ProgressIteration = new JLabel("", 0);
		add( new SimpleProperty("Last progress iteration", new ComponentPresenter(ProgressIteration)) );
		
		iteration = new JLabel("", 0);
		add(new SimpleProperty("Current Iteration", new ComponentPresenter(iteration)));
		
		
		value = new JLabel("", 0);
		add(new SimpleProperty("F(x)", new ComponentPresenter(value)));
		String as[] = set.task().domain().dimensions();
		dimensions = new JLabel[as.length];
		for(int i = 0; i < as.length; i++)
		{
			dimensions[i] = new JLabel("", 0);
			add(new SimpleProperty(as[i], new ComponentPresenter(dimensions[i])));
		}

		complete();
	}

	public ResultSheet()
	{
		set = null;
	}








}
