/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * OptionsDialog.java
 *
 * Created on Apr 30, 2009, 11:42:49 AM
 */

package org.statcato.dialogs.file;

import org.statcato.*;

/**
 *
 * @author  Margaret Yau
 * @version %I%, %G%
 * @since 1.0
 */
public class OptionsDialog extends StatcatoDialog {

    /** Creates new form OptionsDialog */
    public OptionsDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        this.app = (Statcato)parent;
        ParentSpreadsheet = app.getSpreadsheet();
        initComponents();

        getRootPane().setDefaultButton(OKButton);
        setHelpFile("file-options");
        name = "Options";
        description = "For changing various settings of Statcato.";
        helpStrings.add("Statcato automatically saves a back up copy of the " +
                "current project file at one minute after the start of the " +
                "program execution.");
        helpStrings.add("The back up file is saved in the same " +
                "directory as the program files and is named in the form " +
                "~statcato.tmp.mmddyyyyhhmmss.stc, where mmddyyyyhhmmss is " +
                "a timestamp of when the file is created.");
        helpStrings.add("Enter the interval in which a back up file of the " +
                "current project file is saved. The interval must be a positive " +
                "integer and is measured in minutes.");
        helpStrings.add("Select the Delete backup file upon exit checkbox " +
                "if you want the backup file to be deleted when you exit " +
                "the application.");
    }

    /** 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.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        IntTextField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        DelCheckBox = new javax.swing.JCheckBox();
        OKButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Options");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Auto Save"));

        jLabel1.setText("Auto save a backup copy of project every");

        IntTextField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        IntTextField.setText("5");

        jLabel2.setText("minutes.");

        DelCheckBox.setSelected(true);
        DelCheckBox.setText("Delete backup file upon exit.");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(DelCheckBox)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(IntTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel2)))
                .addContainerGap(19, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(IntTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(DelCheckBox)
                .addContainerGap(16, Short.MAX_VALUE))
        );

        OKButton.setText("OK");
        OKButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                OKButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(172, 172, 172)
                        .addComponent(OKButton)))
                .addContainerGap(26, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 74, Short.MAX_VALUE)
                .addComponent(OKButton)
                .addGap(26, 26, 26))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void OKButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OKButtonActionPerformed
        try {
            int saveInterval = Integer.parseInt(IntTextField.getText());
            if (saveInterval <= 0)
                throw new NumberFormatException("positive integer required");
            app.project.setAutoSaveInterval(saveInterval*60);
        }
        catch (NumberFormatException e) {
            app.showErrorDialog("Enter a positive integer for the auto save interval.");
            return;
        }
        app.project.setDeleteAutoSaveFile(DelCheckBox.isSelected());
        setVisible(false);
    }//GEN-LAST:event_OKButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox DelCheckBox;
    private javax.swing.JTextField IntTextField;
    private javax.swing.JButton OKButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables

}
