Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Triggering generator using context menu in xtext(Through context menu i have to trigger the xtend generator.)
Triggering generator using context menu in xtext [message #900826] Wed, 08 August 2012 15:50 Go to next message
narender reddy is currently offline narender reddyFriend
Messages: 7
Registered: July 2012
Junior Member
I have changed the plugin.xml in ui package to disable the default triggering of xtend generator.

I have created "GenerationHandler" class in " org.xtext.example.mydsl.ui.handler" package.

My GenerationHandler class is:

package org.xtext.example.mydsl.ui.handler;
public class GenerationHandler extends AbstractHandler implements IHandler {

private IGenerator generator;

private Provider<EclipseResourceFileSystemAccess> fileAccessProvider;

IResourceDescriptions resourceDescriptions;

IResourceSetProvider resourceSetProvider;

public Object execute(ExecutionEvent event) throws ExecutionException {

ISelection selection = HandlerUtil.getCurrentSelection(event);
if (selection instanceof IStructuredSelection) {
IStructuredSelection structuredSelection = (IStructuredSelection) selection;
Object firstElement = structuredSelection.getFirstElement();
if (firstElement instanceof IFile) {
IFile file = (IFile) firstElement;
IProject project = file.getProject();
IFolder srcGenFolder = project.getFolder("src-gen");
if (!srcGenFolder.exists()) {
try {
srcGenFolder.create(true, true,
new NullProgressMonitor());
} catch (CoreException e) {
return null;

final EclipseResourceFileSystemAccess fsa = fileAccessProvider.get();

URI uri = URI.createPlatformResourceURI(file.getFullPath().toString(), true);
ResourceSet rs = resourceSetProvider.get(project);
Resource r = rs.getResource(uri, true);
generator.doGenerate(r, fsa);

return null;

public boolean isEnabled() {
return true;


I dont know which all packages to import, since it is asking many packages.

Can any one suggests which all packages to import please...?
Re: Triggering generator using context menu in xtext [message #900842 is a reply to message #900826] Wed, 08 August 2012 17:11 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13754
Registered: July 2009
Senior Member

i dont know if i got your problem. never the less if you are asking about my blog post...
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.xtext.builder.EclipseResourceFileSystemAccess;
import org.eclipse.xtext.generator.IGenerator;
import org.eclipse.xtext.resource.IResourceDescriptions;
import org.eclipse.xtext.ui.resource.IResourceSetProvider;


Need training, onsite consulting or any other kind of help for Xtext?
Go visit or send a mail to

Need professional support for Xtext, Xpand, EMF?
Go to:
Twitter : @chrdietrich
Blog :
Previous Topic:Rename/Move Resource refactoring for importURI-style includes
Next Topic:XbaseWithAnnotations - Error for parameters
Goto Forum:

Current Time: Sun Feb 28 08:21:35 GMT 2021

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

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

Back to the top