Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Word-based text undo/redo

 
Steve Sinai
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Thanks.
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic