Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » How to decide which generator to use
How to decide which generator to use [message #757455] Fri, 18 November 2011 13:48 Go to next message
Jens Missing name is currently offline Jens Missing nameFriend
Messages: 27
Registered: November 2011
Junior Member
hi,

I have 2 generators and everything works fine.
But how can I decicde which generator should be taken,
when I start the new Eclipse instance?

first generator: My first generator produces a swing gui.
second generator: My second generator produces a GWT gui.

(The problem is, that gwt needs its own project file..)

So when I try the run as - .- always the first generator is executed in the
new eclipse instance.

Thanks in advance.

regards,

jens
Re: How to decide which generator to use [message #757469 is a reply to message #757455] Fri, 18 November 2011 15:03 Go to previous messageGo to next message
Meinte Boersma is currently offline Meinte BoersmaFriend
Messages: 434
Registered: July 2009
Location: Leiden, Netherlands
Senior Member
Assuming that with 'generator' you mean an IGenerator implementation hooked into XtextBuilderParticipant, I see two possible solutions: (1) let the generator to run depend on a setting in a preferences page (which you'll have to bundle with the .ui plugin) and compose the existing IGenerator implementations in a 3rd IGenerator impl, (2) modify your DSL so you can decide from the instance what to do - this could be considered a hack, but might suit your use case nevertheless.

Re: How to decide which generator to use [message #757583 is a reply to message #757469] Sat, 19 November 2011 21:31 Go to previous messageGo to next message
Jens Missing name is currently offline Jens Missing nameFriend
Messages: 27
Registered: November 2011
Junior Member
hi,

"Assuming that with 'generator' you mean an IGenerator implementation hooked into XtextBuilderParticipant" - yes

package org.xtext.gui.dm.generator.another;

import org.xtext.gui.dm.GuiDslRuntimeModule;
import org.xtext.gui.dm.GuiDslStandaloneSetup; 

import com.google.inject.Guice;
import com.google.inject.Injector;

public class AnotherInjector extends GuiDslStandaloneSetup{

	public Injector createInjector() {
		return Guice.createInjector(new AnotherRuntimeModule());
	}
	
	public class AnotherRuntimeModule extends GuiDslRuntimeModule {
		@Override
		public Class<? extends org.eclipse.xtext.generator.IGenerator> bindIGenerator() {
			return AnotherGenerator.class;
		}
	}	
}


Could you give me an example of how to accomplish this (idea 1 or 2)?

regards jens,



Re: How to decide which generator to use [message #757585 is a reply to message #757583] Sat, 19 November 2011 21:48 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
Hi,


what about sublassing org.eclipse.xtext.builder.BuilderParticipant
(which has an igenerator injected)

and inject 2 igenerators there

public class JensBuilderParticipant extends BuilderParticipant {
	
	@Inject @Named("A") IGenerator genA;
	@Inject @Named("B") IGenerator genB;
	
	@Inject
	private IResourceServiceProvider resourceServiceProvider;
	
	@Override
	protected void handleChangedContents(Delta delta, IBuildContext context, EclipseResourceFileSystemAccess2 fileSystemAccess) throws CoreException {
		if (!resourceServiceProvider.canHandle(delta.getUri()))
			return;
		// TODO: we will run out of memory here if the number of deltas is large enough
		Resource resource = context.getResourceSet().getResource(delta.getUri(), true);
		if (shouldGenerate(resource, context)) {
			try {
				boolean a = true; //read from prefs or whatever
				boolean b = true; //read from prefs or whatever
				if (a) {
					genA.doGenerate(resource, fileSystemAccess);
				}
				if (b) {
					genB.doGenerate(resource, fileSystemAccess);
				}
				
			} catch (RuntimeException e) {
				if (e.getCause() instanceof CoreException) {
					throw (CoreException) e.getCause();
				}
				throw e;
			}
		}
	}


}



public class MyDslRuntimeModule extends org.xtext.example.mydsl.AbstractMyDslRuntimeModule {
	
	public void configureIGenerator(Binder binder) {
		binder.bind(IGenerator.class).annotatedWith(Names.named("A")).to(MyDslGenerator.class);
		binder.bind(IGenerator.class).annotatedWith(Names.named("B")).to(AnotherGenerator.class);
	}

}


public class MyDslUiModule extends org.xtext.example.mydsl.ui.AbstractMyDslUiModule {
	public MyDslUiModule(AbstractUIPlugin plugin) {
		super(plugin);
	}
	
	@Override
	public Class<? extends IXtextBuilderParticipant> bindIXtextBuilderParticipant() {
		return JensBuilderParticipant.class;
	}
}


or (1)

public class StupidGenerator implements IGenerator {
	
	@Inject MyDslGenerator genA;
	@Inject AnotherGenerator genB;

	@Override
	public void doGenerate(Resource input, IFileSystemAccess fsa) {
		boolean a = true; //read from prefs or whatever
		boolean b = true; //read from prefs or whatever
		if (a) {
			genA.doGenerate(input, fsa);
		}
		if (b) {
			genB.doGenerate(input, fsa);
		}
		
	} 
	

}



~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Xtext 2.1 and Xpand
Next Topic:How to add attributes to an abstract component
Goto Forum:
  


Current Time: Thu Mar 28 19:13:22 GMT 2024

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

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

Back to the top