/**
 * PlotSpectraDialog.java
 * Contains the graph that plots the dye spectra
 * Created: September 2 , 2006
 *
 * @author Nathan Stevens
 * @version 0.1 9/5/2006 
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.util.*;

public class PlotSpectraDialog extends JDialog implements ActionListener {
  public FRETViewPlugin fretview = null;
  private PlotSpectraGraph graph = null;
  
  // gui components to do the graphs
  public Color[] colors = null;
  private JTextField[] nameTF = null;
  private JButton[] colorB = null;
  public JCheckBox[] absCB = null;
  public JCheckBox[] emsCB = null;
  
  // hold the dye spectra data
  public DyeSpectra[] spectra = null;
  
  // constructor implememented but not used
  public PlotSpectraDialog() { }
  
  // the default constructor
  public PlotSpectraDialog(FRETViewPlugin fretview) {
    super(fretview, "Dye Spectra Plot", false);
    
    this.fretview = fretview;
    
    // initailize the colors
    colors = new Color[6];
    colors[0] = Color.black;
    colors[1] = Color.blue;
    colors[2] = Color.red;
    colors[3] = Color.green;
    colors[4] = Color.orange;
    colors[5] = Color.magenta;
    
    nameTF = new JTextField[colors.length];
    colorB = new JButton[colors.length];
    absCB = new JCheckBox[colors.length];
    emsCB = new JCheckBox[colors.length];
    spectra = new DyeSpectra[colors.length];
    
    // handles clossing of this plugin
		addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent we) {
        setVisible(false);
      }
		});
    
    // set the layout
    getContentPane().setLayout(new BorderLayout());
    
    // some gui stuff
    JPanel panel;
    JLabel label;
    JButton button;
    TitledBorder titleBorder;
    
    // add the panel for adding the panels
    JPanel mainPanel = new JPanel();
    mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
    
    for(int i = 0; i < colors.length; i++) {
      mainPanel.add(getDyeSpectraPanel(i));
    }
    
    getContentPane().add(mainPanel, "Center");
    
    // add the button panel
    JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
    Border border = BorderFactory.createRaisedBevelBorder();
    buttonPanel.setBorder(border);
    
    button = new JButton("Plot");
		button.setActionCommand("plot");
		button.addActionListener(this);
    buttonPanel.add(button);
    
    button = new JButton("Clear All");
		button.setActionCommand("clear");
		button.addActionListener(this);
    buttonPanel.add(button);
    
    buttonPanel.add(Box.createHorizontalGlue());
    
    button = new JButton("Close");
		button.setActionCommand("close");
		button.addActionListener(this);
    buttonPanel.add(button);
    
    getContentPane().add(buttonPanel, "South");
  }
  
  // Method to get a panel
  private JPanel getDyeSpectraPanel(int index) {
    int number  = index + 1;
    
    JPanel panel = new JPanel();
    panel.setLayout(new FlowLayout(FlowLayout.LEFT, 2,2));
    
    JButton button = new JButton("Dye "  + number);
		button.setActionCommand("dye-" + index);
		button.addActionListener(this);
    panel.add(button);
    
    button = new JButton("Clear");
		button.setActionCommand("clear-" + index);
		button.addActionListener(this);
    panel.add(button);
    
    nameTF[index] = new JTextField("", 20);
    panel.add( nameTF[index]);
    
    colorB[index] = new JButton();
    colorB[index].setBackground(colors[index]);
		colorB[index].setActionCommand("color-" + index);
		colorB[index].addActionListener(this);
    panel.add(colorB[index]);
    
    absCB[index] = new JCheckBox("Abs");
    absCB[index].setSelected(true);
    panel.add(absCB[index]);
    
    emsCB[index] = new JCheckBox("Ems");
    emsCB[index].setSelected(true);
    panel.add(emsCB[index]);
    
    return panel;
  }
  
  // Method to handel action events
  public void actionPerformed(ActionEvent ae) {
    String cmd = ae.getActionCommand();
    
    if(cmd.equals("plot")) {
			showGraphDialog();
		}
    else if(cmd.equals("clear")) {
			clearDyeSpectra();
		}
    else if(cmd.equals("close")) {
      setVisible(false);
      if(graph != null) {
        graph.close();
      }
    }
    else if(cmd.indexOf("dye") != -1) {
      int index = getDyeIndex(cmd);
      setDyeSpectra(index);
    }
    else if(cmd.indexOf("clear") != -1) {
      int index = getDyeIndex(cmd);
      clearDyeSpectra(index);
    }
    else {
      System.err.println("Action Command not handeled in class PlotSpectraDialog ...");
      System.err.println(cmd);
    }
  }
  
  // Method to set the dye spectra
  private void setDyeSpectra(int index) {
    DyeSpectra ds = fretview.currentDye;
    
    if(ds != null) {
      spectra[index] = ds;
      nameTF[index].setText(ds.toString());
    }
  }
  
  // Method to clear a selected dye of spectra
  private void clearDyeSpectra(int index) {
    spectra[index] = null;
    nameTF[index].setText("");
  }
  
  // Method to clear all spectra
  private void clearDyeSpectra() {
    spectra = new DyeSpectra[colors.length];
    
    for(int i  = 0; i < colors.length; i++) {
      nameTF[i].setText("");
    }
    
    // now set the plot dialog invisible
    if(graph != null) {
      graph.setVisible(false);
    }
  }
  
  // Method to actuall show the plot
  private void showGraphDialog() {
    if(graph == null && dataLoaded()) {
      graph = new PlotSpectraGraph(this);
      graph.pack();
      graph.setVisible(true);
    }
    else if(graph != null && dataLoaded()) {
      graph.loadGraphData();
      graph.setVisible(true);
    }
  }
  
  // Method to return if there is any data loaded
  private boolean dataLoaded() {
    boolean loaded=  false;
    for(int i = 0; i < colors.length; i++) {
      if(spectra[i] != null) {
        loaded = true;
        break;
      }
    }
    
    return loaded;
  }
  
  //Method to return the series index
  private int getDyeIndex(String cmd) {
    int index = 0;
    try {
      String[] sa = cmd.split("-");
      index = Integer.parseInt(sa[1]);
    }
    catch(NumberFormatException nfe) {}
    
    return index;
  }
}//--end class PlotSpectraDialog
