/** Save the results to an Excel File. */ private void saveResults() { channelsSelectionForm = new ChannelSelectionForm(channelName); FileChooser chooser = view.createSaveToExcelChooser(); chooser.addComponentToControls(channelsSelectionForm); int results = chooser.showDialog(); if (results != JFileChooser.APPROVE_OPTION) return; File file = chooser.getFormattedSelectedFile(); // TODO: Modify that code when we have various writer. if (!file.getAbsolutePath().endsWith(ExcelFilter.EXCEL)) { String fileName = file.getAbsolutePath() + "." + ExcelFilter.EXCEL; file = new File(fileName); } List<Integer> channels = channelsSelectionForm.getUserSelection(); if (channels == null || channels.size() == 0) { UserNotifier un = MeasurementAgent.getRegistry().getUserNotifier(); un.notifyInfo("Save Results", " Please select at least a channel."); view.setStatus("No Channel selected to output."); return; } try { ExcelWriter writer = new ExcelWriter(file.getAbsolutePath()); writer.openFile(); writer.createSheet("Channel Summary"); Iterator<Coord3D> coordMapIterator = shapeMap.keySet().iterator(); Coord3D currentCoord; int n = channels.size(); Integer channel; if (channelSummarySelected(channels)) outputSummary(writer, shapeMap); BufferedImage originalImage = model.getRenderedImage(); if (originalImage != null) { BufferedImage image = Factory.copyBufferedImage(originalImage); // Add the ROI for the current plane to the image. // TODO: Need to check that. model.setAttributes(MeasurementAttributes.SHOWID, true); model.getDrawingView().print(image.getGraphics()); model.setAttributes(MeasurementAttributes.SHOWID, false); try { writer.addImageToWorkbook("ThumbnailImage", image); } catch (Exception e) { // TODO } int col = writer.getMaxColumn(0); writer.writeImage(0, col + 1, 256, 256, "ThumbnailImage"); } if (channelSummarySelected(channels) && channels.size() != 1) while (coordMapIterator.hasNext()) { currentCoord = coordMapIterator.next(); for (int i = 0; i < n; i++) { channel = channels.get(i); if (channel == ChannelSelectionForm.SUMMARYVALUE) continue; if (!nameMap.containsKey(channelName.get(channel))) continue; int rowIndex = 0; writer.createSheet("Channel Number " + channelName.get(channel)); writeHeader(writer, rowIndex, currentCoord); channel = nameMap.get(channelName.get(channel)); writeData(writer, rowIndex, currentCoord, channel.intValue()); } } writer.close(); } catch (Exception e) { Logger logger = MeasurementAgent.getRegistry().getLogger(); logger.error(this, "Cannot save ROI results: " + e.toString()); UserNotifier un = MeasurementAgent.getRegistry().getUserNotifier(); String message = "An error occurred while trying to" + " save the data.\nPlease try again."; if (e instanceof NumberFormatException) { message = "We only support the British/American style of " + "representing numbers,\nusing a decimal point rather " + "than a comma."; } un.notifyInfo("Save Results", message); return; } Registry reg = MeasurementAgent.getRegistry(); UserNotifier un = reg.getUserNotifier(); un.notifyInfo("Save ROI results", "The ROI results have been " + "successfully saved."); }