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

package lt.ktu.gmj.analysis;

import java.awt.Container;
import java.awt.Window;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import lt.ktu.gmj.*;
import lt.monarch.chart.*;
import lt.monarch.chart.chart2D.*;
import lt.monarch.swing.JChartPanel;

// Referenced classes of package lt.ktu.gmj.analysis:
//            ResultFrame

public class Projection extends ResultFrame
{
    class Provider extends AbstractChartPointProvider
    {

        public int pointCount()
        {
            int i = results.resultCount();
            return i <= 500 ? i : 500;
        }

        public ChartPoint point(int i)
        {
            int j = i;
            int k = results.resultCount();
            if (k > 500)
                j = (j * k) / 500;
            if (dotCache[i] == null)
                dotCache[i] = pointFromResult(results.resultAt(j));
            return dotCache[i];
        }

        public void newResult(Result result)
        {
            int i = results.resultCount();
            if (i < 500 || i % 50 == 0)
            {
                ChartPoint chartpoint = pointFromResult(result);
                if (chartpoint.registerOn(series))
                    firePointsChanged();
                dotCache[(int)(Math.random() * 500D)] = chartpoint;
                firePointAdded(chartpoint);
            }
        }

        protected ChartPoint pointFromResult(Result result)
        {
            _cls2 _lcls2 = new _cls2(new Double(result.point.x[projection]), new Double(result.value));
            return _lcls2;
        }

        ChartPoint dotCache[];

        Provider()
        {
            dotCache = new ChartPoint[500];
        }

        private class _cls2 extends Dot
        {

            public String tip(ChartPointSeries chartpointseries)
            {
                return " (" + chartpointseries.xMapper.formatKey(xValue()) + ", " + chartpointseries.yMapper.formatKey(yValue()) + ") ";
            }

            public void constructor$0(Provider provider1, Object obj, Object obj1)
            {
            }

            public _cls2(Object obj, Object obj1)
            {
                super(obj, obj1);
                constructor$0(Provider.this, obj, obj1);
            }
        }

    }


    protected synchronized void setupChart()
    {
        provider = new Provider();
        MathAxisMapper mathaxismapper = new MathAxisMapper();
        MathAxisMapper mathaxismapper1 = new MathAxisMapper();
        Axis2DX axis2dx = new Axis2DX(mathaxismapper1);
        Axis2DY axis2dy = new Axis2DY(mathaxismapper);
        series = new DotSeries(provider, axis2dx, axis2dy);
        Grid grid = new Grid(axis2dx, axis2dy);
        axis2dy.setLabel("f(x)");
        axis2dx.setLabel(super.results.task().domain().dimensions()[projection]);
        mathaxismapper.range.setRange(super.results.minValue(), super.results.maxValue());
        mathaxismapper1.range.setRange(super.results.task().domain().min[projection], super.results.task().domain().max[projection]);
        chart.reset();
        chart.setLAxis(axis2dy);
        chart.setXAxis(axis2dx);
        chart.setObjects(new ChartObject[] {
            grid, axis2dx, axis2dy, series
        });
        chart.invalidate();
    }

    public synchronized void prepare(ResultRepository resultrepository)
    {
        super.prepare(resultrepository);
        String as[] = resultrepository.task().domain().dimensions();
        for (int i = 0; i < as.length; i++)
            projectionChoice.addItem(as[i]);

        setupChart();
        show();
        resultrepository.addObserver(this);
    }

    public synchronized void newResult(ResultRepository resultrepository)
    {
        provider.newResult(resultrepository.lastResult());
    }

    public void newProgressResult(ResultRepository resultrepository)
    {
    }

    public void finalResult(ResultRepository resultrepository)
    {
    }

    public synchronized void logStarted(ResultRepository resultrepository)
    {
        setupChart();
    }

    public Projection()
    {
        super("Projection");
        projection = 0;
        series = null;
        chart = new Chart2D();
        getContentPane().add("Center", new JChartPanel(chart));
        projectionChoice = new JComboBox();
        getContentPane().add("South", projectionChoice);
        projectionChoice.addItemListener(new _cls1());
    }

    protected Chart2D chart;
    protected int projection;
    protected ChartPointSeries series;
    protected JComboBox projectionChoice;
    private Provider provider;

    private class _cls1
        implements ItemListener
    {

        public void itemStateChanged(ItemEvent itemevent)
        {
            projection = projectionChoice.getSelectedIndex();
            setupChart();
        }

        public void constructor$0(Projection projection1)
        {
        }

        public _cls1()
        {
            constructor$0(Projection.this);
        }
    }

}
