public static UndoRedoController enableUndoRedoForTextComponents(JDialog dlg, TextUndoRedoControllerFactory factory) {
Container cp = dlg.getContentPane();
if (!(cp instanceof JComponent)) {
throw new IllegalArgumentException("The dialog's content pane must be a JComponent");
}
final UndoRedoController ctrl = factory.newController((JComponent) cp);
dlg.addWindowListener(new WindowAdapter() {
@Override
public void windowClosed(WindowEvent e) {
ctrl.dispose();
}
});
return ctrl;
}