Document modification

Questions on using RSyntaxTextArea should go here.

Moderator: robert

Document modification

Postby golana » Tue Jul 05, 2011 7:28 am

Hi,

I was very happy to find this library.
It is very usefull, with great features !.

How can I tell if the document inside the editor has changed ?
I would like to add a "*" in the title to notify that document has changed after last save.
Can you give me a pointer ?

Thanks.
golana
 
Posts: 1
Joined: Tue Jul 05, 2011 7:23 am

Re: Document modification

Postby robert » Tue Jul 05, 2011 11:07 pm

Hi Golana,

The easiest way to do this is to add a javax.swing.event.DocumentListener to the editor's Document, and add the "*" to the displayed file name when the document is modified. Remove it whenever it is saved.

You can also use the org.fife.ui.rsyntaxtextarea.TextEditorPane class, that comes standard in the library. It has a "dirty" flag that is set/cleared properly when you edit/save/reload the file. You can also query it directly via the isDirty() method. Note that you'll still have to add the DocumentListener though to know when it's first made dirty.

From its documentation:

An extension of RSyntaxTextArea that adds information about the file being edited, such as:

Its name and location.
Is it dirty?
Is it read-only?
The last time it was loaded or saved to disk (local files only).
The file's encoding on disk.
Easy access to the line separator.

Loading and saving is also built into the editor.

Both local and remote files (e.g. ftp) are supported. See the FileLocation class for more information.
User avatar
robert
 
Posts: 760
Joined: Sat May 10, 2008 5:16 pm

Re: Document modification

Postby Taneeda » Wed Apr 25, 2012 5:05 pm

Hi,

i am using TextEditorPane inside RTextScrollPane. So how can i access the TextEditorPane inside the RTextScrollpane, which is stored in a tab of JTabbedPane?

Update:
Found it, just:
Code: Select all
RTextScrollPane sp = (RTextScrollPane)jTabbedPane.getComponentAt(i);
JOptionPane.showConfirmDialog(null, sp.getTextArea().getClass().getName());

Example:
Code: Select all
((TextEditorPane)sp.getTextArea()).isDirty()


So, i have another question: When i check with isDirty() i get true, even if hadn't done any changes. Why that?

Greetz Taneeda
Image
User avatar
Taneeda
 
Posts: 10
Joined: Fri Jun 19, 2009 12:35 pm
Location: Oldenburg, Germany

Re: Document modification

Postby robert » Fri Apr 27, 2012 1:46 am

Taneeda wrote:So, i have another question: When i check with isDirty() i get true, even if hadn't done any changes. Why that?


It shouldn't be. How did you load the initial text? If you used TextEditorPane.load(), the dirty flag should not be set. Obviously, calling something like setText() to put the initial text in will set the dirty flag to true. So will calling setEncoding() - TextEditorPane considers an encoding change to be an important change. My guess is you're doing one of those two things. If you are, and you have to do them, just call setDirty(false) after your initialization and you should be fine.
User avatar
robert
 
Posts: 760
Joined: Sat May 10, 2008 5:16 pm

Re: Document modification

Postby Taneeda » Fri Apr 27, 2012 1:19 pm

robert wrote:...Obviously, calling something like setText() to put the initial text in will set the dirty flag to true. ...


That was the reason. I solved it by using load(), thanks for the help :)

robert wrote:...just call setDirty(false) after your initialization...

The visibility of the setDirty() method in TextEditorPane is set to private... Maybe, this is a bug? Am i misunderstanding you?
Image
User avatar
Taneeda
 
Posts: 10
Joined: Fri Jun 19, 2009 12:35 pm
Location: Oldenburg, Germany

Re: Document modification

Postby robert » Fri Apr 27, 2012 3:13 pm

Oops, didn't realize that was a private method. I guess my thinking was that the editor should always know when it was dirty and when it wasn't.
User avatar
robert
 
Posts: 760
Joined: Sat May 10, 2008 5:16 pm

Re: Document modification

Postby omadawn » Sat Aug 18, 2012 12:08 am

How do I open an enhancement request to have isDirty(boolean) created so that I can do isDirty(false) the way you describe above? I would love that functionality.
omadawn
 
Posts: 38
Joined: Sat Jul 21, 2012 7:39 pm

Re: Document modification

Postby robert » Sat Aug 18, 2012 2:54 pm

The method will be public in SVN later today, and in the next release. I'm including information in the doc comment stating that the programmer usually doesn't need to call the method directly. The only time I can imagine you'd need to do so is if you needed to initialize a TextEditorPane with content that did not come from a file or URL.
User avatar
robert
 
Posts: 760
Joined: Sat May 10, 2008 5:16 pm

Re: Document modification

Postby omadawn » Sun Aug 19, 2012 8:43 pm

That was excactly why I needed it. I'm getting the source code I will be editing in a list of a custom object type. It's actually pretty inefficient in my case to get each piece of code separately.

I didn't quite understand FileLocation so I was creating the TextEditorPane empty and using setText() to set the editor text.

I have since then realized that using FileLocation was the only way to get it to work so I monkeyed around with that and have mostly figured it out. I have a separate forum thread for an issue that I'm running in to when a save fails.

This functionality would have let me do it without FileLocation.
omadawn
 
Posts: 38
Joined: Sat Jul 21, 2012 7:39 pm

Re: Document modification

Postby omadawn » Sun Aug 19, 2012 8:43 pm

And thanks
omadawn
 
Posts: 38
Joined: Sat Jul 21, 2012 7:39 pm

Next

Return to Help

Who is online

Users browsing this forum: No registered users and 2 guests