/*
 * Copyright 2009, 2010 Lari Natri <lari.natri@gmail.com>
 *
 *  This program 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 3 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.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


package compid.gui;

import net.aavalla.utils.jobpool.parameters.gui.ParameterDialog;
import compid.application.CompidApp;
import compid.application.DatasetDeleter;
import compid.application.DatasetsChangedListener;
import compid.application.DatasetsHandler;
import compid.tools.FASTACompactor;
import compid.importers.FASTAImporter;
import compid.tools.FASTARandomizer;
import compid.importers.MascotDatImporter;
import compid.importers.ParagonExcelImporter;
import compid.importers.ParagonXmlImporter;
import compid.report.CommonPeptidesReporter;
import compid.report.GeneralStatsReporter;
import compid.report.ManualQueryReporter;
import compid.report.ReportModel;
import compid.report.protgroup.ProtGroupReporter;
import net.aavalla.utils.jobpool.JobResultEvent;
import net.aavalla.utils.jobpool.JobFinishedListener;

import net.aavalla.utils.general.BareBonesBrowserLaunch;
import net.aavalla.utils.gui.docbrowser.SimpleDocBrowser;
import net.aavalla.utils.gui.twovenn.VennDiagram;
import java.awt.Image;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.URL;
import java.util.EventObject;
import org.jdesktop.application.Action;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.ToolTipManager;
import org.jdesktop.application.Application.ExitListener;

/**
 * Compid's main frame
 * @author Lari Natri
 */
