package turgen.config;


import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import turgen.DialogManager;
import turgen.UIPersistor;


public class ConfigDialog extends javax.swing.JDialog implements UIPersistor {
    private boolean werePersistenceDefaultsUsed;
    
    
    
    /** Creates new form ConfigDialog
     * @param f Parent frame */
    public ConfigDialog(JFrame f) {
        super(f);
        initComponents();
        this.getRootPane().setDefaultButton(btnOk);
        werePersistenceDefaultsUsed=false;
    }
    
    public ConfigDialog() {
        initComponents();
    }
    /** 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.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pControls = new javax.swing.JPanel();
        btnOk = new javax.swing.JButton();
        jbtSave = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();

        setTitle("Preferences");
        setModal(true);

        btnOk.setMnemonic('O');
        btnOk.setText("OK");
        btnOk.setToolTipText("Commit changes");
        btnOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOkActionPerformed(evt);
            }
        });
        pControls.add(btnOk);

        jbtSave.setText("OK & Save");
        jbtSave.setToolTipText("Commit changes and save them");
        jbtSave.setDefaultCapable(false);
        jbtSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtSaveActionPerformed(evt);
            }
        });
        pControls.add(jbtSave);

        btnCancel.setMnemonic('C');
        btnCancel.setText("Cancel");
        btnCancel.setToolTipText("Cancel changes");
        btnCancel.setDefaultCapable(false);
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });
        pControls.add(btnCancel);

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

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

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

    private void btnOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOkActionPerformed
        
        String s = Configuration.applyChanges();
        if (s!=null) {
            JOptionPane.showMessageDialog(this, s,"Errors in configuration",JOptionPane.ERROR_MESSAGE);
            return;
        }
        setVisible(false);
    }//GEN-LAST:event_btnOkActionPerformed

    private void jbtSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtSaveActionPerformed
        String s = Configuration.applyChanges();
        if (s!=null) {
            JOptionPane.showMessageDialog(this, s,"Errors in configuration",JOptionPane.ERROR_MESSAGE);
            return;
        }
        s = Configuration.saveOptions();
        JOptionPane.showMessageDialog(null, s);
        setVisible(false);
    }//GEN-LAST:event_jbtSaveActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ConfigDialog().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnOk;
    private javax.swing.JButton jbtSave;
    private javax.swing.JPanel pControls;
    // End of variables declaration//GEN-END:variables
    
    
    public void setConfigPanel(JPanel panel) {
        this.getContentPane().add(panel);
    }

        public Object[] getPersistenceData() {

        Object[] pData = new Object[2];
        pData[0]=getBounds();
        pData[1]=Boolean.valueOf(werePersistenceDefaultsUsed);

        return pData;
    }

    public void setPersistenceData(Object[] data) throws Exception {

        setBounds((Rectangle)data[0]);
        werePersistenceDefaultsUsed=((Boolean)data[1]).booleanValue();
    }

    public String getPersistenceId() {
        return getClass().getName();
    }

    public void setPersistenceDefaults() {
        pack();
        DialogManager.centerContainer(this);
        werePersistenceDefaultsUsed=true;
    }

    public boolean werePersistenceDefaultsUsed() {
        boolean b=werePersistenceDefaultsUsed;
        werePersistenceDefaultsUsed=false;
        return b;
    }

    
}
