guineu.modules.database.openQualityControlFileDB.DatasetOpenDialog.java Source code

Java tutorial

Introduction

Here is the source code for guineu.modules.database.openQualityControlFileDB.DatasetOpenDialog.java

Source

/*
 * Copyright 2007-2013 VTT Biotechnology
 * This file is part of Guineu.
 *
 * Guineu 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 2 of the License, or (at your option) any later
 * version.
 *
 * Guineu 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
 * Guineu; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
 * Fifth Floor, Boston, MA 02110-1301 USA
 */
package guineu.modules.database.openQualityControlFileDB;

import guineu.modules.database.tools.CheckNode;
import guineu.modules.database.tools.CheckRenderer;
import guineu.data.Dataset;
import guineu.data.DatasetType;
import guineu.data.PeakListRow;
import guineu.data.impl.datasets.SimpleQualityControlDataset;
import guineu.data.impl.peaklists.SimplePeakListRowOther;
import guineu.database.retrieve.impl.OracleRetrievement;
import guineu.database.retrieve.DataBase;
import guineu.main.GuineuCore;
import guineu.util.dialogs.ExitCode;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.swing.JDialog;
import javax.swing.JTree;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
import org.apache.commons.math.stat.descriptive.DescriptiveStatistics;

/**
 *
 * @author scsandra
 */
public class DatasetOpenDialog extends JDialog {

    private ExitCode exitCode = ExitCode.UNKNOWN;
    private List<Dataset> datasets;
    private JTree tree;
    private HashMap<CheckNode, String[]> nodeTable;
    private DescriptiveStatistics Stats[], superStats[];
    private DataBase db;

    /** Creates new form DatasetOpenDialog */
    public DatasetOpenDialog() {
        super(GuineuCore.getDesktop().getMainFrame(), "Opening Database...", true);
        initComponents();
        nodeTable = new HashMap<CheckNode, String[]>();
        datasets = new ArrayList<Dataset>();
        tree = createTree();

    }

    ExitCode getExitCode() {
        return exitCode;
    }

    /** This platform is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this platform is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        SPResults = new javax.swing.JScrollPane();
        buttonsPanel = new javax.swing.JPanel();
        okButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setMinimumSize(new java.awt.Dimension(201, 700));
        getContentPane().setLayout(new javax.swing.BoxLayout(getContentPane(), javax.swing.BoxLayout.PAGE_AXIS));

        jPanel2.setPreferredSize(new java.awt.Dimension(647, 500));
        jPanel2.setLayout(new javax.swing.BoxLayout(jPanel2, javax.swing.BoxLayout.LINE_AXIS));
        jPanel2.add(SPResults);

        getContentPane().add(jPanel2);

        buttonsPanel.setOpaque(false);
        buttonsPanel.setPreferredSize(new java.awt.Dimension(647, 48));

        okButton.setText("OK");
        okButton.setPreferredSize(new java.awt.Dimension(70, 26));
        okButton.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });
        buttonsPanel.add(okButton);

        cancelButton.setText("Cancel");
        cancelButton.setPreferredSize(new java.awt.Dimension(90, 26));
        cancelButton.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        buttonsPanel.add(cancelButton);

        deleteButton = new javax.swing.JButton("Delete File");
        cancelButton.setPreferredSize(new java.awt.Dimension(90, 26));
        deleteButton.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteButtonActionPerformed(evt);
            }
        });

        buttonsPanel.add(deleteButton);
        getContentPane().add(buttonsPanel);

        pack();
    }// </editor-fold>

    /**
     * Return the generated data sets.
     *
     * @return A list of data sets
     */
    public List<Dataset> getDatasets() {
        return datasets;
    }

    private void deleteButtonActionPerformed(ActionEvent evt) {
        List<Integer> tobeRemoved = new ArrayList<Integer>();
        if (tree != null) {
            for (int index = 0; index < tree.getRowCount(); index++) {
                TreePath path = tree.getPathForRow(index);
                if (path != null) {
                    CheckNode node = (CheckNode) path.getLastPathComponent();
                    if (node.isSelected) {
                        deleteDataset(node);
                        tobeRemoved.add(index);
                    }
                }
            }
        }
        this.exitCode = ExitCode.CANCEL;
        dispose();
    }

