Search in sources :

Example 6 with CheckBoxNodeData

use of org.scijava.swing.checkboxtree.CheckBoxNodeData in project mafscaling by vimsh.

the class LogView method exportSelectedWotPulls.

private void exportSelectedWotPulls() {
    ArrayList<HashMap<String, ArrayList<Double>>> filePulls;
    HashMap<String, ArrayList<Double>> pullData;
    DefaultMutableTreeNode fileNode;
    CheckBoxNodeData pullNode;
    String fileName = null;
    String nodeName = null;
    String dirName = null;
    int pullIdx;
    int cnt;
    int i, j;
    setCursor(new Cursor(Cursor.WAIT_CURSOR));
    try {
        DefaultMutableTreeNode root = (DefaultMutableTreeNode) wotTree.getModel().getRoot();
        for (int idx = 0; idx < root.getChildCount(); ++idx) {
            fileNode = (DefaultMutableTreeNode) root.getChildAt(idx);
            fileName = fileNode.getUserObject().toString().replaceAll(fileNameReplaceString, "");
            for (cnt = 0; cnt < fileNode.getChildCount(); ++cnt) {
                pullNode = (CheckBoxNodeData) ((DefaultMutableTreeNode) fileNode.getChildAt(cnt)).getUserObject();
                if (!pullNode.isChecked())
                    continue;
                filePulls = filesData.get(fileName);
                nodeName = pullNode.getText();
                pullIdx = Integer.parseInt(nodeName.replaceAll(pullIndexReplaceString, "")) - 1;
                pullData = filePulls.get(pullIdx);
                if (pullData.size() == 0)
                    continue;
                if (dirName == null) {
                    JFileChooser fc = new JFileChooser();
                    fc.setCurrentDirectory((lastPullExportDir == null ? fileChooser.getCurrentDirectory() : lastPullExportDir));
                    fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                    fc.setAcceptAllFileFilterUsed(false);
                    if (JFileChooser.APPROVE_OPTION != fc.showSaveDialog(null))
                        return;
                    File dir = fc.getSelectedFile();
                    if (!dir.exists() || !dir.isDirectory()) {
                        JOptionPane.showMessageDialog(null, "Directory doesn't exist: " + dir.getAbsolutePath(), "Invalid directory", JOptionPane.ERROR_MESSAGE);
                        return;
                    }
                    lastPullExportDir = dir;
                    dirName = dir.getAbsolutePath();
                }
                ArrayList<String> columns = new ArrayList<String>();
                ArrayList<ArrayList<Double>> data = new ArrayList<ArrayList<Double>>();
                for (Map.Entry<String, ArrayList<Double>> entry : pullData.entrySet()) {
                    columns.add(entry.getKey());
                    data.add(entry.getValue());
                }
                Writer out = null;
                try {
                    File file = new File(dirName + File.separator + fileName + "_" + nodeName + ".csv");
                    out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, false), Config.getEncoding()));
                    int lidx = columns.size() - 1;
                    int rows = data.get(0).size();
                    for (i = 0; i <= lidx; ++i) out.write(columns.get(i) + (i < lidx ? "," : ""));
                    out.write("\n");
                    for (j = 0; j < rows; ++j) {
                        for (i = 0; i <= lidx; ++i) out.write(data.get(i).get(j) + (i < lidx ? "," : ""));
                        out.write("\n");
                    }
                } catch (Exception e) {
                    logger.error(e);
                } finally {
                    if (out != null) {
                        try {
                            out.close();
                        } catch (IOException e) {
                            logger.error(e);
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        logger.error(e);
    } finally {
        setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
    }
}
Also used : DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Cursor(java.awt.Cursor) CheckBoxNodeData(org.scijava.swing.checkboxtree.CheckBoxNodeData) Paint(java.awt.Paint) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) JFileChooser(javax.swing.JFileChooser) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) Map(java.util.Map) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 7 with CheckBoxNodeData

use of org.scijava.swing.checkboxtree.CheckBoxNodeData in project mafscaling by vimsh.

the class LogView method loadWotLogFiles.

