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 {

@Inject
private IGenerator generator;

@Inject
private Provider<EclipseResourceFileSystemAccess> fileAccessProvider;

@Inject
IResourceDescriptions resourceDescriptions;

@Inject
IResourceSetProvider resourceSetProvider;

@Override
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();
fsa.setOutputPath(srcGenFolder.getFullPath().toString());

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;
}

@Override
public boolean isEnabled() {
return true;
}

}





Problem:
==============
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: 13529
Registered: July 2009
Senior Member
Hi,

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;

import com.google.inject.Inject;
import com.google.inject.Provider;


~Christian
--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext@itemis.de


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Rename/Move Resource refactoring for importURI-style includes
Next Topic:XbaseWithAnnotations - Error for parameters
Goto Forum:
  


Current Time: Fri Aug 14 09:54:17 GMT 2020

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

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

Back to the top