/*
 *  Initiative - A role playing utility to track turns
 *  Copyright (C) 2002 Devon D Jones
 *
 *  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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  The author of this program grants you the ability to use this code
 *  in conjunction with code that is covered under the Open Gaming License
 *
 * AddCombatant.java
 *
 * Created on January 4, 2002, 2:10 PM
 */

package org.evilsoft.gaming.Init;

import java.util.prefs.*;

/**
 *
 * @author  devon
 */
public class AddCombatant extends javax.swing.JDialog {

    /** Creates new form AddCombatant */
    public AddCombatant(java.awt.Frame parent, boolean modal, Initiative initiative) {
        super(parent, modal);
        initComponents();
        this.initiative = initiative;
        boolean bHP = initPrefs.getBoolean("doHP", true);
        sHP.setEnabled(bHP);
        int maxHp = initPrefs.getInt("dbMaxHP", 100);
        int maxNum = initPrefs.getInt("dbMaxNum", 20);
        sNumber.setMaximum(maxNum);
        sHP.setMaximum(maxHp);
    }

    public AddCombatant(Initiative initiative) {
        initComponents();
        this.initiative = initiative;
        boolean bHP = initPrefs.getBoolean("doHP", true);
        sHP.setEnabled(bHP);
        int maxHp = initPrefs.getInt("dbMaxHP", 100);
        int maxNum = initPrefs.getInt("dbMaxNum", 20);
        sNumber.setMaximum(maxNum);
        sHP.setMaximum(maxHp);
    }

