public static void setTextWithUndo(@NotNull JTextComponent field, @NotNull String text) {
    if (field.getText().equals(text)) return;
    addUndoRedoActions(field);
    Document doc = field.getDocument();
    UndoManager undoManager = Objects.requireNonNull(ClientProperty.get(field, UNDO_MANAGER));
    CompoundEdit edits = new CompoundEdit();
    UndoableEditListener tempListener = new UndoableEditListener() {
      @Override
      public void undoableEditHappened(UndoableEditEvent e) {
        edits.addEdit(e.getEdit());
      }
    };
    doc.removeUndoableEditListener(undoManager);
    doc.addUndoableEditListener(tempListener);
    try {
      doc.remove(0, doc.getLength());
      doc.insertString(0, text, null);
      edits.end();
      undoManager.addEdit(edits);
    }
    catch (BadLocationException e) {
      UIManager.getLookAndFeel().provideErrorFeedback(field);
    }
    finally {
      doc.removeUndoableEditListener(tempListener);
      doc.addUndoableEditListener(undoManager);
    }
  }
public static void setTextWithUndo(@NotNull JTextComponent field, @NotNull String text)