The moose likes Swing / AWT / SWT and the fly likes Word-based text undo/redo Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Word-based text undo/redo" Watch "Word-based text undo/redo" New topic

Word-based text undo/redo

Steve Sinai

Joined: Aug 21, 2004
Posts: 25
Does anyone know of coding example for word-based text undo? By default, Swing undoes and redoes typed-in text one character at a time, but I'd like to undo and redo based on whole words. This is the way most professional word-processing programs do it.

I have a way of doing word-based undo and redo, but it's so complicated and kludgy that I figure there must be a better way out there.

Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10825

These might help:

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

I haven't used many professional word-processing programs, I've only used Microsoft Word. (Insert Microsoft joke here if you like.) And it doesn't use word-based undo and redo, it gathers up continuous sets of added or deleted text and undoes and redoes those sets. So if you type "Hello Word" and then backspace and type an "l" before the "d", you have two units that can be undone. Clicking Ctrl-Z changes "Hello World" to "Hello Word" and clicking it again makes the whole thing disappear.

Anyway I wrote a subclass of UndoManager that replicated that behaviour. Here's an excerpt from it:

And here's the (inner) ExpandableEdit class which it uses:
That's just a bleeding chunk of code, it won't compile as is. But it's the basic core of Word-emulating undo and redo.
I agree. Here's the link:
subject: Word-based text undo/redo
It's not a secret anymore!