@Override
  protected boolean performLeavingAction(WizardStepDirection direction) {
    if (direction == WizardStepDirection.FINISH) {
      try {
        if (state.getTranslator() != null) {
          state.getTranslator().close();
        }
      } catch (OperatorException e) {
        ImportWizardUtils.showErrorMessage(
            state.getDataResultSetFactory().getResourceName(), e.toString(), e);
      }

      // use settings edited by user even if he never pressed Enter or otherwise confirmed his
      // changes
      for (int i = 0; i < previewTable.getColumnCount(); i++) {
        EditableTableHeaderColumn col =
            (EditableTableHeaderColumn) previewTable.getColumnModel().getColumn(i);
        if (col.getHeaderEditor() instanceof MetaDataTableHeaderCellEditor) {
          MetaDataTableHeaderCellEditor editor =
              (MetaDataTableHeaderCellEditor) col.getHeaderEditor();
          editor.updateColumnMetaData();
        }
      }
    }
    return true;
  }
  private void updateTableModel(ExampleSet exampleSet) {
    if (previewTable == null) {
      previewTable = new ExtendedJTable(false, false, false);
    }

    // data model
    DataTableViewerTableModel model =
        new DataTableViewerTableModel(new DataTableExampleSetAdapter(exampleSet, null));
    List<Integer> rowsList = new LinkedList<Integer>();
    int lastHit = -1;
    for (ParsingError error : state.getTranslator().getErrors()) {
      if (error.getExampleIndex() != lastHit) {
        rowsList.add(error.getExampleIndex());
        lastHit = error.getExampleIndex();
      }
    }
    int[] rowMap = new int[rowsList.size()];
    int j = 0;
    for (Integer row : rowsList) {
      rowMap[j++] = row;
    }
    filteredModel = new RowFilteringTableModel(model, rowMap, filterErrorsBox.isSelected());
    previewTable.setModel(filteredModel);

    // Header model

    TableColumnModel columnModel = previewTable.getColumnModel();
    previewTable.setTableHeader(new EditableTableHeader(columnModel));
    // header editors and renderers and values
    MetaDataTableHeaderCellEditor headerRenderer = new MetaDataTableHeaderCellEditor();
    MetaDataTableHeaderCellEditor headerEditor = new MetaDataTableHeaderCellEditor(headerValidator);
    headerValidator.addHeaderRenderer(headerRenderer);
    for (int i = 0; i < previewTable.getColumnCount(); i++) {
      EditableTableHeaderColumn col =
          (EditableTableHeaderColumn) previewTable.getColumnModel().getColumn(i);
      col.setHeaderValue(state.getTranslationConfiguration().getColumnMetaData()[i]);
      col.setHeaderRenderer(headerRenderer);
      col.setHeaderEditor(headerEditor);
    }
    previewTable.getTableHeader().setReorderingAllowed(false);

    previewTable.setCellColorProvider(
        new CellColorProviderAlternating() {

          @Override
          public Color getCellColor(int row, int column) {
            row = filteredModel.translateRow(row);
            ParsingError error = state.getTranslator().getErrorByExampleIndexAndColumn(row, column);
            if (error != null) {
              return SwingTools.DARK_YELLOW;
            } else {
              return super.getCellColor(row, column);
            }
          }
        });
    tableScrollPane.setViewportView(previewTable);
  }
  @Override
  protected boolean performEnteringAction(WizardStepDirection direction) {
    dateFormatField.setSelectedItem(state.getTranslationConfiguration().getDatePattern());
    errorsAsMissingBox.setSelected(state.getTranslationConfiguration().isFaultTolerant());

    new ProgressThread("loading_data") {

      @Override
      public void run() {
        DataResultSet previewResultSet = null;
        try {
          previewResultSet = state.getDataResultSetFactory().makeDataResultSet(state.getOperator());
          state.getTranslationConfiguration().reconfigure(previewResultSet);
        } catch (OperatorException e1) {
          ImportWizardUtils.showErrorMessage(
              state.getDataResultSetFactory().getResourceName(), e1.toString(), e1);
          return;
        } finally {
          if (previewResultSet != null) {
            try {
              previewResultSet.close();
            } catch (OperatorException e) {
              LogService.getRoot()
                  .log(
                      Level.WARNING,
                      "Could not close preview result for "
                          + state.getDataResultSetFactory().getResourceName()
                          + "!");
            }
          }
        }

        try {
          TableModel dataPreview =
              state.getDataResultSetFactory().makePreviewTableModel(getProgressListener());
          // Copy name annotations to name
          int nameIndex = state.getTranslationConfiguration().getNameRow();
          if (nameIndex != -1 && dataPreview != null) {
            for (int i = 0; i < dataPreview.getColumnCount(); i++) {
              ColumnMetaData columnMetaData =
                  state.getTranslationConfiguration().getColumnMetaData(i);
              final String foundName = (String) dataPreview.getValueAt(nameIndex, i);
              if ((foundName != null) && !foundName.isEmpty()) {
                columnMetaData.setUserDefinedAttributeName(foundName);
              }
            }
          }
        } catch (Exception e) {
          ImportWizardUtils.showErrorMessage(
              state.getDataResultSetFactory().getResourceName(), e.toString(), e);
          return;
        }
        guessValueTypes();
      }
    }.start();
    return true;
  }
 @Override
 public void finish() {
   super.finish();
   if (reader != null) { // we are configuring an operator
     state.getTranslationConfiguration().setParameters(reader);
     state.getDataResultSetFactory().setParameters(reader);
     getState().getDataResultSetFactory().close();
   }
 }
  public void updateErrors() {
    final List<ParsingError> errorList = new ArrayList<ParsingError>();

    canProceed = true;
    if (headerValidator.getErrors().size() > 0) {
      List<ParsingError> headerErrors = headerValidator.getErrors();
      errorList.addAll(headerErrors);
      canProceed = false;
    }
    errorList.addAll(state.getTranslator().getErrors());
    SwingUtilities.invokeLater(
        new Runnable() {

          @Override
          public void run() {

            final int size = errorList.size();
            errorLabel.setText(size + " errors.");
            if (size == 0) {
              errorLabel.setIcon(SwingTools.createIcon("16/ok.png"));
            } else {
              errorLabel.setIcon(SwingTools.createIcon("16/error.png"));
            }
          }
        });
    errorTableModel.setErrors(errorList);
    fireStateChanged();
  }
 private void cancelGuessing() {
   state.getTranslator().cancelGuessing();
   state.getTranslator().cancelLoading();
   guessButton.setEnabled(false);
 }
 private void cancelReload() {
   state.getTranslator().cancelLoading();
   reloadButton.setEnabled(false);
 }
  public MetaDataDeclarationWizardStep(WizardState state) {
    super("importwizard.metadata");
    limitedPreviewBox.setSelected(true);

    this.state = state;
    this.headerValidator = new MetaDataValidator(this, state.getTranslationConfiguration());
    dateFormatField.setEditable(true);
    dateFormatField.addActionListener(
        new ActionListener() {

          @Override
          public void actionPerformed(ActionEvent e) {
            MetaDataDeclarationWizardStep.this
                .state
                .getTranslationConfiguration()
                .setDatePattern((String) dateFormatField.getSelectedItem());
          }
        });

    JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
    buttonPanel.add(reloadButton);
    buttonPanel.add(guessButton);
    buttonPanel.add(limitedPreviewBox);

    JLabel label = new ResourceLabel("date_format");
    label.setLabelFor(dateFormatField);
    buttonPanel.add(label);
    buttonPanel.add(dateFormatField);
    panel.add(buttonPanel, BorderLayout.NORTH);

    JPanel errorPanel = new JPanel(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.BOTH;
    c.anchor = GridBagConstraints.FIRST_LINE_START;
    c.ipadx = c.ipady = 4;
    c.weighty = 0;
    c.weightx = 1;

    c.gridwidth = 1;
    c.weightx = 1;
    errorPanel.add(errorLabel, c);

    c.weightx = 0;
    c.gridwidth = GridBagConstraints.RELATIVE;
    errorPanel.add(errorsAsMissingBox, c);
    c.weightx = 0;
    c.gridwidth = GridBagConstraints.REMAINDER;
    errorPanel.add(filterErrorsBox, c);

    final JTable errorTable = new JTable(errorTableModel);
    errorTable
        .getSelectionModel()
        .addListSelectionListener(
            new ListSelectionListener() {

              @Override
              public void valueChanged(ListSelectionEvent e) {
                if (!e.getValueIsAdjusting()) {
                  final int selected = errorTable.getSelectedRow();
                  if (selected >= 0) {
                    ParsingError error = errorTableModel.getErrorInRow(selected);
                    int row = error.getExampleIndex();
                    row = filteredModel.inverseTranslateRow(row);
                    if (row == -1) {
                      return;
                    }
                    int col = error.getColumn();
                    previewTable.setRowSelectionInterval(row, row);
                    previewTable.setColumnSelectionInterval(col, col);
                  }
                }
              }
            });
    final JScrollPane errorScrollPane = new JScrollPane(errorTable);
    errorScrollPane.setPreferredSize(new Dimension(500, 80));
    c.weighty = 1;
    c.gridwidth = GridBagConstraints.REMAINDER;
    errorPanel.add(errorScrollPane, c);

    panel.add(errorPanel, BorderLayout.SOUTH);

    final JLabel dummy = new JLabel("-");
    dummy.setPreferredSize(new Dimension(500, 500));
    dummy.setMinimumSize(new Dimension(500, 500));
    tableScrollPane =
        new JScrollPane(
            dummy, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    panel.add(tableScrollPane, BorderLayout.CENTER);
  }