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

/**
  * This is a dialog which allows the user to choose the options
  *
  * @version unknown
  * @author Jörg Haeger
  */
public class OptionsDialog extends JDialog
{
	public OptionsDialog()
	{
		super(CDcreator.mainWin, i18n("titleOptions"), true);

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

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

		// Create filesystem group
		JPanel fsPanel = new JPanel();
		fsPanel.setLayout(
			new BoxLayout(fsPanel, BoxLayout.Y_AXIS));
		fsPanel.setBorder(
			BorderFactory.createCompoundBorder(
			BorderFactory.createTitledBorder(
			BorderFactory.createLineBorder(Color.black),
			i18n("filesystemGroup")),
			BorderFactory.createEmptyBorder(5,5,5,5)));

		volumeIDField = new JTextField(Options.volumeID);
		fsPanel.add(createLabelItemBox(
			new JLabel(i18n("volumeID")),
			volumeIDField));

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

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

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

		if (CDcreator.sessionType
			== CDcreator.multiSession)
			panel.add(fsPanel);

		// Create writing group
		JPanel b = new JPanel();
		b.setLayout(
			new BoxLayout(b, BoxLayout.Y_AXIS));
		b.setBorder(
			BorderFactory.createCompoundBorder(
			BorderFactory.createTitledBorder(
			BorderFactory.createLineBorder(Color.black),
			i18n("burnGroup")),
			BorderFactory.createEmptyBorder(5,5,5,5)));

		// Add Speed x row
		String[] speeds = new String[CDcreator.maxSpeed / 2 + 1];
		speeds[0] = "1x";
		for (int i = 2; i <= CDcreator.maxSpeed; i += 2)
			speeds[i / 2] = i + "x";
		speedBox = new JComboBox(speeds);
		speedBox.setSelectedIndex(Options.speed / 2);
		speedBox.setToolTipText(i18n("TTspeedBox"));
		b.add(createLabelItemBox(
			new JLabel(i18n("speed")), speedBox));

		// Add simulate box
		simulateBox = new JCheckBox(
			i18n("laserOff"), Options.simulate);
		b.add(simulateBox);

		panel.add(b);

		panel.add(Box.createRigidArea(new Dimension(280, 10)));

		blankTOCBox = new JCheckBox(
			i18n("blankTOC"), Options.blankTOC);
		blankTOCBox.setToolTipText(i18n("TTblankTOCBox"));
		panel.add(blankTOCBox);

		closeCDBox = new JCheckBox(
			i18n("closeCD"), Options.closeCD);
		closeCDBox.setToolTipText(i18n("TTcloseCDBox"));
		panel.add(closeCDBox);

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

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

		panel = new JPanel();
		panel.setLayout(new FlowLayout());

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

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

		getContentPane().add(panel, BorderLayout.SOUTH);
//		pack();
		canceled = false;
	}

	public void apply()
	{
		Options.blankTOC = blankTOCBox.isSelected();
		Options.closeCD = closeCDBox.isSelected();
		Options.HFS = HFSBox.isSelected();
		Options.joliet = jolietBox.isSelected();
		Options.offline = offlineBox.isSelected();
		Options.rockRidge = rockRidgeBox.isSelected();
		Options.simulate = simulateBox.isSelected();
		Options.speed = getSpeed();
		Options.volumeID = volumeIDField.getText();
	}

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

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

	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;
	}

	JCheckBox offlineBox, blankTOCBox, closeCDBox, HFSBox,
		jolietBox, rockRidgeBox, simulateBox;
	JComboBox speedBox;
	JTextField volumeIDField;
	boolean canceled;
}
