// Grab the first x lines from the given file...
  //
  private List<String> getFirst(FixedInputMeta meta, int limit)
      throws IOException, KettleValueException {

    List<String> lines = new ArrayList<String>();

    FixedInputMeta oneMeta = new FixedInputMeta();
    getInfo(oneMeta);

    // Add a single field with the width of the line...
    //
    int lineWidth = Integer.parseInt(oneMeta.getLineWidth());
    if (lineWidth <= 0) {
      throw new IOException("The width of a line can not be 0 or less.");
    }

    oneMeta.allocate(1);

    FixedFileInputField field = new FixedFileInputField();
    field.setName("Field1");
    field.setType(ValueMetaInterface.TYPE_STRING);
    field.setWidth(lineWidth);
    // CHECKSTYLE:Indentation:OFF
    oneMeta.getFieldDefinition()[0] = field;

    TransMeta previewMeta =
        TransPreviewFactory.generatePreviewTransformation(transMeta, oneMeta, wStepname.getText());

    TransPreviewProgressDialog progressDialog =
        new TransPreviewProgressDialog(
            shell, previewMeta, new String[] {wStepname.getText()}, new int[] {limit});
    progressDialog.open();

    Trans trans = progressDialog.getTrans();
    String loggingText = progressDialog.getLoggingText();

    if (!progressDialog.isCancelled()) {
      if (trans.getResult() != null && trans.getResult().getNrErrors() > 0) {
        EnterTextDialog etd =
            new EnterTextDialog(
                shell,
                BaseMessages.getString(PKG, "System.Dialog.PreviewError.Title"),
                BaseMessages.getString(PKG, "System.Dialog.PreviewError.Message"),
                loggingText,
                true);
        etd.setReadOnly();
        etd.open();
      }
    }

    // The rows are in the transformation...
    //
    RowMetaInterface previewRowsMeta = progressDialog.getPreviewRowsMeta(wStepname.getText());
    List<Object[]> previewRowsData = progressDialog.getPreviewRows(wStepname.getText());
    for (int i = 0; i < previewRowsData.size(); i++) {
      String line = previewRowsMeta.getString(previewRowsData.get(i), 0);
      lines.add(line);
    }

    return lines;
  }
  private void getInfo(FixedInputMeta fixedInputMeta) {

    fixedInputMeta.setFilename(wFilename.getText());
    fixedInputMeta.setLineWidth(wLineWidth.getText());
    fixedInputMeta.setBufferSize(wBufferSize.getText());
    fixedInputMeta.setLazyConversionActive(wLazyConversion.getSelection());
    fixedInputMeta.setHeaderPresent(wHeaderPresent.getSelection());
    fixedInputMeta.setLineFeedPresent(wLineFeedPresent.getSelection());
    fixedInputMeta.setRunningInParallel(wRunningInParallel.getSelection());
    fixedInputMeta.setFileType(FixedInputMeta.getFileType(wFileType.getText()));
    fixedInputMeta.setEncoding(wEncoding.getText());
    fixedInputMeta.setAddResultFile(wAddResult.getSelection());

    int nrNonEmptyFields = wFields.nrNonEmpty();
    fixedInputMeta.allocate(nrNonEmptyFields);

    for (int i = 0; i < nrNonEmptyFields; i++) {
      TableItem item = wFields.getNonEmpty(i);
      int colnr = 1;

      FixedFileInputField field = new FixedFileInputField();

      field.setName(item.getText(colnr++));
      field.setType(ValueMetaFactory.getIdForValueMeta(item.getText(colnr++)));
      field.setFormat(item.getText(colnr++));
      field.setWidth(Const.toInt(item.getText(colnr++), -1));
      field.setLength(Const.toInt(item.getText(colnr++), -1));
      field.setPrecision(Const.toInt(item.getText(colnr++), -1));
      field.setCurrency(item.getText(colnr++));
      field.setDecimal(item.getText(colnr++));
      field.setGrouping(item.getText(colnr++));
      field.setTrimType(ValueMetaString.getTrimTypeByDesc(item.getText(colnr++)));

      // CHECKSTYLE:Indentation:OFF
      fixedInputMeta.getFieldDefinition()[i] = field;
    }
    wFields.removeEmptyRows();
    wFields.setRowNums();
    wFields.optWidth(true);

    fixedInputMeta.setChanged();
  }
  private void getFixed() {
    final FixedInputMeta info = new FixedInputMeta();
    getInfo(info);

    Shell sh = new Shell(shell, SWT.DIALOG_TRIM | SWT.RESIZE | SWT.MAX | SWT.MIN);

    try {
      List<String> rows = getFirst(info, 50);
      fields = new ArrayList<FixedFileInputField>();
      fields.addAll(Arrays.asList(info.getFieldDefinition()));

      if (fields.isEmpty()) {
        FixedFileInputField field = new FixedFileInputField();
        field.setName(
            "Field"
                + 1); // TODO: i18n, see also FixedTableDraw class for other references of this
                      // String
        // --> getNewFieldname() method
        field.setType(ValueMetaInterface.TYPE_STRING);
        field.setTrimType(ValueMetaInterface.TRIM_TYPE_NONE);
        field.setWidth(Const.toInt(info.getLineWidth(), 80));
        field.setLength(-1);
        field.setPrecision(-1);
        fields.add(field);
      } else if (info.hasChanged()) {
        // try to reuse the field mappings already set up.
        int width = 0;
        List<FixedFileInputField> reuse = new ArrayList<FixedFileInputField>();
        for (FixedFileInputField field : fields) {
          width += field.getWidth();
          // does this field fit on the line given the line width?
          if (width <= Const.toInt(info.getLineWidth(), width)) {
            // yes, reuse it
            reuse.add(field);
          } else {
            // no, remove its width from the total reused width then quit looking for more
            width -= field.getWidth();
            continue;
          }
        }
        int lineWidth = Const.toInt(info.getLineWidth(), width);

        // set the last reused field to take up the rest of the line.
        FixedFileInputField lastField = reuse.get(reuse.size() - 1);

        // don't let the width be grater than the line width
        if (width > lineWidth) {
          width = lineWidth;
        }

        // determine width of last field : lineWidth - (totalWidthOfFieldsReused - lastFieldWidth)
        int lastWidth = Const.toInt(info.getLineWidth(), width) - (width - lastField.getWidth());
        // make the last field occupy the remaining space
        lastField.setWidth(lastWidth);
        // reuse the fields...
        fields = reuse;
      }

      final FixedFileImportWizardPage1 page1 =
          new FixedFileImportWizardPage1("1", props, rows, fields);
      page1.createControl(sh);
      final FixedFileImportWizardPage2 page2 =
          new FixedFileImportWizardPage2("2", props, rows, fields);
      page2.createControl(sh);

      Wizard wizard =
          new Wizard() {
            public boolean performFinish() {
              wFields.clearAll(false);

              for (int i = 0; i < fields.size(); i++) {
                FixedFileInputField field = fields.get(i);
                if (field.getWidth() > 0) {
                  TableItem item = new TableItem(wFields.table, SWT.NONE);
                  item.setText(1, field.getName());
                  item.setText(2, "" + ValueMetaFactory.getValueMetaName(field.getType()));
                  item.setText(3, "" + field.getFormat());
                  item.setText(4, "" + field.getWidth());
                  item.setText(5, field.getLength() < 0 ? "" : "" + field.getLength());
                  item.setText(6, field.getPrecision() < 0 ? "" : "" + field.getPrecision());
                  item.setText(7, "" + field.getCurrency());
                  item.setText(8, "" + field.getDecimal());
                  item.setText(9, "" + field.getGrouping());
                }
              }
              int size = wFields.table.getItemCount();
              if (size == 0) {
                new TableItem(wFields.table, SWT.NONE);
              }

              wFields.removeEmptyRows();
              wFields.setRowNums();
              wFields.optWidth(true);

              inputMeta.setChanged();

              return true;
            }
          };

      wizard.addPage(page1);
      wizard.addPage(page2);

      WizardDialog wd = new WizardDialog(shell, wizard);
      WizardDialog.setDefaultImage(GUIResource.getInstance().getImageWizard());
      wd.setMinimumPageSize(700, 375);
      wd.updateSize();
      wd.open();
    } catch (Exception e) {
      new ErrorDialog(
          shell,
          BaseMessages.getString(PKG, "FixedInputDialog.ErrorShowingFixedWizard.DialogTitle"),
          BaseMessages.getString(PKG, "FixedInputDialog.ErrorShowingFixedWizard.DialogMessage"),
          e);
    }
  }
  /** Copy information from the meta-data input to the dialog fields. */
  public void getData() {
    wStepname.setText(stepname);
    wFilename.setText(Const.NVL(inputMeta.getFilename(), ""));
    wLineWidth.setText(Const.NVL(inputMeta.getLineWidth(), ""));
    wLineFeedPresent.setSelection(inputMeta.isLineFeedPresent());
    wBufferSize.setText(Const.NVL(inputMeta.getBufferSize(), ""));
    wLazyConversion.setSelection(inputMeta.isLazyConversionActive());
    wHeaderPresent.setSelection(inputMeta.isHeaderPresent());
    wRunningInParallel.setSelection(inputMeta.isRunningInParallel());
    wFileType.setText(inputMeta.getFileTypeDesc());
    wEncoding.setText(Const.NVL(inputMeta.getEncoding(), ""));
    wAddResult.setSelection(inputMeta.isAddResultFile());

    for (int i = 0; i < inputMeta.getFieldDefinition().length; i++) {
      TableItem item = new TableItem(wFields.table, SWT.NONE);
      int colnr = 1;
      FixedFileInputField field = inputMeta.getFieldDefinition()[i];

      item.setText(colnr++, Const.NVL(field.getName(), ""));
      item.setText(colnr++, ValueMetaFactory.getValueMetaName(field.getType()));
      item.setText(colnr++, Const.NVL(field.getFormat(), ""));
      item.setText(colnr++, field.getWidth() >= 0 ? Integer.toString(field.getWidth()) : "");
      item.setText(colnr++, field.getLength() >= 0 ? Integer.toString(field.getLength()) : "");
      item.setText(
          colnr++, field.getPrecision() >= 0 ? Integer.toString(field.getPrecision()) : "");
      item.setText(colnr++, Const.NVL(field.getCurrency(), ""));
      item.setText(colnr++, Const.NVL(field.getDecimal(), ""));
      item.setText(colnr++, Const.NVL(field.getGrouping(), ""));
      item.setText(colnr++, ValueMetaString.getTrimTypeCode(field.getTrimType()));
    }
    wFields.removeEmptyRows();
    wFields.setRowNums();
    wFields.optWidth(true);

    enableFields();

    wStepname.selectAll();
    wStepname.setFocus();
  }