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

/**
 * This dialog informs the user while waiting for a resource.
 *
 * @version 20030519
 * @author Jörg P. M. Haeger
 */
public class QueueDialog extends JDialog {
	JPanel panel;
	JLabel label;
	JButton button;
	boolean buttonPressed;

	public QueueDialog(JFrame parent, String title, String msg) {
		super(parent, title, true);

		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				handleButton();
			}
		});

		panel = new JPanel() {
			public Insets getInsets() {
				return new Insets(10, 10, 10, 10);
			}
		};
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

		JPanel msgPanel = new JPanel() {
			public Insets getInsets() {
				return new Insets(10, 10, 10, 10);
			}
		};
		msgPanel.setLayout(
			new BoxLayout(msgPanel, BoxLayout.Y_AXIS));

		label = new JLabel(msg);
		label.setAlignmentX(CENTER_ALIGNMENT);
		msgPanel.add(label);
		msgPanel.add(Box.createRigidArea(new Dimension(200, 10)));

		label = new JLabel("working");
		label.setAlignmentX(CENTER_ALIGNMENT);
		msgPanel.add(label);
		panel.add(msgPanel);

		JPanel bPanel = new JPanel() {
			public Insets getInsets() {
				return new Insets(10, 10, 10, 10);
			}
		};
		bPanel.setLayout(new FlowLayout());
		button = new JButton(CDcreator.i18n("cancel"));
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				handleButton();
			}
		});
		bPanel.add(button);

		button = new JButton(CDcreator.i18n("showStatus"));
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CDcreator.showServerDocument("");
			}
		});
		bPanel.add(button);
		panel.add(bPanel);

		getContentPane().add(panel, BorderLayout.CENTER);
		pack();
		label.setText("");
	}

	public void handleButton() {
		buttonPressed = true;
		this.setVisible(false);
	}

	public void pack() {
		super.pack();

		// center dialog in parent
		Rectangle r = getParent().getBounds(), r1 = getBounds();
		setLocation(r.x + (r.width - r1.width) / 2,
			r.y + (r.height - r1.height) / 2);
	}

	public void setRank(int rank) {
		label.setText(CDcreator.i18n("queue") + " " + rank);
		pack();
	}

	public boolean wasClosed() {
		return buttonPressed;
	}
}
