package j3deditor.bin.util.preferences;


import j3deditor.bin.util.Utilities;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;


/**
 * User preferences panel.
 *
 * @author Risto Seene
 */
public class PrefPanel extends JPanel implements ActionListener{


	/** reference to <code>JFrame</code> this panel is attached to. */
	private JDialog frame;

	/** view tab. */
	private ViewTab view;

	/** language tab. */
	private LangTab lang;


	/** apply button. */
	private JButton applybutton;

	/** ok button. */
	private JButton okbutton;

	/** cancel button. */
	private JButton cancelbutton;


	/**
	 * Creates an instance of <code>PrefPanel</code>.
	 *
	 * @param frame
	 *	reference to <code>JFrame</code> this panel will be attached to
	 */
	public PrefPanel(JDialog frame){
		this.frame = frame;

		setContents();
	}


	/**
	 * Initializes this panel and its items.
	 */
	private void setContents(){
		setLayout(new BorderLayout(5, 5));

		JPanel base = new JPanel(new BorderLayout(5, 5));
		base.setBackground(new Color(200, 200, 200));
		add(base, BorderLayout.CENTER);

		view = new ViewTab();
		lang = new LangTab();

		JTabbedPane tabs = new JTabbedPane();
		tabs.add(view);
		tabs.add(lang);

		applybutton = new JButton(Utilities.translator.translate(getClass(), "applybutton"));
		okbutton = new JButton(Utilities.translator.translate(getClass(), "okbutton"));
		cancelbutton = new JButton(Utilities.translator.translate(getClass(), "cancelbutton"));

		JPanel buttons = new JPanel(new GridLayout(1, 4, 5, 5));
		buttons.setOpaque(false);
		buttons.add(new JLabel());
		buttons.add(applybutton);
		applybutton.addActionListener(this);
		buttons.add(okbutton);
		okbutton.addActionListener(this);
		buttons.add(cancelbutton);
		cancelbutton.addActionListener(this);

		base.add(new JLabel("<html><body><center>" + Utilities.translator.translate(getClass(), "notelabel") + "</center></body></html>", 0), BorderLayout.NORTH);
		base.add(tabs, BorderLayout.CENTER);
		base.add(buttons, BorderLayout.SOUTH);
	}


	/**
	 * Listens the actions performed on
	 * this panel.
	 *
	 * @param e
	 *	<code>ActionEvent</code> sent by this panel
	 */
	public void actionPerformed(ActionEvent e){
		if(e.getSource() == applybutton){
			saveChanges();
		}
		if(e.getSource() == okbutton){
			saveChanges();
			frame.dispose();
		}
		if(e.getSource() == cancelbutton){
			frame.dispose();
		}
	}


	/**
	 * Passes the values from all tabs to the central
	 * <code>UserData</code> object.
	 */
	private void saveChanges(){
		view.saveChanges();
		lang.saveChanges();

		Utilities.userdata.saveData();
	}

}
