package eu.dnetlib.dlms.swing.wizard;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JPanel;

public class ButtonsPanel extends JPanel implements ActionListener {
	private static final long serialVersionUID = -2472427420823766035L;
	private JButton buttonPrev = new JButton("< Prev");
	private JButton buttonNext = new JButton("Next >");;
	private JButton buttonFinish= new JButton("Finish");;
	private JButton buttonCancel = new JButton("Cancel");

	/*
	 * The Dialog Wizard.
	 */
	private AbstractDialogWizard wizard;
	
	public ButtonsPanel(AbstractDialogWizard wizard) {
		super(new FlowLayout(FlowLayout.RIGHT, 5, 20));
		
		this.wizard = wizard;
		
		add(buttonPrev);
		add(buttonNext);
		add(Box.createHorizontalStrut(30));
		add(buttonFinish);
		add(Box.createHorizontalStrut(30));
		add(buttonCancel);
		buttonPrev.addActionListener(this);
		buttonNext.addActionListener(this);
		buttonFinish.addActionListener(this);
		buttonCancel.addActionListener(this);
		updateStatus();
	}

	public void updateStatus() {
		int current = wizard.getCurrentStep();
		int size = wizard.getSteps().size();
		buttonPrev.setEnabled(current > 0);
		buttonNext.setEnabled(current < size - 1);
		buttonFinish.setEnabled(current == size - 1);
		buttonCancel.setEnabled(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		if      (source == buttonPrev)   { wizard.goToPrevStep(); }
		else if (source == buttonNext)   { wizard.goToNextStep(); }
		else if (source == buttonFinish) { wizard.executeFinalStep(); }
		else if (source == buttonCancel) { wizard.abort(); }
	}
}