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

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.text.*;

/**
 * This is a dialog which allows the user to choose the options.
 *
 * @version 20030130
 * @author Jörg P. M. Haeger
 */
public class OptionsDialog extends JDialog {
	private JCheckBox
		offlineBox,
		blankTOCBox,
		closeCDBox,
		DAOBox,
		formatBox,
		HFSBox,
		jolietBox,
		overburnBox,
		rockRidgeBox,
		simulateBox,
		verifyBox;
	private JComboBox speedBox;
	private MySpinner copiesSpinner;
	private JTextField volumeIDField;
	private boolean canceled;

	public OptionsDialog() {
		super(CDcreator.mainWin, i18n("titleOptions"), true);

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

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

		JPanel hp = new JPanel();
		hp.setLayout(new BoxLayout(hp, BoxLayout.X_AXIS));

		hp.add(createStepsGroup());
//		hp.add(Box.createRigidArea(new Dimension(10, 100)));

		// right column
		JPanel rc = new JPanel();
		rc.setLayout(new BoxLayout(rc, BoxLayout.Y_AXIS));
		rc.add(createWritingGroup());
		rc.add(createCopiesGroup());
		hp.add(rc);

		vp.add(hp);

		if (Mode.makeISOImage()) {
//			vp.add(Box.createRigidArea(new Dimension(100, 10)));
			vp.add(createFilesystemGroup());
		}

		getContentPane().add(vp, BorderLayout.CENTER);
		getContentPane().add(
			createControlPanel(), BorderLayout.SOUTH);
//		pack();
		canceled = false;
	}

	public void apply() {
		if (blankTOCBox != null)
			Options.blankTOC = blankTOCBox.isSelected();
		if (closeCDBox != null)
			Options.closeCD = closeCDBox.isSelected();
		Options.copies = ((Integer)copiesSpinner.getValue()).intValue();
		Options.DAO = DAOBox.isSelected();
		if (formatBox != null)
			Options.format = formatBox.isSelected();
		Options.offline = offlineBox.isSelected();
		if (overburnBox != null)
			Options.overburn = overburnBox.isSelected();
		Options.simulate = simulateBox.isSelected();
		Options.speed = getSpeed();
		Options.verify = verifyBox.isSelected();

		if (Mode.makeISOImage()) {
			Options.HFS = HFSBox.isSelected();
			Options.joliet = jolietBox.isSelected();
			Options.rockRidge = rockRidgeBox.isSelected();
			Options.volumeID = volumeIDField.getText();
		}
	}

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

	private JPanel createControlPanel() {
		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);

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