    /** 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;

        jLabel1 = new javax.swing.JLabel();
        tName = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        tPlayer = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        sBonus = new javax.swing.JSlider();
        lBonus = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        sNumber = new javax.swing.JSlider();
        lNumber = new javax.swing.JLabel();
        bSave = new javax.swing.JButton();
        bCancel = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        sHP = new javax.swing.JSlider();
        lHP = new javax.swing.JLabel();

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

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

        jLabel1.setText("Name");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(10, 5, 10, 0);
        getContentPane().add(jLabel1, gridBagConstraints);

        tName.setMinimumSize(new java.awt.Dimension(100, 21));
        tName.setPreferredSize(new java.awt.Dimension(200, 21));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(10, 0, 0, 0);
        getContentPane().add(tName, gridBagConstraints);

        jLabel2.setText("Player");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 10, 0);
        getContentPane().add(jLabel2, gridBagConstraints);

        tPlayer.setPreferredSize(new java.awt.Dimension(200, 21));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        getContentPane().add(tPlayer, gridBagConstraints);

        jLabel3.setText("Bonus");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 10, 0);
        getContentPane().add(jLabel3, gridBagConstraints);

        sBonus.setMaximum(20);
        sBonus.setMinimum(-20);
        sBonus.setValue(0);
        sBonus.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                sBonusKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                sBonusKeyReleased(evt);
            }
        });

        sBonus.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                sBonusMousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                sBonusMouseReleased(evt);
            }
        });

        sBonus.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                sBonusMouseMoved(evt);
            }
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                sBonusMouseDragged(evt);
            }
        });

        sBonus.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                sBonusPropertyChange(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        getContentPane().add(sBonus, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 10);
        getContentPane().add(lBonus, gridBagConstraints);

        jLabel4.setText("Number");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 10, 0);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        getContentPane().add(jLabel4, gridBagConstraints);

        sNumber.setMaximum(20);
        sNumber.setMinimum(1);
        sNumber.setValue(1);
        sNumber.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                sNumberKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                sNumberKeyReleased(evt);
            }
        });

        sNumber.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                sNumberMousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                sNumberMouseReleased(evt);
            }
        });

        sNumber.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                sNumberMouseMoved(evt);
            }
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                sNumberMouseDragged(evt);
            }
        });

        sNumber.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                sNumberPropertyChange(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        getContentPane().add(sNumber, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 10);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST;
        getContentPane().add(lNumber, gridBagConstraints);

        bSave.setText("Save");
        bSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bSaveActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 10);
        getContentPane().add(bSave, gridBagConstraints);

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

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        getContentPane().add(bCancel, gridBagConstraints);

        jLabel5.setForeground(new java.awt.Color(204, 204, 204));
        jLabel5.setText("-20");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST;
        gridBagConstraints.insets = new java.awt.Insets(10, 0, 0, 10);
        getContentPane().add(jLabel5, gridBagConstraints);

        jLabel6.setText("Hit Points");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 10, 0);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        getContentPane().add(jLabel6, gridBagConstraints);

        sHP.setMinimum(1);
        sHP.setValue(1);
        sHP.setEnabled(false);
        sHP.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                sHPKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                sHPKeyReleased(evt);
            }
        });

        sHP.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                sHPMousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                sHPMouseReleased(evt);
            }
        });

        sHP.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                sHPMouseMoved(evt);
            }
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                sHPMouseDragged(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        getContentPane().add(sHP, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 10);
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST;
        getContentPane().add(lHP, gridBagConstraints);

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

    private void sHPMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_sHPMouseDragged
        setHP();
    }//GEN-LAST:event_sHPMouseDragged

    private void sHPMouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_sHPMouseMoved
        setHP();
    }//GEN-LAST:event_sHPMouseMoved

    private void sHPMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_sHPMouseReleased
        setHP();
    }//GEN-LAST:event_sHPMouseReleased

    private void sHPMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_sHPMousePressed
        setHP();
    }//GEN-LAST:event_sHPMousePressed

    private void sHPKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_sHPKeyReleased
        setHP();
    }//GEN-LAST:event_sHPKeyReleased

    private void sHPKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_sHPKeyPressed
        setHP();
    }//GEN-LAST:event_sHPKeyPressed

    private void bSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bSaveActionPerformed
        if(sNumber.getValue() > 1) {
            for(int i = 1; i <= sNumber.getValue(); i++) {
                initiative.initVector.add(new XMLCombatant(tName.getText() + " (" + i + ")", tPlayer.getText(), 10, 10, sHP.getValue(), sHP.getValue(), sBonus.getValue()));
                //initiative.initVector.add(new Combatant(tName.getText() + i, tPlayer.getText(), sBonus.getValue(), sHP.getValue()));
            }
        }
        else {
            initiative.initVector.add(new XMLCombatant(tName.getText(), tPlayer.getText(), 10, 10, sHP.getValue(), sHP.getValue(), sBonus.getValue()));
            //initiative.initVector.add(new Combatant(tName.getText(), tPlayer.getText(), sBonus.getValue(), sHP.getValue()));
        }
        initiative.refreshTable();
        initiative.focusRoll();
        setVisible(false);
        dispose();
    }//GEN-LAST:event_bSaveActionPerformed

    private void sNumberKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_sNumberKeyReleased
        // Add your handling code here:
        setNumber();
    }//GEN-LAST:event_sNumberKeyReleased

    private void sNumberKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_sNumberKeyPressed
        // Add your handling code here:
        setNumber();
    }//GEN-LAST:event_sNumberKeyPressed

    private void sNumberMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_sNumberMouseReleased
        // Add your handling code here:
        setNumber();
    }//GEN-LAST:event_sNumberMouseReleased

    private void sNumberMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_sNumberMousePressed
        // Add your handling code here:
        setNumber();
    }//GEN-LAST:event_sNumberMousePressed

    private void sNumberMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_sNumberMouseDragged
        // Add your handling code here:
        setNumber();
    }//GEN-LAST:event_sNumberMouseDragged

    private void sNumberMouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_sNumberMouseMoved
        // Add your handling code here:
        setNumber();
    }//GEN-LAST:event_sNumberMouseMoved

    private void sBonusMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_sBonusMouseDragged
        // Add your handling code here:
        setBonus();
    }//GEN-LAST:event_sBonusMouseDragged

    private void sBonusMouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_sBonusMouseMoved
        // Add your handling code here:
        setBonus();
    }//GEN-LAST:event_sBonusMouseMoved

    private void sBonusKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_sBonusKeyReleased
        // Add your handling code here:
        setBonus();
    }//GEN-LAST:event_sBonusKeyReleased

    private void sBonusKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_sBonusKeyPressed
        // Add your handling code here:
        setBonus();
    }//GEN-LAST:event_sBonusKeyPressed

    private void sBonusMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_sBonusMouseReleased
        // Add your handling code here:
        setBonus();
    }//GEN-LAST:event_sBonusMouseReleased

    private void sBonusMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_sBonusMousePressed
        // Add your handling code here:
        setBonus();
    }//GEN-LAST:event_sBonusMousePressed

    private void sBonusPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_sBonusPropertyChange
        // Add your handling code here:
        setBonus();
    }//GEN-LAST:event_sBonusPropertyChange

    private void sNumberPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_sNumberPropertyChange
        // Add your handling code here:
        setNumber();
    }//GEN-LAST:event_sNumberPropertyChange

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

    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        setVisible(false);
        dispose();
    }//GEN-LAST:event_closeDialog

    /**
    * @param args the command line arguments
    */

    public void setBonus() {
        lBonus.setText("" + sBonus.getValue());
    }

    public void setNumber() {
        lNumber.setText("" + sNumber.getValue());
    }

    public void setHP() {
        lHP.setText("" + sHP.getValue());
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bSave;
    private javax.swing.JButton bCancel;
    private javax.swing.JSlider sHP;
    private javax.swing.JTextField tPlayer;
    private javax.swing.JLabel lHP;
    private javax.swing.JSlider sBonus;
    private javax.swing.JTextField tName;
    private javax.swing.JSlider sNumber;
    private javax.swing.JLabel lNumber;
    private javax.swing.JLabel lBonus;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration//GEN-END:variables
    public Initiative initiative;
    private Preferences initPrefs = Preferences.userNodeForPackage(Initiative.class);
}
