// $Id: Preferences.java,v 1.6 2004/09/18 12:21:10 m31 Exp $
//
// This file is part of the program suite "Principia Mathematica II"
// which is a working prototype for the main project:
// "Hilbert II" - http://www.qedeq.org
//
// Copyright 2001-2004  Michael Meyling <mime@qedeq.org>.
//
// "Principia Mathematica II" 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.

package com.meyling.principia.gui;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.filechooser.FileFilter;

import com.meyling.principia.config.Settings;
import com.meyling.principia.log.Trace;


/**
 * Show and edit preferences of this application.
 *
 * @version $Revision: 1.6 $
 * @author  Michael Meyling
 */
public final class Preferences extends JFrame {

    private JCheckBox autoReloadLastSessionCheckedCB;

    private JCheckBox directResponseCB;

    private JCheckBox autoStartHtmlBrowserCB;

    private JCheckBox oldHtmlCodeCB;

    private JLabel moduleBufferLabel;

    private JTextField moduleBufferTextField;

    private JLabel generationPathLabel;

    private JTextField generationPathTextField;

    private JLabel localModulesPathLabel;

    private JTextField localModulesPathTextField;

    private File bufferDirectory;

    private File generationDirectory;

    private File localModulesDirectory;
    
    private boolean autoReloadLastSessionChecked;
    
    private boolean autoStartHtmlBrowser;

    private boolean directResponse;
    
    private boolean oldHtmlCode;

    private boolean changed;


    /**
     * Constructor.
     */
    public Preferences(final String title) {
        super(title);
        final String METHOD = "Constructor"; 
        Trace.traceBegin(this, METHOD);
        try {
            this.changed = false;
            this.setupView();
            pack();
            this.setSize(660,450);
        } catch (Throwable e){
            Trace.trace(this, METHOD, e);
        } finally {
            Trace.traceBegin(this, METHOD);
        }
    }


