/*
 * affectHitPoints.java
 *
 * Created on December 21, 2002, 2:09 AM
 */

package org.evilsoft.gaming.Init;

/**
 *
 * @author  soulcatcher
 */
public class AffectHitPoints extends javax.swing.JDialog {
    
    /** Creates new form affectHitPoints */
    public AffectHitPoints(java.awt.Frame parent, boolean modal, boolean damage, Initiative initiative) {
        super(parent, modal);
        this.initiative = initiative;
        initComponents();
        this.damage = damage;
        initRest();
    }
    
    public AffectHitPoints(boolean damage, Initiative initiative) {
        this.initiative = initiative;
        initComponents();
        this.damage = damage;
        initRest();
    }

    /** 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
        java.awt.GridBagConstraints gridBagConstraints;

        lType = new javax.swing.JLabel();
        tAmount = new javax.swing.JTextField();
        sAmount = new javax.swing.JSlider();
        bOk = new javax.swing.JButton();
        bCancel = new javax.swing.JButton();

        getContentPane().setLayout(new java.awt.GridBagLayout());

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        lType.setText("Damage");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(30, 10, 0, 0);
        getContentPane().add(lType, gridBagConstraints);

        tAmount.setText("0");
        tAmount.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                tAmountPropertyChange(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 66;
        gridBagConstraints.insets = new java.awt.Insets(30, 8, 0, 0);
        getContentPane().add(tAmount, gridBagConstraints);

        sAmount.setValue(0);
        sAmount.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                sAmountKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                sAmountKeyReleased(evt);
            }
        });

        sAmount.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                sAmountMouseMoved(evt);
            }
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                sAmountMouseDragged(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(10, 8, 0, 0);
        getContentPane().add(sAmount, gridBagConstraints);

        bOk.setText("Ok");
        bOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bOkActionPerformed(evt);
            }
        });

        bOk.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                bOkKeyPressed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(14, 10, 0, 0);
        getContentPane().add(bOk, gridBagConstraints);

        bCancel.setText("Cancel");
        bCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bCancelActionPerformed(evt);
            }
        });

        bCancel.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                bCancelKeyPressed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(14, 8, 0, 0);
        getContentPane().add(bCancel, gridBagConstraints);

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

    private void bCancelKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_bCancelKeyPressed
        setVisible(false);
        dispose();
    }//GEN-LAST:event_bCancelKeyPressed

    private void bOkKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_bOkKeyPressed
        if(damage) {
            initiative.doDamage(getAmount());
        }
        else {
            initiative.doHeal(getAmount());
        }
        setVisible(false);
        dispose();
    }//GEN-LAST:event_bOkKeyPressed

    private void bCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bCancelActionPerformed
        setVisible(false);
        dispose();
    }//GEN-LAST:event_bCancelActionPerformed

    private void bOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bOkActionPerformed
        if(damage) {
            initiative.doDamage(getAmount());
        }
        else {
            initiative.doHeal(getAmount());
        }
        setVisible(false);
        dispose();
    }//GEN-LAST:event_bOkActionPerformed

    private void tAmountPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_tAmountPropertyChange
        sAmount.setValue(getAmount());
    }//GEN-LAST:event_tAmountPropertyChange

    private void sAmountKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_sAmountKeyReleased
        setAmount();
    }//GEN-LAST:event_sAmountKeyReleased

    private void sAmountKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_sAmountKeyPressed
        setAmount();
    }//GEN-LAST:event_sAmountKeyPressed

    private void sAmountMouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_sAmountMouseMoved
        setAmount();
    }//GEN-LAST:event_sAmountMouseMoved

    private void sAmountMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_sAmountMouseDragged
        setAmount();
    }//GEN-LAST:event_sAmountMouseDragged
    
    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        setVisible(false);
        dispose();
    }//GEN-LAST:event_closeDialog
    
    private void initRest() {
        if(damage) {
            lType.setText("Damage");
        }
        else {
            lType.setText("Healing");
        }
        this.toFront();
        tAmount.grabFocus();
    }
    
    public void setAmount() {
        tAmount.setText("" + sAmount.getValue());
    }

    public int getAmount() {
        Integer intValue = new Integer(tAmount.getText());
        return intValue.intValue();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bCancel;
    private javax.swing.JTextField tAmount;
    private javax.swing.JSlider sAmount;
    private javax.swing.JButton bOk;
    private javax.swing.JLabel lType;
    // End of variables declaration//GEN-END:variables
    boolean damage;
    public Initiative initiative;
}
