/*
 * ConfigurationView.java
 *
 * Dialog used to set configuration of application.
 *
 * Created on 12.5.2010, 10:43:30
 */
package main;

import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import org.jdesktop.application.Action;

/**
 * Dialog providing graphical configuration tool of application.
 * 
 * @author Marcel Duris
 */
public class ConfigurationView extends javax.swing.JDialog {

    private ConfigurationReader cfg;

    /** Creates new form ConfigurationView */
    public ConfigurationView(java.awt.Frame parent) {
        super(parent);
        cfg = ConfigurationReader.getCfg();
        //initComponents is customized, so it loads current configuration
        initComponents();
        reloadConfig();
    }

    /**
     * Refreshes fields in dialog with most current values from configuration
     * reader.
     */
    public void reloadConfig() {
        if (cfg.usingDefaults()) {
            txtConfig.setText("<DEFAULT FILE>");
        } else {
            txtConfig.setText(cfg.getDefaultConfigFile().getAbsolutePath());
        }

        txtAddress.setText(cfg.getAddress().getHostAddress());
        txtPort.setText(Integer.toString(cfg.getPort()));
        txtFolder.setText(cfg.getFolder().getAbsolutePath());
        txtRepoSize.setText(Integer.toString(cfg.getMaxSize()));
        cbxType.setSelectedIndex(cfg.getType());
        txtFragments.setText(Integer.toString(cfg.getFragmentCount()));
        txtChecksums.setText(Integer.toString(cfg.getChecksumCount()));
    }

    @Action
    public void closeConfigDialog() {
        dispose();
    }

    /**
     * Applies values entered into dialog. If values are invalid, configuration
     * is not changed and user is notified.
     */
    @Action
    public void apply() {
        int resp = JOptionPane.showConfirmDialog(rootPane,
                "Use this configuration values?\n Application will be reloaded, all running jobs will be canseled.",
                "Confirmation",
                JOptionPane.YES_NO_OPTION);
        if (resp != JOptionPane.YES_OPTION) {
            return;
        }
        try {
            cfg.setAddress(txtAddress.getText().trim());
            cfg.setPort(txtPort.getText());
            cfg.setRepositoryFolderPath(txtFolder.getText());
            cfg.setMaxSize(txtRepoSize.getText());
            cfg.setType(cbxType.getSelectedIndex());
            cfg.setFragments(txtFragments.getText());
            cfg.setChecksums(txtChecksums.getText());
            cfg.writeConfig();
            AppManager.stop();
            AppManager.init();
            dispose();
        } catch (Exception ex) {
            String msg = "Unable to save and use config. " + ex.getMessage();
            JOptionPane.showMessageDialog(rootPane, msg);
        }
    }

    /**
     * Deletes configuration file and reload application configuration so
     * default values are used.
     */
    @Action
    public void resetToDefaults() {
        int resp = JOptionPane.showConfirmDialog(rootPane,
                "Delete configuration values and use defaults instead?\n Application will be reloaded, all running jobs will be canseled.",
                "Confirmation",
                JOptionPane.YES_NO_OPTION);
        if (resp != JOptionPane.YES_OPTION) {
            return;
        }
        try {
            cfg.getDefaultConfigFile().delete();
            AppManager.stop();
            AppManager.init();
        } catch (Exception ex) {
            String msg = "Unable to reset. " + ex.getMessage();
            JOptionPane.showMessageDialog(rootPane, msg);
        }
        dispose();
    }

