private void updatePreview() {
    ConnectionHandler connectionHandler = variablesBundle.getActiveConnection();
    Project project = connectionHandler.getProject();

    String previewText =
        variablesBundle.prepareStatementText(connectionHandler, this.statementText, true);

    for (StatementExecutionVariableValueForm variableValueForm : variableValueForms) {
      String errorText = variablesBundle.getError(variableValueForm.getVariable());
      if (errorText == null) variableValueForm.hideErrorLabel();
      else variableValueForm.showErrorLabel(errorText);
    }

    if (previewDocument == null) {
      PsiFileFactory psiFileFactory = PsiFileFactory.getInstance(project);

      SQLFile selectStatementFile =
          (SQLFile)
              psiFileFactory.createFileFromText(
                  "filter.sql",
                  connectionHandler.getLanguageDialect(SQLLanguage.INSTANCE),
                  previewText);

      selectStatementFile.setActiveConnection(connectionHandler);
      selectStatementFile.setCurrentSchema(variablesBundle.getCurrentSchema());
      previewDocument = DocumentUtil.getDocument(selectStatementFile);

      viewer = (EditorEx) EditorFactory.getInstance().createViewer(previewDocument, project);
      viewer.setEmbeddedIntoDialogWrapper(true);
      JScrollPane viewerScrollPane = viewer.getScrollPane();
      SyntaxHighlighter syntaxHighlighter =
          connectionHandler.getLanguageDialect(SQLLanguage.INSTANCE).getSyntaxHighlighter();
      EditorColorsScheme colorsScheme = viewer.getColorsScheme();
      viewer.setHighlighter(HighlighterFactory.createHighlighter(syntaxHighlighter, colorsScheme));
      viewer.setBackgroundColor(colorsScheme.getColor(ColorKey.find("CARET_ROW_COLOR")));
      viewerScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
      viewerScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
      // viewerScrollPane.setBorder(null);
      viewerScrollPane.setViewportBorder(
          new LineBorder(CompatibilityUtil.getEditorBackgroundColor(viewer), 4, false));

      EditorSettings settings = viewer.getSettings();
      settings.setFoldingOutlineShown(false);
      settings.setLineMarkerAreaShown(false);
      settings.setLineNumbersShown(false);
      settings.setVirtualSpace(false);
      settings.setDndEnabled(false);
      settings.setAdditionalLinesCount(2);
      settings.setRightMarginShown(false);
      previewPanel.add(viewer.getComponent(), BorderLayout.CENTER);

    } else {
      final String finalPreviewText = previewText;

      new WriteActionRunner() {
        public void run() {
          previewDocument.setText(finalPreviewText);
        }
      }.start();
    }
  }