private void loadWotLogFiles() {
    File[] files = fileChooser.getSelectedFiles();
    if (files.length < 1)
        return;
    filesData = new HashMap<String, ArrayList<HashMap<String, ArrayList<Double>>>>();
    wotYAxisGroups = new ArrayList<TreeSet<String>>();
    TreeSet<String> columns = new TreeSet<String>();
    ArrayList<String> colNames = null;
    DefaultMutableTreeNode root = (DefaultMutableTreeNode) wotTree.getModel().getRoot();
    root.removeAllChildren();
    ((DefaultTreeModel) wotTree.getModel()).reload(root);
    wotPlotsColumn.removeAllItems();
    wotPlotsColumn.setText("");
    clearWotPlots();
    double val;
    int i = 0;
    int row = 0;
    for (File file : files) {
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(file.getAbsoluteFile()), Config.getEncoding()));
            String line = null;
            String[] elements = null;
            while ((line = br.readLine()) != null && (elements = line.split(Utils.fileFieldSplitter, -1)) != null && elements.length < 2) continue;
            if (line.charAt(line.length() - 1) == ',')
                Arrays.copyOf(elements, elements.length - 1);
            colNames = new ArrayList<String>(Arrays.asList(elements));
            if (false == getColumnsFilters(colNames))
                continue;
            DefaultMutableTreeNode fileNode = new DefaultMutableTreeNode("<html><u>" + file.getName() + "</u></html>");
            ArrayList<HashMap<String, ArrayList<Double>>> pulls = new ArrayList<HashMap<String, ArrayList<Double>>>();
            HashMap<String, ArrayList<Double>> pullData = new HashMap<String, ArrayList<Double>>();
            ArrayList<Double> columnData;
            String[] flds;
            row = 0;
            int pullRows = 0;
            boolean wotFlag = true;
            while ((line = br.readLine()) != null) {
                if (line.length() > 0 && line.charAt(line.length() - 1) == ',')
                    line = line.substring(0, line.length() - 1);
                flds = line.split(Utils.fileFieldSplitter, -1);
                val = Double.valueOf(flds[logThtlAngleColIdx]);
                if (row == 0 && val < 99)
                    wotFlag = false;
                if (val < wotPoint) {
                    if (wotFlag == true) {
                        wotFlag = false;
                        if (pullRows >= 10) {
                            pulls.add(pullData);
                            pullData = new HashMap<String, ArrayList<Double>>();
                            fileNode.add(new DefaultMutableTreeNode(new CheckBoxNodeData(pullIndexReplaceString + pulls.size(), true)));
                        }
                    }
                } else {
                    boolean newPullData = false;
                    if (wotFlag == false || row == 0) {
                        wotFlag = true;
                        newPullData = true;
                        pullRows = 0;
                    }
                    pullRows += 1;
                    for (i = 0; i < colNames.size(); ++i) {
                        if (newPullData) {
                            columnData = new ArrayList<Double>();
                            pullData.put(colNames.get(i), columnData);
                        } else
                            columnData = pullData.get(colNames.get(i));
                        if (flds[i].matches(Utils.tmRegex)) {
                            if (row == 0)
                                Utils.resetBaseTime(flds[i]);
                            columnData.add((double) Utils.parseTime(flds[i]));
                        } else
                            columnData.add(Utils.parseValue(flds[i]));
                    }
                }
                row += 1;
            }
            if (wotFlag == true) {
                if (pullRows >= 10) {
                    pulls.add(pullData);
                    pullData = new HashMap<String, ArrayList<Double>>();
                    fileNode.add(new DefaultMutableTreeNode(new CheckBoxNodeData(pullIndexReplaceString + pulls.size(), true)));
                }
            }
            if (pulls.size() > 0) {
                root.add(fileNode);
                TreePath path = new TreePath(root);
                wotTree.expandPath(path.pathByAddingChild(fileNode));
                filesData.put(file.getName(), pulls);
                columns.addAll(colNames);
            }
        } catch (NumberFormatException ne) {
            logger.error(ne);
            JOptionPane.showMessageDialog(null, "Error parsing number at " + file.getName() + ", column " + colNames.get(i) + " line " + row + ": " + ne, "Error processing file", JOptionPane.ERROR_MESSAGE);
            return;
        } catch (Exception e) {
            logger.error(e);
            JOptionPane.showMessageDialog(null, e.getMessage(), "Error processing file " + file.getName(), JOptionPane.ERROR_MESSAGE);
            return;
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    logger.error(e);
                }
            }
        }
    }
    if (columns.size() > 0) {
        for (String col : columns) wotPlotsColumn.addItem(col);
        ((DefaultTreeModel) wotTree.getModel()).reload(root);
        for (row = 0; row < wotTree.getRowCount(); ++row) wotTree.expandRow(row);
        linkYAxis.setEnabled(true);
    } else {
        JOptionPane.showMessageDialog(null, "No WOT pulls were found in the log file(s)", "Oops", JOptionPane.INFORMATION_MESSAGE);
    }
}
Also used : DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CheckBoxNodeData(org.scijava.swing.checkboxtree.CheckBoxNodeData) TreeSet(java.util.TreeSet) InputStreamReader(java.io.InputStreamReader) DefaultTreeModel(javax.swing.tree.DefaultTreeModel) IOException(java.io.IOException) Paint(java.awt.Paint) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) TreePath(javax.swing.tree.TreePath) BufferedReader(java.io.BufferedReader) File(java.io.File)

Aggregations

DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)7 CheckBoxNodeData (org.scijava.swing.checkboxtree.CheckBoxNodeData)7 Paint (java.awt.Paint)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 XYSeries (org.jfree.data.xy.XYSeries)4 XYSeriesCollection (org.jfree.data.xy.XYSeriesCollection)4 Cursor (java.awt.Cursor)3 IOException (java.io.IOException)3 DefaultTreeModel (javax.swing.tree.DefaultTreeModel)3 XYLineAndShapeRenderer (org.jfree.chart.renderer.xy.XYLineAndShapeRenderer)3 File (java.io.File)2 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 TreePath (javax.swing.tree.TreePath)2 NumberAxis (org.jfree.chart.axis.NumberAxis)2 BasicStroke (java.awt.BasicStroke)1 Stroke (java.awt.Stroke)1 BufferedReader (java.io.BufferedReader)1 BufferedWriter (java.io.BufferedWriter)1