    /**
     * Queries user for configuration file that is later parsed for
     * configuration. Values are displayed to user. If file is invalid, defaults
     * are used
     */
    @Action
    public void openConfig() {
        int returnVal = fcConfig.showOpenDialog(this);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File selected = fcConfig.getSelectedFile();
            txtConfig.setText(selected.getAbsolutePath());
            cfg.readConfig(selected);
            reloadConfig();
        }
    }

    /**
     * Enables user to select repository folder.
     */
    @Action
    public void openRepository() {
        int returnVal = fcFolder.showOpenDialog(this);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            txtFolder.setText(fcFolder.getSelectedFile().toString());
            //This is where a real application would open the file.
        }
    }

    /** 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() {

        fcConfig = new javax.swing.JFileChooser();
        fcFolder = new javax.swing.JFileChooser();
        btnCancel = new javax.swing.JButton();
        btnApply = new javax.swing.JButton();
        txtAddress = new javax.swing.JTextField();
        txtPort = new javax.swing.JTextField();
        txtFolder = new javax.swing.JTextField();
        txtRepoSize = new javax.swing.JTextField();
        txtConfig = new javax.swing.JTextField();
        lblConfig = new javax.swing.JLabel();
        lblPort = new javax.swing.JLabel();
        lblRepository = new javax.swing.JLabel();
        lblRepositorySize = new javax.swing.JLabel();
        lblAddress = new javax.swing.JLabel();
        btnConfigFile = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JSeparator();
        jSeparator3 = new javax.swing.JSeparator();
        jSeparator4 = new javax.swing.JSeparator();
        btnRepository = new javax.swing.JButton();
        btnDefaults = new javax.swing.JButton();
        lblType = new javax.swing.JLabel();
        cbxType = new javax.swing.JComboBox();
        jSeparator5 = new javax.swing.JSeparator();
        lblFragments = new javax.swing.JLabel();
        txtFragments = new javax.swing.JTextField();
        lblChecksums = new javax.swing.JLabel();
        txtChecksums = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();

        fcConfig.setName("fcConfig"); // NOI18N

        fcFolder.setFileSelectionMode(javax.swing.JFileChooser.DIRECTORIES_ONLY);
        fcFolder.setName("fcFolder"); // NOI18N

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(main.BackupApp.class).getContext().getResourceMap(ConfigurationView.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setMinimumSize(new java.awt.Dimension(505, 306));
        setModal(true);
        setName("Form"); // NOI18N
        setResizable(false);

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(main.BackupApp.class).getContext().getActionMap(ConfigurationView.class, this);
        btnCancel.setAction(actionMap.get("closeConfigDialog")); // NOI18N
        btnCancel.setText(resourceMap.getString("btnCancel.text")); // NOI18N
        btnCancel.setToolTipText(resourceMap.getString("btnCancel.toolTipText")); // NOI18N
        btnCancel.setName("btnCancel"); // NOI18N

        btnApply.setAction(actionMap.get("apply")); // NOI18N
        btnApply.setText(resourceMap.getString("btnApply.text")); // NOI18N
        btnApply.setToolTipText(resourceMap.getString("btnApply.toolTipText")); // NOI18N
        btnApply.setName("btnApply"); // NOI18N

        txtAddress.setText(resourceMap.getString("txtAddress.text")); // NOI18N
        txtAddress.setText(cfg.getAddress().getHostAddress());
        txtAddress.setName("txtAddress"); // NOI18N

        txtPort.setText(resourceMap.getString("txtPort.text")); // NOI18N
        txtPort.setText(Integer.toString(cfg.getPort()));
        txtPort.setName("txtPort"); // NOI18N

        txtFolder.setText(resourceMap.getString("txtFolder.text")); // NOI18N
        txtFolder.setText(cfg.getFolder().getAbsolutePath());
        txtFolder.setName("txtFolder"); // NOI18N

        txtRepoSize.setText(resourceMap.getString("txtRepoSize.text")); // NOI18N
        txtRepoSize.setText(Integer.toString(cfg.getMaxSize()));
        txtRepoSize.setName("txtRepoSize"); // NOI18N

        txtConfig.setText(resourceMap.getString("txtConfig.text")); // NOI18N
        txtConfig.setText(cfg.getDefaultConfigFile().getAbsolutePath());
        txtConfig.setName("txtConfig"); // NOI18N

        lblConfig.setText(resourceMap.getString("lblConfig.text")); // NOI18N
        lblConfig.setName("lblConfig"); // NOI18N

        lblPort.setText(resourceMap.getString("lblPort.text")); // NOI18N
        lblPort.setName("lblPort"); // NOI18N

        lblRepository.setText(resourceMap.getString("lblRepository.text")); // NOI18N
        lblRepository.setName("lblRepository"); // NOI18N

        lblRepositorySize.setText(resourceMap.getString("lblRepositorySize.text")); // NOI18N
        lblRepositorySize.setName("lblRepositorySize"); // NOI18N

        lblAddress.setText(resourceMap.getString("lblAddress.text")); // NOI18N
        lblAddress.setName("lblAddress"); // NOI18N

        btnConfigFile.setAction(actionMap.get("openConfig")); // NOI18N
        btnConfigFile.setText(resourceMap.getString("btnConfigFile.text")); // NOI18N
        btnConfigFile.setToolTipText(resourceMap.getString("btnConfigFile.toolTipText")); // NOI18N
        btnConfigFile.setName("btnConfigFile"); // NOI18N

        jSeparator2.setName("jSeparator2"); // NOI18N

        jSeparator3.setName("jSeparator3"); // NOI18N

        jSeparator4.setName("jSeparator4"); // NOI18N

        btnRepository.setAction(actionMap.get("openRepository")); // NOI18N
        btnRepository.setText(resourceMap.getString("btnRepository.text")); // NOI18N
        btnRepository.setToolTipText(resourceMap.getString("btnRepository.toolTipText")); // NOI18N
        btnRepository.setName("btnRepository"); // NOI18N

        btnDefaults.setAction(actionMap.get("resetToDefaults")); // NOI18N
        btnDefaults.setText(resourceMap.getString("btnDefaults.text")); // NOI18N
        btnDefaults.setToolTipText(resourceMap.getString("btnDefaults.toolTipText")); // NOI18N
        btnDefaults.setName("btnDefaults"); // NOI18N

        lblType.setText(resourceMap.getString("lblType.text")); // NOI18N
        lblType.setName("lblType"); // NOI18N

        cbxType.setModel(new javax.swing.DefaultComboBoxModel(cfg.getTypeDesc()));
        cbxType.setSelectedIndex(cfg.getType());
        cbxType.setEditor(null);
        cbxType.setName("cbxType"); // NOI18N

        jSeparator5.setName("jSeparator5"); // NOI18N

        lblFragments.setText(resourceMap.getString("lblFragments.text")); // NOI18N
        lblFragments.setName("lblFragments"); // NOI18N

        txtFragments.setName("txtFragments"); // NOI18N

        lblChecksums.setText(resourceMap.getString("lblChecksums.text")); // NOI18N
        lblChecksums.setName("lblChecksums"); // NOI18N

        txtChecksums.setName("txtChecksums"); // NOI18N

        jSeparator1.setName("jSeparator1"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblType, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblRepositorySize, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblRepository, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblPort, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblAddress, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblConfig, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtPort, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 369, Short.MAX_VALUE)
                            .addComponent(txtAddress, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 369, Short.MAX_VALUE)
                            .addComponent(txtFolder, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 369, Short.MAX_VALUE)
                            .addComponent(txtRepoSize, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 369, Short.MAX_VALUE)
                            .addComponent(txtConfig, javax.swing.GroupLayout.DEFAULT_SIZE, 369, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 9, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btnConfigFile, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(btnRepository, javax.swing.GroupLayout.Alignment.TRAILING)))
                    .addComponent(cbxType, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
            .addComponent(jSeparator2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 582, Short.MAX_VALUE)
            .addComponent(jSeparator3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 582, Short.MAX_VALUE)
            .addComponent(jSeparator4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 582, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(58, 58, 58)
                .addComponent(btnDefaults)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 307, Short.MAX_VALUE)
                .addComponent(btnApply)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnCancel)
                .addContainerGap())
            .addComponent(jSeparator5, javax.swing.GroupLayout.DEFAULT_SIZE, 582, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(50, 50, 50)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblChecksums, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblFragments, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtChecksums, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 369, Short.MAX_VALUE)
                    .addComponent(txtFragments, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 369, Short.MAX_VALUE))
                .addGap(77, 77, 77))
            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 582, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtConfig, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnConfigFile)
                    .addComponent(lblConfig))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblAddress))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblPort))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtFolder, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblRepository)
                    .addComponent(btnRepository))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblRepositorySize)
                    .addComponent(txtRepoSize, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblType)
                    .addComponent(cbxType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtFragments, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblFragments))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtChecksums, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblChecksums))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnApply)
                    .addComponent(btnCancel)
                    .addComponent(btnDefaults))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnApply;
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnConfigFile;
    private javax.swing.JButton btnDefaults;
    private javax.swing.JButton btnRepository;
    private javax.swing.JComboBox cbxType;
    private javax.swing.JFileChooser fcConfig;
    private javax.swing.JFileChooser fcFolder;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JSeparator jSeparator4;
    private javax.swing.JSeparator jSeparator5;
    private javax.swing.JLabel lblAddress;
    private javax.swing.JLabel lblChecksums;
    private javax.swing.JLabel lblConfig;
    private javax.swing.JLabel lblFragments;
    private javax.swing.JLabel lblPort;
    private javax.swing.JLabel lblRepository;
    private javax.swing.JLabel lblRepositorySize;
    private javax.swing.JLabel lblType;
    private javax.swing.JTextField txtAddress;
    private javax.swing.JTextField txtChecksums;
    private javax.swing.JTextField txtConfig;
    private javax.swing.JTextField txtFolder;
    private javax.swing.JTextField txtFragments;
    private javax.swing.JTextField txtPort;
    private javax.swing.JTextField txtRepoSize;
    // End of variables declaration//GEN-END:variables
}
