Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Calling IGenerator from an Handler
Calling IGenerator from an Handler [message #737350] Fri, 14 October 2011 23:40 Go to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

i want to trigger the IGenerator from an Handler via a popup menu entry on the model file in the package explorer.

how can i create/access/fill a resource with a given uri/ifile with the "ui setup" of eclipse (workspaceprojectstate and stuff)

URI uri = URI.createPlatformResourceURI(file.getFullPath().toString(), true);
Resource r = ... //what do do here
generator.doGenerate(r, getFA());


I want to do something like the following but without having an active editor.

public class GenerationHandler extends AbstractHandler implements IHandler {
	
	@Inject
	private IGenerator generator;

	@Inject
	private Provider<EclipseResourceFileSystemAccess> fileAccessProvider;
	
	@Inject
	IResourceDescriptions resourceDescriptions;
	
	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
		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());
				
				if (activeEditor instanceof XtextEditor) {
					((XtextEditor)activeEditor).getDocument().readOnly(new IUnitOfWork<Boolean, XtextResource>() {

						@Override
						public Boolean exec(XtextResource state)
								throws Exception {
							generator.doGenerate(state, fsa);
							return Boolean.TRUE;
						}
						
					}
						);
					
				}
			}
		}
		return null;
	}

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

}


Thanks, Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Calling IGenerator from an Handler [message #737633 is a reply to message #737350] Sat, 15 October 2011 08:28 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Got it

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


And wrote a blog post on it
http://christiandietrich.wordpress.com/2011/10/15/xtext-calling-the-generator-from-a-context-menu/

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Example for import in grammar?
Next Topic:replace references
Goto Forum:
  


Current Time: Thu Apr 25 22:38:18 GMT 2024

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

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

Back to the top