/*
Tiamat - a role-playing game framework
TargetDialog.java - dialog for editing a Target
 
 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.Target;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Rectangle;
import java.awt.Toolkit;
import javax.swing.JDialog;
import javax.swing.JFrame;

/** TargetDialog - dialog for editing a Target
 * an awful lot of this code was generated through the NetBeans 3.6 form editor
 * @author Hugues Johnson
 */
public class TargetDialog 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=-7383912612494000499L;
	
	/** Creates new form TargetDialog */
    public TargetDialog(Frame parent) {
        super(parent, true);
        initComponents();
        this.setSize(240,120);
        this.populateComboBox();
        this.textFieldTargetPath.setText("");
        this.textFieldValue.setText("0");
        this.pack();
        //center the window
        if(parent==null){
            this.centerWindow();
        } else{
            this.centerWindow();
            this.centerToParent(parent);
        }
    }

    public TargetDialog(Frame parent, Target target) {
        super(parent, true);
        initComponents();
        this.setSize(240,120);
        this.populateComboBox();
        this.textFieldTargetPath.setText(target.getTargetPath());
        this.textFieldValue.setText(java.lang.String.valueOf(target.getValue()));
        this.comboBoxAffectedEntity.setSelectedIndex(target.getAffectedEntity());
        this.pack();
        //center the window
        if(parent==null){
            this.centerWindow();
        } else{
            this.centerWindow();
            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 populateComboBox(){
        //AFFECTED_ENTITY_USER
        this.comboBoxAffectedEntity.addItem(new String("User of the Item"));
        //AFFECTED_ENTITY_PARTY_SINGLE
        this.comboBoxAffectedEntity.addItem(new String("Single Member of User's Party"));
        //AFFECTED_ENTITY_PARTY_ALL
        this.comboBoxAffectedEntity.addItem(new String("User's Entire Party"));
        //AFFECTED_ENTITY_ENEMY_SINGLE
        this.comboBoxAffectedEntity.addItem(new String("Single Enemy"));
        //AFFECTED_ENTITY_ENEMY_ALL=4;
        this.comboBoxAffectedEntity.addItem(new String("All Enemies"));
        //AFFECTED_ENTITY_EVERYONE=5;
        this.comboBoxAffectedEntity.addItem(new String("Everyone (User, Party, & Enemies)"));
    }
    
    /** 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.panelTargetPath = new javax.swing.JPanel();
    	this.labelTargetPath = new javax.swing.JLabel();
    	this.textFieldTargetPath = new javax.swing.JTextField();
    	this.panelValue = new javax.swing.JPanel();
    	this.labelValue = new javax.swing.JLabel();
    	this.textFieldValue = new javax.swing.JTextField();
    	this.panelAffectedEntity = new javax.swing.JPanel();
    	this.labelAffectedEntity = new javax.swing.JLabel();
    	this.comboBoxAffectedEntity = new javax.swing.JComboBox();
    	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(getContentPane(), javax.swing.BoxLayout.Y_AXIS));

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

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

    	this.labelTargetPath.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
    	this.labelTargetPath.setText("Target Path: ");
    	this.labelTargetPath.setMaximumSize(new java.awt.Dimension(100, 15));
    	this.labelTargetPath.setMinimumSize(new java.awt.Dimension(100, 15));
    	this.labelTargetPath.setPreferredSize(new java.awt.Dimension(100, 15));
    	this.panelTargetPath.add(this.labelTargetPath);

    	this.textFieldTargetPath.setToolTipText("Path of the attribute that is affected by this Target");
    	this.panelTargetPath.add(this.textFieldTargetPath);

    	this.getContentPane().add(this.panelTargetPath);

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

    	this.labelValue.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
    	this.labelValue.setText("Value: ");
    	this.labelValue.setAlignmentX(-0.5F);
    	this.labelValue.setMaximumSize(new java.awt.Dimension(100, 15));
    	this.labelValue.setMinimumSize(new java.awt.Dimension(100, 15));
    	this.labelValue.setPreferredSize(new java.awt.Dimension(100, 15));
    	this.panelValue.add(this.labelValue);

    	this.textFieldValue.setToolTipText("How much the target is affected");
    	this.panelValue.add(this.textFieldValue);

    	this.getContentPane().add(this.panelValue);

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

    	this.labelAffectedEntity.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
    	this.labelAffectedEntity.setText("Affected Entity: ");
    	this.labelAffectedEntity.setFocusCycleRoot(true);
    	this.labelAffectedEntity.setMaximumSize(new java.awt.Dimension(100, 15));
    	this.labelAffectedEntity.setMinimumSize(new java.awt.Dimension(100, 15));
    	this.labelAffectedEntity.setPreferredSize(new java.awt.Dimension(100, 15));
    	this.panelAffectedEntity.add(this.labelAffectedEntity);

    	this.comboBoxAffectedEntity.setMinimumSize(new java.awt.Dimension(220, 21));
    	this.comboBoxAffectedEntity.setPreferredSize(new java.awt.Dimension(220, 21));
    	this.panelAffectedEntity.add(this.comboBoxAffectedEntity);

    	this.getContentPane().add(this.panelAffectedEntity);

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

    	this.buttonOk.setText("OK");
    	this.buttonOk.setToolTipText("Save this Target");
    	this.buttonOk.setActionCommand("OK");
    	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("Exit 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 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);
    }

    /** return the target */
    public Target getTarget(){ 
        String targetName=new String(this.textFieldTargetPath.getText().trim());
        int affectedEntity=this.comboBoxAffectedEntity.getSelectedIndex();
        long value=0;
        try{
            value=java.lang.Long.parseLong(this.textFieldValue.getText());
        } catch(java.lang.NumberFormatException nfx){
            nfx.printStackTrace();
        } 
        return(new Target(targetName,value,affectedEntity));
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        (new TargetDialog(new JFrame())).setVisible(true);
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonCancel;
    private javax.swing.JButton buttonOk;
    private javax.swing.JComboBox comboBoxAffectedEntity;
    private javax.swing.JLabel labelAffectedEntity;
    private javax.swing.JLabel labelTargetPath;
    private javax.swing.JLabel labelValue;
    private javax.swing.JPanel panelAffectedEntity;
    private javax.swing.JPanel panelButtons;
    private javax.swing.JPanel panelTargetPath;
    private javax.swing.JPanel panelValue;
    private javax.swing.JTextField textFieldTargetPath;
    private javax.swing.JTextField textFieldValue;
    // End of variables declaration//GEN-END:variables
    
}
