undo/redo example?

Questions on using RSyntaxTextArea should go here.

Moderator: robert

undo/redo example?

Postby mpitonia@enterasys.com » Thu Dec 03, 2009 6:23 pm

does anyone have an undo/redo example?

many thanks,

mike
mpitonia@enterasys.com
 

Re: undo/redo example?

Postby robert » Thu Dec 03, 2009 7:08 pm

Along with the new release, I'm hoping to finally get around to adding some more example snippets to the RSTA Examples Page. Could you explain what you'd like to see in an example of undo/redo? These functions should already be mapped to Ctrl+Z/Ctrl+Y, respectively.
User avatar
robert
 
Posts: 801
Joined: Sat May 10, 2008 5:16 pm

Re: undo/redo example?

Postby mpitonia@enterssys.com » Thu Dec 03, 2009 7:51 pm

robert,

i am porting my JEdit classes over to RSyntaxTextArea, and just assumed i would need to register an undomanager. i am still a little confused on what is considered an undoable action, and how to set them. presently cntrl z just removes all text entered in my document. i want to better control what gets removed, and in what increments. any code will be helpful.

thx,

mike
mpitonia@enterssys.com
 

Re: undo/redo example?

Postby mpitonia@enterasys.com » Thu Dec 03, 2009 8:21 pm

quick question...when the textarea is loaded with text how does one clear the undo event list so that when a ctrlZ is pressed the original text loaded is not removed?

thx,

mike
mpitonia@enterasys.com
 

Re: undo/redo example?

Postby Guest » Thu Dec 03, 2009 8:24 pm

sorry for stupid question...just found rSyntaxTextArea.discardAllEdits(); :(
Guest
 

Re: undo/redo example?

Postby robert » Fri Dec 04, 2009 5:42 am

Yes, discardAllEdits() will clear the undo history. If you load the contents of a file into an RSyntaxTextArea say via the read() method, the first thing you'll probably want to do is call discardAllEdits(), so the initial load is not undo-able.

The way undo/redo works in RSTA is that modifications of contiguous groups of characters are remembered as a single edit. So, for example, if you move the caret to the middle of a line, type "abc", then move down one line, and type "def", you'll need to press Ctrl+Z twice to undo both actions. The first time, "def" will be removed, the second time, "abc" will be removed. This is similar to how undo works in Microsoft Word.

This also means that if you paste in several lines of text with one paste, a single undo will remove all of the text.

Hope this helps!
User avatar
robert
 
Posts: 801
Joined: Sat May 10, 2008 5:16 pm

Re: undo/redo example?

Postby MystcMage » Fri Feb 19, 2010 10:36 pm

How could I check if there have been any edits since I last saved the contents without discarding all the edits?
MystcMage
 
Posts: 1
Joined: Fri Feb 19, 2010 10:32 pm

Re: undo/redo example?

Postby robert » Mon Feb 22, 2010 2:32 pm

The best way to do this is to extend TextEditorPane instead of RSyntaxTextArea. TextEditorPane provides the extra functionality on top of RSTA for a text editor in an application (loading and saving, encoding handling, etc.). It keeps a "dirty" flag that is set when the editor's contents change, and is cleared whenever the file is saved. This is probably the best way to know if the contents have been modified at any given time.

If you'd really rather just use an RSyntaxTextArea, you can either use a custom javax.swing.event.DocumentListener or javax.swing.event.UndoableEditListener. Attach either to the Document, and any event notification means the Document has been modified.
User avatar
robert
 
Posts: 801
Joined: Sat May 10, 2008 5:16 pm

Re: undo/redo example?

Postby Matias22 » Thu Mar 25, 2010 8:49 pm

And can you tell us what kind of method we have to use to UNDO/REDO?

As I can see it works in textArea popup, but I cand find these methods.
Matias22
 
Posts: 13
Joined: Fri Feb 26, 2010 4:46 pm

Re: undo/redo example?

Postby robert » Thu Mar 25, 2010 9:21 pm

If you're asking how to undo or redo programmatically, you can use these methods:

Code: Select all
textArea.undoLastAction();
textArea.redoLastAction();


These should essentially do what Ctrl+Z and Ctrl+Y do, respectively.
User avatar
robert
 
Posts: 801
Joined: Sat May 10, 2008 5:16 pm

Next

Return to Help

Who is online

Users browsing this forum: No registered users and 4 guests

cron