package de.rico.editor.gui.dialog;

import java.awt.Font;
import java.awt.Frame;
import java.awt.Rectangle;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import de.rico.editor.WorldEditor;

/**
 * A dialog to open a room file.
 * @author Frank Bruns
 *
 */
public class DialogSetBaseFolder extends JDialog
{

	private static final long serialVersionUID = 1L;

	private DialogSetBaseFolder self = null;

	private JPanel jContentPane = null;

	private JLabel labelHead = null;

	private JTextField txtFolderToChoose = null;

	private JButton btnChooseFolder = null;

	private JButton btnSetFolder = null;

	/**
	 * @param owner
	 */
	public DialogSetBaseFolder(Frame owner)
	{
		super(owner);
		this.initialize();
		this.self = this;
	}

	/**
	 * This method initializes this
	 *
	 * @return void
	 */
	private void initialize()
	{
		this.setSize(300, 153);
		this.setTitle("Load a room");
		this.setContentPane(this.getJContentPane());
	}

	/**
	 * This method initializes jContentPane
	 *
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane()
	{
		if (this.jContentPane == null)
		{
			this.labelHead = new JLabel();
			this.labelHead.setText("Choose the base folder for the room");
			this.labelHead.setBounds(new Rectangle(15, 15, 256, 19));
			this.labelHead.setFont(new Font("Dialog", Font.BOLD, 14));
			this.jContentPane = new JPanel();
			this.jContentPane.setLayout(null);
			this.jContentPane.add(this.labelHead, null);
			this.jContentPane.add(this.getTxtFolderToChoose(), null);
			this.jContentPane.add(this.getBtnChooseFolder(), null);
			this.jContentPane.add(this.getBtnSetFolder(), null);
		}
		return this.jContentPane;
	}

	/**
	 * This method initializes txtFolderToChoose
	 *
	 * @return javax.swing.JTextField
	 */
	private JTextField getTxtFolderToChoose()
	{
		if (this.txtFolderToChoose == null)
		{
			this.txtFolderToChoose = new JTextField();
			this.txtFolderToChoose.setBounds(new Rectangle(15, 45, 211, 16));

			if(WorldEditor.getBaseFolder() != null)
				this.txtFolderToChoose.setText(WorldEditor.getBaseFolder());
		}
		return this.txtFolderToChoose;
	}

	/**
	 * This method initializes btnChooseFolder
	 *
	 * @return javax.swing.JButton
	 */
	private JButton getBtnChooseFolder()
	{
		if (this.btnChooseFolder == null)
		{
			this.btnChooseFolder = new JButton();
			this.btnChooseFolder.setBounds(new Rectangle(240, 45, 31, 16));
			this.btnChooseFolder.setText("...");
			this.btnChooseFolder.addActionListener(new java.awt.event.ActionListener()
			{
				public void actionPerformed(java.awt.event.ActionEvent e)
				{
					JFileChooser fc = new JFileChooser();
					fc.setCurrentDirectory(new File("./"));
					fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

					int returnVal = fc.showOpenDialog(DialogSetBaseFolder.this.self);

					if (returnVal == JFileChooser.APPROVE_OPTION)
					{
						File file = fc.getSelectedFile();
						DialogSetBaseFolder.this.txtFolderToChoose.setText(file.getAbsolutePath());
					}
				}
			});
		}
		return this.btnChooseFolder;
	}

	/**
	 * This method initializes btnSetFolder
	 *
	 * @return javax.swing.JButton
	 */
	private JButton getBtnSetFolder()
	{
		if (this.btnSetFolder == null)
		{
			this.btnSetFolder = new JButton();
			this.btnSetFolder.setBounds(new Rectangle(210, 75, 61, 22));
			this.btnSetFolder.setText("OK");
			this.btnSetFolder.addActionListener(new java.awt.event.ActionListener()
			{
				public void actionPerformed(java.awt.event.ActionEvent e)
				{
					WorldEditor.setBaseFolder(DialogSetBaseFolder.this.txtFolderToChoose.getText());
					DialogSetBaseFolder.this.dispose();
				}
			});
		}
		return this.btnSetFolder;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
