/**
  * Create summary table with horizontal columns.
  *
  * @param writer The Excel writer.
  * @param rowIndex The selected row.
  */
 private void printSummaryHeader(ExcelWriter writer, int rowIndex) {
   writer.writeElement(rowIndex, 0, "channel");
   writer.writeElement(rowIndex, 1, "zsection");
   writer.writeElement(rowIndex, 2, "time");
   for (int y = 0; y < channelSummaryTable.getRowCount(); y++)
     writer.writeElement(rowIndex, 3 + y, channelSummaryTable.getValueAt(y, 0));
 }
 /**
  * Adds the any remaining fields (min, max, mean, stdDev) to the file being saved.
  *
  * @param writer The Excel writer.
  * @param rowIndex The selected row.
  * @param channel The channel to output.
  * @param z z-section to output.
  * @param t timepoint to output.
  */
 private void outputSummaryRow(ExcelWriter writer, int rowIndex, Integer channel, int z, int t) {
   writer.writeElement(rowIndex, 0, channelName.get(channel));
   writer.writeElement(rowIndex, 1, z + "");
   writer.writeElement(rowIndex, 2, t + "");
   int col;
   String v;
   for (int y = 0; y < channelSummaryTable.getRowCount(); y++) {
     col = getColumn(channelName.get(channel));
     if (col == -1) continue;
     v = (String) channelSummaryTable.getValueAt(y, col);
     if (v.contains(".") && v.contains(",")) {
       v = v.replace(".", "");
       v = v.replace(",", ".");
     }
     writer.writeElement(rowIndex, 3 + y, new Double(v));
   }
 }
 /**
  * Writes the header information for the file, image, projects, dataset.
  *
  * @param writer The Excel writer.
  * @param rowIndex The selected row.
  * @param currentCoord The coord of the shape being written.
  * @throws IOException Thrown if the data cannot be written.
  */
 private void writeHeader(ExcelWriter writer, int rowIndex, Coord3D currentCoord) {
   writer.writeElement(rowIndex, 0, "Image ");
   writer.writeElement(rowIndex, 1, model.getImageName());
   rowIndex++;
   writer.writeElement(rowIndex, 0, "Z ");
   writer.writeElement(rowIndex, 1, (currentCoord.getZSection() + 1));
   rowIndex++;
   writer.writeElement(rowIndex, 0, "T ");
   writer.writeElement(rowIndex, 1, (currentCoord.getTimePoint() + 1));
   rowIndex++;
 }
 /**
  * Writes the channel intensities and stats to the files.
  *
  * @param writer The Excel writer.
  * @param rowIndex The selected row.
  * @param coord The specified coordinate.
  * @param channel The channel to output.
  */
 private void writeData(ExcelWriter writer, int rowIndex, Coord3D coord, int channel) {
   populateData(coord, channel);
   writer.writeTableToSheet(rowIndex, 0, tableModel);
 }
  /** 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.");
  }