com.sciaps.view.SpectrometerStackPanel.java Source code

Java tutorial

Introduction

Here is the source code for com.sciaps.view.SpectrometerStackPanel.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sciaps.view;

import Interface.RegionFinderIntf;
import com.devsmart.ThreadUtils;
import com.sciaps.common.SpectrumShotItem;
import com.sciaps.common.Constants;
import static com.sciaps.common.Constants.MAX_SPECTROMETER;
import com.sciaps.common.RegionMarkerItem;
import com.sciaps.common.algorithms.DarkPixSubtract;
import com.sciaps.common.data.Region;
import com.sciaps.common.spectrum.LIBZPixelSpectrum;
import com.sciaps.common.spectrum.Spectrum;
import com.sciaps.common.swing.view.JFreeChartWrapperPanel;
import com.sciaps.common.webserver.ILaserController.RasterParams;
import com.sciaps.listener.JFreeChartMouseListener;
import com.sciaps.utils.CustomDialog;
import static com.sciaps.utils.Util.createAverage;
import static com.sciaps.utils.Util.populateXYSeriesData;
import java.awt.Color;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.FileNameExtensionFilter;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.IntervalMarker;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYSplineRenderer;
import org.jfree.data.xy.XYSeriesCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author jchen
 */
public class SpectrometerStackPanel extends javax.swing.JPanel implements RegionFinderIntf {

    private final Logger logger_ = LoggerFactory.getLogger(SpectrometerStackPanel.class);

    private final XYSeriesCollection xySeriesCollection_;
    private final JFreeChartWrapperPanel jFreeChartPanel_;
    private final XYPlot plot_;
    private final JFreeChartMouseListener chartMouseListener_;
    private final RegionsPanel regionPanels_;
    private final SpectrumShotPanel shotCheckListPanel_;
    private final SpecialRasterPanel specialRasterPanel_;
    private final PlotConfigPanel plotSettingPanel_;
    private final SpectrumAnalysisReportPanel spectrumAnalysisReportPanel_;
    private int scanCount_ = 0;
    private boolean darkPixelSubtractionOn_ = true;

    /**
     * Creates new form SpecktrometerStackPanel
     */
    public SpectrometerStackPanel() {
        initComponents();

        regionPanels_ = new RegionsPanel(this);
        regionContainerPanel_.add(regionPanels_);

        specialRasterPanel_ = new SpecialRasterPanel();
        rasterSettingPanel_.add(specialRasterPanel_);

        xySeriesCollection_ = new XYSeriesCollection();
        jFreeChartPanel_ = new JFreeChartWrapperPanel();
        jFreeChartPanel_.populateSpectrumChartWithAbstractXYDataset(xySeriesCollection_, "Spectrum",
                "Wave Length(nm)", "Intensity");
        charDisplayPanel_.add(jFreeChartPanel_);

        chartMouseListener_ = new JFreeChartMouseListener(jFreeChartPanel_.getJFreeChart(),
                jFreeChartPanel_.getChartPanel(), regionPanels_);
        jFreeChartPanel_.getChartPanel().addChartMouseListener(chartMouseListener_);

        plot_ = jFreeChartPanel_.getJFreeChart().getXYPlot();
        XYSplineRenderer renderer = new XYSplineRenderer(5);

        StandardXYToolTipGenerator ttG = new StandardXYToolTipGenerator("{1}, {2}", new DecimalFormat("#0.00"),
                new DecimalFormat("#0.00"));
        renderer.setBaseToolTipGenerator(ttG);

        plot_.setRenderer(renderer);
        plot_.setBackgroundPaint(Color.WHITE);
        plot_.setDomainPannable(true);
        plot_.setRangePannable(true);

        plotSettingPanel_ = new PlotConfigPanel(plot_);
        chartRangeControlPanel_.add(plotSettingPanel_);

        shotCheckListPanel_ = new SpectrumShotPanel(this);
        shotListContainerPanel_.add(shotCheckListPanel_);

        spectrumAnalysisReportPanel_ = new SpectrumAnalysisReportPanel(this);
        analysisPanel_.add(spectrumAnalysisReportPanel_);

        // disable them by default
        shotListContainerPanel_.setVisible(false);
        rasterSettingPanel_.setVisible(false);
        regionContainerPanel_.setVisible(false);
        analysisPanel_.setVisible(false);
        jPanel6.setVisible(false);
    }