    private void deleteDataset(CheckNode node) {
        String[] data = nodeTable.get(node);
        db.deleteQualityControlDataset(Integer.parseInt(data[6]));
        this.nodeTable.remove(node);
    }

    /**
     * Creates selected datasets
     * @param evt
     */
    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {
        if (tree != null) {
            for (int index = 0; index < tree.getRowCount(); index++) {
                TreePath path = tree.getPathForRow(index);
                if (path != null) {
                    CheckNode node = (CheckNode) path.getLastPathComponent();
                    if (node.isSelected) {
                        createDataset(node);
                    }
                }
            }
        }

        exitCode = ExitCode.OK;
        dispose();
    }

    /**
     * Creates the datasets from selected nodes. Each selected node can be the name of one sample, or
     * the name of a complete data set.
     *
     * @param node Node from the tree
     */
    private void createDataset(CheckNode node) {
        Dataset dataset = null;
        DecimalFormat formatter = new DecimalFormat("####.##");

        String[] data = nodeTable.get(node);
        try {
            if (data != null) {
                dataset = new SimpleQualityControlDataset(data[0]);
                dataset.setType(DatasetType.QUALITYCONTROL);

                ((SimpleQualityControlDataset) dataset).setParameters(data[1], data[0], data[2], data[3], data[4],
                        data[5]);

                for (int i = 1; i <= 12; i++) {
                    dataset.addColumnName(String.valueOf(i));
                }

                dataset.addRow(getRow("Date:", data[1]));

                dataset.addRow(getRow("SampleSet:", data[0]));

                dataset.addRow(getRow("Ion Mode:", data[2]));

                dataset.addRow(getRow("Injection volume:", data[3]));

                dataset.addRow(getRow("Sample type:", data[4]));

                dataset.addRow(getRow("", ""));
                dataset.addRow(getRow("", ""));

                dataset.addRow(getRow("Basic parameters for seronorm control samples & batch standard:", ""));

                dataset.addRow(this.getTitles());

                Stats = new DescriptiveStatistics[9];
                for (int i = 0; i < 9; i++) {
                    Stats[i] = new DescriptiveStatistics();
                }

                List<SampleInfo> samples = db.getQualityControlRows(Integer.parseInt(data[6]));

                for (SampleInfo s : samples) {
                    PeakListRow row = s.getRow(Stats);
                    dataset.addRow(row);
                    ((SimpleQualityControlDataset) dataset).setRow(row);
                }

                SimplePeakListRowOther row = new SimplePeakListRowOther();
                row.setPeak("1", "MEAN");
                for (int i = 0; i < 9; i++) {
                    row.setPeak(String.valueOf(i + 2), formatter.format(Stats[i].getMean()).toString());
                }
                dataset.addRow(row);

                row = new SimplePeakListRowOther();
                row.setPeak("1", "RSD");
                for (int i = 0; i < 9; i++) {
                    row.setPeak(String.valueOf(i + 2), formatter
                            .format((Stats[i].getStandardDeviation() * 100) / Stats[i].getMean()).toString());
                }
                dataset.addRow(row);

                dataset.addRow(getRow("", ""));
                dataset.addRow(getRow("", ""));
                // row8
                SimplePeakListRowOther row8 = new SimplePeakListRowOther();
                row8.setPeak("1", "Additional parameters for seronorm control samples & batch standard:");
                dataset.addRow(row8);

                dataset.addRow(this.getTitle2());

                superStats = new DescriptiveStatistics[9];
                for (int i = 0; i < 9; i++) {
                    superStats[i] = new DescriptiveStatistics();
                }
                for (SampleInfo s : samples) {
                    PeakListRow row2 = s.getRow2(superStats);
                    dataset.addRow(row2);
                    ((SimpleQualityControlDataset) dataset).setAdditionalRow(row2);
                }

                row = new SimplePeakListRowOther();
                row.setPeak("1", "MEAN");
                for (int i = 0; i < 9; i++) {
                    row.setPeak(String.valueOf(i + 2), formatter.format(superStats[i].getMean()).toString());
                }
                dataset.addRow(row);

                row = new SimplePeakListRowOther();
                row.setPeak("1", "RSD");
                for (int i = 0; i < 9; i++) {
                    row.setPeak(String.valueOf(i + 2),
                            formatter.format((superStats[i].getStandardDeviation() * 100) / superStats[i].getMean())
                                    .toString());
                }
                dataset.addRow(row);

                dataset.addRow(getRow("", ""));
                dataset.addRow(getRow("", ""));
                dataset.addRow(getRow("Comments:", data[5]));
            }
            this.datasets.add(dataset);
        } catch (Exception e) {
        }

    }

