Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » xText + EMF Editor
xText + EMF Editor [message #636661] Tue, 02 November 2010 07:36 Go to next message
Christian Saad is currently offline Christian Saad
Messages: 38
Registered: July 2009
Member
Hi,

I'm trying to do a *lightweight* integration of a xtext editor with a EMF generated tree editor which serializes to XMI. I've added the xtext editor as a new page in the EMF MultiPartEditor.

To synchronize them, my idea is to

1. EMF->xtext
a) Attach an EMF CommandStackListener which updates the xtext model resource and text on EMF model changes with a copy of the EMF resource
b) Replace xtext CommandStack with EMF CommandStack (by redirecting Undo/Redo avaliablility/calls) so the model is always under full control of the EditingDomain

2. xtext -> EMF
Attach a DocumentListener which creates and executes an EMF command switching EMF's resource contents with a copy of the xtext resource contents

The obvious drawbacks of this approach are that it's probably not that resource efficient and manual formatting of the text will be lost, but that's acceptable for my use case.

I've successfully implemented 1a and part of 2. My current problems are:

* 1b) Unfortunately I've no idea how xtext manages its changes. Could you give me a hint on how this can be accomplished?

* For 2., I'd like to restrict the synchronization to cases where xtext can successfully parse the new contents without errors and there actually have been changes to the underlying model's structure or reference/attribute values. Unfortunately, all listeners which I found seem to fire on every entered character instead.

Any help would be highly appreciated,
Chris
Re: xText + EMF Editor [message #637419 is a reply to message #636661] Fri, 05 November 2010 08:22 Go to previous messageGo to next message
Jan Koehnlein is currently offline Jan Koehnlein
Messages: 655
Registered: July 2009
Senior Member
Chrus,

that's actually a complicated task you're facing, as the editing
concepts of EMF (EditingDomain and the like) and Xtext (Jface text
editors) are quite different. I'd guess replacing the command stacks of
one framework with the one form the other wouldn't work.

I'd recommend you have a look at the GMF integration example ship with
Xtext. There, we're synchronizing the GMF (editing domain based) editor
with an Xtext editor here. We keep the two models separate and use
parsing and serialising to transfer changes from one representation to
the other.

So my recommendation is: Separate the two command stacks, but use
DocumentListers and ResourceSetListeners to update the resp other editor
on changes. Some pointers
1) XtextResource.serialize() converts the in memeory EMF representation
of your Xtext model into text again
2) ResourceWorkingCopyFileEditorInput can be used as editor input for an
Xtext editor based on an existing resource
3) Read my blog entry on model synchronization
http://koehnlein.blogspot.com/2010/06/semantic-model-access- in-xtext.html

Hope you get some good ideas from it
Jan

Am 02.11.10 12:36, schrieb C. Saad:
> Hi,
>
> I'm trying to do a *lightweight* integration of a xtext editor with a
> EMF generated tree editor which serializes to XMI. I've added the xtext
> editor as a new page in the EMF MultiPartEditor.
>
> To synchronize them, my idea is to
>
> 1. EMF->xtext
> a) Attach an EMF CommandStackListener which updates the xtext model
> resource and text on EMF model changes with a copy of the EMF resource
> b) Replace xtext CommandStack with EMF CommandStack (by redirecting
> Undo/Redo avaliablility/calls) so the model is always under full control
> of the EditingDomain
>
> 2. xtext -> EMF
> Attach a DocumentListener which creates and executes an EMF command
> switching EMF's resource contents with a copy of the xtext resource
> contents
>
> The obvious drawbacks of this approach are that it's probably not that
> resource efficient and manual formatting of the text will be lost, but
> that's acceptable for my use case.
>
> I've successfully implemented 1a and part of 2. My current problems are:
>
> * 1b) Unfortunately I've no idea how xtext manages its changes. Could
> you give me a hint on how this can be accomplished?
>
> * For 2., I'd like to restrict the synchronization to cases where xtext
> can successfully parse the new contents without errors and there
> actually have been changes to the underlying model's structure or
> reference/attribute values. Unfortunately, all listeners which I found
> seem to fire on every entered character instead.
>
> Any help would be highly appreciated,
> Chris


