/*
 * GeneralPreferencesPane.java
 *
 * Created on September 23, 2004, 8:32 PM
 */

package kiyut.ekspos.prefs;

import java.awt.*;
import java.io.*;

import javax.swing.*;

/**
 *
 * @author  Kiyut
 */
public class GeneralPreferencesPane extends PreferencesPane {
    
    /** Creates new form GeneralPreferencesPane */
    public GeneralPreferencesPane() {
        initComponents();
        initCustom();
    }
    
    /** 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() {
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel4 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        progressRepaintSpinner = new javax.swing.JSpinner();
        jPanel1 = new javax.swing.JPanel();
        cacheDirectoryText = new javax.swing.JTextField();
        useCacheCheck = new javax.swing.JCheckBox();
        jPanel3 = new javax.swing.JPanel();
        cacheDirectoryButton = new javax.swing.JButton();
        clearCacheButton = new javax.swing.JButton();
        totdCheck = new javax.swing.JCheckBox();
        jPanel2 = new javax.swing.JPanel();

        setLayout(new java.awt.GridBagLayout());

        jPanel4.setLayout(new java.awt.GridBagLayout());

        jLabel1.setText("Image Loading Progress Repaint (Only Hint)");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        jPanel4.add(jLabel1, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.ipadx = 30;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 0);
        jPanel4.add(progressRepaintSpinner, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        add(jPanel4, gridBagConstraints);

        jPanel1.setLayout(new java.awt.GridBagLayout());

        cacheDirectoryText.setEditable(false);
        cacheDirectoryText.setHorizontalAlignment(javax.swing.JTextField.LEFT);
        cacheDirectoryText.setText("cache directory");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 20, 0, 0);
        jPanel1.add(cacheDirectoryText, gridBagConstraints);

        useCacheCheck.setText("Use Cache");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        jPanel1.add(useCacheCheck, gridBagConstraints);

        jPanel3.setLayout(new java.awt.GridLayout(1, 0, 5, 0));

        cacheDirectoryButton.setText("Browse");
        cacheDirectoryButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cacheDirectoryButtonActionPerformed(evt);
            }
        });

        jPanel3.add(cacheDirectoryButton);

        clearCacheButton.setText("Clear cache");
        clearCacheButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearCacheButtonActionPerformed(evt);
            }
        });

        jPanel3.add(clearCacheButton);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 20, 0, 0);
        jPanel1.add(jPanel3, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(10, 0, 0, 0);
        add(jPanel1, gridBagConstraints);

        totdCheck.setText("Show Thought of the day");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.ipadx = 117;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(10, 0, 0, 0);
        add(totdCheck, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        add(jPanel2, gridBagConstraints);

    }
    // </editor-fold>//GEN-END:initComponents

    private void cacheDirectoryButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cacheDirectoryButtonActionPerformed
        JFileChooser fc = new JFileChooser();
        fc.setCurrentDirectory(new File(cacheDirectoryText.getText()));
        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        fc.setFileHidingEnabled(false);
        fc.setMultiSelectionEnabled(false);
        int returnVal = fc.showOpenDialog(this);
        if(returnVal == JFileChooser.APPROVE_OPTION) {
            cacheDirectoryText.setText(fc.getSelectedFile().toString());
        }
    }//GEN-LAST:event_cacheDirectoryButtonActionPerformed

    private void clearCacheButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearCacheButtonActionPerformed
        Runnable doRun = new Runnable() {
            public void run() { clearCache(); }
        };
        EventQueue.invokeLater(doRun);
    }//GEN-LAST:event_clearCacheButtonActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cacheDirectoryButton;
    private javax.swing.JTextField cacheDirectoryText;
    private javax.swing.JButton clearCacheButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JSpinner progressRepaintSpinner;
    private javax.swing.JCheckBox totdCheck;
    private javax.swing.JCheckBox useCacheCheck;
    // End of variables declaration//GEN-END:variables

    private void initCustom() {
        SpinnerNumberModel model = new SpinnerNumberModel(10, 0, 100, 5);
        progressRepaintSpinner.setModel(model);
    }
    
    /** {@inheritDoc} */
    public void loadPrefs() {
        GeneralPreferences prefs = GeneralPreferences.getInstance();
        
        progressRepaintSpinner.getModel().setValue(new Double(prefs.getDouble(GeneralPreferences.PROGRESS_REPAINT_KEY)));
        
        useCacheCheck.setSelected(prefs.getBoolean(GeneralPreferences.USE_CACHE_KEY));
        File file = new File(prefs.getString(GeneralPreferences.CACHE_DIRECTORY_KEY));
        cacheDirectoryText.setText(file.toString());
        
        totdCheck.setSelected(prefs.getBoolean(GeneralPreferences.TOTD_KEY));
        
        
    }
    
    /** {@inheritDoc} */
    public void savePrefs() {
        GeneralPreferences prefs = GeneralPreferences.getInstance();
        
        double doubleValue = 10;
        try {
            if (progressRepaintSpinner.getValue() != null) {
                doubleValue = Double.parseDouble(progressRepaintSpinner.getValue().toString());
                if (doubleValue > 100) {
                    doubleValue = 100;
                } else if (doubleValue < 0) {
                    doubleValue = 0;
                }
            }
        } catch (Exception ex) { }
        
        
        prefs.putDouble(GeneralPreferences.PROGRESS_REPAINT_KEY, doubleValue);
        
        prefs.putBoolean(GeneralPreferences.USE_CACHE_KEY, useCacheCheck.isSelected());
        prefs.putString(GeneralPreferences.CACHE_DIRECTORY_KEY, cacheDirectoryText.getText());
        prefs.putBoolean(GeneralPreferences.TOTD_KEY, totdCheck.isSelected());
        
        prefs.save();
    }
    
    private void clearCache() {
        FileFilter filter = new FileFilter() {
            public boolean accept(File file) {
                boolean b = false;
                if (file.toString().toLowerCase().endsWith(".png")) {
                    b = true;
                }
                return b;
            }
        };
        
        File dir = new File(cacheDirectoryText.getText());
        File[] files = dir.listFiles(filter);
        
        ProgressMonitor mon = new ProgressMonitor(this, "Deleting cache...", "", 0,files.length);
        mon.setProgress(0);
        mon.setMillisToDecideToPopup(2*1000);   // 2 seconds
        for (int i=0; i<files.length; i++) {
            try {
                files[i].delete();
                Thread.currentThread().yield();
            } catch (Exception e) {}

            if (mon.isCanceled()) { break; }
            mon.setProgress(i);
            mon.setNote("");
        }
        mon.close();
    }
}
