/**
 * DBInfoDialog.java
 * Used to view and set the information about the database.
 *
 * Created: June 19, 2006
 *
 * @author Nathan Stevens
 * @version 0.1 7/20/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 DBInfoDialog extends JDialog implements ActionListener {
  private FRETViewPlugin fretView = null; // the main fret view program
  private JTextField nameTF = null; // the name of this database
  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 notesTF = null; // the owner's email
  private JLabel dcLB = null; // the dye count
  private JLabel fnLB = null; // the filename
  private String[] meta = null; // the meta information
  private String notes = null; // the notes information
  private String name = null; // the name of the database
  
  public DBInfoDialog() { }
  
  public DBInfoDialog(FRETViewPlugin fretView) {
    super(fretView, "Spectral Database Info", false);
    this.fretView = fretView;
    
    // close this window
		addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent we) {
        close();
      }
		});
    
    meta = fretView.dyeData.getMetaInfo();
    notes = fretView.dyeData.getNotes();
    name = fretView.dyeData.getName();
    String dc = "" + fretView.dyeData.size();
    
    File file = fretView.dyeData.getFile();
    String filename = "Not Set";
    if(file != null) {
      filename = file.toString();
    }
    
    // some gui components
    JButton button;
    JLabel label;
    
    // Set the layout to border layout
    getContentPane().setLayout(new BorderLayout());
    
    JPanel labelPanel = new JPanel(new GridLayout(0,1,2,2));
    
    label = new JLabel(" Name ");
    labelPanel.add(label);
    
    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(" Notes ");
    labelPanel.add(label);
    
    label = new JLabel(" Dye Count ");
    labelPanel.add(label);
    
    label = new JLabel(" Filename ");
    labelPanel.add(label);
    
    getContentPane().add(labelPanel, "West");
    int tfs = 35;
    
    JPanel tfPanel = new JPanel(new GridLayout(0,1,2,2));
    JPanel panel;
    
    panel = new JPanel();
    panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
    nameTF = new JTextField(name, tfs);
    panel.add(nameTF);
    tfPanel.add(panel);
    
    panel = new JPanel();
    panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
    orgTF = new JTextField(meta[0], tfs);
    panel.add(orgTF);
    tfPanel.add(panel);
    
    panel = new JPanel();
    panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
    labTF = new JTextField(meta[1], tfs);
    panel.add(labTF);
    tfPanel.add(panel);
    
    panel = new JPanel();
    panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
    userTF = new JTextField(meta[2], tfs);
    panel.add(userTF);
    tfPanel.add(panel);
    
    panel = new JPanel();
    panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
    emailTF = new JTextField(meta[3], tfs);
    panel.add(emailTF);
    tfPanel.add(panel);
    
    panel = new JPanel();
    panel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
    notesTF = new JTextField(notes, tfs);
    panel.add(notesTF);
    tfPanel.add(panel);
    
    dcLB = new JLabel(dc);
    dcLB.setForeground(Color.blue);
    tfPanel.add(dcLB);
    
    fnLB = new JLabel(filename);
    fnLB.setForeground(Color.blue);
    tfPanel.add(fnLB);
    
    getContentPane().add(tfPanel, "Center");
    
    // Add the update, 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("Update");
		button.setActionCommand("update");
		button.addActionListener(this);
    buttonPanel.add(button);
    
    button = new JButton("Close");
		button.setActionCommand("close");
		button.addActionListener(this);
    buttonPanel.add(button);
    
    getContentPane().add(buttonPanel, "South");
  }
  
  // This method hadels action commands
  public void actionPerformed(ActionEvent e) {
    String cmd = e.getActionCommand();
    
    if(cmd.equals("update")) {
      updateInfo();
    }
    
    else if(cmd.equals("close")) {
      close();
    }
    else {
      System.err.println("Action Command not handeled in class PreferenceDialog");
      System.err.println(cmd);
    }
  }
  
  // Method to update the DB info
  private void updateInfo() {
    fretView.dyeData.setName(nameTF.getText());
    fretView.dyeData.setNotes(notesTF.getText());
    
    meta[0] = orgTF.getText();
    meta[1] = labTF.getText();
    meta[2] = userTF.getText();
    meta[3] = emailTF.getText();
  }
  
  // Method to update the displat
  public void updateDisplay() {
    nameTF.setText(fretView.dyeData.getName());
    notesTF.setText(fretView.dyeData.getNotes());
    
    meta = fretView.dyeData.getMetaInfo();
    orgTF.setText(meta[0]);
    labTF.setText(meta[1]);
    userTF.setText(meta[2]);
    emailTF.setText(meta[3]);
    
    String dc = "" + fretView.dyeData.size();
    
    File file = fretView.dyeData.getFile();
    String filename = "Not Set";
    if(file != null) {
      filename = file.toString();
    }
    
    dcLB.setText(dc);
    fnLB.setText(filename);
  }
  
  // close the window
  private void close() {
    setVisible(false);
  }
}//--end class DBInfoDialog
