/*
 * @(#)PutDialog.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.*;

/**
 * A file transmission dialog.
 *
 * @version 20030701
 * @author Jörg P. M. Haeger
 */
class PutDialog extends JDialog {
	JLabel label;
	JProgressBar bar;
	boolean buttonPressed = false;
	JCheckBox ignoreAllUnreadableBox, shortenAllBox;

	PutDialog() {
		super(CDcreator.frame, "PutDialog", true);

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

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

		JPanel hp = new JPanel();
		hp.setLayout(
			new BoxLayout(hp, BoxLayout.X_AXIS));
		hp.setBorder(
			BorderFactory.createCompoundBorder(
				BorderFactory.createTitledBorder(
					CDcreator.i18n("file")),
				BorderFactory.createEmptyBorder(5, 5, 5, 5)));
		label = new JLabel("init");
//		label.setEditable(false);
		hp.add(label);
		hp.add(Box.createHorizontalGlue());
		panel.add(hp);

		panel.add(Box.createRigidArea(new Dimension(500, 20)));

		bar = new JProgressBar();
		bar.setStringPainted(true);
		panel.add(bar);

		panel.add(Box.createVerticalStrut(20));

		hp = new JPanel();
		hp.setLayout(
			new BoxLayout(hp, BoxLayout.X_AXIS));
		hp.setBorder(
			BorderFactory.createCompoundBorder(
				BorderFactory.createTitledBorder(
					CDcreator.i18n("PutDialog.options")),
				BorderFactory.createEmptyBorder(5, 5, 5, 5)));
		ignoreAllUnreadableBox = new JCheckBox(
			CDcreator.i18n("PutDialog.ignoreAllUnreadable"), false);
		hp.add(ignoreAllUnreadableBox);
		hp.add(Box.createHorizontalStrut(20));
		shortenAllBox = new JCheckBox(
			CDcreator.i18n("PutDialog.shortenAll"), false);
		hp.add(shortenAllBox);
		hp.add(Box.createHorizontalGlue());
		panel.add(hp);

		getContentPane().add(panel, BorderLayout.CENTER);

		JPanel buttons = new JPanel() {
			public Insets getInsets() {
				return new Insets(5, 5, 5, 5);
			}
		};
		buttons.setLayout(new FlowLayout(FlowLayout.CENTER));

		JButton button = new JButton(CDcreator.i18n("cancel"));
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				handleButton();
			}
		});
		buttons.add(button);
		getContentPane().add(buttons, BorderLayout.SOUTH);
		pack();
	}

	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 setMessage(String str) {
		if (str.length() > 70) {
			int p = str.lastIndexOf("/");
			if (p >= 0)
				str = str.substring(0, 70 - 2 - (str.length() - p))
					+ "..." + str.substring(p);
		}
		label.setText(str);
//		label.setCaretPosition(str.length());
	}

	public void setRange(int a, int b) {
		bar.setMinimum(a);
		bar.setMaximum(b);
	}

	public void setProgress(int n) {
		bar.setValue(n);
	}

	public boolean wasButtonPressed() {
		return buttonPressed;
	}
}