		return p;
	}

	private JPanel createCopiesGroup() {
		JPanel p = new JPanel();
		p.setLayout(
			new BoxLayout(p, BoxLayout.Y_AXIS));
		p.setBorder(
			BorderFactory.createCompoundBorder(
			BorderFactory.createTitledBorder(
			BorderFactory.createLineBorder(Color.black),
			i18n("OptionsDialog.copiesGroup")),
			BorderFactory.createEmptyBorder(5, 5, 5, 5)));

//		System.out.println(getClass());
		copiesSpinner = new MySpinner(
			1, 1, CDcreator.maxCopies, 1);
		copiesSpinner.setValue(new Integer(Options.copies));
		p.add(createLabelItemBox(
			new JLabel(i18n("OptionsDialog.copies")),
			copiesSpinner.getComponent()));

		p.add(Box.createHorizontalStrut(0));

		return p;
	}

	private JPanel createFilesystemGroup() {
		JPanel p = new JPanel();
		p.setLayout(
			new BoxLayout(p, BoxLayout.Y_AXIS));
		p.setBorder(
			BorderFactory.createCompoundBorder(
			BorderFactory.createTitledBorder(
			BorderFactory.createLineBorder(Color.black),
			i18n("filesystemGroup")),
			BorderFactory.createEmptyBorder(5, 5, 5, 5)));

		Document volIDDoc = new PlainDocument() {
			public void insertString(int offset, String str,
					AttributeSet a) throws BadLocationException {
				int free = 32 - getLength();
				if (free < 0)
					return;
				if (free < str.length())
					str = str.substring(0, free);
				super.insertString(offset, str, a);
			}
		};
		volumeIDField = new JTextField(volIDDoc, Options.volumeID, 32);
		volumeIDField.setColumns(16);
		p.add(createLabelItemBox(
			new JLabel(i18n("volumeID")),
			volumeIDField));

		jolietBox = new JCheckBox(
			i18n("jolietOption"), Options.joliet);
		p.add(jolietBox);

		rockRidgeBox = new JCheckBox(
			i18n("rockRidgeOption"), Options.rockRidge);
		p.add(rockRidgeBox);

		HFSBox = new JCheckBox(
			i18n("HFSOption"), Options.HFS);
		p.add(HFSBox);

		return p;
	}

	static Box createLabelItemBox(JComponent label, JComponent item) {
		Box box = Box.createHorizontalBox();
		box.add(label);
		box.add(Box.createHorizontalStrut(10));
		box.add(item);
		return box;
	}

	static JComboBox createSpeedBox() {
		String[] speeds = new String[CDcreator.maxSpeed / 2 + 1];
		speeds[0] = "1x";
		for (int i = 2; i <= CDcreator.maxSpeed; i += 2)
			// add speed x row
			speeds[i / 2] = i + "x";
		JComboBox speedBox = new JComboBox(speeds);
//		speedBox.setMaximumSize(new Dimension(100, 30));
		speedBox.setSelectedIndex(Options.speed / 2);
		speedBox.setToolTipText(i18n("TTspeedBox"));
		return speedBox;
	}

	private JPanel createStepsGroup() {
		JPanel p = new JPanel();
		p.setLayout(
			new BoxLayout(p, BoxLayout.Y_AXIS));
		p.setBorder(
			BorderFactory.createCompoundBorder(
			BorderFactory.createTitledBorder(
			BorderFactory.createLineBorder(Color.black),
			i18n("stepsGroup")),
			BorderFactory.createEmptyBorder(5, 5, 5, 5)));

		if (Options.medium == Options.DVDPLUSRW) {
			formatBox = new JCheckBox(
				i18n("OptionsDialog.format"), Options.format);
			formatBox.setToolTipText(i18n("OptionsDialog.formatTip"));
			p.add(formatBox);
		}

		if (Mode.copyCD()
			|| Options.medium == Options.CDRW
			|| Options.medium == Options.DVDPLUSRW) {
			blankTOCBox = new JCheckBox(
				i18n("blankTOC"), Options.blankTOC);
			blankTOCBox.setToolTipText(i18n("TTblankTOCBox"));
			p.add(blankTOCBox);
		}

		if (!Mode.copyCD() || !CDcreator.cdrdao)
		if (Options.medium == Options.CDR
				|| Options.medium == Options.CDRW) {
			closeCDBox = new JCheckBox(
				i18n("closeCD"), Options.closeCD);
			closeCDBox.setToolTipText(i18n("TTcloseCDBox"));
			p.add(closeCDBox);
		}

		verifyBox = new JCheckBox(
			i18n("verifyOption"), Options.verify);
		verifyBox.setToolTipText(i18n("TTverifyOption"));
		verifyBox.setEnabled(CDcreator.verifySupport);
		if (!Mode.audioCD())
			p.add(verifyBox);

		offlineBox = new JCheckBox(
			i18n("burnOffline"), Options.offline);
		offlineBox.setToolTipText(i18n("TTofflineBox"));
		p.add(offlineBox);

		p.add(Box.createHorizontalStrut(0));

		return p;
	}

	private JPanel createWritingGroup() {
		JPanel p = new JPanel();
		p.setLayout(
			new BoxLayout(p, BoxLayout.Y_AXIS));
		p.setBorder(
			BorderFactory.createCompoundBorder(
			BorderFactory.createTitledBorder(
			BorderFactory.createLineBorder(Color.black),
			i18n("burnGroup")),
			BorderFactory.createEmptyBorder(5, 5, 5, 5)));

		speedBox = createSpeedBox();
		p.add(createLabelItemBox(
			new JLabel(i18n("speed")), speedBox));

		// Add simulate box
		simulateBox = new JCheckBox(
			i18n("optionSimulate"), Options.simulate);
		simulateBox.setToolTipText(i18n("optionSimulateTip"));
		p.add(simulateBox);

		DAOBox = new JCheckBox(i18n("optionDAO"), Options.DAO);
		DAOBox.setToolTipText(i18n("optionDAOTip"));
		if (Mode.audioCD())
			p.add(DAOBox);

		overburnBox = new JCheckBox(i18n("OptionsDialog.overburn"),
			Options.overburn);
		overburnBox.setToolTipText(i18n("OptionsDialog.overburnTip"));
		if (CDcreator.overburnSupport)
			p.add(overburnBox);

		p.add(Box.createHorizontalStrut(0));

		return p;
	}

	private int getSpeed() {
		try {
			StreamTokenizer tokenizer
			= new StreamTokenizer(
				new StringReader(
				(String)speedBox.getSelectedItem()));

		if (tokenizer.nextToken() != StreamTokenizer.TT_NUMBER)
			return 1;
		else
			return (int)tokenizer.nval;
		} catch (Exception e)
		{
			return 1;
		}
	}

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

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

	public boolean wasCanceled() {
		return canceled;
	}
}
