views:

368

answers:

2

In Eclipse, if I change a file programmatically, and it is open in a text editor, it doesn't always reload, not even when refreshing the resource programmatically. How can I forcibly reload the text editor from code so that it show the changed file contents?

+2  A: 

In your project explorer or navigator, you can right-click on the file that's currently open and select refresh. This has always worked for me, even when editing files with several programs. Make sure to click the file itself, not parent objects like packages or folders or projects.

Edit

Refreshing programmatically? I would look into an Eclipse scripting tool:

http://eclipse-shell.sourceforge.net/

I guess there was another one called Monkey, but it doesn't appear to be maintained.

Jonathon
I know this, but I want to do it programmatically. Sorry for not being clear enough, edited question to avoid misunderstanding.
thSoft
Thanks for the tip. I solved the problem more simply: I replace the editor's contents without overwriting the old file - this way, the user has an option to undo the processing.
thSoft
@thSoft Good, cause I hate when my editor contents get replaced in front of my eyes and I frantically hit Ctrl-Z and nothing happens cause the program has helpfully reloaded the file.
Jonathon
+1  A: 

I don't know of any possibility to programmatically reload the file.

Some editors (e.g. GMF editors) look for changes in the underlying files, and refresh themselves, but this is not required at all.

I don't think that a forced reload is an option implemented globally, as in some cases there could be some merging steps involved that can be quite erroneous.

My ideas to solve this:

  • Have a specific editor that refreshes its content when the used resource changes (this can be timeconsuming);
  • Or close the editors of the file and reopen them (this is ugly in the eye of the user).
Zoltán Ujhelyi
I use Xtext's predefined editor, so the former option was not viable. Instead of the latter, I tried to call the editor's init() method, but this behaved the same way - not refreshing indeterministically. Fortunately, I could query the new contents instead of updating the file, and hence solved it like I described it above.
thSoft