/*
 * @(#)SequenceDialog.java
 *
 * This file is part of webCDwriter - Network CD Writing.
 *
 * Copyright (C) 2003 Jörg P. M. Haeger
 *
 * webCDwriter is free software. See CDcreator.java for details.
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * A sequence of dialogs with previous and next buttons.
 *
 * @version 20030306
 * @author Jörg P. M. Haeger
 */
abstract public class SequenceDialog extends JDialog {
	protected JPanel controlPanel;
	protected int state = 0;
	protected boolean helpState;
	private boolean canceled;
	protected JButton
		helpBackButton,
		helpButton,
		cancelButton,
		backButton,
		nextButton,
		OKButton;

	public SequenceDialog(String title) {
		super(CDcreator.mainWin, i18n(title), true);

		initControlPanel();
		getContentPane().add(controlPanel, BorderLayout.SOUTH);

		addWindowListener(new WindowAdapter() {
			public void windowActivated(WindowEvent e) {
				if (OKButton != null)
					OKButton.requestFocus();
			}
			public void windowClosing(WindowEvent e) {
				cancelPressed();
			}
		});
	}

	abstract protected boolean apply();

	protected boolean backPressed() {
		if (state > 0)
			state--;
		updateButtons();
		enterState(state);
		return false;
	}

	public void cancelPressed() {
		canceled = true;
		setVisible(false);
	}

	protected void enterHelpState(int aState) {
	}

	protected void enterState(int aState) {
	}

	protected boolean hasHelp(int aState) {
		return false;
	}

	protected boolean hasMore() {
		return false;
	}

	protected static String i18n(String str) {
		return CDcreator.i18n(str);
	}

	private void initControlPanel() {
		JPanel hp = new JPanel();
		hp.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		hp.setLayout(new BoxLayout(hp, BoxLayout.X_AXIS));

		helpBackButton = new JButton(i18n("MainWin.back"));
		helpBackButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				helpState = false;
				updateButtons();
				enterState(state);
			}
		});
		helpBackButton.setVisible(false);
		hp.add(helpBackButton);

		helpButton = new JButton(i18n("help"));
		helpButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				helpState = true;
				updateButtons();
				enterHelpState(state);
			}
		});
		hp.add(helpButton);

		cancelButton = new JButton(i18n("cancel"));
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				cancelPressed();
			}
		});
		hp.add(cancelButton);

		hp.add(Box.createHorizontalGlue());
		hp.add(Box.createHorizontalStrut(10));

		backButton = new JButton(i18n("MainWin.back"));
		backButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				backPressed();
			}
		});
		backButton.setVisible(false);
		hp.add(backButton);

		nextButton = new JButton(i18n("MainWin.continue"));
		nextButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				nextPressed();
			}
		});
		nextButton.setVisible(hasMore());
		hp.add(nextButton);

		OKButton = new JButton(i18n("OK")) {
			public Dimension getPreferredSize() {
				Dimension a = nextButton.getPreferredSize();
				Dimension b = super.getPreferredSize();
				if (b.width > a.width)
					a.width = b.width;
				if (b.height > a.height)
					b.height = a.height;
				return a;
			}
		};
		OKButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				OKPressed();
			}
		});
		OKButton.setVisible(!hasMore());
		hp.add(OKButton);

		JPanel vp = new JPanel();
		vp.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0));
		vp.setLayout(new BoxLayout(vp, BoxLayout.Y_AXIS));
		vp.add(new JSeparator(JSeparator.HORIZONTAL));
		vp.add(hp);
		controlPanel = vp;
	}

	private void updateButtons() {
		helpBackButton.setVisible(helpState);
		helpButton.setEnabled(hasHelp(state));
		helpButton.setVisible(!helpState);
		cancelButton.setVisible(!helpState);
		backButton.setVisible(!helpState && state > 0);
		nextButton.setVisible(!helpState && hasMore());
		OKButton.setVisible(!helpState && !hasMore());
	}

	protected boolean nextPressed() {
		if (!hasMore())
			return false;

		state++;
		updateButtons();
		enterState(state);
		return false;
	}

	protected void OKPressed() {
		if (apply())
			setVisible(false);
	}

	public void pack() {
		boolean v = backButton.isVisible();
		backButton.setVisible(true);
		super.pack();
		backButton.setVisible(v);
	}

	protected boolean wasCanceled() {
		return canceled;
	}
}
