public void dispose() {
   super.dispose();
   for (StatementExecutionVariableValueForm variableValueForm : variableValueForms) {
     variableValueForm.dispose();
   }
   variableValueForms.clear();
   variablesBundle = null;
   EditorFactory.getInstance().releaseEditor(viewer);
 }
  public StatementExecutionVariablesForm(
      StatementExecutionVariablesBundle variablesBundle, String statementText) {
    this.variablesBundle = variablesBundle;
    this.statementText = statementText;

    variablesPanel.setLayout(new BoxLayout(variablesPanel, BoxLayout.Y_AXIS));

    for (StatementExecutionVariable variable : variablesBundle.getVariables()) {
      StatementExecutionVariableValueForm variableValueForm =
          new StatementExecutionVariableValueForm(variable);
      variableValueForms.add(variableValueForm);
      variablesPanel.add(variableValueForm.getComponent());
      variableValueForm.addDocumentListener(
          new DocumentAdapter() {
            protected void textChanged(DocumentEvent e) {
              updatePreview();
            }
          });
      variableValueForm.addActionListener(
          new ActionListener() {
            public void actionPerformed(ActionEvent e) {
              updatePreview();
            }
          });
    }

    int[] metrics = new int[] {0, 0};
    for (StatementExecutionVariableValueForm variableValueForm : variableValueForms) {
      metrics = variableValueForm.getMetrics(metrics);
    }

    for (StatementExecutionVariableValueForm variableValueForm : variableValueForms) {
      variableValueForm.adjustMetrics(metrics);
    }
    updatePreview();
    GuiUtils.replaceJSplitPaneWithIDEASplitter(mainPanel);

    Dimension preferredSize = variablesPanel.getPreferredSize();
    int height = (int) Math.min(preferredSize.getHeight(), 200) + 24;
    splitPane.setDividerLocation(height);
    mainPanel.setPreferredSize(
        new Dimension((int) mainPanel.getPreferredSize().getWidth(), height + 250));
  }
  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();
    }
  }
 public void saveValues() {
   for (StatementExecutionVariableValueForm variableValueForm : variableValueForms) {
     variableValueForm.saveValue();
   }
 }