/* Copyright (c) 1997-2009
   Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Darmstadt, Germany)
   http://www.polymake.de

   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 2, or (at your option) any
   later version: http://www.gnu.org/licenses/gpl.txt.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
--------------------------------------------------------------------------------
   $Project: polymake $$Id: SpringEmbedderAnimationPanel.java 7774 2007-10-26 10:42:39Z thilosch $
*/

package de.tuberlin.polymake.graph;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;

public class SpringEmbedderAnimationPanel extends JPanel {

	/** autogenerated ID for serialization */
	private static final long serialVersionUID = 4483041565568619063L;

	protected ButtonGroup stepGroup = new ButtonGroup();

	protected JButton stopButton = new JButton("Stop");

	protected JRadioButton noneRadioButton = new JRadioButton("none");
	
	protected JSpinner delaySpinner;
	
	protected String delay = "50";
	
	public SpringEmbedderAnimationPanel() {
		super();
		setLayout(new GridLayout(1, 7));
		
		add(new JLabel("AnimationSteps"));
		noneRadioButton.setSelected(true);
		noneRadioButton.setActionCommand("none");
		stepGroup.add(noneRadioButton);
		add(noneRadioButton);
		JRadioButton rButton = new JRadioButton("1");
		stepGroup.add(rButton);
		rButton.setActionCommand("1");
		add(rButton);
		rButton = new JRadioButton("5");
		stepGroup.add(rButton);
		rButton.setActionCommand("5");
		add(rButton);
		rButton = new JRadioButton("10");
		stepGroup.add(rButton);
		rButton.setActionCommand("10");
		add(rButton);
		rButton = new JRadioButton("50");
		stepGroup.add(rButton);
		rButton.setActionCommand("50");
		add(rButton);
		add(stopButton);
		JPanel delayPanel = new JPanel();
		JLabel delayLabel = new JLabel("Delay");
		delayPanel.add(delayLabel);
		SpinnerNumberModel delayModel = new SpinnerNumberModel(50,0,1000,10);
		delaySpinner = new JSpinner(delayModel);
		delaySpinner.setPreferredSize(new Dimension(50,20));
		delayPanel.add(delaySpinner);
		add(delayPanel);
	}

	public String getStepValue() {

		if (stepGroup.getSelection() == null
				|| stepGroup.getSelection().getActionCommand().equals("none")) {
			return "0";
		} else {
			return stepGroup.getSelection().getActionCommand();
		}
	}

	public void resetStepValue() {
		noneRadioButton.setSelected(true);
	}
	
	public void addStopButtonListener(ActionListener al) {
		stopButton.addActionListener(al);
	}

	/**
	 * Enable or disable the stop button of the animation panel.
	 * @param autoRecompute
	 */
	public void setStopButtonEnabled(boolean autoRecompute) {
		stopButton.setEnabled(autoRecompute);
	}

	public void setDelay(String val) {
		delaySpinner.setValue(Integer.parseInt(val));
	}

	public String getDelay() {
		return delaySpinner.getValue().toString();
	}
}