--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com
Re: xText + EMF Editor [message #637761 is a reply to message #636661] Mon, 08 November 2010 13:05 Go to previous messageGo to next message
Christian Saad is currently offline Christian Saad
Messages: 38
Registered: July 2009
Member
Hi Jan,

thanks for your help. Indeed, it's not an easy task but I think it's manageable as I intend to use only one main command stack (the one from the editing domain) and sync the textual representation on demand. For a very basic implementation, it would be sufficient if the Xtext editor could display and execute the undo/redo actions of the EMF editor instead of its own. This is probably not an Xtext specific question but maybe you already have an idea how this could be implemented.

Concerning the synchronization issue, I now have the following problem:
I've attached an IXtextDocumentContentObserver to the document in order to update the EMF resource on changes. If I access the document's resource (directly), then I get an outdated version of the model. But if I use synchronization as described in your blog entry, the Xtext GUI gets corrupted (probably sync requests are not allowed inside documentchange listeners).
I already tried using a model listener on the document instead which runs in the SWT thread, but this creates new issues with the CommandStackListener.

Is there a way to do something like
while (document.hasPendingChanges());
inside an IXtextDocumentContentObserver to block execution until the resource is updated? (actually, if I halt the execution with the debugger just before the readOnly() call, it works)

Thanks,
Chris
Re: xText + EMF Editor [message #637887 is a reply to message #637761] Tue, 09 November 2010 04:18 Go to previous messageGo to next message
Jan Koehnlein is currently offline Jan Koehnlein
Messages: 655
Registered: July 2009
Senior Member
You should use IXtextModelListener to be notified of resource changes.
The IXtextDocumentContentObserver is used by the reconciler to
synchronize the EMF model instance with textual document changes inside
the editor.

Am 08.11.10 19:05, schrieb C. Saad:
> Hi Jan,
>
> thanks for your help. Indeed, it's not an easy task but I think it's
> manageable as I intend to use only one main command stack (the one from
> the editing domain) and sync the textual representation on demand. For a
> very basic implementation, it would be sufficient if the Xtext editor
> could display and execute the undo/redo actions of the EMF editor
> instead of its own. This is probably not an Xtext specific question but
> maybe you already have an idea how this could be implemented.
>
> Concerning the synchronization issue, I now have the following problem:
> I've attached an IXtextDocumentContentObserver to the document in order
> to update the EMF resource on changes. If I access the document's
> resource (directly), then I get an outdated version of the model. But if
> I use synchronization as described in your blog entry, the Xtext GUI
> gets corrupted (probably sync requests are not allowed inside
> documentchange listeners).
> I already tried using a model listener on the document instead which
> runs in the SWT thread, but this creates new issues with the
> CommandStackListener.
>
> Is there a way to do something like
> while (document.hasPendingChanges());
> inside an IXtextDocumentContentObserver to block execution until the
> resource is updated? (actually, if I halt the execution with the
> debugger just before the readOnly() call, it works)
>
> Thanks,
> Chris


--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com
Re: xText + EMF Editor [message #1061711 is a reply to message #637887] Mon, 03 June 2013 10:57 Go to previous messageGo to next message
Edwin Tuzar is currently offline Edwin Tuzar
Messages: 33
Registered: April 2013
Location: Kaiserslautern
Member

Hi guys,
I have a similar problem, I'm trying to synchronize a Scale element with the XtextEditor and I don't get where I'm wrong about it.
I'm adding a listener to the Scale element
scale.addListener(SWT.Selection, new Listener() {
					
					@Override
					public void handleEvent(Event event) {
						scale.setToolTipText(scale.getSelection());
						if(editor!=null){
							final IXtextDocument doc = editor.getDocument();
							if (doc != null){
								doc.modify(new IUnitOfWork<Object, XtextResource>(){				
									
								@Override
								public Object exec(XtextResource state)
										throws Exception {
									TreeIterator<EObject> object = state.getAllContents();
									while(object.hasNext()){
										EObject eobj=object.next();
										if(eobj instanceof Command ){
											eobj=com;
											com.setDuration(scale.getSelection());
											
										}
									}									
									return null;
								}
								  
							  });
							}
						}

this works only for the first increment of the scale (so if I have ----|---- = 5 and I move to right -----|--- = 6 will work, but if I want to move it further or change will remain stuck on 6)
Do you have any Ideea what could be the problem?
Thanks in advance!


Edwin W.T.
Re: xText + EMF Editor [message #1061712 is a reply to message #637887] Mon, 03 June 2013 10:58 Go to previous message
Edwin Tuzar is currently offline Edwin Tuzar
Messages: 33
Registered: April 2013
Location: Kaiserslautern
Member

Sorry for double post.
Inside the if can be also something like
Command c= (Command)eobj;
c.setDuration(scale.getSelection());

but has the same result.


Edwin W.T.

[Updated on: Mon, 03 June 2013 11:19]

Report message to a moderator

Previous Topic:retrieving the model from a different plugin
Next Topic:how to use xtext features in a html textarea
Goto Forum:
  


Current Time: Fri Jul 25 14:33:32 EDT 2014

Powered by FUDForum. Page generated in 0.02536 seconds