
/**
 * InstruViewFrame.java
 * This is the main frame of the InstruView program
 *
 * Created: Dec 25, 2003
 *
 * @author Nathan Stevens
 * @version 0.6 2/15/2009
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.File;
import java.util.*;

public class InstruViewFrame extends JFrame implements ActionListener, ItemListener {

    // The Current Version and Date
    private static String VERSION_DATE = "0.6 (02-15-2009)";
    private static int OPEN_WINDOWS = 1; // Number of windows open
    public final static String FILE_EXT = ".sdl"; // the extension binary files should be saved with
    private final JFileChooser fc = new JFileChooser(); // The file chooser object
    private InstruView instruView = null; // The InstruView Panel
    private HelpViewer helpViewer = null; // used to view the help file
    private TransformDialog transformDialog = null; // To do arithmatic on data
    private DataSnoopDialog dataSnoopDialog = null; // used to search the data
    private ExperimentDataFileFilter sdlFileFilter = null;
    private ExperimentDataFileFilter textFileFilter = null;
    private ExperimentDataFileFilter ooFileFilter = null;
    private ExperimentDataFileFilter defaultFileFilter = null;
    private ExperimentData experimentData = null;
    private PreferenceDialog preferenceDialog = null;
    // checkbox menu items to allow which experimental panels
    private JCheckBoxMenuItem abMI = null;
    private JCheckBoxMenuItem emMI = null;
    private JCheckBoxMenuItem irMI = null;
    private JCheckBoxMenuItem rmMI = null;
    private JCheckBoxMenuItem srMI = null;
    private JCheckBoxMenuItem ppMI = null;
    private JCheckBoxMenuItem xyMI = null;
    // checkbox menu items to allow some configuration of the displayed plots
    private JCheckBoxMenuItem logPlotMI = null;
    private JCheckBoxMenuItem aaPlotMI = null;
    private JCheckBoxMenuItem chPlotMI = null;
    private JMenu pluginMenu = null; // used to dynamically add pugin to te menu

    public InstruViewFrame(InstruView instruView) {
        super("InstruView v-" + VERSION_DATE);
        this.instruView = instruView;

        // exits the program
        addWindowListener(new WindowAdapter() {

            public void windowClosing(WindowEvent we) {
                if (InstruViewFrame.OPEN_WINDOWS > 1) {
                    closeWindow();
                } else {
                    exit();
                }
            }
        });

        // set the program icon
        String iname = instruView.imageDirectory + "log.png";
        ImageIcon picon = new ImageIcon(iname);

        setIconImage(picon.getImage());

        // Add the menu bar now
        JMenuBar menubar = new JMenuBar();

        JMenu menu;
        JMenu submenu;
        JMenuItem item;

        // add the file menu
        menu = new JMenu("File");
        menu.setMnemonic('F');

        item = new JMenuItem("New Window");
        item.addActionListener(this);
        item.setActionCommand("nwindow");
        item.setMnemonic('n');
        menu.add(item);

        submenu = new JMenu("New Experiment Data");
        submenu.setMnemonic('w');

        if (instruView.getProperty("show.absorption").equals("yes")) {
            item = new JMenuItem("Absorption");
            item.addActionListener(this);
            item.setActionCommand("absorption");
            item.setMnemonic('a');
            submenu.add(item);
        }

        if (instruView.getProperty("show.emission").equals("yes")) {
            item = new JMenuItem("Emission");
            item.addActionListener(this);
            item.setActionCommand("emission");
            item.setMnemonic('e');
            submenu.add(item);
        }

        if (instruView.getProperty("show.raman").equals("yes")) {
            item = new JMenuItem("Raman");
            item.addActionListener(this);
            item.setActionCommand("nraman");
            item.setMnemonic('r');
            submenu.add(item);
        }

        if (instruView.getProperty("show.streak").equals("yes")) {
            item = new JMenuItem("Streak Camera");
            item.addActionListener(this);
            item.setActionCommand("nstreakcamera");
            item.setMnemonic('s');
            submenu.add(item);
        }

        if (instruView.getProperty("show.pump-probe").equals("yes")) {
            item = new JMenuItem("Pump-Probe");
            item.addActionListener(this);
            item.setActionCommand("npump-probe");
            item.setMnemonic('p');
            submenu.add(item);
        }

        item = new JMenuItem("XY Data");
        item.addActionListener(this);
        item.setActionCommand("nxydata");
        item.setMnemonic('x');
        submenu.add(item);

        menu.add(submenu);

        item = new JMenuItem("Open");
        item.addActionListener(this);
        item.setActionCommand("open");
        item.setMnemonic('o');
        menu.add(item);

        item = new JMenuItem("Save");
        item.addActionListener(this);
        item.setActionCommand("save");
        item.setMnemonic('s');
        menu.add(item);

        item = new JMenuItem("Save As");
        item.addActionListener(this);
        item.setActionCommand("saveas");
        item.setMnemonic('a');
        menu.add(item);

        menu.addSeparator();

        submenu = new JMenu("Import");
        submenu.setMnemonic('I');

        if (instruView.getProperty("show.absorption").equals("yes")) {
            item = new JMenuItem("Lambda UV-VIS");
            item.addActionListener(this);
            item.setActionCommand("lambda18");
            item.setMnemonic('l');
            submenu.add(item);

            item = new JMenuItem("Cary 1 Bio");
            item.addActionListener(this);
            item.setActionCommand("cary");
            item.setMnemonic('c');
            submenu.add(item);
        }

        if (instruView.getProperty("show.emission").equals("yes")) {
            item = new JMenuItem("Tau 2/3");
            item.addActionListener(this);
            item.setActionCommand("tau2");
            item.setMnemonic('t');
            submenu.add(item);

            item = new JMenuItem("HR4000");
            item.addActionListener(this);
            item.setActionCommand("hr4000");
            item.setMnemonic('h');
            submenu.add(item);

            item = new JMenuItem("LS Fluorescence");
            item.addActionListener(this);
            item.setActionCommand("lsfluorescence");
            item.setMnemonic('f');
            submenu.add(item);
        }

        if (instruView.getProperty("show.raman").equals("yes")) {
            item = new JMenuItem("Raman");
            item.addActionListener(this);
            item.setActionCommand("raman");
            item.setMnemonic('r');
            submenu.add(item);

            item = new JMenuItem("LabRam");
            item.addActionListener(this);
            item.setActionCommand("labram");
            item.setMnemonic('b');
            submenu.add(item);
        }

        if (instruView.getProperty("show.streak").equals("yes")) {
            item = new JMenuItem("Streak Camera");
            item.addActionListener(this);
            item.setActionCommand("streakcamera");
            item.setMnemonic('s');
            submenu.add(item);

            item = new JMenuItem("F900");
            item.addActionListener(this);
            item.setActionCommand("f900");
            item.setMnemonic('F');
            submenu.add(item);
        }

        if (instruView.getProperty("show.pump-probe").equals("yes")) {
            item = new JMenuItem("OO Data");
            item.addActionListener(this);
            item.setActionCommand("oodata");
            item.setMnemonic('o');
            submenu.add(item);

            item = new JMenuItem("Pump-Probe");
            item.addActionListener(this);
            item.setActionCommand("pump-probe");
            item.setMnemonic('p');
            submenu.add(item);
        }

        submenu.addSeparator();

        // add any xyi instrument files now
        addXYIMenuItems(submenu);

        item = new JMenuItem("XY Data");
        item.addActionListener(this);
        item.setActionCommand("xydata");
        item.setMnemonic('x');
        submenu.add(item);

        menu.add(submenu);

        submenu = new JMenu("Export");
        submenu.setMnemonic('E');

        item = new JMenuItem("XY Data");
        item.addActionListener(this);
        item.setActionCommand("xy1");
        item.setMnemonic('d');
        submenu.add(item);

        item = new JMenuItem("XY Data + Labels");
        item.addActionListener(this);
        item.setActionCommand("xy2");
        item.setMnemonic('l');
        submenu.add(item);

        item = new JMenuItem("XY Data + Header");
        item.addActionListener(this);
        item.setActionCommand("xy3");
        item.setMnemonic('h');
        submenu.add(item);

        item = new JMenuItem("XY Data + Header + Labels");
        item.addActionListener(this);
        item.setActionCommand("xy4");
        item.setMnemonic('a');
        submenu.add(item);

        item = new JMenuItem("Worksheet");
        item.addActionListener(this);
        item.setActionCommand("xy5");
        item.setMnemonic('w');
        submenu.add(item);

        menu.add(submenu);

        menu.addSeparator();

        item = new JMenuItem("Close");
        item.addActionListener(this);
        item.setActionCommand("cwindow");
        item.setMnemonic('c');
        menu.add(item);
        menubar.add(menu);

        item = new JMenuItem("Exit");
        item.addActionListener(this);
        item.setActionCommand("exit");
        item.setMnemonic('x');
        menu.add(item);
        menubar.add(menu);

        // Add the Options Menu
        menu = new JMenu("Options");
        menu.setMnemonic('O');

        item = new JMenuItem("Preferences");
        item.addActionListener(this);
        item.setActionCommand("preference");
        item.setMnemonic('p');
        menu.add(item);

        submenu = new JMenu("Experiment Panel");
        submenu.setMnemonic('e');

        addExperimentPanelMenuItems(submenu);

        menu.add(submenu);
        menubar.add(menu);

        // Add the Tools Menu
        menu = new JMenu("Tools");
        menu.setMnemonic('t');

        item = new JMenuItem("View Data");
        item.addActionListener(this);
        item.setActionCommand("view");
        item.setMnemonic('v');
        menu.add(item);

        item = new JMenuItem("Worksheet");
        item.addActionListener(this);
        item.setActionCommand("worksheet");
        item.setMnemonic('w');
        menu.add(item);

        item = new JMenuItem("Plot Data");
        item.addActionListener(this);
        item.setActionCommand("plot");
        item.setMnemonic('p');
        menu.add(item);

        item = new JMenuItem("Edit Data");
        item.addActionListener(this);
        item.setActionCommand("editdata");
        item.setMnemonic('e');
        menu.add(item);

        if (instruView.getProperty("show.emission").equals("yes")) {
            item = new JMenuItem("Emission View");
            item.addActionListener(this);
            item.setActionCommand("emissionview");
            item.setMnemonic('m');
            menu.add(item);
        }

        if (instruView.getProperty("show.streak").equals("yes")) {
            item = new JMenuItem("Streak View");
            item.addActionListener(this);
            item.setActionCommand("streakview");
            item.setMnemonic('s');
            menu.add(item);


            item = new JMenuItem("Lifetime Fit");
            item.addActionListener(this);
            item.setActionCommand("lifetimefit");
            item.setMnemonic('l');
            menu.add(item);
        }

        if (instruView.getProperty("show.pump-probe").equals("yes")) {
            item = new JMenuItem("Pump-Probe View");
            item.addActionListener(this);
            item.setActionCommand("ppview");
            item.setMnemonic('u');
            menu.add(item);
        }

        menu.addSeparator();

        item = new JMenuItem("Transform");
        item.addActionListener(this);
        item.setActionCommand("transform");
        item.setMnemonic('r');
        menu.add(item);

        item = new JMenuItem("Data Snoop");
        item.addActionListener(this);
        item.setActionCommand("datasnoop");
        item.setMnemonic('a');
        menu.add(item);

        item = new JMenuItem("Data Sort");
        item.addActionListener(this);
        item.setActionCommand("datasort");
        item.setMnemonic('s');
        menu.add(item);

        item = new JMenuItem("Data Rename");
        item.addActionListener(this);
        item.setActionCommand("datarename");
        item.setMnemonic('n');
        menu.add(item);

        item = new JMenuItem("Clear Data Folder");
        item.addActionListener(this);
        item.setActionCommand("clearfolder");
        item.setMnemonic('o');
        menu.add(item);

        item = new JMenuItem("Igor Talk Server");
        item.addActionListener(this);
        item.setActionCommand("igortalk");
        item.setMnemonic('g');
        menu.add(item);

        item = new JMenuItem("DB4O Talk");
        item.addActionListener(this);
        item.setActionCommand("db4otalk");
        item.setMnemonic('4');
        menu.add(item);

        menu.addSeparator();

        item = new JMenuItem("Console");
        item.addActionListener(this);
        item.setActionCommand("console");
        item.setMnemonic('c');
        menu.add(item);

        menubar.add(menu);

        // Add the Plugins Menu
        pluginMenu = new JMenu("Plugins");
        pluginMenu.setMnemonic('p');

        item = new JMenuItem("Plugin Manager");
        item.addActionListener(this);
        item.setActionCommand("plugin_manager");
        item.setMnemonic('m');
        pluginMenu.add(item);

        pluginMenu.addSeparator();

        // add menu items for the loaded plugins
        addPluginMenuItems();

        menubar.add(pluginMenu);

        // Add menu for setting plot properties
        menu = new JMenu("Plot Properties");
        menu.setMnemonic('l');

        logPlotMI = new JCheckBoxMenuItem("Log Plot");
        logPlotMI.setMnemonic('g');
        logPlotMI.addItemListener(this);
        menu.add(logPlotMI);

        aaPlotMI = new JCheckBoxMenuItem("Antialias");
        aaPlotMI.setMnemonic('d');
        aaPlotMI.addItemListener(this);
        menu.add(aaPlotMI);

        chPlotMI = new JCheckBoxMenuItem("Crosshair");
        chPlotMI.setMnemonic('c');
        chPlotMI.setSelected(true);
        chPlotMI.addItemListener(this);
        menu.add(chPlotMI);

        item = new JMenuItem("More...");
        item.addActionListener(this);
        item.setActionCommand("plotconfig");
        item.setMnemonic('m');
        menu.add(item);

        menubar.add(menu);

        // Add a glue component to the menu
        menubar.add(Box.createHorizontalGlue());

        // Add the Help menu
        menu = new JMenu("Help");
        menu.setMnemonic('H');

        item = new JMenuItem("Contents");
        item.addActionListener(this);
        item.setActionCommand("contents");
        item.setMnemonic('c');
        menu.add(item);

        menu.addSeparator();

        item = new JMenuItem("About");
        item.addActionListener(this);
        item.setActionCommand("about");
        item.setMnemonic('a');
        menu.add(item);

        menubar.add(menu);

        // set the menu bar
        setJMenuBar(menubar);

        // set the layout
        getContentPane().setLayout(new BorderLayout());

        // add the J tool bar
        JToolBar toolBar = new JToolBar();
        toolBar.setFloatable(false);

        JButton button;
        ImageIcon imIcon;
        String imName;

        imName = instruView.imageDirectory + "window_new.png";
        imIcon = new ImageIcon(imName);
        button = new JButton(imIcon);
        button.setActionCommand("nwindow");
        button.addActionListener(this);
        button.setToolTipText("Open New InstruView Window");
        toolBar.add(button);

        imName = instruView.imageDirectory + "fileopen.png";
        imIcon = new ImageIcon(imName);
        button = new JButton(imIcon);
        button.setActionCommand("open");
        button.addActionListener(this);
        button.setToolTipText("Open SDL file");
        toolBar.add(button);

        imName = instruView.imageDirectory + "filesave.png";
        imIcon = new ImageIcon(imName);
        button = new JButton(imIcon);
        button.setActionCommand("save");
        button.addActionListener(this);
        button.setToolTipText("Save SDL File");
        toolBar.add(button);

        imName = instruView.imageDirectory + "filesaveas.png";
        imIcon = new ImageIcon(imName);
        button = new JButton(imIcon);
        button.setActionCommand("saveas");
        button.addActionListener(this);
        button.setToolTipText("Save SDL File As ...");
        toolBar.add(button);

        imName = instruView.imageDirectory + "math_matrix.png";
        imIcon = new ImageIcon(imName);
        button = new JButton(imIcon);
        button.setActionCommand("view");
        button.addActionListener(this);
        button.setToolTipText("Open Data Dialog");
        toolBar.add(button);

        imName = instruView.imageDirectory + "inline_table.png";
        imIcon = new ImageIcon(imName);
        button = new JButton(imIcon);
        button.setActionCommand("worksheet");
        button.addActionListener(this);
        button.setToolTipText("Open Worksheet Dialog");
        toolBar.add(button);

        imName = instruView.imageDirectory + "frame_chart.png";
        imIcon = new ImageIcon(imName);
        button = new JButton(imIcon);
        button.setActionCommand("plot");
        button.addActionListener(this);
        button.setToolTipText("Open Plot Dialog");
        toolBar.add(button);

        imName = instruView.imageDirectory + "edit.png";
        imIcon = new ImageIcon(imName);
        button = new JButton(imIcon);
        button.setActionCommand("editdata");
        button.addActionListener(this);
        button.setToolTipText("Open Edit Data Dialog");
        toolBar.add(button);

        if (instruView.getProperty("show.emission").equals("yes")) {
            imName = instruView.imageDirectory + "idea.png";
            imIcon = new ImageIcon(imName);
            button = new JButton(imIcon);
            button.setActionCommand("emissionview");
            button.setToolTipText("Open Emission View Dialog");
            button.addActionListener(this);
            toolBar.add(button);
        }

        if (instruView.getProperty("show.streak").equals("yes")) {
            imName = instruView.imageDirectory + "camera.png";
            imIcon = new ImageIcon(imName);
            button = new JButton(imIcon);
            button.setActionCommand("streakview");
            button.addActionListener(this);
            button.setToolTipText("Open Streak View Dialog");
            toolBar.add(button);


            imName = instruView.imageDirectory + "roll.png";
            imIcon = new ImageIcon(imName);
            button = new JButton(imIcon);
            button.setActionCommand("lifetimefit");
            button.addActionListener(this);
            button.setToolTipText("Open Lifetime Fit Dialog");
            toolBar.add(button);
        }

        if (instruView.getProperty("show.pump-probe").equals("yes")) {
            imName = instruView.imageDirectory + "clock.png";
            imIcon = new ImageIcon(imName);
            button = new JButton(imIcon);
            button.setActionCommand("ppview");
            button.addActionListener(this);
            button.setToolTipText("Open Pump Probe View Dialog");
            toolBar.add(button);
        }

        imName = instruView.imageDirectory + "math_sum.png";
        imIcon = new ImageIcon(imName);
        button = new JButton(imIcon);
        button.setActionCommand("transform");
        button.addActionListener(this);
        button.setToolTipText("Open Math Transform Dialog");
        toolBar.add(button);

        imName = instruView.imageDirectory + "filefind.png";
        imIcon = new ImageIcon(imName);
        button = new JButton(imIcon);
        button.setActionCommand("datasnoop");
        button.addActionListener(this);
        button.setToolTipText("Open Data Snoop Dialog");
        toolBar.add(button);

        imName = instruView.imageDirectory + "thumb.png";
        imIcon = new ImageIcon(imName);
        button = new JButton(imIcon);
        button.setActionCommand("tofolder");
        button.addActionListener(this);
        button.setToolTipText("Add Data To Folder");
        toolBar.add(button);

        toolBar.add(Box.createHorizontalGlue());

        imName = instruView.imageDirectory + "exit.png";
        imIcon = new ImageIcon(imName);
        button = new JButton(imIcon);
        button.setActionCommand("exit");
        button.addActionListener(this);
        button.setToolTipText("Exit InstruView");
        toolBar.add(button);

        getContentPane().add(toolBar, "North");

        // add the instruview panel now
        getContentPane().add(instruView, "Center");

        // initialize some stuff
        File dataDirectory = new File(InstruView.dataDirectoryName);
        fc.setCurrentDirectory(dataDirectory);
        fc.setMultiSelectionEnabled(true);

        sdlFileFilter = new ExperimentDataFileFilter("sdl");
        textFileFilter = new ExperimentDataFileFilter("text");
        ooFileFilter = new ExperimentDataFileFilter("oofile");
        defaultFileFilter = new ExperimentDataFileFilter("default");
    }

    // Method to add any xy data instrument menu items
    private void addXYIMenuItems(JMenu submenu) {
        Enumeration keys = XYInstrumentLoader.xyInstruments.keys();
        String xyi_name = null;

        while (keys.hasMoreElements()) {
            xyi_name = keys.nextElement().toString();

            JMenuItem item = new JMenuItem(xyi_name);
            item.addActionListener(this);
            item.setActionCommand(xyi_name + "-xyi");
            submenu.add(item);
        }
    }

    // Method to add the experiment panel check box menu item
    private void addExperimentPanelMenuItems(JMenu submenu) {
        boolean[] ep_show = new boolean[7]; // must read this from properties file

        if (instruView.getProperty("show.absorption").equals("yes")) {
            ep_show[0] = true;
        }

        if (instruView.getProperty("show.emission").equals("yes")) {
            ep_show[1] = true;
        }

        if (instruView.getProperty("show.ir").equals("yes")) {
            ep_show[2] = true;
        }

        if (instruView.getProperty("show.raman").equals("yes")) {
            ep_show[3] = true;
        }

        if (instruView.getProperty("show.streak").equals("yes")) {
            ep_show[4] = true;
        }

        if (instruView.getProperty("show.pump-probe").equals("yes")) {
            ep_show[5] = true;
        }

        if (instruView.getProperty("show.xydata").equals("yes")) {
            ep_show[6] = true;
        }

        abMI = new JCheckBoxMenuItem("Absorption Panel");
        abMI.setMnemonic('a');
        abMI.setSelected(ep_show[0]);
        abMI.addItemListener(this);
        submenu.add(abMI);

        emMI = new JCheckBoxMenuItem("Emission");
        emMI.setMnemonic('e');
        emMI.setSelected(ep_show[1]);
        emMI.addItemListener(this);
        submenu.add(emMI);

        /*irMI = new JCheckBoxMenuItem("FT-IR");
        irMI.setMnemonic('f');
        irMI.setSelected(ep_show[2]);
        irMI.addItemListener(this);
        submenu.add(irMI);*/

        rmMI = new JCheckBoxMenuItem("Raman");
        rmMI.setMnemonic('r');
        rmMI.setSelected(ep_show[3]);
        rmMI.addItemListener(this);
        submenu.add(rmMI);

        srMI = new JCheckBoxMenuItem("Streak Camera");
        srMI.setMnemonic('s');
        srMI.setSelected(ep_show[4]);
        srMI.addItemListener(this);
        submenu.add(srMI);

        ppMI = new JCheckBoxMenuItem("Pump-Probe");
        ppMI.setMnemonic('p');
        ppMI.setSelected(ep_show[5]);
        ppMI.addItemListener(this);
        submenu.add(ppMI);

        xyMI = new JCheckBoxMenuItem("XY Data");
        xyMI.setMnemonic('x');
        xyMI.setSelected(ep_show[6]);
        xyMI.addItemListener(this);
        submenu.add(xyMI);
    }

    // Method to add plugin menu items to the menu bar
    private void addPluginMenuItems() {
        Enumeration keys = InstruViewPluginLoader.pluginFactories.keys();
        String plugin_name = null;

        while (keys.hasMoreElements()) {
            plugin_name = keys.nextElement().toString();

            JMenuItem item = new JMenuItem(plugin_name);
            item.addActionListener(this);
            item.setActionCommand(plugin_name + "-plugin");
            pluginMenu.add(item);
        }
    }

    // This method hadels action commands
    public void actionPerformed(ActionEvent e) {
        String cmd = e.getActionCommand();

        if (cmd.equals("nwindow")) {
            showNewWindow();
        } else if (cmd.equals("absorption")) {
            createNewExperimentData("absorption");
        } else if (cmd.equals("emission")) {
            createNewExperimentData("emission");
        } else if (cmd.equals("nraman")) {
            createNewExperimentData("raman");
        } else if (cmd.equals("nftir")) {
            createNewExperimentData("ftir");
        } else if (cmd.equals("nstreakcamera")) {
            createNewExperimentData("streak");
        } else if (cmd.equals("npump-probe")) {
            createNewExperimentData("pump-probe");
        } else if (cmd.equals("nxydata")) {
            createNewExperimentData("xydata");
        } else if (cmd.equals("open")) {
            openSpectraDataListFile();
        } else if (cmd.equals("save")) {
            saveSpectraDataListFile();
        } else if (cmd.equals("saveas")) {
            saveSpectraDataListFileAs();
        } else if (cmd.equals("lambda18")) {
            openInstrumentDataFile("lambda18");
        } else if (cmd.equals("lambda18drs")) {
            openInstrumentDataFile("lambda18drs");
        } else if (cmd.equals("cary")) {
            openInstrumentDataFile("cary");
        } else if (cmd.equals("tau2")) {
            openInstrumentDataFile("tau2");
        } else if (cmd.equals("tau2ref")) {
            openInstrumentDataFile("tau2ref");
        } else if (cmd.equals("hr4000")) {
            openInstrumentDataFile("hr4000");
        } else if (cmd.equals("raman")) {
            openInstrumentDataFile("raman");
        } else if (cmd.equals("labram")) {
            openInstrumentDataFile("labram");
        } else if (cmd.equals("streakcamera")) {
            openInstrumentDataFile("streak");
        } else if (cmd.equals("streakcameradata")) {
            openInstrumentDataFile("streakdata");
        } else if (cmd.equals("f900")) {
            openInstrumentDataFile("f900");
        } else if (cmd.equals("oodata")) {
            openInstrumentDataFile("oodata");
        } else if (cmd.equals("pump-probe")) {
            openInstrumentDataFile("pump-probe");
        } else if (cmd.equals("xydata")) {
            openInstrumentDataFile("xydata");
        } else if (cmd.indexOf("-xyi") != -1) {
            openInstrumentDataFile(cmd);
        } else if (cmd.equals("xy1")) {
            exportData("xy1");
        } else if (cmd.equals("xy2")) {
            exportData("xy2");
        } else if (cmd.equals("xy3")) {
            exportData("xy3");
        } else if (cmd.equals("xy4")) {
            exportData("xy4");
        } else if (cmd.equals("xy5")) {
            exportWorksheet();
        } else if (cmd.equals("cwindow")) {
            closeWindow();
        } else if (cmd.equals("exit")) {
            exit();
        } else if (cmd.equals("preference")) {
            showPreferenceDialog();
        } else if (cmd.equals("contents")) {
            showHelpDialog();
        } else if (cmd.equals("about")) {
            showAboutDialog();
        } else if (cmd.equals("view")) {
            instruView.showDataViewDialog();
        } else if (cmd.equals("worksheet")) {
            instruView.showWorksheetDialog();
        } else if (cmd.equals("plot")) {
            instruView.showPlotDialog();
        } else if (cmd.equals("editdata")) {
            instruView.showDataEditDialog();
        } else if (cmd.equals("emissionview")) {
            instruView.showEmissionViewDialog();
        } else if (cmd.equals("streakview")) {
            instruView.showStreakViewDialog();
        } else if (cmd.equals("lifetimefit")) {
            instruView.showLifeTimeFitDialog();
        } else if (cmd.equals("ppview")) {
            instruView.showPumpProbeViewDialog();
        } else if (cmd.equals("transform")) {
            showTransformDialog();
        } else if (cmd.equals("datasnoop")) {
            showDataSnoopDialog();
        } else if (cmd.equals("datasort")) {
            instruView.showDataSortDialog();
        } else if (cmd.equals("datarename")) {
            instruView.showDataRenameDialog();
        } else if (cmd.equals("tofolder")) {
            instruView.addSpectraDataToFolder();
        } else if (cmd.equals("clearfolder")) {
            instruView.initializeDataFolder();
        } else if (cmd.equals("igortalk")) {
            instruView.showIgorTalkDialog();
        } else if (cmd.equals("db4otalk")) {
            instruView.showDB4ODialog();
        } else if (cmd.equals("console")) {
            instruView.showMessageConsole();
        } else if (cmd.equals("plugin_manager")) {
            showPluginManagerDialog();
        } else if (cmd.indexOf("-plugin") != -1) {
            showPluginDialog(cmd);
        } else {
            // debug code
            System.err.println("Event not handel in class InstruViewFrame " + cmd);
        }
    }

    // Method to handel the item change events
    public void itemStateChanged(ItemEvent e) {
        Object source = e.getItemSelectable();

        if (source == abMI) {
            if (e.getStateChange() == ItemEvent.SELECTED) {
                instruView.properties.setProperty("show.absorption", "yes");
            } else {
                instruView.properties.setProperty("show.absorption", "no");
            }
        } else if (source == emMI) {
            if (e.getStateChange() == ItemEvent.SELECTED) {
                instruView.properties.setProperty("show.emission", "yes");
            } else {
                instruView.properties.setProperty("show.emission", "no");
            }
        } else if (source == irMI) {
            if (e.getStateChange() == ItemEvent.SELECTED) {
                instruView.properties.setProperty("show.ir", "yes");
            } else {
                instruView.properties.setProperty("show.ir", "no");
            }
        } else if (source == rmMI) {
            if (e.getStateChange() == ItemEvent.SELECTED) {
                instruView.properties.setProperty("show.raman", "yes");
            } else {
                instruView.properties.setProperty("show.raman", "no");
            }
        } else if (source == srMI) {
            if (e.getStateChange() == ItemEvent.SELECTED) {
                instruView.properties.setProperty("show.streak", "yes");
            } else {
                instruView.properties.setProperty("show.streak", "no");
            }
        } else if (source == ppMI) {
            if (e.getStateChange() == ItemEvent.SELECTED) {
                instruView.properties.setProperty("show.pump-probe", "yes");
            } else {
                instruView.properties.setProperty("show.pump-probe", "no");
            }
        } else if (source == xyMI) {
            if (e.getStateChange() == ItemEvent.SELECTED) {
                instruView.properties.setProperty("show.xydata", "yes");
            } else {
                instruView.properties.setProperty("show.xydata", "no");
            }
        } else if (source == logPlotMI || source == aaPlotMI || source == chPlotMI) {
            updatePlotProperties();
            return; // return now so that we don't do any writting to the disk
        } else {
            System.err.println("Item change not handel in class InstruViewFrame");
        }

        // store this value in the initiation file
        instruView.storeProperties();
    }

    // Method to open spectra data list file
    private void openSpectraDataListFile() {
        fc.resetChoosableFileFilters();
        fc.addChoosableFileFilter(sdlFileFilter);

        // now add it to the chooseable file filter
        File[] files = openDataFile();
        if (files != null) {
            for (int i = 0; i < files.length; i++) {
                if (files[i].exists()) {
                    Object object = BinaryFileManager.getObjectFromFile(files[i]);
                    experimentData = (ExperimentData) object;
                    if (checkSaved(experimentData.getType())) {
                        instruView.loadSDL(experimentData, files[i]);
                    }
                }
            }
        }
    }

    // Method to open and instrument data file
    private void openInstrumentDataFile(String instrumentName) {
        fc.resetChoosableFileFilters();
        if (instrumentName.equals("oodata") || instrumentName.equals("pump-probe") ||
                instrumentName.equals("hr4000")) {
            fc.addChoosableFileFilter(ooFileFilter);
        } else if (instrumentName.equals("cary")) {
            fc.addChoosableFileFilter(defaultFileFilter);
        } else {
            fc.addChoosableFileFilter(textFileFilter);
        }

        // now add it to the chooseable file filter
        File[] files = openDataFile();
        if (files != null) {
            instruView.loadInstrumentData(instrumentName, files);
        }
    }

    // Method to open data files
    private File[] openDataFile() {
        File[] files = null;
        int returnVal = fc.showOpenDialog(InstruViewFrame.this);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            files = fc.getSelectedFiles();
        }
        return files;
    }

    // Method to export a data to a file
    private void exportData(String exnum) {
        SpectraData[] sdata = instruView.experimentPanel.getSelectedSpectraData();

        if (sdata != null) {
            fc.resetChoosableFileFilters();
            fc.addChoosableFileFilter(defaultFileFilter);
            int returnVal = fc.showSaveDialog(this);

            if (returnVal == JFileChooser.APPROVE_OPTION) {
                File directory = fc.getCurrentDirectory();
                new DataFileWriter(sdata, directory, exnum).start();
            }
        }
    }

    // Method to export the selected data to a text work sheet
    private void exportWorksheet() {
        SpectraData[] sdata = instruView.experimentPanel.getSelectedSpectraData();

        if (sdata != null) {
            fc.resetChoosableFileFilters();
            fc.addChoosableFileFilter(defaultFileFilter);
            int returnVal = fc.showSaveDialog(this);

            if (returnVal == JFileChooser.APPROVE_OPTION) {
                File file = fc.getSelectedFile();
                new WorksheetFileWriter(sdata, file, true).start();
            }
        }
    }

    // Method save the spectra data list file
    private void saveSpectraDataListFile() {
        File file = instruView.experimentPanel.getSpectraDataListFile();
        if (file != null) {
            experimentData = instruView.experimentPanel.getExperimentData();
            BinaryFileManager.saveObjectToFile(experimentData, file);
            instruView.setSaved();
        } else {
            saveSpectraDataListFileAs();
        }
    }

    // Method save the spectra data list file
    private void saveSpectraDataListFileAs() {
        experimentData = instruView.experimentPanel.getExperimentData();

        fc.resetChoosableFileFilters();
        fc.addChoosableFileFilter(sdlFileFilter);
        int returnVal = fc.showSaveDialog(InstruViewFrame.this);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            if (file != null) {
                // check to see if correct file extension is there.  if not add it
                String name = file.toString();
                if (name.toLowerCase().indexOf(FILE_EXT) == -1) {
                    name += FILE_EXT;
                    file = new File(name);
                }

                BinaryFileManager.saveObjectToFile(experimentData, file);
                instruView.experimentPanel.setSpectraDataListFile(file);
                instruView.setSaved();
            } else {
                String message = "Spectra Data List (*.sdl) not saved.\nTry Saving it again...";
                showErrorDialog("File IO Error", message);
            }
        }
    }

    // Method to create new experimentData object
    private void createNewExperimentData(String expname) {
        String[] meta = new String[5];
        meta[0] = InstruView.getProperty("organization");
        meta[1] = InstruView.getProperty("laboratory");
        meta[2] = InstruView.getProperty("owner");
        meta[3] = InstruView.getProperty("email");
        meta[4] = "mm-dd-yyyy";

        ExperimentData edata;
        if (expname.equals("absorption")) {
            if (checkSaved(expname)) {
                edata = new ExperimentData(expname, meta);
                instruView.loadSDL(edata);
            }
        } else if (expname.equals("emission")) {
            if (checkSaved(expname)) {
                edata = new ExperimentData(expname, meta);
                instruView.loadSDL(edata);
            }
        } else if (expname.equals("raman")) {
            if (checkSaved(expname)) {
                edata = new ExperimentData(expname, meta);
                instruView.loadSDL(edata);
            }
        } else if (expname.equals("streak")) {
            if (checkSaved(expname)) {
                edata = new ExperimentData(expname, meta);
                instruView.loadSDL(edata);
            }
        } else if (expname.equals("pump-probe")) {
            if (checkSaved(expname)) {
                edata = new ExperimentData(expname, meta);
                instruView.loadSDL(edata);
            }
        } else if (expname.equals("xydata")) {
            if (checkSaved(expname)) {
                edata = new ExperimentData(expname, meta);
                instruView.loadSDL(edata);
            }
        } else {
            System.err.println("Unkown experiment type in createNewExperimentData() InstruViewFrame");
        }
    }

    // Method to check to see if a data file was saved
    private boolean checkSaved(String expname) {
        if (!instruView.isSaved(expname)) {

            int n = JOptionPane.showConfirmDialog(
                    this, "Data not saved (" + expname + "). Continue anyway?",
                    "UnSaved Data",
                    JOptionPane.YES_NO_OPTION);
            if (n == JOptionPane.YES_OPTION) {
                return true;
            } else {
                return false;
            }
        } else {
            return true;
        }
    }

    // Method to see is all the data were saved
    private boolean checkAllSaved() {
        StringBuffer buffer = new StringBuffer();

        if (!instruView.isSaved("absorption")) {
            buffer.append("\"Absorption\"\n");
        }

        if (!instruView.isSaved("emission")) {
            buffer.append("\"Emission\"\n");
        }

        if (!instruView.isSaved("raman")) {
            buffer.append("\"Raman\"\n");
        }

        if (!instruView.isSaved("streak")) {
            buffer.append("\"Streak Camera\"\n");
        }

        if (!instruView.isSaved("pump-probe")) {
            buffer.append("\"Pump-Probe\"\n");
        }

        if (!instruView.isSaved("xydata")) {
            buffer.append("\"XYData\"\n");
        }

        int ln = buffer.length();
        if (ln > 0) {
            String message = "The following Experiment Data are not saved\n" +
                    buffer.toString() + "Exit or Close anyway?";
            int n = JOptionPane.showConfirmDialog(
                    this, message,
                    "UnSaved Data",
                    JOptionPane.YES_NO_OPTION);
            if (n == JOptionPane.YES_OPTION) {
                return true;
            } else {
                return false;
            }
        } else {
            return true;
        }
    }

    // Method to display a new window
    private void showNewWindow() {
        InstruView instruView2 = new InstruView();
        InstruViewFrame instruViewFrame2 = new InstruViewFrame(instruView2);
        instruViewFrame2.setSize(InstruView.width, InstruView.height);
        instruViewFrame2.setVisible(true);
        InstruViewFrame.OPEN_WINDOWS++;
    }

    // Method to display the transform dialog
    private void showTransformDialog() {
        if (transformDialog == null) {
            transformDialog = new TransformDialog(instruView);
            transformDialog.pack();
        }

        transformDialog.setVisible(true);
    }

    // Method to display the transform dialog
    private void showDataSnoopDialog() {
        if (dataSnoopDialog == null) {
            dataSnoopDialog = new DataSnoopDialog(instruView);
            dataSnoopDialog.pack();
        }

        dataSnoopDialog.setVisible(true);
    }

    // show the program preferences dialog
    private void showPreferenceDialog() {
        if (preferenceDialog == null) {
            preferenceDialog = new PreferenceDialog(instruView);
            preferenceDialog.pack();
        }

        preferenceDialog.setVisible(true);
    }
    // Method to display the plugin manager dialog

    private void showPluginManagerDialog() {
        System.out.println("Test of plugin manager");
    }

    // Method to display a plugin dialog
    private void showPluginDialog(String plugin_name) {
        String[] name = plugin_name.split("-");
        PluginFactory pluginFactory = (PluginFactory) InstruViewPluginLoader.pluginFactories.get(name[0]);

        if (pluginFactory != null) {
            InstruViewPlugin instruViewPlugin = pluginFactory.create(instruView);
        } else {
            System.err.println("Unable to load \"" + name[0] + "\" plugin, no factory found...");
        }
    }

    // Method to say something about the program, who wrote it and so forth
    private void showAboutDialog() {
        String[] message = {" ", "Version " + VERSION_DATE, "By", "Nathan Stevens",
            "nathan.stevens@instras.com", " "};
        AboutDialog aboutDialog = new AboutDialog(instruView, "About", message);
    }

    // Method to update the plot properties
    private void updatePlotProperties() {
        if (logPlotMI.isSelected()) {
            instruView.plotProperties.setProperty("log", "yes");
        } else {
            instruView.plotProperties.setProperty("log", "no");
        }

        if (aaPlotMI.isSelected()) {
            instruView.plotProperties.setProperty("antialias", "yes");
        } else {
            instruView.plotProperties.setProperty("antialias", "no");
        }

        if (chPlotMI.isSelected()) {
            instruView.plotProperties.setProperty("crosshair", "yes");
        } else {
            instruView.plotProperties.setProperty("crosshair", "no");
        }
    }

    // Method to show the help dialog
    private void showHelpDialog() {
        if (helpViewer == null) {
            helpViewer = new HelpViewer(instruView);
            helpViewer.setSize(500, 450);
        }
        helpViewer.setVisible(true);
    }

    // Method to exit the program
    private void exit() {
        if (checkAllSaved()) {
            setVisible(false);
            System.exit(0);
        }
    }

    // Method to close the window
    private void closeWindow() {
        if (checkAllSaved()) {
            setVisible(false);
            if (InstruViewFrame.OPEN_WINDOWS == 1) {
                System.exit(1);
            } else {
                dispose();
                InstruViewFrame.OPEN_WINDOWS--;
            }
        }
    }

    // Method to show the error dialog
    public void showErrorDialog(String title, String message) {
        JOptionPane.showMessageDialog(this, message, title,
                JOptionPane.ERROR_MESSAGE);
    }
} // --end class InstruViewFrame
