/**
 * PreferenceDialog.java
 * Used to configure program data.
 *
 * Created: March 8, 2006
 *
 * @author Nathan Stevens
 * @version 0.1 3/9/06
 */
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import java.util.*;
import java.io.*;

public class PreferenceDialog extends JDialog implements ActionListener {
  private InstruView instruView = null; // the instruview object
  private JFileChooser fc = new JFileChooser(); // The file chooser object
  private Properties properties  = null; // the properties object
  private File initFile = null; // the default initiation file in the users home directory
  private JTextField orgTF = null; // the organization name
  private JTextField labTF = null; // the laboratory name/PI
  private JTextField userTF = null; // the owner of the data
  private JTextField emailTF = null; // the owner's email
  private JTextField ddirTF = null; // the data directory
  private JTextField abTF = null; // the absorption spectrometer
  private JTextField emTF = null; // the emission spectrometer
  private JTextField rmTF = null; // the raman spectrometer
  private JTextField mrmTF = null; // the micro raman spectrometer
  private JTextField srTF = null; // the streak camera
  private JTextField ppTF = null; // the pump-probe system
  private JTextField qysTF = null; // the quantum yield standard
  
  // main constructor
  public PreferenceDialog(InstruView instruView) {
    super(instruView.instruViewFrame, "Preferences", false);
    this.instruView = instruView;
    this.properties = instruView.properties;
    
    // close this window
		addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent we) {
        close();
      }
		});
    
    // set the initiation file name and set default values for stuff
    String initFileName = System.getProperty("user.home") +
                                         System.getProperty("file.separator") + "instruview.ini";
    
    initFile = new File(initFileName);
    
    String org = "Enter Institution or University Name";
    String lab = "Enter Laboratory PI";
    String user = "Enter Your Name";
    String email = "Enter Your Email";
    String ddir = "Enter Data Directory";
    String ab = properties.getProperty("instrument.absorption", "Not Set");
    String em = properties.getProperty("instrument.emission", "Not Set");
    String rm = properties.getProperty("instrument.raman", "Not Set");
    String mrm = properties.getProperty("instrument.mraman", "Not Set");
    String sr = properties.getProperty("instrument.streak", "Not Set");
    String pp = properties.getProperty("instrument.pump-probe", "Not Set");
    String qys = properties.getProperty("qstandard", "Not Set");
    
    if(initFile.exists()) {
      org = properties.getProperty("organization", "Not Set");
      lab = properties.getProperty("laboratory", "Not Set");
      user = properties.getProperty("owner", "Not Set");
      email = properties.getProperty("email", "Not Set");
      ddir = properties.getProperty("data.directory", "Not Set");
    }
    
    // Set the layout to border layout
    getContentPane().setLayout(new BorderLayout());
    
    // some gui components
    JButton button;
    JLabel label;
    
    // panel that holds the lables
    JPanel labelPanel = new JPanel(new GridLayout(0,1,2,2));
    
    label = new JLabel(" Organization ");
    labelPanel.add(label);
    
    label = new JLabel(" Laboratory ");
    labelPanel.add(label);
    
    label = new JLabel(" User ");
    labelPanel.add(label);
    
    label = new JLabel(" Email ");
    labelPanel.add(label);
    
    label = new JLabel(" Data Directory ");
    labelPanel.add(label);
    
    label = new JLabel(" Absorbtion ");
    labelPanel.add(label);
    
    label = new JLabel(" Emission ");
    labelPanel.add(label);
    
    label = new JLabel(" Raman ");
    labelPanel.add(label);
    
    label = new JLabel(" Micro Raman ");
    labelPanel.add(label);
    
    label = new JLabel(" Streak Camera ");
    labelPanel.add(label);
    
    label = new JLabel(" Pump-Probe ");
    labelPanel.add(label);
    
    label = new JLabel(" QY Standard ");
    labelPanel.add(label);
    
    getContentPane().add(labelPanel, "West");
    
    // add the panel that holds the textfields and selection buttons
    JPanel tfPanel = new JPanel(new GridLayout(0,1,2,2));
    JPanel panel;
    
    panel = new JPanel();
    panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
    orgTF = new JTextField(org, 25);
    panel.add(orgTF);
    tfPanel.add(panel);
    
    panel = new JPanel();
    panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
    labTF = new JTextField(lab, 25);
    panel.add(labTF);
    tfPanel.add(panel);
    
    panel = new JPanel();
    panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
    userTF = new JTextField(user, 25);
    panel.add(userTF);
    tfPanel.add(panel);
    
    panel = new JPanel();
    panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
    emailTF = new JTextField(email, 25);
    panel.add(emailTF);
    tfPanel.add(panel);
    
    panel = new JPanel();
    panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
    ddirTF = new JTextField(ddir, 25);
    panel.add(ddirTF);
    
    button = new JButton("Browse");
    button.setActionCommand("ddir");
		button.addActionListener(this);
    button.setMargin(new Insets(0,2,0,2));
    panel.add(button);
    
    tfPanel.add(panel); 
    
    panel = new JPanel();
    panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
    abTF = new JTextField(ab, 25);
    panel.add(abTF);
    tfPanel.add(panel);
    
    panel = new JPanel();
    panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
    emTF = new JTextField(em, 25);
    panel.add(emTF);
    tfPanel.add(panel);
    
    panel = new JPanel();
    panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
    rmTF = new JTextField(rm, 25);
    panel.add(rmTF);
    tfPanel.add(panel);
    
    panel = new JPanel();
    panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
    mrmTF = new JTextField(mrm, 25);
    panel.add(mrmTF);
    tfPanel.add(panel);
    
    panel = new JPanel();
    panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
    srTF = new JTextField(sr, 25);
    panel.add(srTF);
    tfPanel.add(panel);
    
    panel = new JPanel();
    panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
    ppTF = new JTextField(pp, 25);
    panel.add(ppTF);
    tfPanel.add(panel);
    
    panel = new JPanel();
    panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
    qysTF = new JTextField(qys, 25);
    panel.add(qysTF);
    
    button = new JButton("Browse");
    button.setActionCommand("qys");
		button.addActionListener(this);
    button.setMargin(new Insets(0,2,0,2));
    panel.add(button);
    
    tfPanel.add(panel);
    
    getContentPane().add(tfPanel, "Center");
    
    // Add the ok, cancel buttons
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
    
    Border raisedbevel = BorderFactory.createRaisedBevelBorder();
    buttonPanel.setBorder(raisedbevel);
    
    buttonPanel.add(Box.createHorizontalGlue());
    
    button = new JButton("OK");
		button.setActionCommand("ok");
		button.addActionListener(this);
    buttonPanel.add(button);
    
    button = new JButton("Cancel");
		button.setActionCommand("cancel");
		button.addActionListener(this);
    buttonPanel.add(button);
    
    getContentPane().add(buttonPanel, "South");
    
    // set the file filter
    ExperimentDataFileFilter sdlFileFilter = new ExperimentDataFileFilter("sdl");
    fc.addChoosableFileFilter(sdlFileFilter);
  }
  
  // This method hadels action commands
  public void actionPerformed(ActionEvent e) {
    String cmd = e.getActionCommand();
    
    if(cmd.equals("ok")) {
      savePreferences();
    }
    else if(cmd.equals("ddir")) {
      setDataDirectory();
    }
    else if(cmd.equals("qys")) {
      setQYStandard();
    }
    else if(cmd.equals("cancel")) {
      close();
    }
    else {
      System.err.println("Action Command not handeled in class PreferenceDialog");
      System.err.println(cmd);
    }
  }
  
  // Method to set the directory name
  private void setDataDirectory() {
    fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    
    int returnVal = fc.showOpenDialog(instruView.instruViewFrame);
    
    if (returnVal == JFileChooser.APPROVE_OPTION) {
      File dir = fc.getSelectedFile();
      ddirTF.setText(dir.toString());
    }
    
     fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
  }
  
  // Method to set the directory name
  private void setQYStandard() {
    File qysdir = new File(instruView.standardDataDirectory);
    fc.setCurrentDirectory(qysdir);
    
    int returnVal = fc.showOpenDialog(instruView.instruViewFrame);
    
    if (returnVal == JFileChooser.APPROVE_OPTION) {
      File file = fc.getSelectedFile();
      qysTF.setText(file.getName());
    }
    
    fc.setCurrentDirectory(null);
  }
  
  // Method to save the preferences
  private void savePreferences() {
    String org = orgTF.getText();
    String lab = labTF.getText();
    String user = userTF.getText();
    String email = emailTF.getText();
    String ddir = ddirTF.getText();
    String ab = abTF.getText();
    String em = emTF.getText();
    String rm = rmTF.getText();
    String mrm = mrmTF.getText();
    String sr = srTF.getText();
    String pp = ppTF.getText();
    String qys = qysTF.getText();
    
    properties.setProperty("organization", org);
    properties.setProperty("laboratory", lab);
    properties.setProperty("owner", user);
    properties.setProperty("email", email);
    properties.setProperty("data.directory", ddir);
    properties.setProperty("instrument.absorption", ab);
    properties.setProperty("instrument.emission", em);
    properties.setProperty("instrument.raman", rm);
    properties.setProperty("instrument.mraman", mrm);
    properties.setProperty("instrument.streak", sr);
    properties.setProperty("instrument.pump-probe", pp);
    properties.setProperty("qstandard", qys);
    
    instruView.storeProperties(); // save the propeties to the file now
    
    close();
  }
  
  // method to close the window
  private void close() {
    setVisible(false);
  }
} //--end class PreferenceDialog
