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

import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;

/**
 * NewProjectDialog
 *
 * @version 20030311
 * @author Jörg P. M. Haeger
 */
public class NewProjectDialog extends SequenceDialog {
	protected JPanel panel;
	protected ActionListener eventHandler;

	protected JComponent modesBox;
	protected JRadioButton
		dataButton,
		imageButton,
		audioButton,
		copyButton,
		blankButton;

	protected JComponent mediumBox;
	protected JRadioButton
		CDRButton,
		CDRWButton,
		DVDPlusRWButton;

	protected JRadioButton
		MB650Button,
		MB700Button,
		MB800Button,
		MB4482Button;

	protected JRadioButton
		warnButton,
		overwriteButton,
		hideButton,
		addButton;

	protected JComponent blankBox;
	protected JRadioButton
		CDRWBlankButton,
		DVDPlusRWBlankButton;

	protected JRadioButton
		blankFastButton,
		blankAllButton;

	protected JComboBox speedBox;

	public NewProjectDialog(boolean canBeCancelled) {
		super("NewProjectDialog.title");
		if (!canBeCancelled)
			setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);

		eventHandler = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == DVDPlusRWButton)
					MB4482Button.setSelected(true);

				if (state == 1)
					validateMediumBox();
			}
		};

		initModesBox();
		initMediumBox();
		initBlankBox();
		eventHandler.actionPerformed(new ActionEvent(this, 0, ""));

		enterState(0);
		pack();
	}

	protected boolean apply() {
		if (dataButton.isSelected()) {
			Mode.type = Mode.data;
			Mode.makeImage = true;
		}
		else
		if (imageButton.isSelected()) {
			Mode.type = Mode.data;
			Mode.makeImage = false;
		}
		else
		if (audioButton.isSelected()) {
			Mode.type = Mode.audio;
		}

		if (CDRButton.isSelected() || CDRWButton.isSelected()) {
			if (MB650Button.isSelected())
				Mode.setMedium(Mode.CDR74);
			else
			if (MB700Button.isSelected())
				Mode.setMedium(Mode.CDR80);
			else
			if (MB800Button.isSelected())
				Mode.setMedium(Mode.CDR90);
		}

		if (MB4482Button.isSelected())
			Mode.setMedium(Mode.DVDPLUSRW);

		Options.reset();
		Options.warnIfNotEmpty = warnButton.isSelected();
		Options.blankTOC = overwriteButton.isSelected();
		Options.hideContents = hideButton.isSelected();

		if (copyButton.isSelected()) {
			setVisible(false);
			CDcreator.mainWin.burnCopy();
		}
		else
		if (blankButton.isSelected()) {
			setVisible(false);

//			CDcreator.mainWin.blank();
			if (CDRWBlankButton.isSelected())
				Mode.setMedium(Mode.CDR74);
			else
			if (DVDPlusRWBlankButton.isSelected())
				Mode.setMedium(Mode.DVDPLUSRW);
			String speed = (String) speedBox.getSelectedItem();
			Options.speed = Integer.parseInt(
				speed.substring(0, speed.indexOf('x')));
			try {
				new Blank(blankAllButton.isSelected()).exec();
			} catch (CommandError cmdErr) {};
		}

		return true;
	}

	private JRadioButton createButton(JComponent box,
			ButtonGroup group, String label) {
		if (label.startsWith("."))
			label = "NewProjectDialog" + label;
		label = i18n(label);

		JRadioButton button = new JRadioButton(label);
		button.addActionListener(eventHandler);
		box.add(button);
		group.add(button);
		return button;
	}

	protected JComponent createHeader(String label) {
		if (label.startsWith("."))
			label = "NewProjectDialog" + label;
		label = i18n(label);

		MyBox hbox = new MyBox(BoxLayout.X_AXIS);
		hbox.setAlignmentX(LEFT_ALIGNMENT);

		JLabel l = new JLabel(label);
		l.setAlignmentY(CENTER_ALIGNMENT);
		hbox.add(l);

		hbox.add(Box.createHorizontalStrut(5));

		JSeparator s = new JSeparator();
		s.setAlignmentY(TOP_ALIGNMENT);
		hbox.add(s);

		return hbox;
	}

	protected JComponent createText(String label) {
		if (label.startsWith("."))
			label = "NewProjectDialog" + label;
		label = i18n(label);

		MyBox vbox = new MyBox(BoxLayout.Y_AXIS);
		vbox.setAlignmentX(LEFT_ALIGNMENT);
		vbox.add(Box.createVerticalGlue());

		while (true) {
			int pos = label.indexOf("<br>");
			if (pos < 0) {
				vbox.add(new JLabel(label));
				break;
			}
			else
			if (pos == 0)
//				vbox.add(Box.createVerticalGlue());
				vbox.add(new JLabel(" "));
			else
				vbox.add(new JLabel(label.substring(0, pos)));
			label = label.substring(pos + 4);
		}

		vbox.add(Box.createVerticalGlue());
		return vbox;
	}

	protected void enterHelpState(int aState) {
		if (panel != null)
			getContentPane().remove(panel);
		validate();
		repaint();

		panel = new JPanel();
		panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

		if (aState == 0)
			panel.add(getHelp("NewProjectModes"));
		else
		if (aState == 1)
			panel.add(getHelp("NewProjectMedium"));

		getContentPane().add(panel, BorderLayout.CENTER);
		validate();
		repaint();
	}

	protected void enterState(int aState) {
		if (panel != null)
			getContentPane().remove(panel);

		panel = new JPanel() {
			public Dimension getPreferredSize() {
				Dimension a = mediumBox.getPreferredSize();
				Dimension b = modesBox.getPreferredSize();
				if (b.width > a.width)
					a.width = b.width;
				if (b.height > a.height)
					b.height = a.height;
				Insets insets = getInsets();
				a.width += insets.left + insets.right;
				a.height += insets.top + insets.bottom;
				return a;
			}
		};
		panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
		if (aState == 0)
			panel.add(modesBox);
		else
		if (aState == 1) {
			DVDPlusRWButton.setEnabled(
				CDcreator.DVDplusRWSupport
				&& !audioButton.isSelected()
				&& !copyButton.isSelected());
			if (dataButton.isSelected()) {
				addButton.setEnabled(true);
				addButton.setSelected(true);
			}
			else {
				addButton.setEnabled(false);
			}
			validateMediumBox();
			if (blankButton.isSelected()) {
				DVDPlusRWBlankButton.setEnabled(
					CDcreator.DVDplusRWSupport);
				panel.add(blankBox);
			}
			else
				panel.add(mediumBox);
		}
		else
		if (aState == 2)
			if (copyButton.isSelected())
				panel.add(createText("NewProjectDialog.pressOK"));
			else
			if (blankButton.isSelected())
				panel.add(createText("NewProjectDialog.blankWarning"));
			else
				panel.add(createText("NewProjectDialog.instructions"));
		else
			panel.add(new JLabel("state " + aState));
		getContentPane().add(panel, BorderLayout.CENTER);
		validate();
	}

	static JComponent getHelp(String key) {
		JEditorPane viewer;
		try {
			URL url = CDcreator.getURL("help/" + key
				+ "_" + CDcreator.locale.getLanguage() + ".html");
			viewer = new JEditorPane(url);
		}
		catch (java.io.IOException e) {
			URL url = CDcreator.getURL("help/" + key + ".html");
			try {
				viewer = new JEditorPane(url);
			}
			catch (java.io.IOException e2) {
				return new JLabel("no help available");
			}
		}
		viewer.setEditable(false);
		JScrollPane scroller = new JScrollPane();
		scroller.getViewport().add(viewer);
		return scroller;
	}

	protected boolean hasHelp(int aState) {
		return aState == 0;
	}

	protected boolean hasMore() {
		return state < 2;
	}

	protected void initBlankBox() {
		MyBox vbox = new MyBox(BoxLayout.Y_AXIS);

		vbox.add(createHeader(".typeLabel"));
		MyBox hbox = new MyBox(BoxLayout.X_AXIS);
		hbox.setAlignmentX(LEFT_ALIGNMENT);
		ButtonGroup group = new ButtonGroup();
		CDRWBlankButton = createButton(hbox, group, "CD-RW");
		CDRWBlankButton.setSelected(true);
		DVDPlusRWBlankButton = createButton(hbox, group, "DVD+RW");
		DVDPlusRWBlankButton.setSelected(Mode.isMedium(Mode.DVDPLUSRW));
		vbox.add(hbox);

		vbox.add(Box.createVerticalStrut(10));
		vbox.add(createHeader(".blankModeLabel"));
		group = new ButtonGroup();
		blankFastButton = createButton(vbox, group, i18n("ODfast"));
		blankFastButton.setSelected(true);
		blankAllButton = createButton(vbox, group, i18n("ODall"));

		vbox.add(Box.createVerticalStrut(10));
		vbox.add(createHeader(i18n("speed")));
		speedBox = OptionsDialog.createSpeedBox();
		speedBox.setAlignmentX(LEFT_ALIGNMENT);
		vbox.add(speedBox);

		vbox.add(Box.createVerticalGlue());
		blankBox = vbox;
	}

	protected void initMediumBox() {
		MyBox vbox = new MyBox(BoxLayout.Y_AXIS);

		vbox.add(createHeader(".typeLabel"));
		MyBox hbox = new MyBox(BoxLayout.X_AXIS);
		hbox.setAlignmentX(LEFT_ALIGNMENT);
		ButtonGroup group = new ButtonGroup();
		CDRButton = createButton(hbox, group, "CD-R");
		CDRButton.setSelected(Mode.isMedium(Mode.CDR));
		CDRWButton = createButton(hbox, group, "CD-RW");
		DVDPlusRWButton = createButton(hbox, group, "DVD+RW");
		DVDPlusRWButton.setSelected(Mode.isMedium(Mode.DVDPLUSRW));
		vbox.add(hbox);

		vbox.add(Box.createVerticalStrut(10));
		vbox.add(createHeader(".mediumSize"));
		hbox = new MyBox(BoxLayout.X_AXIS);
		hbox.setAlignmentX(LEFT_ALIGNMENT);
		group = new ButtonGroup();
		MB650Button = createButton(hbox, group, "74/650 MB");
		MB650Button.setSelected(Mode.mediumSize == 650);
		MB700Button = createButton(hbox, group, "80/700 MB");
		MB700Button.setSelected(Mode.mediumSize == 700);
		MB800Button = createButton(hbox, group, "90/800 MB");
		MB800Button.setSelected(Mode.mediumSize == 800);
		MB4482Button = createButton(hbox, group, "4482 MB");
		MB4482Button.setSelected(Mode.mediumSize == 4482);
		vbox.add(hbox);

		vbox.add(Box.createVerticalStrut(10));
		vbox.add(createHeader(".ifNotEmpty"));
		MyBox vbox2 = new MyBox(BoxLayout.Y_AXIS);
		vbox2.setAlignmentX(LEFT_ALIGNMENT);
		group = new ButtonGroup();
		warnButton = createButton(vbox2, group, ".warnIfNotEmpty");
		warnButton.setSelected(Options.warnIfNotEmpty);
		overwriteButton = createButton(vbox2, group, ".overwrite");
		hideButton = createButton(vbox2, group, ".hide");
		addButton = createButton(vbox2, group, ".add");
		addButton.setSelected(!Options.warnIfNotEmpty);
		vbox.add(vbox2);

		vbox.add(Box.createVerticalGlue());
		mediumBox = vbox;
	}

	protected void initModesBox() {
		JPanel vbox = new JPanel();
		vbox.setLayout(new BoxLayout(vbox, BoxLayout.Y_AXIS));
		ButtonGroup group = new ButtonGroup();

		vbox.add(createHeader("NewProjectDialog.selectMode"));
		dataButton = createButton(vbox, group, ".storeFiles");
		dataButton.setSelected(true);
		imageButton = createButton(vbox, group, ".imageCD");
		audioButton = createButton(vbox, group, ".audioCD");
		audioButton.setEnabled(CDcreator.audioCDSupport);
		copyButton = createButton(vbox, group, ".copyCD");
		copyButton.setEnabled(CDcreator.copySupport);
		blankButton = createButton(vbox, group, ".blank");
		vbox.add(Box.createVerticalGlue());
		modesBox = vbox;
	}

	protected void validateMediumBox() {
		for (int i = 0; i < 3; i++) {
			MB650Button.setEnabled(!DVDPlusRWButton.isSelected());
			MB700Button.setEnabled(!DVDPlusRWButton.isSelected());
			MB800Button.setEnabled(CDcreator.overburnSupport
				&& !DVDPlusRWButton.isSelected());
			MB4482Button.setEnabled(DVDPlusRWButton.isSelected());

			overwriteButton.setEnabled(!CDRButton.isSelected());
			hideButton.setEnabled(dataButton.isSelected()
				&& !DVDPlusRWButton.isSelected());
			addButton.setEnabled(dataButton.isSelected());

			speedBox.setEnabled(CDRWBlankButton.isSelected());

			if (DVDPlusRWButton.isSelected()
					&& !DVDPlusRWButton.isEnabled())
				CDRButton.setSelected(true);

			if (MB4482Button.isSelected()
					&& !MB4482Button.isEnabled())
				MB650Button.setSelected(true);

			if (addButton.isSelected()
					&& !addButton.isEnabled())
				warnButton.setSelected(true);
		}
	}

	/**
	 * Since Netscape 4 does not like a "new Box(BoxLayout.Y_AXIS)"
	 */
	private class MyBox extends JPanel {
		MyBox(int axis) {
			setLayout(new BoxLayout(this, axis));
		}
	}
}
