/**
   * Creating cvs format file with the statistics. Option to select the two columns separator.
   *
   * <p>Example with semicolon: Name;data\n Name2;data2\n
   *
   * @param valores - Pairs: String name (key) + Double value (
   * @param endFile - File to write the information
   */
  private void exportToCSVFile(List<MyObjectStatistics> valores, File endFile) {

    try {
      CSVSeparatorOptionsPanel csvSeparatorOptions = new CSVSeparatorOptionsPanel();
      PluginServices.getMDIManager().addWindow(csvSeparatorOptions);

      String separator = csvSeparatorOptions.getSeparator();

      if (separator != null) {

        FileWriter fileCSV = new FileWriter(endFile);

        fileCSV.write(
            PluginServices.getText(this, "Nombre")
                + separator
                + PluginServices.getText(this, "Valor")
                + "\n");

        Iterator<MyObjectStatistics> iterador = valores.listIterator();

        while (iterador.hasNext()) { // Writing value,value\n
          MyObjectStatistics data = (MyObjectStatistics) iterador.next();
          fileCSV.write(data.getKey() + separator + (data.getValue()) + "\n");
        }
        fileCSV.close();
        JOptionPane.showMessageDialog(
            null,
            PluginServices.getText(this, "fichero_creado_en") + " " + endFile.getAbsolutePath(),
            PluginServices.getText(this, "fichero_creado_en_formato")
                + " csv "
                + PluginServices.getText(this, "mediante_el_separador")
                + " \""
                + separator
                + "\"",
            JOptionPane.INFORMATION_MESSAGE); // Informing the user
      } else return;

    } catch (IOException e) { // Informing the user
      logger.error("Error exportando a formato csv");
      JOptionPane.showMessageDialog(
          null,
          PluginServices.getText(this, "Error_exportando_las_estadisticas")
              + " "
              + endFile.getAbsolutePath(),
          PluginServices.getText(this, "Error"),
          JOptionPane.ERROR_MESSAGE);
    }
  }
  /**
   * Creating dbf format file with the statistics
   *
   * @param valores - Pairs String name (key) + Double value
   * @param endFile - File to write the information
   */
  private void exportToDBFFile(List<MyObjectStatistics> valores, File endFile) {

    try {
      FileDriver driver = null;
      try {
        driver = (FileDriver) LayerFactory.getDM().getDriver("gdbms dbf driver");
      } catch (DriverLoadException e1) {
        logger.error("Error Creando el driver dbf");
      }

      try {
        if (!endFile.exists()) {
          try {
            driver.createSource(
                endFile.getAbsolutePath(), new String[] {"0"}, new int[] {Types.DOUBLE});
          } catch (ReadDriverException e) {
            logger.error("Error en createSource");
          }
          endFile.createNewFile();
        }

        try {
          driver.open(endFile);
        } catch (OpenDriverException e) {
          logger.error("Error abriendo el fichero de destino");
        }
      } catch (IOException e) {
        try {
          throw new Exception("Error creando el fichero de destino", e);
        } catch (Exception e1) {
          logger.error("Error creando el fichero de destino");
        }
      }

      IWriter writer = ((IWriteable) driver).getWriter();
      ITableDefinition orgDef = new TableDefinition();
      try {
        // Preparing the total rows in the new dbf file, in this case
        // two rows : Name Value
        FieldDescription[] fields = new FieldDescription[2];
        fields[0] = new FieldDescription();
        fields[0].setFieldType(Types.VARCHAR);
        fields[0].setFieldName(PluginServices.getText(this, "Nombre"));
        fields[0].setFieldLength(50);
        fields[1] = new FieldDescription();
        fields[1].setFieldType(Types.DOUBLE);
        fields[1].setFieldName(PluginServices.getText(this, "Valor"));
        fields[1].setFieldLength(50);
        fields[1].setFieldDecimalCount(25);
        fields[1].setFieldLength(100);
        orgDef.setFieldsDesc(fields);
        writer.initialize(orgDef);
      } catch (InitializeWriterException e) {
        logger.error("Error en la inicialización del writer");
      }
      try {
        writer.preProcess();
      } catch (StartWriterVisitorException e) {
        logger.error("Error en el preProcess del writer");
      }
      try {
        int index = 0;
        Value[] value = new Value[2];
        IFeature feat = null;

        Iterator<MyObjectStatistics> iterador = valores.listIterator();

        while (iterador.hasNext()) {
          MyObjectStatistics data = (MyObjectStatistics) iterador.next();
          value[0] = ValueFactory.createValue(data.getKey());
          value[1] = ValueFactory.createValue(data.getValue());
          feat = new DefaultFeature(null, value, "" + index++);
          write(writer, feat, index);
        }
      } catch (Exception e) {
        logger.error("Error en el write");
      }
      try {
        writer.postProcess(); // Operation finished
        JOptionPane.showMessageDialog(
            null,
            PluginServices.getText(this, "fichero_creado_en") + " " + endFile.getAbsolutePath(),
            PluginServices.getText(this, "fichero_creado_en_formato") + " dbf",
            JOptionPane.INFORMATION_MESSAGE); // Informing the user
      } catch (StopWriterVisitorException e) {
        logger.error("Error en el postProcess del writer");
      }
    } catch (Exception e) { // Informing the user
      logger.error("Error exportando a formato dbf");
      JOptionPane.showMessageDialog(
          null,
          PluginServices.getText(this, "Error_exportando_las_estadisticas")
              + " "
              + endFile.getAbsolutePath(),
          PluginServices.getText(this, "Error"),
          JOptionPane.ERROR_MESSAGE);
    }
  }