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

/** AttributeDialog - dialog for editing an Attribute
 * an awful lot of this code was generated through the NetBeans 3.6 form editor
 * @author Hugues Johnson
 */
public class AttributeDialog 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=6998239301517092849L;
	
	/** Creates new form AttributeDialog 
     * @param parent the parent window
     */
    public AttributeDialog(Frame parent){
        super(parent,true);
        initComponents();
        //center the window
        if(parent==null){
            this.centerWindow();
        } else{
            this.centerToParent(parent);
        }
    }

    /** Creates new form AttributeDialog 
     * @param parent the parent window
     * @param attribute the attribute to load
     */
    public AttributeDialog(Frame parent,Attribute attribute){
        super(parent,true);
        initComponents();
        //copy the attribute properties into the dialog
        this.textFieldName.setText(attribute.getName());
        this.textFieldDescription.setText(attribute.getDescription());
        this.textFieldNameBaseValue.setText(java.lang.String.valueOf(attribute.getBaseValue()));
        this.textFieldNameCurrentValue.setText(java.lang.String.valueOf(attribute.getCurrentValue()));
        //center the window
        if(parent==null){
            this.centerWindow();
        } else{
            this.centerToParent(parent);
        }
    }
    
     /** centers the window in the middle of the screen
     */     
     private 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
     */     
     private 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.panelBaseValue = new javax.swing.JPanel();
    	this.labelBaseValue = new javax.swing.JLabel();
    	this.textFieldNameBaseValue = new javax.swing.JTextField();
    	this.labelNameCurrentValue = new javax.swing.JLabel();
    	this.textFieldNameCurrentValue = new javax.swing.JTextField();
    	this.panelCurrentValue = 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(getContentPane(), javax.swing.BoxLayout.Y_AXIS));

    	this.setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
    	this.setTitle("Attribute");
    	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(90, 15));
    	this.labelName.setMinimumSize(new java.awt.Dimension(90, 15));
    	this.labelName.setPreferredSize(new java.awt.Dimension(90, 15));
    	this.panelName.add(this.labelName);

    	this.textFieldName.setToolTipText("Name of this Attribute");
    	this.textFieldName.setMaximumSize(new java.awt.Dimension(2147483647, 21));
    	this.textFieldName.setMinimumSize(new java.awt.Dimension(180, 21));
    	this.textFieldName.setPreferredSize(new java.awt.Dimension(180, 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(90, 15));
    	this.labelDescription.setMinimumSize(new java.awt.Dimension(90, 15));
    	this.labelDescription.setPreferredSize(new java.awt.Dimension(90, 15));
    	this.panelDescription.add(this.labelDescription);

    	this.textFieldDescription.setToolTipText("Description of this Attribute");
    	this.textFieldDescription.setMaximumSize(new java.awt.Dimension(2147483647, 21));
    	this.textFieldDescription.setMinimumSize(new java.awt.Dimension(180, 21));
    	this.textFieldDescription.setPreferredSize(new java.awt.Dimension(180, 21));
    	this.panelDescription.add(this.textFieldDescription);

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

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

    	this.labelBaseValue.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
    	this.labelBaseValue.setText("Base Value: ");
    	this.labelBaseValue.setMaximumSize(new java.awt.Dimension(90, 15));
    	this.labelBaseValue.setMinimumSize(new java.awt.Dimension(90, 15));
    	this.labelBaseValue.setPreferredSize(new java.awt.Dimension(90, 15));
    	this.panelBaseValue.add(this.labelBaseValue);

    	this.textFieldNameBaseValue.setText("0");
    	this.textFieldNameBaseValue.setToolTipText("Base value of this Attribute");
    	this.textFieldNameBaseValue.setMaximumSize(new java.awt.Dimension(2147483647, 21));
    	this.textFieldNameBaseValue.setMinimumSize(new java.awt.Dimension(60, 21));
    	this.textFieldNameBaseValue.setPreferredSize(new java.awt.Dimension(60, 21));
    	this.panelBaseValue.add(this.textFieldNameBaseValue);

    	this.labelNameCurrentValue.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
    	this.labelNameCurrentValue.setText("Current Value: ");
    	this.labelNameCurrentValue.setMaximumSize(new java.awt.Dimension(90, 15));
    	this.labelNameCurrentValue.setMinimumSize(new java.awt.Dimension(90, 15));
    	this.labelNameCurrentValue.setPreferredSize(new java.awt.Dimension(90, 15));
    	this.panelBaseValue.add(this.labelNameCurrentValue);

    	this.textFieldNameCurrentValue.setText("0");
    	this.textFieldNameCurrentValue.setToolTipText("Current value of this Attribute");
    	this.textFieldNameCurrentValue.setMaximumSize(new java.awt.Dimension(2147483647, 21));
    	this.textFieldNameCurrentValue.setMinimumSize(new java.awt.Dimension(60, 21));
    	this.textFieldNameCurrentValue.setPreferredSize(new java.awt.Dimension(60, 21));
    	this.panelBaseValue.add(this.textFieldNameCurrentValue);

    	this.getContentPane().add(this.panelBaseValue);

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

    	this.getContentPane().add(this.panelCurrentValue);

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

    	this.buttonOk.setText("OK");
    	this.buttonOk.setToolTipText("Save this Attribute");
    	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("Cancel 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

    /** get the Attribute represented by this dialog 
     * @return the Attribute represented by this dialog 
     */
    public Attribute getAttribute(){
        String name=this.textFieldName.getText();
        String description=this.textFieldDescription.getText();
        long baseValue=0;
        long currentValue=0;
        try{
            baseValue=Long.parseLong(this.textFieldNameBaseValue.getText());
            currentValue=Long.parseLong(this.textFieldNameCurrentValue.getText());
        } catch(NumberFormatException nfx){
            nfx.printStackTrace();
        } 
        return(new Attribute(name,description,baseValue,currentValue));
    }
    
    /** event that fires when dialog is closed
     * override this in the implementing class 
     */
    public void closeDialog(boolean cancel){
        this.dispose();
        System.exit(0);
    }
    
    /** create a new AttributeDialog
     * @param args the command line arguments
     */
    public static void main(String args[]){
        (new AttributeDialog(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.JLabel labelBaseValue;
    private javax.swing.JLabel labelDescription;
    private javax.swing.JLabel labelName;
    private javax.swing.JLabel labelNameCurrentValue;
    private javax.swing.JPanel panelBaseValue;
    private javax.swing.JPanel panelButtons;
    private javax.swing.JPanel panelCurrentValue;
    private javax.swing.JPanel panelDescription;
    private javax.swing.JPanel panelName;
    private javax.swing.JTextField textFieldDescription;
    private javax.swing.JTextField textFieldName;
    private javax.swing.JTextField textFieldNameBaseValue;
    private javax.swing.JTextField textFieldNameCurrentValue;
    // End of variables declaration//GEN-END:variables
}