Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » GEF » [GEF3] File changed outside editor
[GEF3] File changed outside editor [message #1752825] Fri, 27 January 2017 18:41 Go to next message
Stefan Winkler is currently offline Stefan WinklerFriend
Messages: 307
Registered: July 2009
Location: Germany
Senior Member
Hi all,

when I have a text editor open in Eclipse and the underlying file is changed on the file system outside the editor, I get a MessageDialog which asks if the editors contents shall be reloaded with the changed file.

I need something like this in GEF. I have had a look, but I didn't find any builtin solution (have I missed something?)

Therefore, I have implemented something myself:

// Xtend code:
def checkForFileModification()
{
	val file = editorInput.getAdapter(IFile)
	if (file != null)
	{
		// I remember the last known modification stamp in a field in my editor class
		if (file.getModificationStamp() != modificationStamp)
		{
			if (isDirty())
			{
				val answer = MessageDialog.openQuestion(site.shell,
					"File changed", '''Do you want to reload the file?''')
			if (!answer)
			{
				// do not read the file, but remember the modification stamp so we won't ask the user again
				modificationStamp = file.getModificationStamp()
				return
			}
		}

		// reload file contents
		readModelFromFile()
	}
}


I call this method from a few central places (e.g., commandStackChanged, partActivated), as I have seen the TextEditor does it similarly.

Now the question is how to implement readModelFromFile()?

I have tried this:
// read the resource.contents.get(0) from the IFile in the FileEditorInput
val model = getModel(getEditorInput()) 
getGraphicalViewer().setContents(model)
modificationStamp = file.getModificationStamp()
getCommandStack().flush()


But actually, this gives me SWTExceptions because GEF is trying to use disposed Color objects in my EditParts/Figures.

So, should I call something to clean up the graphicalViewer or the editor, before calling graphicalViewer.setContents() with a different model root object?

Does anyone know of some piece of example code which demonstrates file reloading when the underlying file changes?

Thanks in advance!

Best,
Stefan


Re: [GEF3] File changed outside editor [message #1752910 is a reply to message #1752825] Mon, 30 January 2017 14:56 Go to previous message
Stefan Winkler is currently offline Stefan WinklerFriend
Messages: 307
Registered: July 2009
Location: Germany
Senior Member
Hi all,

UPDATE:

after debugging a bit what is going on, I have figured out that my code above is generally correct. So, if anyone comes across this post, the above solution (getGraphicalViewer().setContents(newModel)) seems to work.

My problem was (and, more or less, still is) that I have created an additional custom layer on the root edit part (as described in several other posts in this forum), because I wanted that figure not to be scaled.
Now, when graphicalViewer.setContents() is called, this will remove the figure of the content (and all child figures recursively). But as my unscaled figure is located on another layer, it won't be removed.
Plus, during this call chain, editPart.removeChild or editPart.removeChildVisual are only called for the root edit part, not for the child edit parts, although the child edit parts are deactivated. Therefore, the figure remains alive while the edit part is deactivated. This leads to disposed resources in my case because the figure uses a Color object managed by the edit part.

I will try to add a hook to explicitly remove my stale figure or solve this another way.

Best,
Stefan



Previous Topic:Context buttons
Next Topic:Enable to create GEF_legacy logic plugin
Goto Forum:
  


Current Time: Fri Apr 26 18:39:59 GMT 2024

Powered by FUDForum. Page generated in 0.03336 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top