Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Repost : Reload-refresh resource supporting opened editor
Repost : Reload-refresh resource supporting opened editor [message #687163] Wed, 08 June 2011 15:17 Go to next message
Steven Derrien is currently offline Steven DerrienFriend
Messages: 50
Registered: July 2009
Member
Hi,

After claoing and reopenign my editor at lest 3000 time upon model file change, I realized that
I would need to make it refresh itself upon a model file change caused by an
external modification (editor, program, etc).

I searched the newsgroup and found the thread below, but since I am
not enough confortable with EMF editor/JFace stuff, I could not see how
to use Ed's answer to solve the problem.

> Carlos,
>
> Have a look in a generated editor how it handles a resource delta. Look
> for the code that calls Resource.unload...
>
>
> Carlos wrote:
>> Hello.
>>
>> I want to implement an utility for reloading a XMI resource into the
>> editor.
>> I have tried to close the editor and open it, but this is not very
>> smart. I would prefer loading the model without closing the editor.
>>
>> Is there any method in the EMF API to do this?
>> Have you got any suggestion?
>>
>> Thanks in advance.
>> Best regards.
>> Carlos.


Looking at the generated code I found the method below, but I don't know how to
customize is so as to make a proper use of resource.unload().

Any help would be greatly appreciated.


protected IResourceChangeListener resourceChangeListener =
new IResourceChangeListener() {
public void resourceChanged(IResourceChangeEvent event) {
IResourceDelta delta = event.getDelta();
try {
class ResourceDeltaVisitor implements IResourceDeltaVisitor {
protected ResourceSet resourceSet = editingDomain.getResourceSet();
protected Collection<Resource> changedResources = new ArrayList<Resource>();
protected Collection<Resource> removedResources = new ArrayList<Resource>();

public boolean visit(IResourceDelta delta) {
int type = delta.getResource().getType();
if (type == IResource.FILE) {
if (delta.getKind() == IResourceDelta.REMOVED ||
delta.getKind() == IResourceDelta.CHANGED && delta.getFlags() != IResourceDelta.MARKERS) {
URI createPlatformResourceURI = URI.createPlatformResourceURI(delta.getFullPath().toString(), true);
Resource resource = resourceSet.getResource(createPlatformResourceURI, false);

if (resource != null) {
if (delta.getKind() == IResourceDelta.REMOVED) {
removedResources.add(resource);
}
else if (!savedResources.remove(resource)) {
changedResources.add(resource);

/****

I understand I should reload the model somewhere in
here using resource.unload(); but always get an NPE exception


*****/




}
}
}
}

return true;
}

public Collection<Resource> getChangedResources() {
return changedResources;
}

public Collection<Resource> getRemovedResources() {
return removedResources;
}
}

thks,

Steven Derrien
Re: Repost : Reload-refresh resource supporting opened editor [message #687166 is a reply to message #687163] Wed, 08 June 2011 15:47 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
Steven,

Comments below.

Steven Derrien wrote:
> Hi,
>
> After claoing and reopenign my editor at lest 3000 time upon model
> file change, I realized that
> I would need to make it refresh itself upon a model file
> change caused by an
> external modification (editor, program, etc).
Keep in mind that the code EMF has in the editor kicks in when the
workspace knows about the change, i.e., when the resource is changed
using the Eclipse resource APIs by say another editor. If the change is
external, the workspace needs to be refreshed. There's a preference to
auto refresh it, so maybe that's all you're missing...
>
> I searched the newsgroup and found the thread below, but
> since I am
> not enough confortable with EMF editor/JFace stuff, I could
> not see how
> to use Ed's answer to solve the problem.
>
> > Carlos,
> >
> > Have a look in a generated editor how it handles a
> resource delta. Look
> > for the code that calls Resource.unload...
> >
> >
> > Carlos wrote:
> >> Hello.
> >>
> >> I want to implement an utility for reloading a XMI
> resource into the
> >> editor.
> >> I have tried to close the editor and open it, but this
> is not very
> >> smart. I would prefer loading the model without closing
> the editor.
> >>
> >> Is there any method in the EMF API to do this?
> >> Have you got any suggestion?
> >>
> >> Thanks in advance.
> >> Best regards.
> >> Carlos.
>
>
> Looking at the generated code I found the method below, but I
> don't know how to
> customize is so as to make a proper use of resource.unload().
>
> Any help would be greatly appreciated.
>
>
> protected IResourceChangeListener resourceChangeListener =
> new IResourceChangeListener() {
> public void resourceChanged(IResourceChangeEvent event) {
> IResourceDelta delta = event.getDelta();
> try {
> class ResourceDeltaVisitor implements IResourceDeltaVisitor {
> protected ResourceSet resourceSet =
> editingDomain.getResourceSet();
> protected Collection<Resource> changedResources = new
> ArrayList<Resource>();
> protected Collection<Resource> removedResources = new
> ArrayList<Resource>();
>
> public boolean visit(IResourceDelta delta) {
> int type = delta.getResource().getType();
> if (type == IResource.FILE) {
> if (delta.getKind() == IResourceDelta.REMOVED ||
> delta.getKind() == IResourceDelta.CHANGED &&
> delta.getFlags() != IResourceDelta.MARKERS) {
> URI createPlatformResourceURI =
> URI.createPlatformResourceURI(delta.getFullPath().toString(), true);
> Resource resource =
> resourceSet.getResource(createPlatformResourceURI, false);
>
> if (resource != null) {
> if (delta.getKind() == IResourceDelta.REMOVED) {
> removedResources.add(resource);
> }
> else if (!savedResources.remove(resource)) {
> changedResources.add(resource);
>
> /****
>
> I understand I should reload the model somewhere in
> here using resource.unload(); but always get an NPE exception
Why do you get one. This code should already just work...
>
>
> *****/
>
>
>
>
> }
> }
> }
> }
>
> return true;
> }
>
> public Collection<Resource> getChangedResources() {
> return changedResources;
> }
>
> public Collection<Resource> getRemovedResources() {
> return removedResources;
> }
> }
>
> thks,
>
> Steven Derrien
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Repost : Reload-refresh resource supporting opened editor [message #687169 is a reply to message #687166] Thu, 09 June 2011 05:46 Go to previous message
Steven Derrien is currently offline Steven DerrienFriend
Messages: 50
Registered: July 2009
Member
On 08/06/2011 09:47, Ed Merks wrote:
> Steven,
>
> Comments below.
>
> Steven Derrien wrote:
>> Hi,
>>
>> After claoing and reopenign my editor at lest 3000 time upon model file change, I realized that
>> I would need to make it refresh itself upon a model file change caused by an
>> external modification (editor, program, etc).

> Keep in mind that the code EMF has in the editor kicks in when the workspace knows about the change, i.e., when the
> resource is changed using the Eclipse resource APIs by say another editor. If the change is external, the workspace
> needs to be refreshed. There's a preference to auto refresh it, so maybe that's all you're missing...
>>


>> I understand I should reload the model somewhere in
>> here using resource.unload(); but always get an NPE exception

> Why do you get one. This code should already just work...

I set up the reference, and after an Eclipse restart it would
indeed just work.


Thanks a lot for the hint !

Regards,

Steven
>>
>>
>> *****/
>>
>>
>>
>>
>> }
>> }
>> }
>> }
>>
>> return true;
>> }
>>
>> public Collection<Resource> getChangedResources() {
>> return changedResources;
>> }
>>
>> public Collection<Resource> getRemovedResources() {
>> return removedResources;
>> }
>> }
>>
>> thks,
>>
>> Steven Derrien
>>
Re: Repost : Reload-refresh resource supporting opened editor [message #687419 is a reply to message #687163] Wed, 08 June 2011 15:47 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
Steven,

Comments below.

Steven Derrien wrote:
> Hi,
>
> After claoing and reopenign my editor at lest 3000 time upon model
> file change, I realized that
> I would need to make it refresh itself upon a model file
> change caused by an
> external modification (editor, program, etc).
Keep in mind that the code EMF has in the editor kicks in when the
workspace knows about the change, i.e., when the resource is changed
using the Eclipse resource APIs by say another editor. If the change is
external, the workspace needs to be refreshed. There's a preference to
auto refresh it, so maybe that's all you're missing...
>
> I searched the newsgroup and found the thread below, but
> since I am
> not enough confortable with EMF editor/JFace stuff, I could
> not see how
> to use Ed's answer to solve the problem.
>
> > Carlos,
> >
> > Have a look in a generated editor how it handles a
> resource delta. Look
> > for the code that calls Resource.unload...
> >
> >
> > Carlos wrote:
> >> Hello.
> >>
> >> I want to implement an utility for reloading a XMI
> resource into the
> >> editor.
> >> I have tried to close the editor and open it, but this
> is not very
> >> smart. I would prefer loading the model without closing
> the editor.
> >>
> >> Is there any method in the EMF API to do this?
> >> Have you got any suggestion?
> >>
> >> Thanks in advance.
> >> Best regards.
> >> Carlos.
>
>
> Looking at the generated code I found the method below, but I
> don't know how to
> customize is so as to make a proper use of resource.unload().
>
> Any help would be greatly appreciated.
>
>
> protected IResourceChangeListener resourceChangeListener =
> new IResourceChangeListener() {
> public void resourceChanged(IResourceChangeEvent event) {
> IResourceDelta delta = event.getDelta();
> try {
> class ResourceDeltaVisitor implements IResourceDeltaVisitor {
> protected ResourceSet resourceSet =
> editingDomain.getResourceSet();
> protected Collection<Resource> changedResources = new
> ArrayList<Resource>();
> protected Collection<Resource> removedResources = new
> ArrayList<Resource>();
>
> public boolean visit(IResourceDelta delta) {
> int type = delta.getResource().getType();
> if (type == IResource.FILE) {
> if (delta.getKind() == IResourceDelta.REMOVED ||
> delta.getKind() == IResourceDelta.CHANGED &&
> delta.getFlags() != IResourceDelta.MARKERS) {
> URI createPlatformResourceURI =
> URI.createPlatformResourceURI(delta.getFullPath().toString(), true);
> Resource resource =
> resourceSet.getResource(createPlatformResourceURI, false);
>
> if (resource != null) {
> if (delta.getKind() == IResourceDelta.REMOVED) {
> removedResources.add(resource);
> }
> else if (!savedResources.remove(resource)) {
> changedResources.add(resource);
>
> /****
>
> I understand I should reload the model somewhere in
> here using resource.unload(); but always get an NPE exception
Why do you get one. This code should already just work...
>
>
> *****/
>
>
>
>
> }
> }
> }
> }
>
> return true;
> }
>
> public Collection<Resource> getChangedResources() {
> return changedResources;
> }
>
> public Collection<Resource> getRemovedResources() {
> return removedResources;
> }
> }
>
> thks,
>
> Steven Derrien
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Repost : Reload-refresh resource supporting opened editor [message #687422 is a reply to message #687166] Thu, 09 June 2011 05:46 Go to previous message
Steven Derrien is currently offline Steven DerrienFriend
Messages: 50
Registered: July 2009
Member
On 08/06/2011 09:47, Ed Merks wrote:
> Steven,
>
> Comments below.
>
> Steven Derrien wrote:
>> Hi,
>>
>> After claoing and reopenign my editor at lest 3000 time upon model file change, I realized that
>> I would need to make it refresh itself upon a model file change caused by an
>> external modification (editor, program, etc).

> Keep in mind that the code EMF has in the editor kicks in when the workspace knows about the change, i.e., when the
> resource is changed using the Eclipse resource APIs by say another editor. If the change is external, the workspace
> needs to be refreshed. There's a preference to auto refresh it, so maybe that's all you're missing...
>>


>> I understand I should reload the model somewhere in
>> here using resource.unload(); but always get an NPE exception

> Why do you get one. This code should already just work...

I set up the reference, and after an Eclipse restart it would
indeed just work.


Thanks a lot for the hint !

Regards,

Steven
>>
>>
>> *****/
>>
>>
>>
>>
>> }
>> }
>> }
>> }
>>
>> return true;
>> }
>>
>> public Collection<Resource> getChangedResources() {
>> return changedResources;
>> }
>>
>> public Collection<Resource> getRemovedResources() {
>> return removedResources;
>> }
>> }
>>
>> thks,
>>
>> Steven Derrien
>>
Previous Topic:Re: [Teneo] Need ClassMetadata but it's not present
Next Topic:(no subject)
Goto Forum:
  


Current Time: Thu Apr 25 06:47:48 GMT 2024

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

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

Back to the top