/*
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.PathResolver;
import com.huguesjohnson.tiamat.Item;
import com.huguesjohnson.tiamat.Target;
import com.huguesjohnson.ui.ImageBrowsePanel;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Rectangle;
import java.awt.Toolkit;
import javax.swing.JDialog;

/** ItemDialog - dialog for editing an Item
 * an awful lot of this code was generated through the NetBeans 3.6 form editor
 * @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 String absoluteDir;
    
    /** Creates new form ItemDialog */
    public ItemDialog(Frame parent,String absoluteDir){
        super(parent,true);
        this.absoluteDir=absoluteDir;
        this.initComponents();
        //add the target panel
        this.targetList=new TargetListPanel();
        this.panelTargets.add(this.targetList);
        //add the image panels
        this.thumbnailImagePanel=new ImageBrowsePanel("Thumbnail Image");
        this.panelThumbnailImage.add(this.thumbnailImagePanel);
        this.fullSizeImagePanel=new ImageBrowsePanel("Full Size Image");
        this.panelFullSizeImage.add(this.fullSizeImagePanel);
        this.pack();
        //center the window
        if(parent==null){
            this.centerWindow();
        } else{
            this.centerToParent(parent);
        }
    }

    public ItemDialog(Frame parent,Item item,String absolutePath){
        super(parent,true);
        this.absoluteDir=absolutePath;
        this.initComponents();
        this.targetList=new TargetListPanel();
        this.panelTargets.add(this.targetList);
        this.thumbnailImagePanel=new ImageBrowsePanel("Thumbnail Image");
        this.panelThumbnailImage.add(this.thumbnailImagePanel);
        this.fullSizeImagePanel=new ImageBrowsePanel("Full Size Image");
        this.panelFullSizeImage.add(this.fullSizeImagePanel);
        //add the item properties to the dialog
        this.textFieldName.setText(item.getName());
        this.textFieldDescription.setText(item.getDescription());
        if(item.getEquipable()){
            this.checkBoxEquipable.setSelected(true);
            this.checkBoxEquipped.setEnabled(true);
            this.checkBoxEquipped.setSelected(item.getEquipped());
        }
        this.checkBoxDisappearOnUse.setSelected(item.getDisappearOnUse());
        String fullImagePath=item.getFullImagePathAbsolute();
        this.fullSizeImagePanel.setImage(fullImagePath);
        String thumbnailImagePath=item.getThumbnailImagePathAbsolute();
        this.thumbnailImagePanel.setImage(thumbnailImagePath);
        int targetCount=item.getTargetCount();
        for(int index=0;index<targetCount;index++){
            this.targetList.addTarget(item.getTarget(index));
        }        
        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);
     }         
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
    	this.panelName = new javax.swing.JPanel();
    	this.labelName = new javax.swing.JLabel();
    	this.textFieldName = new javax.swing.JTextField();
    	this.panelDescription = new javax.swing.JPanel();
    	this.labelDescription = new javax.swing.JLabel();
    	this.textFieldDescription = new javax.swing.JTextField();
    	this.panelCheckboxes = new javax.swing.JPanel();
    	this.checkBoxEquipable = new javax.swing.JCheckBox();
    	this.checkBoxEquipped = new javax.swing.JCheckBox();
    	this.checkBoxDisappearOnUse = new javax.swing.JCheckBox();
    	this.panelFullSizeImage = new javax.swing.JPanel();
    	this.panelThumbnailImage = new javax.swing.JPanel();
    	this.panelTargets = new javax.swing.JPanel();
    	this.panelTargetList = new javax.swing.JPanel();
    	this.panelButtons = new javax.swing.JPanel();
    	this.buttonOk = new javax.swing.JButton();
    	this.buttonCancel = new javax.swing.JButton();

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

    	this.setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
    	this.setTitle("Item");
    	this.setResizable(false);
    	this.addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

    	this.panelName.setLayout(new javax.swing.BoxLayout(this.panelName, javax.swing.BoxLayout.X_AXIS));

    	this.labelName.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
    	this.labelName.setText("Name: ");
    	this.labelName.setMaximumSize(new java.awt.Dimension(85, 15));
    	this.labelName.setMinimumSize(new java.awt.Dimension(85, 15));
    	this.labelName.setPreferredSize(new java.awt.Dimension(85, 15));
    	this.panelName.add(this.labelName);

    	this.textFieldName.setToolTipText("Name of the Item");
    	this.textFieldName.setMaximumSize(new java.awt.Dimension(2147483647, 21));
    	this.textFieldName.setMinimumSize(new java.awt.Dimension(300, 21));
    	this.panelName.add(this.textFieldName);

    	this.getContentPane().add(this.panelName);

    	this.panelDescription.setLayout(new javax.swing.BoxLayout(this.panelDescription, javax.swing.BoxLayout.X_AXIS));

    	this.labelDescription.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
    	this.labelDescription.setText("Description: ");
    	this.labelDescription.setMaximumSize(new java.awt.Dimension(85, 15));
    	this.labelDescription.setMinimumSize(new java.awt.Dimension(85, 15));
    	this.labelDescription.setPreferredSize(new java.awt.Dimension(85, 15));
    	this.panelDescription.add(this.labelDescription);

    	this.textFieldDescription.setToolTipText("Description of the Item");
    	this.textFieldDescription.setMaximumSize(new java.awt.Dimension(2147483647, 21));
    	this.panelDescription.add(this.textFieldDescription);

    	this.getContentPane().add(this.panelDescription);

    	this.panelCheckboxes.setLayout(new javax.swing.BoxLayout(this.panelCheckboxes, javax.swing.BoxLayout.X_AXIS));

    	this.checkBoxEquipable.setText("Equipable");
    	this.checkBoxEquipable.setToolTipText("Check if this Item can be equipped");
    	this.checkBoxEquipable.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                checkBoxEquipableActionPerformed(evt);
            }
        });

    	this.panelCheckboxes.add(this.checkBoxEquipable);

    	this.checkBoxEquipped.setText("Equipped");
    	this.checkBoxEquipped.setToolTipText("Check if this Item is equipped");
    	this.checkBoxEquipped.setEnabled(false);
    	this.panelCheckboxes.add(this.checkBoxEquipped);

    	this.checkBoxDisappearOnUse.setText("Disappear on use");
    	this.checkBoxDisappearOnUse.setToolTipText("Check if this Item should disappear after being used");
    	this.panelCheckboxes.add(this.checkBoxDisappearOnUse);

    	this.getContentPane().add(this.panelCheckboxes);

    	this.panelFullSizeImage.setLayout(new javax.swing.BoxLayout(this.panelFullSizeImage, javax.swing.BoxLayout.X_AXIS));

    	this.getContentPane().add(this.panelFullSizeImage);

    	this.panelThumbnailImage.setLayout(new javax.swing.BoxLayout(this.panelThumbnailImage, javax.swing.BoxLayout.X_AXIS));

    	this.getContentPane().add(this.panelThumbnailImage);

    	this.panelTargets.setLayout(new javax.swing.BoxLayout(this.panelTargets, javax.swing.BoxLayout.Y_AXIS));

    	this.panelTargetList.setLayout(new javax.swing.BoxLayout(this.panelTargetList, javax.swing.BoxLayout.X_AXIS));

    	this.panelTargets.add(this.panelTargetList);

    	this.getContentPane().add(this.panelTargets);

    	this.panelButtons.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

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

    	this.panelButtons.add(this.buttonOk);

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

    	this.panelButtons.add(this.buttonCancel);

    	this.getContentPane().add(this.panelButtons);

    	this.pack();
    }//GEN-END:initComponents

    public void buttonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCancelActionPerformed
        this.closeDialog(true);
    }//GEN-LAST:event_buttonCancelActionPerformed

    public void buttonOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonOkActionPerformed
        this.closeDialog(false);
    }//GEN-LAST:event_buttonOkActionPerformed

    public void checkBoxEquipableActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_checkBoxEquipableActionPerformed
        if(this.checkBoxEquipable.isSelected()){
            if(!this.checkBoxEquipped.isEnabled()){
                this.checkBoxEquipped.setEnabled(true);
            }
        } else{
            this.checkBoxEquipped.setEnabled(false);
            this.checkBoxEquipped.setSelected(false);
        }
    }//GEN-LAST:event_checkBoxEquipableActionPerformed

    public void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        this.closeDialog(true);
    }//GEN-LAST:event_formWindowClosing

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

    /** get the Item represented by this dialog */
    public Item getItem(){
        String name=this.textFieldName.getText();
        String description=this.textFieldDescription.getText();
        if((this.absoluteDir==null)||(this.absoluteDir.length()<1)){
            this.absoluteDir=this.fullSizeImagePanel.getDirectory();
        }
        boolean equipable=this.checkBoxEquipable.isSelected();
        boolean equipped=this.checkBoxEquipped.isSelected();
        boolean disappearOnUse=this.checkBoxDisappearOnUse.isSelected();
        String fullImagePath=PathResolver.getRelativePath(this.absoluteDir,this.fullSizeImagePanel.getImagePath());
        String thumbnailImagePath=PathResolver.getRelativePath(this.absoluteDir,this.thumbnailImagePanel.getImagePath());
        Target[] targets=this.targetList.getAllTargets();
        return(new Item(name,description,this.absoluteDir,fullImagePath,thumbnailImagePath,targets,equipable,disappearOnUse));
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonCancel;
    private javax.swing.JButton buttonOk;
    private javax.swing.JCheckBox checkBoxDisappearOnUse;
    private javax.swing.JCheckBox checkBoxEquipable;
    private javax.swing.JCheckBox checkBoxEquipped;
    private javax.swing.JLabel labelDescription;
    private javax.swing.JLabel labelName;
    private javax.swing.JPanel panelButtons;
    private javax.swing.JPanel panelCheckboxes;
    private javax.swing.JPanel panelDescription;
    private javax.swing.JPanel panelFullSizeImage;
    private javax.swing.JPanel panelName;
    private javax.swing.JPanel panelTargetList;
    private javax.swing.JPanel panelTargets;
    private javax.swing.JPanel panelThumbnailImage;
    private javax.swing.JTextField textFieldDescription;
    private javax.swing.JTextField textFieldName;
    // End of variables declaration//GEN-END:variables
    private TargetListPanel targetList;
    private ImageBrowsePanel thumbnailImagePanel;
    private ImageBrowsePanel fullSizeImagePanel;
}