@Override public void safeActionPerformed(ActionEvent e) throws Exception { String fileName; fileName = study.getStudyName() + "_wholenetwork_nodes"; File currentDirectory = new File(studyFile.getParent() + "/Graphs"); currentDirectory.mkdir(); JFileChooser fileChooser = new JFileChooser(); fileChooser.setFileFilter(new FileNameExtensionFilter("Comma-Separated Values", "csv")); fileChooser.setCurrentDirectory(currentDirectory); fileChooser.setSelectedFile(new File(fileName + ".csv")); fileChooser.setDialogTitle("Save Alter Attributes (CSV)"); fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); int returnValue = JFileChooser.APPROVE_OPTION; while (returnValue == JFileChooser.APPROVE_OPTION) { returnValue = fileChooser.showSaveDialog(parent); File dataFile = fileChooser.getSelectedFile(); try { if (dataFile != null && !dataFile.isDirectory()) { String path = dataFile.getAbsolutePath(); if (!path.endsWith(".csv")) { path += ".csv"; dataFile = new File(path); } FileWriter fw = new FileWriter(dataFile); CSVWriter csv = new CSVWriter(fw); Set<String> questionSet = Sets.newHashSet(); for (WholeNetworkAlter alter : net.getWholeNetworkAlters().values()) { questionSet.addAll(alter.getAttributes().keySet()); } List<String> questionList = new ArrayList<String>(questionSet); List<String> heading = Lists.newArrayList("MappingId", "Name"); heading.addAll(questionList); csv.writeNext(heading.toArray(new String[] {})); for (WholeNetworkAlter alter : net.getWholeNetworkAlters().values()) { ArrayList<String> row = Lists.newArrayList( alter.getId() + "", alter .getOccurences() .get(0) .toString() .replaceAll("[^a-zA-Z_\\-0-9]+", "_")); Map<String, String> answers = alter.getAttributes(); for (String question : questionList) { String value = answers.get(question); row.add(value == null ? "" : value); } csv.writeNext(row.toArray(new String[] {})); } csv.flush(); fw.close(); } } catch (Exception e1) { throw new RuntimeException(e1); } break; } }