Search code examples
eclipsextextemfxtend

Eclipse: Getting a resource set after saving


Here is a code segment of my code (Working in xtext) (Saving a model in a file)

generatedModel.save(null)    //here generatedModel is an object of resource

Then when I try to retrieve info about this model using

...getResource(...)

I get the error that the following resource is not found But when I added Thread.sleep(3000) in between, it worked. Hence this is happening as the resource is being accessed before it get saved in the file. I tried to add a listener to check when the file is being saved. Here is the code:

var listener = new IResourceChangeListener() {

            override resourceChanged(IResourceChangeEvent event) {

                if (event.type === IResourceChangeEvent.POST_CHANGE) {
                    delta = event.delta
                }
                if (delta.kind === IResourceDelta.CHANGED) {
                    delta.accept(
                        new IResourceDeltaVisitor() {

                            override visit(IResourceDelta innerdelta) throws CoreException {
                                if (innerdelta.resource.type === IResource.FILE)
                                    println("YESYESYEYESYEYESYSY")
                                    else if(innerdelta.resource.type === IResource.FOLDER)
                                    println("PPPPPPPPPPPPPPPPPP")
                                    else if (innerdelta.resource.type === IResource.PROJECT)
                                    println("WWWWWWWWWWWWWWWWWWWW")
                                else
                                    println("NONONONONO")
                                    return false
                            }

                        }
                    )
                }
            }

        }
        var workspace = ResourcesPlugin.workspace
        workspace.addResourceChangeListener(listener)

but this isn't helping. It is printing NONONO.. always upon changes. I am saving the files in other folder in the same work space. b(project) -> scr-gen(folder)->code(folder)-> files...


Solution

  • Your visit method should return true so that all the resources in the delta are visited.

    Resource change events can contain multiple nested deltas, you need to look at them all. The delta will normally have the workspace at the top level delta, with the project at the next level, then any folders, finally the files.