package j3deditor.bin.editor.tools;

import j3deditor.bin.editor.GeometryEditor;

import j3deditor.bin.hierarchy.GeometryManipulator;

import j3deditor.bin.util.Utilities;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;


/**
 * UI for controlling <code>Scene2D</code> properties.
 * 
 * @author Risto Seene
 */
public class ViewPanel2D extends JPanel{

	private GeometryManipulator gm;

	private GeometryEditor parent;

	private JCheckBox edges;
	
	private JCheckBox stickyVerts;
	
	private JCheckBox[] scaleAxes;


	/**
	 * Creates an instance of <code>ViewPanel2D</code>.
	 * 
	 * @param gm
	 * 	<code>GeometryManipulator</code> which properties can be controlled
	 *        with this panel
	 * @param parent
	 * 	geometry editor main component
	 */
	public ViewPanel2D(GeometryManipulator gm, GeometryEditor parent){
		this.gm = gm;
		this.parent = parent;
		this.setOpaque(false);
		initContent();
	}

	/**
	 * Initializes the content of this panel.
	 */
	private void initContent(){
		this.setLayout(new GridLayout(2, 2, 1, 1));
		
		this.add(new JLabel(Utilities.translator.translate(this.getClass(), "title")));
		
	          stickyVerts = new JCheckBox(Utilities.translator.translate(this.getClass(), "stickyVerts"));
	          stickyVerts.setSelected(this.gm.isStickyVertsEnabled());
	          stickyVerts.addActionListener(new ActionListener(){

	                              public void actionPerformed(ActionEvent e){
	                                        gm.setStickyVertsEnabled(stickyVerts.isSelected());
	                              }
	                    });
	          stickyVerts.setOpaque(false);
	          this.add(stickyVerts);
		
	          edges = new JCheckBox(Utilities.translator.translate(this.getClass(), "edges"), true);
	          edges.addActionListener(new ActionListener(){

	                              public void actionPerformed(ActionEvent e){
	                                        parent.setEdgeVisibility(edges.isSelected());
	                              }
	                    });
	          edges.setOpaque(false);
	          this.add(edges);
		
		JPanel panel = new JPanel(new GridLayout(1, 3, 1, 1));
		panel.setOpaque(false);
		String scale = Utilities.translator.translate(this.getClass(), "scale");
		scaleAxes = new JCheckBox[3];
		scaleAxes[0] = new JCheckBox(Utilities.translator.translate(this.getClass(), "scaleX"), true);
	          scaleAxes[0].addActionListener(new ActionListener(){

	                              public void actionPerformed(ActionEvent e){
	                                        updateScaleAxes();
	                              }
	                    });
		scaleAxes[0].setToolTipText(scale);
		scaleAxes[0].setOpaque(false);
		panel.add(scaleAxes[0]);
	          scaleAxes[1] = new JCheckBox(Utilities.translator.translate(this.getClass(), "scaleY"), true);
	          scaleAxes[1].addActionListener(new ActionListener(){

	                              public void actionPerformed(ActionEvent e){
	                                        updateScaleAxes();
	                              }
	                    });
	          scaleAxes[1].setToolTipText(scale);
	          scaleAxes[1].setOpaque(false);
	          panel.add(scaleAxes[1]);
	          scaleAxes[2] = new JCheckBox(Utilities.translator.translate(this.getClass(), "scaleZ"), true);
	          scaleAxes[2].addActionListener(new ActionListener(){

	                              public void actionPerformed(ActionEvent e){
	                                        updateScaleAxes();
	                              }
	                    });
	          scaleAxes[2].setToolTipText(scale);
	          scaleAxes[2].setOpaque(false);
	          panel.add(scaleAxes[2]);
		this.add(panel);
	}

	/**
	 * Updates scale axes.
	 */
	private void updateScaleAxes(){
		int[] axes = new int[3];
		for(int i=0; i<3; i++){
			if(scaleAxes[i].isSelected()){
				axes[i] = 1;
			}
			else {
				axes[i] = 0;
			}
		}
		this.parent.updateScaleAxes(axes);
	}

}
