/*
 *  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
 *
 * PreferencesDialog.java
 *
 * Created on August 29, 2002, 2:17 PM
 */

package org.evilsoft.gaming.Init;

import javax.swing.tree.*;
import java.util.prefs.*;
/**
 *
 * @author  devon
 */
public class PreferencesDialog extends javax.swing.JDialog {
    
    /** Creates new form PreferencesDialog */
    public PreferencesDialog(java.awt.Frame parent, boolean modal, Initiative initiative) {
        super(parent, modal);
        initComponents();
        initTree();
        initLast();
        initPrefs();
        this.initiative = initiative;
    }

    public PreferencesDialog(Initiative initiative) {
        initComponents();
        initTree();
        initLast();
        initPrefs();
        this.initiative = initiative;
    }
    
    /** 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
        jPanel2 = new javax.swing.JPanel();
        bOk = new javax.swing.JButton();
        bCancel = new javax.swing.JButton();
        bApply = new javax.swing.JButton();
        jSplitPane1 = new javax.swing.JSplitPane();
        prefsTree = new javax.swing.JTree();
        prefsPane = new javax.swing.JTabbedPane();

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

        jPanel2.setLayout(new javax.swing.BoxLayout(jPanel2, javax.swing.BoxLayout.X_AXIS));

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

        jPanel2.add(bOk);

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

        jPanel2.add(bCancel);

        bApply.setText("Apply");
        bApply.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bApplyActionPerformed(evt);
            }
        });

        jPanel2.add(bApply);

        getContentPane().add(jPanel2, java.awt.BorderLayout.SOUTH);

        jSplitPane1.setLeftComponent(prefsTree);

        jSplitPane1.setRightComponent(prefsPane);

        getContentPane().add(jSplitPane1, java.awt.BorderLayout.CENTER);

    }//GEN-END:initComponents

    private void bApplyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bApplyActionPerformed
        // Add your handling code here:
        savePrefs();
        initiative.applyPrefs();
        initiative.refreshTable();
        tpMisc.setResetCheckBoxChecked(false);
    }//GEN-LAST:event_bApplyActionPerformed

    private void bOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bOkActionPerformed
        // Add your handling code here:
        savePrefs();
        initiative.applyPrefs();
        initiative.refreshTable();
        setVisible(false);
        dispose();
    }//GEN-LAST:event_bOkActionPerformed

    private void bCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bCancelActionPerformed
        // Add your handling code here:
        setVisible(false);
        dispose();
    }//GEN-LAST:event_bCancelActionPerformed
    
    private void PrefsTreeActionPerformed(javax.swing.event.TreeSelectionEvent evt) {
        // Add your handling code here:
        String selected = prefsTree.getLastSelectedPathComponent().toString();
        int i = 0;
        while(i < prefsPane.getTabCount()) {
            prefsPane.removeTabAt(i);
            i++;
        }
        if(selected.equals("Misc")) {
            prefsPane.addTab("Misc", tpMisc);
        }
        else if(selected.equals("Game")) {
            prefsPane.addTab("Game", tpGame);
        }
    }

    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        initPrefs.putInt("PrefsDividerLocation", jSplitPane1.getDividerLocation());
        initPrefs.putInt("PrefsWindowX", this.getX());
        initPrefs.putInt("PrefsWindowY", this.getY());
        initPrefs.putInt("PrefsWindowWidth", this.getSize().width);
        initPrefs.putInt("PrefsWindowHeight", this.getSize().height);
        try {
            initPrefs.flush();
        }
        catch(BackingStoreException e) {
        }
        setVisible(false);
        dispose();
    }//GEN-LAST:event_closeDialog
    
    /**
     * @param args the command line arguments
     */
    private void savePrefs() {
        initPrefs.putInt("PrefsDividerLocation", jSplitPane1.getDividerLocation());
        initPrefs.putInt("PrefsWindowX", this.getX());
        initPrefs.putInt("PrefsWindowY", this.getY());
        initPrefs.putInt("PrefsWindowWidth", this.getSize().width);
        initPrefs.putInt("PrefsWindowHeight", this.getSize().height);
        initPrefs.putBoolean("doSpells", tpGame.isSpellCheckBoxChecked());
        initPrefs.putBoolean("doDeath", tpGame.isDeathCheckBoxChecked());
        initPrefs.putBoolean("doHP", tpGame.isHPCheckBoxChecked());
        initPrefs.putInt("dbMaxHP", tpMisc.getMaxHP());
        initPrefs.putInt("dbMaxNum", tpMisc.getNumber());
        if(tpMisc.isResetCheckBoxChecked()) {
            try {
                initPrefs.clear();
                initiative.initPrefs();
                initiative.initLast();
            }
            catch(BackingStoreException e) {
            }
        }
    }
    
    private void initTree() {
        prefsTree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                PrefsTreeActionPerformed(evt);
            }
        });
        bCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bCancelActionPerformed(evt);
            }
        });

        DefaultMutableTreeNode parentNode = new DefaultMutableTreeNode("Preferences");
        //DefaultMutableTreeNode AppearanceNode = new DefaultMutableTreeNode("Appearance");
        DefaultMutableTreeNode gameSettingsNode = new DefaultMutableTreeNode("Game");
        //ParentNode.add(AppearanceNode);
        parentNode.add(gameSettingsNode);
        DefaultMutableTreeNode miscSettingsNode = new DefaultMutableTreeNode("Misc");
        parentNode.add(miscSettingsNode);
        prefsTree.setModel(new DefaultTreeModel(parentNode));
    }

    private void initLast() {
        int iDividerLocation = initPrefs.getInt("PrefsDividerLocation", 118);
        jSplitPane1.setDividerLocation(iDividerLocation);
       
        int iWinX = initPrefs.getInt("PrefsWindowX", 0);
        int iWinY = initPrefs.getInt("PrefsWindowY", 0);
        this.setLocation(iWinX, iWinY);
       
        int iWinWidth = initPrefs.getInt("PrefsWindowWidth", 550);
        int iWinHeight = initPrefs.getInt("PrefsWindowHeight", 385);
        this.setSize(iWinWidth, iWinHeight);
    }
    
    private void initPrefs() {
        boolean bSpells = initPrefs.getBoolean("doSpells", true);
        boolean bDeath = initPrefs.getBoolean("doDeath", true);
        boolean bHP = initPrefs.getBoolean("doHP", true);
        tpGame.setSpellCheckBoxChecked(bSpells);
        tpGame.setDeathCheckBoxChecked(bDeath);
        tpGame.setHPCheckBoxChecked(bHP);
        int maxHp = initPrefs.getInt("dbMaxHP", 100);
        int maxNum = initPrefs.getInt("dbMaxNum", 20);
        tpMisc.setMaxHP(maxHp);
        tpMisc.setNumber(maxNum);
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bCancel;
    private javax.swing.JTabbedPane prefsPane;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTree prefsTree;
    private javax.swing.JButton bApply;
    private javax.swing.JButton bOk;
    private javax.swing.JSplitPane jSplitPane1;
    // End of variables declaration//GEN-END:variables
    private Preferences initPrefs = Preferences.userNodeForPackage(Initiative.class);
    private PreferencesTrackingPanel tpGame = new PreferencesTrackingPanel();
    private PreferencesMiscPanel tpMisc = new PreferencesMiscPanel();
    public Initiative initiative;
}