public class CompidView extends FrameView 
                        implements JobFinishedListener, 
                                   DatasetsChangedListener
{

    public final Image applicationIcon;
    private final SimpleDocBrowser helpBrowser;


    public CompidView(SingleFrameApplication app) {
        super(app);
        initComponents();

        URL url = this.getClass().getResource("/doc/help"); //NOI18N
        this.helpBrowser = new SimpleDocBrowser(url,"index.xml",true,"Help Browser - Compid");
        ParameterDialog.setBrowser(this.helpBrowser);

        ToolTipManager.sharedInstance().setDismissDelay(TOOLTIPDURATION_MS);

        // close
        final JFrame mainFrame = this.getFrame(); // can't use Application.getApplication().getMainFrame(), coz main frame is not yet constructed!
        mainFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        mainFrame.addWindowListener(new WindowAdapter() {
            @Override public void windowClosing(WindowEvent e) {
                CompidApp.getApplication().exit(e);
            }
        });
        CompidApp.getApplication().addExitListener(new ExitListener() {
            @Override public boolean canExit(EventObject e) {
                int unfinishedJobs = CompidApp.getApplication().getJobPool().getUnfinishedRootJobsCount();
                boolean dbInitialized = CompidApp.getApplication().isDatabaseInitialized();
                if ((!dbInitialized || unfinishedJobs==0) && CompidView.this.mainTabbedPane.getTabCount()<2) {
                    return true;
                } else {
                    String exitStr = "Do you really want to exit?";
                    if (unfinishedJobs>0) {
                        exitStr+="\nThere are "+unfinishedJobs+" unfinished jobs active.";
                    }
                    boolean bOkToExit = JOptionPane.showConfirmDialog(CompidView.this.getFrame(),
                        exitStr, "Really exit?", JOptionPane.YES_NO_OPTION) ==
                        JOptionPane.YES_OPTION;
                    return bOkToExit;
                }
            }
            @Override public void willExit(EventObject event) {

            }
        });


        // set icon
        this.applicationIcon = CompidApp.getInstance().getContext().getResourceMap(CompidView.class).getImageIcon("application.icon").getImage();
        mainFrame.setIconImage(this.applicationIcon); // NOI18N

        this.reportMenu.setVisible(false);

        this.importDatasetMenu.setEnabled(false);
        this.sequencesMenuItem.setEnabled(false);
        this.createReportMenu.setEnabled(false);
        this.deleteMenuItem.setEnabled(false);

        DatasetsHandler dh = CompidApp.getApplication().getDatasetsHandler();
        dh.addDatasetsChangedListenerSwing(this);
        datasetsTable.setTooltipBreakSymbol("; "); //NOI18N
        datasetsTable.setModel(dh.getAsReportModel());

        helpBrowser.showPageById("intro"); //NOI18N
        helpBrowser.expandAll();

    }

    @Action
    public void showAboutBox() {
        JFrame mainFrame = CompidApp.getApplication().getMainFrame();
        JDialog aboutBox = new CompidAboutBox(mainFrame);
        aboutBox.setLocationRelativeTo(mainFrame);
        CompidApp.getApplication().show(aboutBox);
    }

    @Action public void showHelp() {

        this.helpBrowser.setVisible(true);
    }


    @Override
    public void datasetsChanged(DatasetsHandler source) {
        this.datasetsTable.setModel(source.getAsReportModel());
        boolean connected = source.isConnected();
        int rowCount = this.datasetsTable.getModel().getRowCount();
        if (connected) {
            this.connStatusLabel.setText("Using database: "+source.getUrl()+" ("+this.datasetsTable.getRowCount()+" datasets)");
        } else {
            this.connStatusLabel.setText("Not connected to a database");
        }
        this.importDatasetMenu.setEnabled(connected);
        this.sequencesMenuItem.setEnabled(connected);
        this.createReportMenu.setEnabled(connected && rowCount>0);
        this.deleteMenuItem.setEnabled(connected && rowCount>0);
    }


    @Override
    public void jobFinished(final JobResultEvent event) {
        if (event.cancelled()) {
            return;
        }
        if (event.failed()) {
            Throwable error = (Throwable) event.getFailReason();
            String message;
            if (error instanceof OutOfMemoryError) { //NOI18N
                message = "Out of memory (heap).\n" +
                        "You might want to increase memory available by \n" +
                        "editing Compid startup script and increasing \n" +
                        "the value of  -Xmx parameter found therein.";
            } else {
                message = error.getMessage();
            }
            error.printStackTrace();
            JOptionPane.showMessageDialog(CompidApp.getApplication().getMainFrame(),
                event.getSource().getName() + " failed with error: \n" + message,
                "A Job has failed",
                 JOptionPane.ERROR_MESSAGE);
        } else {
            Object result = event.getResult();
            if (result instanceof ReportModel) {
                ReportModel report = (ReportModel)result;
                ReportViewPanel rvp = new ReportViewPanel(report);
                mainTabbedPane.addTab(report.getTitle(),rvp);
                mainTabbedPane.setTabComponentAt(mainTabbedPane.getTabCount()-1, new ClosableTabComponent(mainTabbedPane));
            } else if (result instanceof String) {
                JOptionPane.showMessageDialog(CompidApp.getApplication().getMainFrame(),
                    result.toString(), event.getSource().getName()+" has finished",
                    JOptionPane.INFORMATION_MESSAGE);
            }
        }

    }

    


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        mainTabbedPane = new javax.swing.JTabbedPane();
        datasetsPanel = new javax.swing.JPanel();
        datasetsToolbar = new javax.swing.JToolBar();
        connStatusLabel = new javax.swing.JLabel();
        datasetsScrollPane = new javax.swing.JScrollPane();
        datasetsTable = new compid.gui.CompidTable();
        statusPanel1 = new compid.gui.StatusPanel(CompidApp.getInstance().getContext().getResourceMap(CompidView.class).getImageIcon("application.icon").getImage());
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu operationsMenu = new javax.swing.JMenu();
        importDatasetMenu = new javax.swing.JMenu();
        importMascotDatMenuItem = new javax.swing.JMenuItem();
        importParagonXmlMenuItem = new javax.swing.JMenuItem();
        importParagonExcelMenuItem = new javax.swing.JMenuItem();
        sequencesMenuItem = new javax.swing.JMenuItem();
        createReportMenu = new javax.swing.JMenu();
        proteinGroupsMenuItem = new javax.swing.JMenuItem();
        commonPeptidesMenuItem = new javax.swing.JMenuItem();
        generalStatsMenuItem = new javax.swing.JMenuItem();
        manualQueryMenuItem = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JSeparator();
        deleteMenuItem = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JSeparator();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        reportMenu = new javax.swing.JMenu();
        showReportParametersMenuItem = new javax.swing.JMenuItem();
        exportReportMenu = new javax.swing.JMenu();
        exportToExcelMenuItem = new javax.swing.JMenuItem();
        toolsMenu = new javax.swing.JMenu();
        decoyMenuItem = new javax.swing.JMenuItem();
        compactMenuItem = new javax.swing.JMenuItem();
        vennCreatorMenuItem = new javax.swing.JMenuItem();
        jSeparator3 = new javax.swing.JPopupMenu.Separator();
        viewJobsMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        helpMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
        visitWebsiteMenuItem = new javax.swing.JMenuItem();

        mainPanel.setName("mainPanel"); // NOI18N

        mainTabbedPane.setName("mainTabbedPane"); // NOI18N
        mainTabbedPane.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                mainTabbedPaneStateChanged(evt);
            }
        });

        datasetsPanel.setName("datasetsPanel"); // NOI18N

        datasetsToolbar.setFloatable(false);
        datasetsToolbar.setRollover(true);
        datasetsToolbar.setName("datasetsToolbar"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(compid.application.CompidApp.class).getContext().getResourceMap(CompidView.class);
        connStatusLabel.setText(resourceMap.getString("connStatusLabel.text")); // NOI18N
        connStatusLabel.setName("connStatusLabel"); // NOI18N
        datasetsToolbar.add(connStatusLabel);

        datasetsScrollPane.setName("datasetsScrollPane"); // NOI18N

        datasetsTable.setAutoCreateRowSorter(true);
        datasetsTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        datasetsTable.setToolTipText(resourceMap.getString("datasetsTable.toolTipText")); // NOI18N
        datasetsTable.setName("datasetsTable"); // NOI18N
        datasetsScrollPane.setViewportView(datasetsTable);

        javax.swing.GroupLayout datasetsPanelLayout = new javax.swing.GroupLayout(datasetsPanel);
        datasetsPanel.setLayout(datasetsPanelLayout);
        datasetsPanelLayout.setHorizontalGroup(
            datasetsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(datasetsScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 770, Short.MAX_VALUE)
            .addGroup(datasetsPanelLayout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addComponent(datasetsToolbar, javax.swing.GroupLayout.DEFAULT_SIZE, 746, Short.MAX_VALUE)
                .addContainerGap())
        );
        datasetsPanelLayout.setVerticalGroup(
            datasetsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(datasetsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(datasetsToolbar, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(datasetsScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 355, Short.MAX_VALUE))
        );

        mainTabbedPane.addTab(resourceMap.getString("datasetsPanel.TabConstraints.tabTitle"), datasetsPanel); // NOI18N

        statusPanel1.setName("statusPanel1"); // NOI18N

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(mainTabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 775, Short.MAX_VALUE)
            .addComponent(statusPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 775, Short.MAX_VALUE)
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                .addComponent(mainTabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 427, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(statusPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        menuBar.setName("menuBar"); // NOI18N

        operationsMenu.setMnemonic('O');
        operationsMenu.setText(resourceMap.getString("operationsMenu.text")); // NOI18N
        operationsMenu.setName("operationsMenu"); // NOI18N

        importDatasetMenu.setIcon(resourceMap.getIcon("importDatasetMenu.icon")); // NOI18N
        importDatasetMenu.setMnemonic('1');
        importDatasetMenu.setText(resourceMap.getString("importDatasetMenu.text")); // NOI18N
        importDatasetMenu.setName("importDatasetMenu"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(compid.application.CompidApp.class).getContext().getActionMap(CompidView.class, this);
        importMascotDatMenuItem.setAction(actionMap.get("importDataset")); // NOI18N
        importMascotDatMenuItem.setText(resourceMap.getString("importMascotDatMenuItem.text")); // NOI18N
        importMascotDatMenuItem.setName("importMascotDatMenuItem"); // NOI18N
        importMascotDatMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                importMascotDatMenuItemActionPerformed(evt);
            }
        });
        importDatasetMenu.add(importMascotDatMenuItem);

        importParagonXmlMenuItem.setMnemonic('P');
        importParagonXmlMenuItem.setText(resourceMap.getString("importParagonXmlMenuItem.text")); // NOI18N
        importParagonXmlMenuItem.setName("importParagonXmlMenuItem"); // NOI18N
        importParagonXmlMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                importParagonXmlMenuItemActionPerformed(evt);
            }
        });
        importDatasetMenu.add(importParagonXmlMenuItem);

        importParagonExcelMenuItem.setText(resourceMap.getString("importParagonExcelMenuItem.text")); // NOI18N
        importParagonExcelMenuItem.setName("importParagonExcelMenuItem"); // NOI18N
        importParagonExcelMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                importParagonExcelMenuItemActionPerformed(evt);
            }
        });
        importDatasetMenu.add(importParagonExcelMenuItem);

        operationsMenu.add(importDatasetMenu);

        sequencesMenuItem.setIcon(resourceMap.getIcon("sequencesMenuItem.icon")); // NOI18N
        sequencesMenuItem.setMnemonic('2');
        sequencesMenuItem.setText(resourceMap.getString("sequencesMenuItem.text")); // NOI18N
        sequencesMenuItem.setToolTipText(resourceMap.getString("sequencesMenuItem.toolTipText")); // NOI18N
        sequencesMenuItem.setName("sequencesMenuItem"); // NOI18N
        sequencesMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sequencesMenuItemActionPerformed(evt);
            }
        });
        operationsMenu.add(sequencesMenuItem);

        createReportMenu.setIcon(resourceMap.getIcon("createReportMenu.icon")); // NOI18N
        createReportMenu.setMnemonic('3');
        createReportMenu.setText(resourceMap.getString("createReportMenu.text")); // NOI18N
        createReportMenu.setName("createReportMenu"); // NOI18N

        proteinGroupsMenuItem.setText(resourceMap.getString("proteinGroupsMenuItem.text")); // NOI18N
        proteinGroupsMenuItem.setName("proteinGroupsMenuItem"); // NOI18N
        proteinGroupsMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                proteinGroupsMenuItemActionPerformed(evt);
            }
        });
        createReportMenu.add(proteinGroupsMenuItem);

        commonPeptidesMenuItem.setText(resourceMap.getString("commonPeptidesMenuItem.text")); // NOI18N
        commonPeptidesMenuItem.setName("commonPeptidesMenuItem"); // NOI18N
        commonPeptidesMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                commonPeptidesMenuItemActionPerformed(evt);
            }
        });
        createReportMenu.add(commonPeptidesMenuItem);

        generalStatsMenuItem.setText(resourceMap.getString("generalStatsMenuItem.text")); // NOI18N
        generalStatsMenuItem.setName("generalStatsMenuItem"); // NOI18N
        generalStatsMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                generalStatsMenuItemActionPerformed(evt);
            }
        });
        createReportMenu.add(generalStatsMenuItem);

        manualQueryMenuItem.setText(resourceMap.getString("manualQueryMenuItem.text")); // NOI18N
        manualQueryMenuItem.setName("manualQueryMenuItem"); // NOI18N
        manualQueryMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                manualQueryMenuItemActionPerformed(evt);
            }
        });
        createReportMenu.add(manualQueryMenuItem);

        operationsMenu.add(createReportMenu);

        jSeparator1.setName("jSeparator1"); // NOI18N
        operationsMenu.add(jSeparator1);

        deleteMenuItem.setIcon(resourceMap.getIcon("deleteMenuItem.icon")); // NOI18N
        deleteMenuItem.setMnemonic('D');
        deleteMenuItem.setText(resourceMap.getString("deleteMenuItem.text")); // NOI18N
        deleteMenuItem.setName("deleteMenuItem"); // NOI18N
        deleteMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteMenuItemActionPerformed(evt);
            }
        });
        operationsMenu.add(deleteMenuItem);

        jSeparator2.setName("jSeparator2"); // NOI18N
        operationsMenu.add(jSeparator2);

        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setIcon(resourceMap.getIcon("exitMenuItem.icon")); // NOI18N
        exitMenuItem.setToolTipText(resourceMap.getString("exitMenuItem.toolTipText")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        operationsMenu.add(exitMenuItem);

        menuBar.add(operationsMenu);

        reportMenu.setMnemonic('R');
        reportMenu.setText(resourceMap.getString("reportMenu.text")); // NOI18N

        showReportParametersMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_P, java.awt.event.InputEvent.CTRL_MASK));
        showReportParametersMenuItem.setIcon(resourceMap.getIcon("showReportParametersMenuItem.icon")); // NOI18N
        showReportParametersMenuItem.setMnemonic('P');
        showReportParametersMenuItem.setText(resourceMap.getString("showReportParametersMenuItem.text")); // NOI18N
        showReportParametersMenuItem.setToolTipText(resourceMap.getString("showReportParametersMenuItem.toolTipText")); // NOI18N
        showReportParametersMenuItem.setName("showReportParametersMenuItem"); // NOI18N
        showReportParametersMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showReportParametersMenuItemActionPerformed(evt);
            }
        });
        reportMenu.add(showReportParametersMenuItem);

        exportReportMenu.setIcon(resourceMap.getIcon("exportReportMenu.icon")); // NOI18N
        exportReportMenu.setMnemonic('E');
        exportReportMenu.setText(resourceMap.getString("exportReportMenu.text")); // NOI18N
        exportReportMenu.setName("exportReportMenu"); // NOI18N

        exportToExcelMenuItem.setText(resourceMap.getString("exportToExcelMenuItem.text")); // NOI18N
        exportToExcelMenuItem.setName("exportToExcelMenuItem"); // NOI18N
        exportToExcelMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exportToExcelMenuItemActionPerformed(evt);
            }
        });
        exportReportMenu.add(exportToExcelMenuItem);

        reportMenu.add(exportReportMenu);

        menuBar.add(reportMenu);

        toolsMenu.setMnemonic('T');
        toolsMenu.setText(resourceMap.getString("toolsMenu.text")); // NOI18N
        toolsMenu.setName("toolsMenu"); // NOI18N

        decoyMenuItem.setIcon(resourceMap.getIcon("decoyMenuItem.icon")); // NOI18N
        decoyMenuItem.setMnemonic('D');
        decoyMenuItem.setText(resourceMap.getString("decoyMenuItem.text")); // NOI18N
        decoyMenuItem.setToolTipText(resourceMap.getString("decoyMenuItem.toolTipText")); // NOI18N
        decoyMenuItem.setName("decoyMenuItem"); // NOI18N
        decoyMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                decoyMenuItemActionPerformed(evt);
            }
        });
        toolsMenu.add(decoyMenuItem);

        compactMenuItem.setIcon(resourceMap.getIcon("compactMenuItem.icon")); // NOI18N
        compactMenuItem.setMnemonic('C');
        compactMenuItem.setText(resourceMap.getString("compactMenuItem.text")); // NOI18N
        compactMenuItem.setName("compactMenuItem"); // NOI18N
        compactMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                compactMenuItemActionPerformed(evt);
            }
        });
        toolsMenu.add(compactMenuItem);

        vennCreatorMenuItem.setIcon(resourceMap.getIcon("vennCreatorMenuItem.icon")); // NOI18N
        vennCreatorMenuItem.setMnemonic('V');
        vennCreatorMenuItem.setText(resourceMap.getString("vennCreatorMenuItem.text")); // NOI18N
        vennCreatorMenuItem.setToolTipText(resourceMap.getString("vennCreatorMenuItem.toolTipText")); // NOI18N
        vennCreatorMenuItem.setName("vennCreatorMenuItem"); // NOI18N
        vennCreatorMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                vennCreatorMenuItemActionPerformed(evt);
            }
        });
        toolsMenu.add(vennCreatorMenuItem);

        jSeparator3.setName("jSeparator3"); // NOI18N
        toolsMenu.add(jSeparator3);

        viewJobsMenuItem.setIcon(resourceMap.getIcon("viewJobsMenuItem.icon")); // NOI18N
        viewJobsMenuItem.setMnemonic('J');
        viewJobsMenuItem.setText(resourceMap.getString("viewJobsMenuItem.text")); // NOI18N
        viewJobsMenuItem.setToolTipText(resourceMap.getString("viewJobsMenuItem.toolTipText")); // NOI18N
        viewJobsMenuItem.setName("viewJobsMenuItem"); // NOI18N
        viewJobsMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                viewJobsMenuItemActionPerformed(evt);
            }
        });
        toolsMenu.add(viewJobsMenuItem);

        menuBar.add(toolsMenu);

        helpMenu.setMnemonic('H');
        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        helpMenuItem.setAction(actionMap.get("showHelp")); // NOI18N
        helpMenuItem.setIcon(resourceMap.getIcon("helpMenuItem.icon")); // NOI18N
        helpMenuItem.setText(resourceMap.getString("helpMenuItem.text")); // NOI18N
        helpMenuItem.setToolTipText(resourceMap.getString("helpMenuItem.toolTipText")); // NOI18N
        helpMenuItem.setName("helpMenuItem"); // NOI18N
        helpMenu.add(helpMenuItem);

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setIcon(resourceMap.getIcon("aboutMenuItem.icon")); // NOI18N
        aboutMenuItem.setText(resourceMap.getString("aboutMenuItem.text")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        visitWebsiteMenuItem.setIcon(resourceMap.getIcon("visitWebsiteMenuItem.icon")); // NOI18N
        visitWebsiteMenuItem.setMnemonic('w');
        visitWebsiteMenuItem.setText(resourceMap.getString("visitWebsiteMenuItem.text")); // NOI18N
        visitWebsiteMenuItem.setToolTipText(resourceMap.getString("visitWebsiteMenuItem.toolTipText")); // NOI18N
        visitWebsiteMenuItem.setName("visitWebsiteMenuItem"); // NOI18N
        visitWebsiteMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                visitWebsiteMenuItemActionPerformed(evt);
            }
        });
        helpMenu.add(visitWebsiteMenuItem);

        menuBar.add(helpMenu);

        setComponent(mainPanel);
        setMenuBar(menuBar);
    }// </editor-fold>//GEN-END:initComponents

    @Action
    private void importMascotDatMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_importMascotDatMenuItemActionPerformed
        if (CompidApp.getApplication().isMascotParserLoaded()==false) {
            String[] choices = {"Help", "Download", "Cancel" };
            String msParserUrl = CompidApp.getApplication().getHardResource().getString("Application.MascotParserURL"); //NOI18N
            int response = JOptionPane.showOptionDialog(CompidApp.getApplication().getMainFrame(),
                        "Can not parse Mascot .DAT files, because Mascot parser library \n" +
                        "is not available. Please download it from Matrix Science at \n" +
                        ""+msParserUrl+" and \n" +
                        "place it in a correct location in your file system (see help).",
                        "Mascot Parser is not available",
                         JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.ERROR_MESSAGE, null, choices, choices[0]);
            switch (response) {
                case 0:
                    this.helpBrowser.showPageById("import"); //NOI18N
                    this.helpBrowser.setVisible(true);
                    break;
                case 1:
                    try {
                        BareBonesBrowserLaunch.openURL(msParserUrl); //NOI18N
                    } catch (RuntimeException ex) {
                        JOptionPane.showMessageDialog(CompidApp.getApplication().getMainFrame(), ex.getMessage(),
                            "Could not launch external browser",
                            JOptionPane.ERROR_MESSAGE);
                    }
                    break;
                case 2:
                default:
                    return;
            }
            return;
        }

        MascotDatImporter imp = new MascotDatImporter();
        imp.addJobFinishedListenerSwing(this);
        ParameterDialog dialog = new ParameterDialog(CompidApp.getApplication().getMainFrame(), false, imp, CompidApp.getApplication().getJobPool(),applicationIcon);
        dialog.setVisible(true);
    }//GEN-LAST:event_importMascotDatMenuItemActionPerformed

    private void importParagonExcelMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_importParagonExcelMenuItemActionPerformed
        ParagonExcelImporter imp = new ParagonExcelImporter();
        imp.addJobFinishedListenerSwing(this);
        ParameterDialog dialog = new ParameterDialog(CompidApp.getApplication().getMainFrame(), false, imp, CompidApp.getApplication().getJobPool(),applicationIcon);
        dialog.setVisible(true);
    }//GEN-LAST:event_importParagonExcelMenuItemActionPerformed

    private void sequencesMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sequencesMenuItemActionPerformed
        FASTAImporter imp = new FASTAImporter();
        imp.addJobFinishedListenerSwing(this);
        ParameterDialog dialog = new ParameterDialog(CompidApp.getApplication().getMainFrame(), false, imp, CompidApp.getApplication().getJobPool(),applicationIcon);
        dialog.setVisible(true);
    }//GEN-LAST:event_sequencesMenuItemActionPerformed

    private void decoyMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_decoyMenuItemActionPerformed
        FASTARandomizer rand = new FASTARandomizer();
        rand.addJobFinishedListenerSwing(this);
        ParameterDialog dialog = new ParameterDialog(CompidApp.getApplication().getMainFrame(), false, rand, CompidApp.getApplication().getJobPool(),applicationIcon);
        dialog.setVisible(true);
    }//GEN-LAST:event_decoyMenuItemActionPerformed
    

    private void commonPeptidesMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_commonPeptidesMenuItemActionPerformed
        CommonPeptidesReporter rep = new CommonPeptidesReporter();
        rep.addJobFinishedListenerSwing(this);
        ParameterDialog dialog = new ParameterDialog(CompidApp.getApplication().getMainFrame(), false,rep, CompidApp.getApplication().getJobPool(),applicationIcon);
        dialog.setVisible(true);
    }//GEN-LAST:event_commonPeptidesMenuItemActionPerformed

    private void mainTabbedPaneStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_mainTabbedPaneStateChanged
        if (mainTabbedPane.getSelectedComponent() instanceof ReportViewPanel) {
            this.reportMenu.setVisible(true);
        } else {
            this.reportMenu.setVisible(false);
        }
    }//GEN-LAST:event_mainTabbedPaneStateChanged

    private void showReportParametersMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showReportParametersMenuItemActionPerformed
        if (!(mainTabbedPane.getSelectedComponent() instanceof ReportViewPanel)) {
            return;
        }
        ReportViewPanel rvp = (ReportViewPanel)mainTabbedPane.getSelectedComponent();
        rvp.showParameters();
    }//GEN-LAST:event_showReportParametersMenuItemActionPerformed

    private void exportToExcelMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exportToExcelMenuItemActionPerformed
        if (!(mainTabbedPane.getSelectedComponent() instanceof ReportViewPanel)) {
            return;
        }
        ReportViewPanel rvp = (ReportViewPanel)mainTabbedPane.getSelectedComponent();
        rvp.exportToExcel();
    }//GEN-LAST:event_exportToExcelMenuItemActionPerformed

    private void generalStatsMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_generalStatsMenuItemActionPerformed
        GeneralStatsReporter rep = new GeneralStatsReporter();
        rep.addJobFinishedListenerSwing(this);
        ParameterDialog dialog = new ParameterDialog(CompidApp.getApplication().getMainFrame(), false, rep, CompidApp.getApplication().getJobPool(),applicationIcon);
        dialog.setVisible(true);
    }//GEN-LAST:event_generalStatsMenuItemActionPerformed

    private void compactMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_compactMenuItemActionPerformed
        FASTACompactor comp = new FASTACompactor();
        comp.addJobFinishedListenerSwing(this);
        ParameterDialog dialog = new ParameterDialog(CompidApp.getApplication().getMainFrame(), false, comp, CompidApp.getApplication().getJobPool(),applicationIcon);
        dialog.setVisible(true);
    }//GEN-LAST:event_compactMenuItemActionPerformed

    private void deleteMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteMenuItemActionPerformed
        DatasetDeleter del = new DatasetDeleter();
        del.addJobFinishedListenerSwing(this);
        ParameterDialog dialog = new ParameterDialog(CompidApp.getApplication().getMainFrame(),false,del,CompidApp.getApplication().getJobPool(),applicationIcon);
        dialog.setVisible(true);
    }//GEN-LAST:event_deleteMenuItemActionPerformed

    private void manualQueryMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_manualQueryMenuItemActionPerformed
        ManualQueryReporter mqr = new ManualQueryReporter();
        mqr.addJobFinishedListenerSwing(this);
        ParameterDialog dialog = new ParameterDialog(CompidApp.getApplication().getMainFrame(),false,mqr,CompidApp.getApplication().getJobPool(),applicationIcon);
        dialog.setVisible(true);
    }//GEN-LAST:event_manualQueryMenuItemActionPerformed

    private void visitWebsiteMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_visitWebsiteMenuItemActionPerformed
        try {
            String websiteUrl = CompidApp.getApplication().getSoftProperties().getProperty("website.url"); // NOI18N
            BareBonesBrowserLaunch.openURL(websiteUrl);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(CompidApp.getApplication().getMainFrame(),
                ex.getMessage(), "Error starting browser", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_visitWebsiteMenuItemActionPerformed

    private void importParagonXmlMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_importParagonXmlMenuItemActionPerformed
        ParagonXmlImporter imp = new ParagonXmlImporter();
        imp.addJobFinishedListenerSwing(this);
        ParameterDialog dialog = new ParameterDialog(CompidApp.getApplication().getMainFrame(), false, imp, CompidApp.getApplication().getJobPool(),applicationIcon);
        dialog.setVisible(true);
    }//GEN-LAST:event_importParagonXmlMenuItemActionPerformed

    private void proteinGroupsMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_proteinGroupsMenuItemActionPerformed
        ProtGroupReporter rep = new ProtGroupReporter();
        rep.addJobFinishedListenerSwing(this);
        ParameterDialog dialog = new ParameterDialog(CompidApp.getApplication().getMainFrame(), false, rep, CompidApp.getApplication().getJobPool(),applicationIcon);
        dialog.setVisible(true);
    }//GEN-LAST:event_proteinGroupsMenuItemActionPerformed

    private void vennCreatorMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_vennCreatorMenuItemActionPerformed

        VennDiagram venn = new VennDiagram(CompidApp.getApplication().getJobPool(),true);
        JFrame f = new JFrame();
        f.setTitle("Venn Diagram Creator");
        f.setLocationRelativeTo(CompidApp.getApplication().getMainFrame());
        f.setIconImage(this.applicationIcon);
        f.add(venn);
        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        f.setSize(600,500);
        f.setVisible(true);

    }//GEN-LAST:event_vennCreatorMenuItemActionPerformed

    private void viewJobsMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_viewJobsMenuItemActionPerformed
        this.statusPanel1.viewJobDialog();
    }//GEN-LAST:event_viewJobsMenuItemActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem commonPeptidesMenuItem;
    private javax.swing.JMenuItem compactMenuItem;
    private javax.swing.JLabel connStatusLabel;
    private javax.swing.JMenu createReportMenu;
    private javax.swing.JPanel datasetsPanel;
    private javax.swing.JScrollPane datasetsScrollPane;
    private compid.gui.CompidTable datasetsTable;
    private javax.swing.JToolBar datasetsToolbar;
    private javax.swing.JMenuItem decoyMenuItem;
    private javax.swing.JMenuItem deleteMenuItem;
    private javax.swing.JMenu exportReportMenu;
    private javax.swing.JMenuItem exportToExcelMenuItem;
    private javax.swing.JMenuItem generalStatsMenuItem;
    private javax.swing.JMenuItem helpMenuItem;
    private javax.swing.JMenu importDatasetMenu;
    private javax.swing.JMenuItem importMascotDatMenuItem;
    private javax.swing.JMenuItem importParagonExcelMenuItem;
    private javax.swing.JMenuItem importParagonXmlMenuItem;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JPopupMenu.Separator jSeparator3;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JTabbedPane mainTabbedPane;
    private javax.swing.JMenuItem manualQueryMenuItem;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem proteinGroupsMenuItem;
    private javax.swing.JMenu reportMenu;
    private javax.swing.JMenuItem sequencesMenuItem;
    private javax.swing.JMenuItem showReportParametersMenuItem;
    private compid.gui.StatusPanel statusPanel1;
    private javax.swing.JMenu toolsMenu;
    private javax.swing.JMenuItem vennCreatorMenuItem;
    private javax.swing.JMenuItem viewJobsMenuItem;
    private javax.swing.JMenuItem visitWebsiteMenuItem;
    // End of variables declaration//GEN-END:variables

    private static final int TOOLTIPDURATION_MS = 15000;

}
