/*
 *  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
 *
 * Init.java
 *
 * Created on December 20, 2001, 6:33 PM
 */

package org.evilsoft.gaming.Init;

import java.util.prefs.*;
import java.util.*;
import java.io.*;
/**
 *
 * @author  devon
 */
public class main extends javax.swing.JFrame {

    /** Creates new form Init */
    public main() {
        initComponents();
        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
        jMenuBar1 = new javax.swing.JMenuBar();
        mFile = new javax.swing.JMenu();
        mLoad = new javax.swing.JMenuItem();
        mSave = new javax.swing.JMenuItem();
        mEdit = new javax.swing.JMenu();
        mCopy = new javax.swing.JMenuItem();
        mPasteNew = new javax.swing.JMenuItem();
        mPreferences = new javax.swing.JMenuItem();
        mHelp = new javax.swing.JMenu();
        mAbout = new javax.swing.JMenuItem();

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

        setTitle("Initiative Tracker");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        mFile.setText("File");
        mLoad.setText("Load");
        mLoad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mLoadActionPerformed(evt);
            }
        });

        mFile.add(mLoad);
        mSave.setText("Save As");
        mSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mSaveActionPerformed(evt);
            }
        });

        mFile.add(mSave);
        jMenuBar1.add(mFile);
        mEdit.setText("Edit");
        mEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mEditActionPerformed(evt);
            }
        });

        mEdit.addMenuListener(new javax.swing.event.MenuListener() {
            public void menuSelected(javax.swing.event.MenuEvent evt) {
                mEditMenuSelected(evt);
            }
            public void menuDeselected(javax.swing.event.MenuEvent evt) {
            }
            public void menuCanceled(javax.swing.event.MenuEvent evt) {
            }
        });

        mCopy.setText("Copy Combatant");
        mCopy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mCopyActionPerformed(evt);
            }
        });

        mEdit.add(mCopy);
        mPasteNew.setText("Paste as New");
        mPasteNew.setEnabled(false);
        mPasteNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mPasteNewActionPerformed(evt);
            }
        });

        mEdit.add(mPasteNew);
        mPreferences.setText("Preferences");
        mPreferences.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mPreferencesActionPerformed(evt);
            }
        });

        mEdit.add(mPreferences);
        jMenuBar1.add(mEdit);
        mHelp.setText("Help");
        mAbout.setText("About");
        mAbout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mAboutActionPerformed(evt);
            }
        });

        mHelp.add(mAbout);
        jMenuBar1.add(mHelp);
        setJMenuBar(jMenuBar1);

    }//GEN-END:initComponents

    private void mPasteNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mPasteNewActionPerformed
        Initiative.pasteNew();
    }//GEN-LAST:event_mPasteNewActionPerformed

    private void mCopyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mCopyActionPerformed
        Initiative.copy();
    }//GEN-LAST:event_mCopyActionPerformed

    private void mEditMenuSelected(javax.swing.event.MenuEvent evt) {//GEN-FIRST:event_mEditMenuSelected
        if(Initiative.getSelectedCount() == 1) {
            if(Initiative.isXMLCombatantSelected()) {
                mCopy.setEnabled(true);
            }
            else {
                mCopy.setEnabled(false);
            }
        }
        else {
            mCopy.setEnabled(false);
        }
        if(Initiative.isPastable()) {
            mPasteNew.setEnabled(true);
        }
        else {
            mPasteNew.setEnabled(false);
        }
    }//GEN-LAST:event_mEditMenuSelected

    private void mEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mEditActionPerformed
    }//GEN-LAST:event_mEditActionPerformed

    private void mSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mSaveActionPerformed
        Initiative.saveToFile();
    }//GEN-LAST:event_mSaveActionPerformed

    private void mAboutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mAboutActionPerformed
        double ver = initPrefs.getDouble("Version", 0);
        double subver = initPrefs.getDouble("SubVersion", 0);

        AboutBox dialog = new AboutBox(this, true, ver, subver);
        dialog.setVisible(true);

    }//GEN-LAST:event_mAboutActionPerformed

    private void mPreferencesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mPreferencesActionPerformed
        Initiative.showPreferences();
        Initiative.applyPrefs();
    }//GEN-LAST:event_mPreferencesActionPerformed

    private void bCastActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bCastActionPerformed

    }//GEN-LAST:event_bCastActionPerformed

    
    private void mLoadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mLoadActionPerformed
        Initiative.loadFromFile();
        Initiative.refreshTable();
    }//GEN-LAST:event_mLoadActionPerformed

    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
        Initiative.setExitPrefs();
        initPrefs.putInt("WindowX", this.getX());
        initPrefs.putInt("WindowY", this.getY());
        initPrefs.putInt("WindowWidth", this.getSize().width);
        initPrefs.putInt("WindowHeight", this.getSize().height);
        System.exit(0);
    }//GEN-LAST:event_exitForm

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        new main().show();
    }
    
    private void initRest() {
        Initiative = new Initiative(this);
        getContentPane().add(Initiative);

        int iWinX = initPrefs.getInt("WindowX", 0);
        int iWinY = initPrefs.getInt("WindowY", 0);
        this.setLocation(iWinX, iWinY);
       
        int iWinWidth = initPrefs.getInt("WindowWidth", 710);
        int iWinHeight = initPrefs.getInt("WindowHeight", 653);
        this.setSize(iWinWidth, iWinHeight);
    }

    public void applyPrefs() {
        Initiative.applyPrefs();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu mEdit;
    private javax.swing.JMenuItem mPreferences;
    private javax.swing.JMenu mHelp;
    private javax.swing.JMenuItem mAbout;
    private javax.swing.JMenu mFile;
    private javax.swing.JMenuItem mSave;
    private javax.swing.JMenuItem mCopy;
    private javax.swing.JMenuItem mLoad;
    private javax.swing.JMenuItem mPasteNew;
    private javax.swing.JMenuBar jMenuBar1;
    // End of variables declaration//GEN-END:variables

    public Initiative Initiative;
    private Preferences initPrefs = Preferences.userNodeForPackage(Initiative.class);
}
