/*
Tiamat - a role-playing game framework
LocationExitDialog.java - dialog for editing a LocationExit
 
 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.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import com.huguesjohnson.tiamat.LocationExit;

/** LocationExitDialog - dialog for editing a LocationExit
 * @author Hugues Johnson
 */
public class LocationExitDialog 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=763491619749085707L;

	//private ui components
    private JTextField textFieldDirection;
    private JTextField textFieldName;
	
    public LocationExitDialog(Frame parent){
        super(parent,true);
        this.initComponents();
        //center the window
        if(parent==null){
            this.centerWindow();
        } else{
            this.centerToParent(parent);
        }
    }

    public LocationExitDialog(Frame parent,LocationExit locationExit){
        super(parent,true);
        this.initComponents();
        //center the window
        if(parent==null){
            this.centerWindow();
        } else{
            this.centerToParent(parent);
        }
        //set text fields
        this.textFieldDirection.setText(locationExit.getDirection());
        this.textFieldName.setText(locationExit.getName());
    }
    
    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("Location Exit");
        this.setResizable(false);
        this.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent evt){
            	closeDialog(true);
            }
        });
        //add direction
        JPanel panelDirection=new JPanel();
        panelDirection.setLayout(new BoxLayout(panelDirection,BoxLayout.X_AXIS));
        JLabel labelDirection=new JLabel("Direction: ");
        labelDirection.setHorizontalAlignment(SwingConstants.RIGHT);
        labelDirection.setMaximumSize(new Dimension(65,15));
        labelDirection.setMinimumSize(new Dimension(65,15));
        labelDirection.setPreferredSize(new Dimension(65,15));
        panelDirection.add(labelDirection);
        this.textFieldDirection=new JTextField();
        this.textFieldDirection.setToolTipText("Direction");
        this.textFieldDirection.setMinimumSize(new Dimension(160,21));
        this.textFieldDirection.setPreferredSize(new Dimension(160,21));
        panelDirection.add(this.textFieldDirection);
        this.getContentPane().add(panelDirection);
        //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(65,15));
        labelName.setMinimumSize(new Dimension(65,15));
        labelName.setPreferredSize(new Dimension(65,15));
        panelName.add(labelName);
        this.textFieldName=new JTextField();
        this.textFieldName.setToolTipText("Name of Location this LocationExit leads to");
        panelName.add(this.textFieldName);
        this.getContentPane().add(panelName);
        //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);
    }

    /** override this in the implementing class */
    public void closeDialog(boolean cancel){
        this.dispose();
        System.exit(0);
    }        
    
    public LocationExit getLocationExit(){
    	return(new LocationExit(this.textFieldDirection.getText(),this.textFieldName.getText()));
    }
}