Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Build and add hidden EObjects to XtextEditor at runtime...
Build and add hidden EObjects to XtextEditor at runtime... [message #658004] Fri, 04 March 2011 22:31 Go to next message
Robert Longo is currently offline Robert LongoFriend
Messages: 3
Registered: March 2011
Junior Member
Hi All,

I have an application that uses the XtextEditor to edit instances of my custom DSL ("mydsl"). From what I understand, when a user loads "foo.mydsl" the XTextEditor parses the text and builds an in-memory graph of EObjects. Once this in-memory graph is created, I want to add some additional EObjects. I want to do this without modifying the text that's displayed to the end user.

The only way I've figured out how to do this is by doing this:

final XtextDocument document = ( XtextDocument ) getXTextEditor().getDocument();
      
      document.addXtextDocumentContentObserver( new IXtextDocumentContentObserver(){
	       ...	
	       @Override
	       public void performNecessaryUpdates( Processor processor )
	       {
	          IStateAccess.AbstractImpl<XtextResource> stateAccess = ( AbstractImpl< XtextResource > ) processor;
	          
	          IUnitOfWork uow = new IUnitOfWork.Void() {
	             
	             @Override
	             public void process( Object state ) throws Exception
	             {
	                LazyLinkingResource resource = ( LazyLinkingResource ) state;
	               
	                EList<EObject> top = resource.getContents();
	                MyDSLFactoryImpl factory = new MyDSLFactoryImpl();
	                
	                DomainModel domainModel;
	                if(top.size() == 0)
	                {
	                   domainModel = factory.createDomainModel();
	                   top.add( domainModel );
	                }
	                else {
	                   domainModel = ( DomainModel ) top.get( 0 );
	                }
	                   
	                /////////////////////////  Add some EObjects to the model...
                        Action action = factory.createAction();
	                action.setName( "open" );
	                domainModel.getElements().add( action );
	                
	                action = factory.createAction();
	                action.setName( "close" );
	                domainModel.getElements().add( action );
	               
	                document.setInput( ( XtextResource ) resource );
	             }
	          };
	          stateAccess.readOnly( uow );
       }} );


Is there a better (more standard) way to do this?

Any help would be greatly appreciated.

Thanks,
Robert Longo
Re: Build and add hidden EObjects to XtextEditor at runtime... [message #658031 is a reply to message #658004] Sat, 05 March 2011 09:14 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3118
Registered: July 2009
Senior Member
Hi Robert,

you should try to implement AbstractCleaningLinker#afterModelLinked and
by inheriting from the LazyLinker.

Regards,
Sebastian
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com


Am 04.03.11 23:31, schrieb Robert Longo:
> Hi All,
>
> I have an application that uses the XtextEditor to edit instances of my
> custom DSL ("mydsl"). From what I understand, when a user loads
> "foo.mydsl" the XTextEditor parses the text and builds an in-memory
> graph of EObjects. Once this in-memory graph is created, I want to add
> some additional EObjects. I want to do this without modifying the text
> that's displayed to the end user.
>
> The only way I've figured out how to do this is by doing this:
>
>
> final XtextDocument document = ( XtextDocument )
> getXTextEditor().getDocument();
> document.addXtextDocumentContentObserver( new
> IXtextDocumentContentObserver(){
> ...
> @Override
> public void performNecessaryUpdates( Processor processor )
> {
> IStateAccess.AbstractImpl<XtextResource> stateAccess = ( AbstractImpl<
> XtextResource > ) processor;
> IUnitOfWork uow = new IUnitOfWork.Void() {
> @Override
> public void process( Object state ) throws Exception
> {
> LazyLinkingResource resource = ( LazyLinkingResource ) state;
> EList<EObject> top = resource.getContents();
> MyDSLFactoryImpl factory = new MyDSLFactoryImpl();
> DomainModel domainModel;
> if(top.size() == 0)
> {
> domainModel = factory.createDomainModel();
> top.add( domainModel );
> }
> else {
> domainModel = ( DomainModel ) top.get( 0 );
> }
> ///////////////////////// Add some EObjects to the model...
> Action action = factory.createAction();
> action.setName( "open" );
> domainModel.getElements().add( action );
> action = factory.createAction();
> action.setName( "close" );
> domainModel.getElements().add( action );
> document.setInput( ( XtextResource ) resource );
> }
> };
> stateAccess.readOnly( uow );
> }} );
>
>
> Is there a better (more standard) way to do this?
>
> Any help would be greatly appreciated.
>
> Thanks,
> Robert Longo
>
Re: Build and add hidden EObjects to XtextEditor at runtime... [message #658047 is a reply to message #658031] Sat, 05 March 2011 15:07 Go to previous message
Robert Longo is currently offline Robert LongoFriend
Messages: 3
Registered: March 2011
Junior Member
That worked perfectly! Thank you so much for your help.

Best regards,
Robert Longo
Previous Topic:Xtext problem concerning grammar
Next Topic:Customizing generated java classes
Goto Forum:
  


Current Time: Fri Apr 26 19:49:23 GMT 2024

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

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

Back to the top