public static void writeGraphs(Component parentComponent, String title, SnapFileFilter[] fileFilters, PropertyMap preferences, DiagramGraph[] graphs) { if (graphs.length == 0) { JOptionPane.showMessageDialog(parentComponent, "Nothing to save."); return; } File selectedFile = selectGraphFile(parentComponent, title, fileFilters, preferences, false); if (selectedFile != null) { try { FileWriter fileWriter = new FileWriter(selectedFile); try { writeGraphs(graphs, fileWriter); } finally { fileWriter.close(); } } catch (IOException e) { JOptionPane.showMessageDialog(parentComponent, "I/O error: " + e.getMessage()); } } }
public static DiagramGraph[] readGraphs(Component parentComponent, String title, SnapFileFilter[] fileFilters, PropertyMap preferences) { File selectedFile = selectGraphFile(parentComponent, title, fileFilters, preferences, true); if (selectedFile != null) { try { FileReader fileReader = new FileReader(selectedFile); try { return readGraphs(fileReader); } finally { fileReader.close(); } } catch (IOException e) { JOptionPane.showMessageDialog(parentComponent, "I/O error: " + e.getMessage()); } } return new DiagramGraph[0]; }
public static void writeGraphs(DiagramGraph[] graphs, Writer writer) throws IOException { List<List<DiagramGraph>> graphGroups = computeGraphGroups(graphs); for (List<DiagramGraph> graphGroup : graphGroups) { writeGraphGroup(graphGroup, writer); } }
private void testIO(DiagramGraph[] expectedGraphs) throws IOException { StringWriter writer1 = new StringWriter(); DiagramGraphIO.writeGraphs(expectedGraphs, writer1); DiagramGraph[] actualGraphs = DiagramGraphIO.readGraphs(new StringReader(writer1.toString())); assertEqualGraphs(actualGraphs, expectedGraphs); StringWriter writer2 = new StringWriter(); DiagramGraphIO.writeGraphs(expectedGraphs, writer2); assertEquals(writer1.toString(), writer2.toString()); }
public void actionPerformed(ActionEvent e) { ensureDefaultDirSet(); DiagramGraphIO.writeGraphs(null, "Export Endmembers", new SnapFileFilter[]{DiagramGraphIO.SPECTRA_CSV_FILE_FILTER}, appContext.getPreferences(), endmemberDiagram.getGraphs()); } }
public void actionPerformed(ActionEvent e) { ensureDefaultDirSet(); DiagramGraph[] diagramGraphs = DiagramGraphIO.readGraphs(null, "Add Endmembers", new SnapFileFilter[]{DiagramGraphIO.SPECTRA_CSV_FILE_FILTER}, appContext.getPreferences()); Endmember[] endmembers = convertGraphsToEndmembers(diagramGraphs); for (Endmember endmember : endmembers) { addEndmember(endmember); } }
public static DiagramGraph[] readGraphs(Reader reader) throws IOException { CsvReader csvReader = new CsvReader(reader, new char[]{'\t'}); List<DiagramGraph> graphGroup = new ArrayList<DiagramGraph>(5); List<double[]> dataRecords = new ArrayList<double[]>(20); String[] headerRecord = csvReader.readRecord(); while (true) { if (headerRecord.length < 2) { throw new IOException("Invalid format."); } String[] record = csvReader.readRecord(); if (record == null) { break; } double[] dataRecord = toDoubles(record); if (dataRecord != null) { if (dataRecord.length != headerRecord.length) { throw new IOException("Invalid format."); } dataRecords.add(dataRecord); } else { readGraphGroup(headerRecord, dataRecords, graphGroup); headerRecord = record; } } readGraphGroup(headerRecord, dataRecords, graphGroup); return graphGroup.toArray(new DiagramGraph[0]); }
private static List<List<DiagramGraph>> computeGraphGroups(DiagramGraph[] graphs) { List<List<DiagramGraph>> graphGroups = new ArrayList<List<DiagramGraph>>(3); for (DiagramGraph graph : graphs) { boolean found = false; for (List<DiagramGraph> graphGroup : graphGroups) { if (equalXValues(graph, graphGroup.get(0))) { graphGroup.add(graph); found = true; break; } } if (!found) { ArrayList<DiagramGraph> graphGroup = new ArrayList<DiagramGraph>(3); graphGroup.add(graph); graphGroups.add(graphGroup); } } return graphGroups; }
private void exportSpectra() { final List<DisplayableSpectrum> selectedSpectra = spectrumTopComponent.getSelectedSpectra(); Placemark[] pins = spectrumTopComponent.getDisplayedPins(); final List<SpectrumGraph> spectrumGraphList = new ArrayList<SpectrumGraph>(); for (Placemark pin : pins) { for (DisplayableSpectrum spectrumInDisplay : selectedSpectra) { final SpectrumGraph spectrumGraph = new SpectrumGraph(pin, spectrumInDisplay.getSelectedBands()); spectrumGraph.readValues(); spectrumGraphList.add(spectrumGraph); } } DiagramGraph[] pinGraphs = spectrumGraphList.toArray(new DiagramGraph[0]); //todo move diagramgraphio to snap DiagramGraphIO.writeGraphs(SwingUtilities.getWindowAncestor(spectrumTopComponent), "Export Pin Spectra", new SnapFileFilter[]{DiagramGraphIO.SPECTRA_CSV_FILE_FILTER}, SnapApp.getDefault().getPreferencesPropertyMap(), pinGraphs); }