/**
   * Evaluates the page
   *
   * <p>This checks whether the current settings on the page make any sense. If everything is fine,
   * the settings are being put into the appropriate data container {@link ImportWizardModel} and
   * the current page is marked as complete by invoking {@link #setPageComplete(boolean)}. Otherwise
   * an error message is set, which will make sure the user is informed about the reason for the
   * error.
   */
  private void evaluatePage() {

    setPageComplete(false);
    setErrorMessage(null);
    tablePreview.setVisible(false);

    if (comboLocation.getText().equals("")) { // $NON-NLS-1$
      return;
    }

    try {
      if (!customLinebreak) {
        detectLinebreak();
        comboLinebreak.select(selectedLinebreak);
      }
      if (!customDelimiter) {
        detectDelimiter();
        comboDelimiter.select(selectedDelimiter);
      }
      readPreview();

    } catch (IOException | IllegalArgumentException e) {
      setErrorMessage(e.getMessage());
      return;
    } catch (TextParsingException e) {
      setErrorMessage(Resources.getMessage("ImportWizardPageCSV.16")); // $NON-NLS-1$
      return;
    } catch (RuntimeException e) {
      if (e.getCause() != null) {
        setErrorMessage(e.getCause().getMessage());
      } else {
        setErrorMessage(e.getMessage());
      }
      return;
    }

    /* Put data into container */
    ImportWizardModel data = wizardImport.getData();

    data.setWizardColumns(wizardColumns);
    data.setPreviewData(previewData);
    data.setFirstRowContainsHeader(btnContainsHeader.getSelection());
    data.setFileLocation(comboLocation.getText());
    data.setCsvDelimiter(delimiters[selectedDelimiter]);
    data.setCsvQuote(quotes[selectedQuote]);
    data.setCsvEscape(escapes[selectedEscape]);
    data.setCharset(
        Charsets.getCharsetForName(Charsets.getNamesOfAvailableCharsets()[selectedCharset]));
    data.setCsvLinebreak(
        CSVSyntax.getLinebreakForLabel(CSVSyntax.getAvailableLinebreaks()[selectedLinebreak]));

    /* Mark page as completed */
    setPageComplete(true);
  }
  /**
   * Reads in preview data
   *
   * <p>This goes through up to {@link ImportWizardModel#PREVIEW_MAX_LINES} lines within the
   * appropriate file and reads them in. It uses {@link ImportAdapter} in combination with {@link
   * ImportConfigurationCSV} to actually read in the data.
   *
   * @throws IOException Signals that an I/O exception has occurred.
   */
  private void readPreview() throws IOException {

    /* Reset preview data */
    previewData.clear();

    /* Parameters from the user interface */
    final String location = comboLocation.getText();
    final char delimiter = delimiters[selectedDelimiter];
    final char[] linebreak =
        CSVSyntax.getLinebreakForLabel(CSVSyntax.getAvailableLinebreaks()[selectedLinebreak]);
    final char quote = quotes[selectedQuote];
    final char escape = escapes[selectedEscape];
    final boolean containsHeader = btnContainsHeader.getSelection();
    final Charset charset =
        Charsets.getCharsetForName(Charsets.getNamesOfAvailableCharsets()[selectedCharset]);

    /* Variables needed for processing */
    final CSVDataInput in =
        new CSVDataInput(location, charset, delimiter, quote, escape, linebreak);
    final Iterator<String[]> it = in.iterator();
    final String[] firstLine;
    wizardColumns = new ArrayList<ImportWizardModelColumn>();
    ImportConfigurationCSV config =
        new ImportConfigurationCSV(
            location, charset, delimiter, quote, escape, linebreak, containsHeader);

    /* Check whether there is at least one line in file and retrieve it */
    if (it.hasNext()) {
      firstLine = it.next();
    } else {
      in.close();
      throw new IOException(Resources.getMessage("ImportWizardPageCSV.17")); // $NON-NLS-1$
    }

    /* Iterate over columns and add it to {@link #allColumns} */
    for (int i = 0; i < firstLine.length; i++) {

      ImportColumn column = new ImportColumnCSV(i, DataType.STRING);
      ImportWizardModelColumn wizardColumn = new ImportWizardModelColumn(column);

      wizardColumns.add(wizardColumn);
      config.addColumn(column);
    }

    /* Create adapter to import data with given configuration */
    ImportAdapter importAdapter = ImportAdapter.create(config);

    /* Get up to {ImportData#previewDataMaxLines} lines for previewing */
    int count = 0;
    while (importAdapter.hasNext() && (count <= ImportWizardModel.PREVIEW_MAX_LINES)) {
      previewData.add(importAdapter.next());
      count++;
    }

    in.close();

    /* Remove first entry as it always contains name of columns */
    previewData.remove(0);

    /* Check whether there is actual any data */
    if (previewData.size() == 0) {
      throw new IOException(Resources.getMessage("ImportWizardPageCSV.18")); // $NON-NLS-1$
    }

    /*
     * Show preview in appropriate table
     */

    /* Disable redrawing once redesign is finished */
    tablePreview.setRedraw(false);

    /* Remove all of the old columns */
    while (tablePreview.getColumnCount() > 0) {
      tablePreview.getColumns()[0].dispose();
    }

    /* Add new columns */
    for (ImportWizardModelColumn column : wizardColumns) {

      TableViewerColumn tableViewerColumn = new TableViewerColumn(tableViewerPreview, SWT.NONE);
      tableViewerColumn.setLabelProvider(
          new CSVColumnLabelProvider(((ImportColumnCSV) column.getColumn()).getIndex()));

      TableColumn tableColumn = tableViewerColumn.getColumn();
      tableColumn.setWidth(100);

      if (btnContainsHeader.getSelection()) {
        tableColumn.setText(column.getColumn().getAliasName());
        tableColumn.setToolTipText(
            Resources.getMessage("ImportWizardPageCSV.19")
                + ((ImportColumnCSV) column.getColumn()).getIndex()); // $NON-NLS-1$
      }
      ColumnViewerToolTipSupport.enableFor(tableViewerPreview, ToolTip.NO_RECREATE);
    }

    /* Setup preview table */
    tableViewerPreview.setInput(previewData);
    tablePreview.setHeaderVisible(btnContainsHeader.getSelection());
    tablePreview.setVisible(true);
    tablePreview.layout();
    tablePreview.setRedraw(true);
  }