/*
Tiamat - a role-playing game framework
ItemDialog.java - dialog for editing an Item
 
 Copyright  (C) 2004 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 com.huguesjohnson.tiamat.Item;
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.JPanel;
import javax.swing.WindowConstants;

/** ItemDialog - dialog for editing an Item
 * @author Hugues Johnson
 */
public class ItemDialog 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=-7726499282970985694L;
	
	//private ui components
    private JButton buttonCancel;
    private JButton buttonOk;
	private ItemPanel itemPanel;
	
    public ItemDialog(Frame parent,String absoluteDir){
        super(parent,true);
    	this.itemPanel=new ItemPanel(absoluteDir);
        this.initComponents();
        this.pack();
        //center the window
        if(parent==null){
            this.centerWindow();
        } else{
            this.centerToParent(parent);
        }
    }

    public ItemDialog(Frame parent,Item item,String absoluteDir){
        super(parent,true);
    	this.itemPanel=new ItemPanel(item,absoluteDir);
        this.initComponents();
        this.pack();
        //center the window
        if(parent==null){
            this.centerWindow();
        } else{
            this.centerToParent(parent);
        }
    }

     /* centers the window in the middle of the screen */     
     public void centerWindow(){
          Dimension screenDimension=Toolkit.getDefaultToolkit().getScreenSize();
          Rectangle windowDimension=this.getBounds();
          this.setLocation((screenDimension.width-windowDimension.width)/2,(screenDimension.height-windowDimension.height)/2);
     }
     
     /* centers the window in the middle of the screen */     
     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.buttonOk=new JButton();
    	this.buttonCancel=new JButton();

    	this.getContentPane().setLayout(new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS));

    	this.getContentPane().add(this.itemPanel);
    	
    	this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
    	this.setTitle("Item");
    	this.setResizable(false);
    	this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent evt) {
                closeDialog(true);
            }
        });

    	JPanel panelButtons=new JPanel();
    	panelButtons.setLayout(new FlowLayout(FlowLayout.RIGHT));

    	this.buttonOk.setText("OK");
    	this.buttonOk.setToolTipText("Save this Item");
    	this.buttonOk.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                buttonOkActionPerformed(evt);
            }
        });

    	panelButtons.add(this.buttonOk);

    	this.buttonCancel.setText("Cancel");
    	this.buttonCancel.setToolTipText("Canel without saving");
    	this.buttonCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                buttonCancelActionPerformed(evt);
            }
        });

    	panelButtons.add(this.buttonCancel);

    	this.getContentPane().add(panelButtons);

    	this.pack();
    }

    public void buttonCancelActionPerformed(ActionEvent evt){
        this.closeDialog(true);
    }
    
    public void buttonOkActionPerformed(ActionEvent evt){
    	this.closeDialog(false);
    }
    
    /* override this in the implementing class */
    public void closeDialog(boolean cancel){
        this.dispose();
        System.exit(0);
    }

    public Item getItem(){
    	return(this.itemPanel.getItem());
    }
}