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