/*
Tiamat - a role-playing game framework
LocationObjectDataDialog.java - dialog for editing LocationObjectData
 
 Copyright  (C) 2006 Hugues Johnson
 
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
 
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
the GNU General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

package com.huguesjohnson.tiamat.tools;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import com.huguesjohnson.tiamat.LocationObjectData;

/** LocationExitDialog - dialog for editing LocationObjectData
 * @author Hugues Johnson
 */
public class LocationObjectDataDialog extends JDialog{
	/* Determines if a de-serialized file is compatible with this class. See <a href="http://java.sun.com/j2se/1.5.0/docs/guide/serialization/spec/class.html">Java Object Serialization Specifications</a>. */
	private static final long serialVersionUID=-8957230691634665381L;

	//used for combo boxes
	private class XEntry{
		public LocationObjectData.XAlignment xalign;
		public String caption;
		public XEntry(LocationObjectData.XAlignment xalign,String caption){
			this.xalign=xalign;
			this.caption=new String(caption);
		}
		public String toString(){ return(this.caption); }
	}
	private class YEntry{
		public LocationObjectData.YAlignment yalign;
		public String caption;
		public YEntry(LocationObjectData.YAlignment yalign,String caption){
			this.yalign=yalign;
			this.caption=new String(caption);
		}
		public String toString(){ return(this.caption); }
	}
	
	//private ui components
    private JTextField textFieldName;
    private JCheckBox checkboxVisible;
    private JComboBox comboBoxXAlign;
    private JComboBox comboBoxYAlign;
    private JTextField textFieldX;
    private JTextField textFieldY;
	
    public LocationObjectDataDialog(Frame parent){
        super(parent,true);
        this.initComponents();
        //center the window
        if(parent==null){
            this.centerWindow();
        } else{
            this.centerToParent(parent);
        }
    }

    public LocationObjectDataDialog(Frame parent,LocationObjectData locationData){
        super(parent,true);
        this.initComponents();
        //center the window
        if(parent==null){
            this.centerWindow();
        } else{
            this.centerToParent(parent);
        }
        //set fields
        this.setLocationObjectData(locationData);
    }

    public void centerWindow(){
         Dimension screenDimension=Toolkit.getDefaultToolkit().getScreenSize();
         Rectangle windowDimension=this.getBounds();
         this.setLocation((screenDimension.width-windowDimension.width)/2,(screenDimension.height-windowDimension.height)/2);
    }
    
    public void centerToParent(Frame parent){
         Rectangle parentDimension=parent.getBounds();
         Rectangle windowDimension=this.getBounds();
         this.setLocation((parentDimension.width-windowDimension.width)/2,(parentDimension.height-windowDimension.height)/2);
    }