    private SimplePeakListRowOther getRow(String title, String content) {
        SimplePeakListRowOther row = new SimplePeakListRowOther();
        row.setPeak("1", title);
        row.setPeak("2", content);
        return row;
    }

    private SimplePeakListRowOther getTitles() {
        SimplePeakListRowOther row = new SimplePeakListRowOther();
        row.setPeak("1", "Sample name");
        row.setPeak("2", "LysoPC RT");
        row.setPeak("3", "LysoPC height/area");
        row.setPeak("4", "LysoPC height ratio");
        row.setPeak("5", "PC RT");
        row.setPeak("6", "PC height/area");
        row.setPeak("7", "PC height ratio");
        row.setPeak("8", "TG RT");
        row.setPeak("9", "TG height/area");
        row.setPeak("10", "TG height ratio");
        row.setPeak("11", "Date");
        row.setPeak("12", "Time");
        return row;
    }

    private SimplePeakListRowOther getTitle2() {
        SimplePeakListRowOther row = new SimplePeakListRowOther();
        row.setPeak("1", "Sample name");
        row.setPeak("2", "LysoPC height");
        row.setPeak("3", "PC height");
        row.setPeak("4", "TG height");
        row.setPeak("5", "*LysoPC height");
        row.setPeak("6", "*LysoPC RT");
        row.setPeak("7", "*PC height");
        row.setPeak("8", "*PC RT");
        row.setPeak("9", "*TG height");
        row.setPeak("10", "*TG RT");
        return row;
    }

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {
        exitCode = ExitCode.CANCEL;
        dispose();
    }

    /**
     * Creates a tree with the studies and their correspondents data sets.
     * The information is taken from the database.
     *
     * @return New tree
     */
    private JTree createTree() {
        db = new OracleRetrievement();
        String rootName = "Data";
        CheckNode rootNode = new CheckNode(rootName);

        List<String[]> QCdatasets = db.getQualityControlDatasets();

        for (String[] dataset : QCdatasets) {
            String info = "";
            for (String d : dataset) {
                info += d + " - ";
            }
            CheckNode stNode = new CheckNode(info);
            this.nodeTable.put(stNode, dataset);
            rootNode.add(stNode);
        }

        tree = new JTree(rootNode);

        tree.setCellRenderer(new CheckRenderer());
        tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        tree.putClientProperty("JTree.lineStyle", "Angled");
        tree.addMouseListener(new NodeSelectionListener(tree));
        this.SPResults.setViewportView((Component) tree);

        return tree;
    }

    class NodeSelectionListener extends MouseAdapter {

        JTree tree;

        NodeSelectionListener(JTree tree) {
            this.tree = tree;
        }

        @Override
        public void mousePressed(MouseEvent e) {
            int x = e.getX();
            int y = e.getY();
            int row = tree.getRowForLocation(x, y);
            TreePath path = tree.getPathForRow(row);
            if (path != null) {
                CheckNode node = (CheckNode) path.getLastPathComponent();
                boolean isSelected = !(node.isSelected());
                node.setSelected(isSelected);
                tree.repaint();
            }
        }
    }

    // Variables declaration - do not modify
    private javax.swing.JScrollPane SPResults;
    private javax.swing.JPanel buttonsPanel;
    private javax.swing.JButton cancelButton;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JButton okButton;
    private javax.swing.JButton deleteButton;
    // End of variables declaration
}