package org.lucci.madhoc.gui.runtime;

import java.awt.GridLayout;

import javax.swing.JLabel;

import org.lucci.madhoc.gui.SimulationRuntime;
import org.lucci.math.Utilities;

/*
 * Created on May 8, 2005
 */

/**
 * @author luc.hogie
 */
public class InformationComponent extends RuntimeComponentElement
{
    private JLabel iterationLabel;
    private JLabel timeLabel;
    private JLabel stateLabel;
    private JLabel terminationLabel;

    public InformationComponent()
    {
        timeLabel = new JLabel();
        iterationLabel = new JLabel();
        stateLabel = new JLabel();
        terminationLabel = new JLabel();

        setLayout(new GridLayout(4, 2));
        add(new JLabel("Iteration:   ", JLabel.RIGHT));
        add(iterationLabel);
        add(new JLabel("Time:   ", JLabel.RIGHT));
        add(timeLabel);
        add(new JLabel("State:   ", JLabel.RIGHT));
        add(stateLabel);
        add(new JLabel("Terminated:   ", JLabel.RIGHT));
        add(terminationLabel);
    }
    
        /*
         * (non-Javadoc)
         * 
         * @see org.lucci.madhoc.ui.SimulationRuntimeListener#stateChanged()
         */
        public void stateChanged()
        {
            if (getRuntimeComponent().getRuntime().getState() == SimulationRuntime.STATE_RUNNING)
            {
                stateLabel.setText("<html><i>Running...");
            }
            else if (getRuntimeComponent().getRuntime().getState() == SimulationRuntime.STATE_SLEEPING)
            {
                stateLabel.setText("<html><i>Sleeping...");
            }
            else if (getRuntimeComponent().getRuntime().getState() == SimulationRuntime.STATE_COMPLETED)
            {
                stateLabel.setText("<html><i>Completed");
            }
            else
            {
                throw new IllegalStateException("unknow runtime state");
            }
            
            stateLabel.repaint(0);
        }

        /* (non-Javadoc)
         * @see org.lucci.madhoc.gui.SimulationRuntimeListener#iterationPerformed()
         */
        public void iterationPerformed()
        {
            iterationLabel.setText("<html><i>" + getRuntimeComponent().getRuntime().getSimulation().getIteration());
            iterationLabel.repaint(0);
            double acceleration = getRuntimeComponent().getRuntime().getSimulation().getAcceleration();
            acceleration = Utilities.round(acceleration, 2);
            timeLabel.setText("<html><i>" + getRuntimeComponent().getRuntime().getSimulation().getSimulatedTime() + "s (acc factor: " + acceleration + ")" );
            timeLabel.repaint(0);
            
            terminationLabel.setText("<html><i>" + getRuntimeComponent().getRuntime().getSimulation().findRunningApplications().isEmpty());
        }
}