    private void initComponents(){
        this.getContentPane().setLayout(new BoxLayout(this.getContentPane(),BoxLayout.Y_AXIS));
        this.setTitle("Object Data");
        this.setResizable(false);
        this.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent evt){
            	closeDialog(true);
            }
        });
        //add name
        JPanel panelName=new JPanel();
        panelName.setLayout(new BoxLayout(panelName,BoxLayout.X_AXIS));
        JLabel labelName=new JLabel("Name: ");
        labelName.setHorizontalAlignment(SwingConstants.RIGHT);
        labelName.setMaximumSize(new Dimension(45,15));
        labelName.setMinimumSize(new Dimension(45,15));
        labelName.setPreferredSize(new Dimension(45,15));
        panelName.add(labelName);
        this.textFieldName=new JTextField();
        this.textFieldName.setToolTipText("Name of the object");
        panelName.add(this.textFieldName);
        this.getContentPane().add(panelName);
        //add checkbox
        JPanel panelVisible=new JPanel();
        panelVisible.setLayout(new FlowLayout(FlowLayout.LEFT));
        this.checkboxVisible=new JCheckBox("Visible");
        panelVisible.add(this.checkboxVisible);
        this.getContentPane().add(panelVisible);
        //add xalignment
        JPanel panelXAlign=new JPanel();
        panelXAlign.setLayout(new BoxLayout(panelXAlign,BoxLayout.X_AXIS));
        JLabel labelXAlign=new JLabel("X-Alignment: ");
        labelXAlign.setHorizontalAlignment(SwingConstants.RIGHT);
        labelXAlign.setMaximumSize(new Dimension(90,15));
        labelXAlign.setMinimumSize(new Dimension(90,15));
        labelXAlign.setPreferredSize(new Dimension(90,15));
        panelXAlign.add(labelXAlign);
        this.comboBoxXAlign=new JComboBox();
        this.comboBoxXAlign.setToolTipText("X-alignment of object");
        //TODO better way to add these items?
        this.comboBoxXAlign.insertItemAt(new XEntry(LocationObjectData.XAlignment.FIXED,"Fixed"),0);
        this.comboBoxXAlign.insertItemAt(new XEntry(LocationObjectData.XAlignment.CENTER,"Center"),1);
        this.comboBoxXAlign.insertItemAt(new XEntry(LocationObjectData.XAlignment.LEFT,"Left"),2);
        this.comboBoxXAlign.insertItemAt(new XEntry(LocationObjectData.XAlignment.RIGHT,"Right"),3);
        this.comboBoxXAlign.setSelectedIndex(0);
        panelXAlign.add(this.comboBoxXAlign);
        this.getContentPane().add(panelXAlign);
        //add yalignment
        JPanel panelYAlign=new JPanel();
        panelYAlign.setLayout(new BoxLayout(panelYAlign,BoxLayout.X_AXIS));
        JLabel labelYAlign=new JLabel("Y-Alignment: ");
        labelYAlign.setHorizontalAlignment(SwingConstants.RIGHT);
        labelYAlign.setMaximumSize(new Dimension(90,15));
        labelYAlign.setMinimumSize(new Dimension(90,15));
        labelYAlign.setPreferredSize(new Dimension(90,15));
        panelYAlign.add(labelYAlign);
        this.comboBoxYAlign=new JComboBox();
        this.comboBoxYAlign.setToolTipText("Y-alignment of object");
        //TODO better way to add these items?
        this.comboBoxYAlign.insertItemAt(new YEntry(LocationObjectData.YAlignment.FIXED,"Fixed"),0);
        this.comboBoxYAlign.insertItemAt(new YEntry(LocationObjectData.YAlignment.CENTER,"Center"),1);
        this.comboBoxYAlign.insertItemAt(new YEntry(LocationObjectData.YAlignment.TOP,"Top"),2);
        this.comboBoxYAlign.insertItemAt(new YEntry(LocationObjectData.YAlignment.BOTTOM,"Bottom"),3);
        this.comboBoxYAlign.setSelectedIndex(0);
        panelYAlign.add(this.comboBoxYAlign);
        this.getContentPane().add(panelYAlign);
        //add x and y 
        JPanel panelXY=new JPanel();
        panelXY.setLayout(new BoxLayout(panelXY,BoxLayout.X_AXIS));
        JLabel labelX=new JLabel("X: ");
        labelX.setHorizontalAlignment(SwingConstants.RIGHT);
        panelXY.add(labelX);
        this.textFieldX=new JTextField();
        this.textFieldX.setToolTipText("X location of object");
        panelXY.add(this.textFieldX);
        JLabel labelY=new JLabel("Y: ");
        labelY.setHorizontalAlignment(SwingConstants.RIGHT);
        panelXY.add(labelY);
        this.textFieldY=new JTextField();
        this.textFieldY.setToolTipText("X location of object");
        panelXY.add(this.textFieldY);
        this.getContentPane().add(panelXY);
        //add the ok and cancel buttons
        JPanel panelButtons=new JPanel();
        panelButtons.setLayout(new FlowLayout(FlowLayout.RIGHT));
        JButton buttonOk=new JButton();
        buttonOk.setText("OK");
        buttonOk.setToolTipText("Save this LocationExit");
        buttonOk.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent evt){
                onOk();
            }
        });
        panelButtons.add(buttonOk);
        JButton buttonCancel=new JButton();
        buttonCancel.setText("Cancel");
        buttonCancel.setToolTipText("Cancel");
        buttonCancel.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent evt){
                onCancel();
            }
        });
        panelButtons.add(buttonCancel);
        this.getContentPane().add(panelButtons);
        //pack
        this.pack();
    }
    
    public void onCancel(){
        this.closeDialog(true);
    }

    public void onOk(){
        this.closeDialog(false);
    }
    
    public void setLocationObjectData(LocationObjectData data){
    	this.textFieldName.setText(data.getName());
    	this.textFieldX.setText(String.valueOf(data.getX()));
    	this.textFieldY.setText(String.valueOf(data.getY()));
    	this.checkboxVisible.setSelected(data.getVisible());
    	//TODO does this work as expected?
    	this.comboBoxXAlign.setSelectedIndex(data.getXalign().ordinal());
    	this.comboBoxYAlign.setSelectedIndex(data.getYalign().ordinal());
    }
    
    public LocationObjectData getLocationObjectData(){
    	int x=-1;
    	int y=-1;
    	try{
    		x=Integer.parseInt(this.textFieldX.getText());
    		y=Integer.parseInt(this.textFieldY.getText());
    	} catch(Exception ex){
    		ex.printStackTrace();
    	}
    	return(new LocationObjectData(
    				this.textFieldName.getText(),
    				this.checkboxVisible.isSelected(),
    				((XEntry)this.comboBoxXAlign.getSelectedItem()).xalign,
    				((YEntry)this.comboBoxYAlign.getSelectedItem()).yalign,x,y));
    }

    /** override this in the implementing class */
    public void closeDialog(boolean cancel){
        this.dispose();
        System.exit(0);
    }    
}