    /**
     * 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() {
        java.awt.GridBagConstraints gridBagConstraints;

        regionContainerPanel_ = new javax.swing.JPanel();
        controlPanel_ = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        toggleShotList_ = new javax.swing.JToggleButton();
        jLabel2 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        toggleRasterSetting_ = new javax.swing.JToggleButton();
        jLabel3 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        toggleRegion_ = new javax.swing.JToggleButton();
        jLabel4 = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        btnScan_ = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jPanel6 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        btnCloseAnalyzePanel_ = new javax.swing.JButton();
        rasterSettingPanel_ = new javax.swing.JPanel();
        shotListContainerPanel_ = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        charDisplayPanel_ = new javax.swing.JPanel();
        chartRangeControlPanel_ = new javax.swing.JPanel();
        analysisPanel_ = new javax.swing.JPanel();

        setName(""); // NOI18N
        setLayout(new java.awt.GridBagLayout());

        regionContainerPanel_.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        regionContainerPanel_.setLayout(new java.awt.BorderLayout());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        add(regionContainerPanel_, gridBagConstraints);

        controlPanel_.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        controlPanel_.setLayout(new java.awt.GridBagLayout());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        controlPanel_.add(jLabel1, gridBagConstraints);

        jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel1.setLayout(new java.awt.GridBagLayout());

        toggleShotList_.setText("Show");
        toggleShotList_.setToolTipText("Show/Hide Shot List Panel");
        toggleShotList_.setMaximumSize(new java.awt.Dimension(120, 30));
        toggleShotList_.setMinimumSize(new java.awt.Dimension(120, 30));
        toggleShotList_.setPreferredSize(new java.awt.Dimension(120, 30));
        toggleShotList_.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                toggleShotList_ActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        jPanel1.add(toggleShotList_, gridBagConstraints);

        jLabel2.setText("Shot List");
        jPanel1.add(jLabel2, new java.awt.GridBagConstraints());

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        controlPanel_.add(jPanel1, gridBagConstraints);

        jPanel2.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel2.setLayout(new java.awt.GridBagLayout());

        toggleRasterSetting_.setText("Show");
        toggleRasterSetting_.setToolTipText("Show/Hide Raster Setting Panel");
        toggleRasterSetting_.setMaximumSize(new java.awt.Dimension(120, 30));
        toggleRasterSetting_.setMinimumSize(new java.awt.Dimension(120, 30));
        toggleRasterSetting_.setPreferredSize(new java.awt.Dimension(120, 30));
        toggleRasterSetting_.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                toggleRasterSetting_ActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        jPanel2.add(toggleRasterSetting_, gridBagConstraints);

        jLabel3.setText("Raster Setting");
        jPanel2.add(jLabel3, new java.awt.GridBagConstraints());

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 0;
        controlPanel_.add(jPanel2, gridBagConstraints);

        jPanel3.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel3.setLayout(new java.awt.GridBagLayout());

        toggleRegion_.setText("Show");
        toggleRegion_.setToolTipText("Show/Hide Region Panel");
        toggleRegion_.setMaximumSize(new java.awt.Dimension(120, 30));
        toggleRegion_.setMinimumSize(new java.awt.Dimension(120, 30));
        toggleRegion_.setPreferredSize(new java.awt.Dimension(120, 30));
        toggleRegion_.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                toggleRegion_ActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        jPanel3.add(toggleRegion_, gridBagConstraints);

        jLabel4.setText("Region List");
        jPanel3.add(jLabel4, new java.awt.GridBagConstraints());

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        controlPanel_.add(jPanel3, gridBagConstraints);

        jPanel4.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel4.setLayout(new java.awt.GridBagLayout());

        btnScan_.setBackground(new java.awt.Color(0, 255, 51));
        btnScan_.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        btnScan_.setText("SCAN");
        btnScan_.setToolTipText("Start Raster Test");
        btnScan_.setMaximumSize(new java.awt.Dimension(120, 30));
        btnScan_.setMinimumSize(new java.awt.Dimension(120, 30));
        btnScan_.setPreferredSize(new java.awt.Dimension(120, 30));
        btnScan_.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnScan_ActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        jPanel4.add(btnScan_, gridBagConstraints);

        jLabel5.setText("Raster Test");
        jPanel4.add(jLabel5, new java.awt.GridBagConstraints());

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 0;
        controlPanel_.add(jPanel4, gridBagConstraints);

        jPanel6.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel6.setLayout(new java.awt.GridBagLayout());

        jLabel6.setText("Analyze");
        jPanel6.add(jLabel6, new java.awt.GridBagConstraints());

        btnCloseAnalyzePanel_.setText("Close");
        btnCloseAnalyzePanel_.setMaximumSize(new java.awt.Dimension(120, 30));
        btnCloseAnalyzePanel_.setMinimumSize(new java.awt.Dimension(120, 30));
        btnCloseAnalyzePanel_.setPreferredSize(new java.awt.Dimension(120, 30));
        btnCloseAnalyzePanel_.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCloseAnalyzePanel_ActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        jPanel6.add(btnCloseAnalyzePanel_, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        controlPanel_.add(jPanel6, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.PAGE_START;
        gridBagConstraints.weightx = 1.0;
        add(controlPanel_, gridBagConstraints);

        rasterSettingPanel_.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        rasterSettingPanel_.setLayout(new java.awt.BorderLayout());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        add(rasterSettingPanel_, gridBagConstraints);

        shotListContainerPanel_.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        shotListContainerPanel_.setLayout(new java.awt.BorderLayout());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.15;
        add(shotListContainerPanel_, gridBagConstraints);

        jPanel5.setLayout(new java.awt.GridBagLayout());

        charDisplayPanel_.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        charDisplayPanel_.setLayout(new java.awt.BorderLayout());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        jPanel5.add(charDisplayPanel_, gridBagConstraints);

        chartRangeControlPanel_.setMinimumSize(new java.awt.Dimension(300, 75));
        chartRangeControlPanel_.setPreferredSize(new java.awt.Dimension(300, 75));
        chartRangeControlPanel_.setLayout(new java.awt.BorderLayout());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        jPanel5.add(chartRangeControlPanel_, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        add(jPanel5, gridBagConstraints);

        analysisPanel_.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        analysisPanel_.setLayout(new java.awt.BorderLayout());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.4;
        add(analysisPanel_, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void btnScan_ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnScan_ActionPerformed
        prepareForRasterTest();
    }//GEN-LAST:event_btnScan_ActionPerformed

    private void toggleShotList_ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_toggleShotList_ActionPerformed
        setShotListPanelVisible(toggleShotList_.isSelected());
    }//GEN-LAST:event_toggleShotList_ActionPerformed

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

        setRasterSettingPanelVisible(toggleRasterSetting_.isSelected());
    }//GEN-LAST:event_toggleRasterSetting_ActionPerformed

    private void toggleRegion_ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_toggleRegion_ActionPerformed
        setRegionPanelVisible(toggleRegion_.isSelected());
    }//GEN-LAST:event_toggleRegion_ActionPerformed

    private void btnCloseAnalyzePanel_ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCloseAnalyzePanel_ActionPerformed
        setAnalysisPanelVisible(false);
    }//GEN-LAST:event_btnCloseAnalyzePanel_ActionPerformed

    private void setAnalysisPanelVisible(boolean val) {
        jPanel6.setVisible(val);
        analysisPanel_.setVisible(val);

        if (val == true) {
            jPanel1.setVisible(false);
            jPanel2.setVisible(false);
            jPanel3.setVisible(false);
            jPanel4.setVisible(false);

            setShotListPanelVisible(false);
            setRegionPanelVisible(false);
            setRasterSettingPanelVisible(false);
        } else {

            spectrumAnalysisReportPanel_.doCleanUp();

            jPanel1.setVisible(true);
            jPanel2.setVisible(true);
            jPanel3.setVisible(true);
            jPanel4.setVisible(true);

            setShotListPanelVisible(true);
        }
    }

    private void setRegionPanelVisible(boolean val) {
        regionContainerPanel_.setVisible(val);
        toggleRegion_.setSelected(val);
        if (val == true) {
            toggleRegion_.setText("Hide");
        } else {
            toggleRegion_.setText("Show");
        }
    }

    private void setRasterSettingPanelVisible(boolean val) {
        rasterSettingPanel_.setVisible(val);
        toggleRasterSetting_.setSelected(val);
        if (val == true) {
            toggleRasterSetting_.setText("Hide");
        } else {
            toggleRasterSetting_.setText("Show");
        }
    }

    private void setShotListPanelVisible(boolean val) {
        shotListContainerPanel_.setVisible(val);
        toggleShotList_.setSelected(val);
        if (val == true) {
            toggleShotList_.setText("Hide");
        } else {
            toggleShotList_.setText("Show");
        }
    }

    private void prepareForRasterTest() {

        final int sampleRate = shotCheckListPanel_.getSampleRate();
        if (sampleRate < 1) {
            showErrorDialog("Sample Rate can not be less than 1");
            return;
        }

        if (Constants.mHttpClient == null) {
            showErrorDialog("Can't find LIBZ IP Address.");
            return;
        }

        final RasterParams rasterData = specialRasterPanel_.getRasterData();

        if (rasterData != null) {

            setShotListPanelVisible(true);
            setRasterSettingPanelVisible(false);
            setRegionPanelVisible(false);

            ThreadUtils.IOThreads.execute(new Runnable() {

                @Override
                public void run() {
                    startRasterTest(rasterData, sampleRate);
                }
            });
        }
    }

    // This function is call from a in house IOThread
    private void startRasterTest(RasterParams rasterData, int sampleRate) {
        logger_.info("Starting a raster test");

        StringBuilder errMsg = new StringBuilder();

        ProgressStatusPanel progressbar = new ProgressStatusPanel();
        final CustomDialog progressDialog = new CustomDialog(Constants.MAIN_FRAME, "Raster Test In Progress",
                progressbar, CustomDialog.NONE_OPTION);
        progressDialog.setSize(400, 100);
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                progressDialog.setVisible(true);
            }
        });

        try {

            List<LIBZPixelSpectrum> shots = Constants.mHttpClient.rasterTest(rasterData);

            if (shots != null && !shots.isEmpty()) {

                scanCount_++;

                int shotCount = 1;
                final ArrayList<Spectrum> listOfSpectrums = new ArrayList<Spectrum>();
                final ArrayList<SpectrumShotItem> shotItems = new ArrayList<SpectrumShotItem>();
                for (LIBZPixelSpectrum shot : shots) {

                    if (darkPixelSubtractionOn_) {
                        for (int i = 0; i < MAX_SPECTROMETER; i++) {
                            DarkPixSubtract.doDarkPixSubtract(shot.pixels[i], 2066);
                        }
                    }

                    final SpectrumShotItem item = new SpectrumShotItem(scanCount_, shotCount);
                    item.setShot(shot.createSpectrum(), SpectrumShotItem.RAW);
                    shotItems.add(item);
                    shotCount++;

                    listOfSpectrums.add(item.getShot());
                }
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        shotCheckListPanel_.addItems(shotItems);
                    }
                });

                Spectrum avgSpectrum = createAverage(listOfSpectrums, sampleRate);
                String name = "Scan " + scanCount_ + ": Avg";
                final SpectrumShotItem avgShotItem = new SpectrumShotItem(name);
                avgShotItem.setShot(avgSpectrum, SpectrumShotItem.AVERAGED);
                populateXYSeriesData(avgShotItem);

                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        if (shotCheckListPanel_.addItem(0, avgShotItem)) {
                            avgShotItem.setSelected(true);
                            doShowShotXYSeries(avgShotItem);
                        }
                    }
                });

                logger_.info("Raster test completed.");

            } else {
                errMsg.append("Raster test failed, no data returned from Libz unit.");
            }

        } catch (Exception ex) {
            errMsg.append("Raster test failed: ");
            errMsg.append(ex.getMessage());
        }

        progressDialog.dispose();

        if (errMsg.length() != 0) {
            showErrorDialog(errMsg.toString());
        }
    }

    public void getRegionTextFile() {

        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setFileFilter(new FileNameExtensionFilter("Text files", "txt", "TXT"));
        int retval = fileChooser.showOpenDialog(Constants.MAIN_FRAME);

        if (retval == JFileChooser.APPROVE_OPTION) {
            File file = fileChooser.getSelectedFile();

            try {
                FileReader fr = new FileReader(file.getAbsoluteFile());
                BufferedReader br = new BufferedReader(fr);

                StringBuilder text = new StringBuilder();
                String line;
                while ((line = br.readLine()) != null) {
                    text.append(line);
                }

                br.close();

                createRegionItem(text.toString());

            } catch (FileNotFoundException ex) {
                showErrorDialog(ex.getMessage());
            } catch (IOException ex) {
                showErrorDialog(ex.getMessage());
            }

        }

    }

    public void getRegionTextFromUser() {

        String retval = JOptionPane.showInputDialog(Constants.MAIN_FRAME,
                "Enter region string (ex. Fe371.76-372.16,Co257.88-258.12):", "");

        if (retval != null && !retval.isEmpty()) {
            createRegionItem(retval);
        }
    }

    private void createRegionItem(String regionStr) {
        boolean iValide = true;
        StringBuilder errText = new StringBuilder();

        if (regionStr != null && !regionStr.isEmpty()) {
            String[] regions = regionStr.split(",");

            for (String region : regions) {

                try {
                    Region r = Region.parse(region);
                    if (r != null) {

                        RegionMarkerItem markerItem = new RegionMarkerItem();

                        markerItem.setName(r.name);
                        if (r.getElement() != null && r.getElement().symbol != null) {
                            markerItem.setSymbol(r.getElement().symbol);
                        } else {
                            markerItem.setSymbol("");
                        }
                        markerItem.setMin(r.wavelengthRange.getMinimumDouble());
                        markerItem.setMax(r.wavelengthRange.getMaximumDouble());
                        markerItem.setValue(0);

                        regionPanels_.addRow(markerItem);
                    }
                } catch (Exception ex) {
                    errText.append(region);
                    errText.append(" ");
                    iValide = false;
                }
            }

            if (iValide == false) {
                showErrorDialog("Input contains invalid data:\n" + errText);
            }
            setRegionPanelVisible(true);
        }
    }

    public void exportCSVAll() {
        shotCheckListPanel_.exportCSVAll();
    }

    public void exportCSVSelected() {
        shotCheckListPanel_.exportCSVSelected();
    }

    public void importCSV() {
        shotCheckListPanel_.importCSV();
        setShotListPanelVisible(true);
    }

    public void importJsonGzip() {
        shotCheckListPanel_.importJsonGzip();
        setShotListPanelVisible(true);
    }

    public void exportJsonGzipAll() {
        shotCheckListPanel_.exportJsonGzipAll();
    }

    public void exportJsonGzipSelected() {
        shotCheckListPanel_.exportJsonGzipSelected();
    }

    public void setDarkPixelSubtractionOnOff(boolean val) {
        darkPixelSubtractionOn_ = val;
    }

    public void showBaselineRemovalSettings() {
        shotCheckListPanel_.showBaselineRemovalSettings();
    }

    private void showErrorDialog(String msg) {
        logger_.error(msg);
        JOptionPane.showMessageDialog(Constants.MAIN_FRAME, msg, "ERROR", JOptionPane.ERROR_MESSAGE);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel analysisPanel_;
    private javax.swing.JButton btnCloseAnalyzePanel_;
    private javax.swing.JButton btnScan_;
    private javax.swing.JPanel charDisplayPanel_;
    private javax.swing.JPanel chartRangeControlPanel_;
    private javax.swing.JPanel controlPanel_;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel rasterSettingPanel_;
    private javax.swing.JPanel regionContainerPanel_;
    private javax.swing.JPanel shotListContainerPanel_;
    private javax.swing.JToggleButton toggleRasterSetting_;
    private javax.swing.JToggleButton toggleRegion_;
    private javax.swing.JToggleButton toggleShotList_;
    // End of variables declaration//GEN-END:variables

    @Override
    public void doShowShotXYSeries(com.sciaps.common.SpectrumShotItem item) {
        logger_.info("Displaying selected shot");

        int cnt = 0;
        for (int i = 0; i < xySeriesCollection_.getSeriesCount(); i++) {
            if (plot_.getRenderer().isSeriesVisible(i)) {
                cnt++;
            }
        }

        if (cnt == 0) {
            plot_.getDomainAxis().setAutoRange(true);
            plot_.getRangeAxis().setAutoRange(true);
        }

        if (item.getXYSeries().isEmpty()) {
            populateXYSeriesData(item);
        }

        try {
            int index = xySeriesCollection_.indexOf(item.getXYSeries());
            if (index < 0) {
                xySeriesCollection_.addSeries(item.getXYSeries());
                index = xySeriesCollection_.indexOf(item.getXYSeries());
                ((XYSplineRenderer) plot_.getRenderer()).setSeriesShapesVisible(index,
                        plotSettingPanel_.getLineShapeVisibility());
                plot_.getRenderer().setSeriesVisible(index, true);
            } else {
                plot_.getRenderer().setSeriesVisible(index, true);
            }
        } catch (Exception ex) {
            logger_.error("Failed to show XYSeries: " + ex.getMessage());
        }
    }

    @Override
    public void doHideShotXYSeries(com.sciaps.common.SpectrumShotItem item) {
        logger_.info("Hiding selected shot");
        try {
            int index = xySeriesCollection_.indexOf(item.getXYSeries());
            if (index >= 0) {
                plot_.getRenderer().setSeriesVisible(index, false);
            }
        } catch (Exception ex) {
            logger_.error("Failed to hide XYSeries: " + ex.getMessage());
        }
    }

    @Override
    public void doDeleteShotXYSeries(com.sciaps.common.SpectrumShotItem item) {
        try {
            xySeriesCollection_.removeSeries(item.getXYSeries());
        } catch (Exception ex) {
            logger_.error("Failed to delete XYSeries: " + ex.getMessage());
        }
    }

    @Override
    public void doAddMarker(IntervalMarker marker) {
        plot_.addDomainMarker(marker);

        if (xySeriesCollection_.getSeriesCount() > 0 && plotSettingPanel_.getMarkerAutoZoom()) {

            plot_.getDomainAxis().setAutoRange(true);
            plot_.getRangeAxis().setAutoRange(true);

            plot_.getDomainAxis().setLowerBound(marker.getStartValue() - 3);
            plot_.getDomainAxis().setUpperBound(marker.getEndValue() + 3);
        }
    }

    @Override
    public void doAddMarker(IntervalMarker[] markers) {
        for (IntervalMarker marker : markers) {
            plot_.addDomainMarker(marker);
        }

        plot_.getDomainAxis().setAutoRange(true);
        plot_.getRangeAxis().setAutoRange(true);
    }

    @Override
    public void doRemoveMarker(IntervalMarker marker) {
        plot_.removeDomainMarker(marker);
    }

    @Override
    public void doSpectrumAnalysisLayout(SpectrumShotItem spectrumShotItem) {
        setAnalysisPanelVisible(true);

        spectrumAnalysisReportPanel_.doAnalysis(spectrumShotItem);
    }

    @Override
    public double getIntensityOfLine(int type, double wavelength, double regionwidth) {
        return shotCheckListPanel_.getIntensityOfLine(type, wavelength, regionwidth);
    }
}