/*
 * @(#)Dialog.java
 *
 * This file is part of webCDwriter - Network CD Writing.
 *
 * Copyright (C) 2001-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.*;

abstract public class Dialog extends JDialog {
	private boolean canceled;
	protected JButton OKButton = null;
	protected static int tab = 0;

	public Dialog(String title) {
		super(CDcreator.frame, i18n(title), true);

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

	abstract protected boolean apply();

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

	protected JPanel createControlPanel() {
		return createControlPanel("continue");
	}

	protected JPanel createControlPanel(String OK) {
		JPanel p = new JPanel();
		p.setLayout(new FlowLayout());

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

		OKButton = new JButton(i18n(OK));
		OKButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				OKPressed();
			}
		});
		p.add(OKButton);

		return p;
	}

	public static JPanel createLabelItemBox(
		JComponent label, JComponent item) {
		JPanel p = new JPanel();
		p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
		if (tab > 0) {
/*			Dimension d = label.getPreferredSize();
			if (tab > d.getWidth())
				d.setSize(tab, d.getHeight());
			label.setPreferredSize(d);
*/			label.setPreferredSize(new Dimension(tab, 20));
		}
		else
			label.setPreferredSize(new Dimension(120, 20));
		p.add(label);
		p.add(Box.createRigidArea(new Dimension(0, 10)));
//		item.setPreferredSize(new Dimension(100, 20));
		p.add(item);
		p.add(Box.createHorizontalGlue());
		p.setAlignmentX(LEFT_ALIGNMENT);
		return p;
	}

	public static JPanel createLabelItemBox(
			JComponent label, int tab, JComponent item) {
		Dialog.tab = tab;
		return createLabelItemBox(label, item);
	}

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

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

	protected boolean wasCanceled() {
		return canceled;
	}
}
