package j3deditor.bin.editor.tools;

import j3deditor.bin.hierarchy.util.HierarchyEvent;
import j3deditor.bin.hierarchy.util.HierarchyEventListener;

import j3deditor.bin.hierarchy.J3DeAppearance;

import j3deditor.bin.util.Utilities;

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

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JSeparator;


/**
 * User interface for manipulating  the attributes of
 * active <code>J3DeAppearance</code> node.
 *
 * @author Risto Seene
 */
public class AppearancePanel extends JPanel implements ActionListener{


	/** <code>J3DeAppearance</code> this panel manipulates */
	private J3DeAppearance appearance;

	/** listener that listens hierarchy manipulation events */
	private HierarchyEventListener listener;


	/** @see J3DeAppearance#setMaterial() */
	private JButton mat;

	/** Not in use yet */
	private JButton tex;

	/** @see J3DeAppearance#setTextureAttributes() */
	private JButton ta;


	/**
	 * Creates an instance of <code>AppearancePanel</code>
	 * that manipulates the given <code>J3DeAppearance</code> node.
	 *
	 * @param appearance
	 *	hierarchy node this panel manipulates
	 * @param listener
	 *	listener that listens hierarchy manipulation events
	 *	made by this panel
	 */
	public AppearancePanel(J3DeAppearance appearance, HierarchyEventListener listener){
		this.appearance = appearance;
		this.listener = listener;

		setLayout(new BorderLayout());
		setOpaque(false);
		initContent();
	}


	/**
	 * Initializes the content of this panel
	 */
	private void initContent(){
		add(new JSeparator(), BorderLayout.NORTH);


		Container tools = new Container();
		tools.setLayout(new GridLayout(3, 1, 5, 5));
		add(tools, BorderLayout.CENTER);

		mat = new JButton(Utilities.translator.translate(this.getClass(), "material"));
		mat.setToolTipText(Utilities.translator.translate(this.getClass(), "matinfo"));
		mat.setEnabled(!appearance.hasMaterial());
		mat.addActionListener(this);
		tools.add(mat);

		tex = new JButton(Utilities.translator.translate(this.getClass(), "texture"));
		tex.setToolTipText(Utilities.translator.translate(this.getClass(), "texinfo"));
		tex.setEnabled(!appearance.hasTexture());
		tex.addActionListener(this);
		tools.add(tex);

		ta = new JButton(Utilities.translator.translate(this.getClass(), "texattrib"));
		ta.setToolTipText(Utilities.translator.translate(this.getClass(), "tainfo"));
		ta.setEnabled(!appearance.hasTextureAttributes() && appearance.hasTexture());
		ta.addActionListener(this);
		tools.add(ta);
	}


	/**
	 * Listens action events sent by control buttons attached to this panel.
	 *
	 * @param e
	 *	event sent by a control button
	 */
	public void actionPerformed(ActionEvent e){
		if(e.getSource() == mat){
			appearance.setMaterial();
			mat.setEnabled(false);
		}
		else if(e.getSource() == tex){
			new TextureDialog(this.appearance);
			if(appearance.hasTexture()){
				tex.setEnabled(false);
				if(!appearance.hasTextureAttributes()){
					ta.setEnabled(true);
				}
			}
		}
		else if(e.getSource() == ta){
			appearance.setTextureAttributes();
			ta.setEnabled(false);
		}

		if(listener != null){
			listener.hierarchyEventOccured(new HierarchyEvent(this, HierarchyEvent.OBJECT_ADDITION));
		}
	}

}