    /**
     * Assembles the gui components of the panel.
     */
    public final void setupView() {
        final String METHOD = "setupView";
        final Container contents = getContentPane();

        contents.setLayout(null);

        this.autoReloadLastSessionChecked = Settings.getInstance().isAutoReloadLastSessionChecked();
        this.autoReloadLastSessionCheckedCB 
            = new JCheckBox(" Auto loading of in last session successfully checked modules",
            this.autoReloadLastSessionChecked);
        contents.add(this.autoReloadLastSessionCheckedCB);
        this.autoReloadLastSessionCheckedCB.setBounds(33, 20, 400, 17);
        this.autoReloadLastSessionCheckedCB.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                Preferences.this.autoReloadLastSessionChecked 
                    = Preferences.this.autoReloadLastSessionCheckedCB.isSelected();
                Preferences.this.changed = true;
            }
        });

        this.directResponse = Settings.getInstance().isDirectResponse();
        this.directResponseCB = new JCheckBox(" Direct message response for actions", 
            this.directResponse);
        contents.add(this.directResponseCB);
        this.directResponseCB.setBounds(33, 60, 400, 17);
        this.directResponseCB.addActionListener(new  ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                Preferences.this.directResponse = Preferences.this.directResponseCB.isSelected();
                Preferences.this.changed = true;
            }
        });

        this.autoStartHtmlBrowser = Settings.getInstance().isAutoStartHtmlBrowser();
        this.autoStartHtmlBrowserCB = new JCheckBox(" Auto start web browser after HTML generation", 
            this.autoStartHtmlBrowser);
        contents.add(this.autoStartHtmlBrowserCB);
        this.autoStartHtmlBrowserCB.setBounds(33, 100, 400, 17);
        this.autoStartHtmlBrowserCB.addActionListener(new  ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                Preferences.this.autoStartHtmlBrowser = Preferences.this.autoStartHtmlBrowserCB.isSelected();
                Preferences.this.changed = true;
            }
        });

        this.oldHtmlCode = Settings.getInstance().isOldHtml();
        this.oldHtmlCodeCB = new JCheckBox(" Use System font HTML at web browser start", 
            this.oldHtmlCode);
        contents.add(this.oldHtmlCodeCB);
        this.oldHtmlCodeCB.setBounds(33, 140, 400, 17);
        this.oldHtmlCodeCB.addActionListener(new  ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                Preferences.this.oldHtmlCode = Preferences.this.oldHtmlCodeCB.isSelected();
                Preferences.this.changed = true;
            }
        });

        this.moduleBufferLabel = new JLabel("Local file path for qedeq module buffer");
        contents.add(this.moduleBufferLabel);
        this.moduleBufferLabel.setBounds(33, 180, 400, 17);

        this.bufferDirectory = new File(Settings.getInstance().getBufferDirectory());
        this.moduleBufferTextField = new JTextField(bufferDirectory.getAbsolutePath());
        this.moduleBufferTextField.setEditable(false);
        contents.add(this.moduleBufferTextField);
        this.moduleBufferTextField.setBounds(33, 210, 600, 21);
        
        final JButton chooseBufferLocation = new JButton("Choose");
        contents.add(chooseBufferLocation);
        chooseBufferLocation.setBounds(33 + 600 - 90, 
            180, 90, 21);
 
        chooseBufferLocation.addActionListener(new  ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                try {
                    Trace.trace(this, METHOD, "mkdirs=" 
                        + new Boolean(Preferences.this.bufferDirectory.mkdirs()));
                    JFileChooser chooser = new JFileChooser(Preferences.this.bufferDirectory);
                    FileFilter filter = new FileFilter() {
                        public boolean accept(File f) {
                            if (f.isDirectory()) {
                                return true;
                            }
                            return false;
                        }

                        // description of this filter
                        public String getDescription() {
                            return "Directory";
                        }
                    };

                    chooser.setFileFilter(filter);
                    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                    final int returnVal = chooser.showOpenDialog(Preferences.this);

                    if (returnVal == JFileChooser.APPROVE_OPTION) {
                        moduleBufferTextField.setText(chooser.getSelectedFile().getPath());
                        Preferences.this.bufferDirectory = chooser.getSelectedFile();
                        Preferences.this.changed = true;
                    }
                } catch (Exception e) {
                     JOptionPane.showMessageDialog(Preferences.this, e.getMessage(), "Alert",
                     JOptionPane.ERROR_MESSAGE);
                }
            }
        });
       

        this.generationPathLabel = new JLabel("Path for generated files");
        contents.add(this.generationPathLabel);
        this.generationPathLabel.setBounds(33, 250, 400, 17);

        this.generationDirectory = new File(Settings.getInstance().getGenerationDirectory());
        this.generationPathTextField = new JTextField(this.generationDirectory.getAbsolutePath());
        this.generationPathTextField.setEditable(false);
        contents.add(this.generationPathTextField);
        this.generationPathTextField.setBounds(33, 280, 600, 21);

        final JButton chooseGenerationLocation = new JButton("Choose");
        contents.add(chooseGenerationLocation);
        chooseGenerationLocation.setBounds(33 + 600 - 90, 
            250, 90, 21);
 
        chooseGenerationLocation.addActionListener(new  ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                try {
                    Trace.trace(this, METHOD, "mkdirs=" 
                        + new Boolean(Preferences.this.generationDirectory.mkdirs()));
                    JFileChooser chooser = new JFileChooser(Preferences.this.generationDirectory);
                    FileFilter filter = new FileFilter() {
                        public boolean accept(File f) {
                            if (f.isDirectory()) {
                                return true;
                            }
                            return false;
                        }

                        // description of this filter
                        public String getDescription() {
                            return "Directory";
                        }
                    };

                    chooser.setFileFilter(filter);
                    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                    final int returnVal = chooser.showOpenDialog(Preferences.this);

                    if (returnVal == JFileChooser.APPROVE_OPTION) {
                        generationPathTextField.setText(chooser.getSelectedFile().getPath());
                        Preferences.this.generationDirectory = chooser.getSelectedFile();
                        Preferences.this.changed = true;
                    }
                } catch (Exception e) {
                     JOptionPane.showMessageDialog(Preferences.this, e.getMessage(), "Alert",
                     JOptionPane.ERROR_MESSAGE);
                }
            }
        });
       

        this.localModulesPathLabel = new JLabel("Path for newly created module files");
        contents.add(this.localModulesPathLabel);
        this.localModulesPathLabel.setBounds(33, 310, 400, 17);

        this.localModulesDirectory = new File(Settings.getInstance().getLocalModulesDirectory());
        this.localModulesPathTextField = new JTextField(this.localModulesDirectory.getAbsolutePath());
        this.localModulesPathTextField.setEditable(false);
        contents.add(this.localModulesPathTextField);
        this.localModulesPathTextField.setBounds(33, 340, 600, 21);

        final JButton chooselocalModulesLocation = new JButton("Choose");
        contents.add(chooselocalModulesLocation);
        chooselocalModulesLocation.setBounds(33 + 600 - 90, 
            310, 90, 21);
 
        chooselocalModulesLocation.addActionListener(new  ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                try {
                    Trace.trace(this, METHOD, "mkdirs=" 
                        + new Boolean(Preferences.this.localModulesDirectory.mkdirs()));
                    JFileChooser chooser = new JFileChooser(Preferences.this.localModulesDirectory);
                    FileFilter filter = new FileFilter() {
                        public boolean accept(File f) {
                            if (f.isDirectory()) {
                                return true;
                            }
                            return false;
                        }

                        // description of this filter
                        public String getDescription() {
                            return "Directory";
                        }
                    };

                    chooser.setFileFilter(filter);
                    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                    final int returnVal = chooser.showOpenDialog(Preferences.this);

                    if (returnVal == JFileChooser.APPROVE_OPTION) {
                        localModulesPathTextField.setText(chooser.getSelectedFile().getPath());
                        Preferences.this.localModulesDirectory = chooser.getSelectedFile();
                        Preferences.this.changed = true;
                    }
                } catch (Exception e) {
                     JOptionPane.showMessageDialog(Preferences.this, e.getMessage(), "Alert",
                     JOptionPane.ERROR_MESSAGE);
                }
            }
        });
       

        final JButton cancel = new JButton("Cancel");
        contents.add(cancel);
        cancel.setBounds(33 + 600 - 190, 
            390, 90, 21);
        cancel.addActionListener(new  ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                Preferences.this.dispose();
            }
        });           

        final JButton ok = new JButton("OK");
        contents.add(ok);
        ok.setBounds(33 + 600 - 90, 
            390, 90, 21);
        ok.addActionListener(new  ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                Preferences.this.save();
                Preferences.this.dispose();
            }
        });           

    }

    
    final void save() {
        if (changed) {
            Settings.getInstance().setBufferDirectory(this.bufferDirectory);
            Settings.getInstance().setGenerationDirectory(this.generationDirectory);
            Settings.getInstance().setLocalModulesDirectory(this.localModulesDirectory);
            Settings.getInstance().setDirectResponse(this.directResponse);
            Settings.getInstance().setAutoReloadLastSessionChecked(
                this.autoReloadLastSessionChecked);
            Settings.getInstance().setAutoStartHtmlBrowser(this.autoStartHtmlBrowser);
            Settings.getInstance().setOldHtml(this.oldHtmlCode);
            Message.getInstance().setDirectResponse(Settings.getInstance().isDirectResponse());
        }
    }


}
