/** * Embeds an xText editor for the given resource into the given <code>parent</code>. * * @param file the file representing the resource * @param parent the parent UI component * @param injector the injector configured for the editor and the grammar of <code>file</code> * @param content a split of <code>file</code> consisting of the actual editable content in terms of prefix not to * be shown [0], content to be displayed [1] and postfix not to be shown[2] * @param requireWorkspaceResource whether the editor requires a workspace resource and shall complain at least in * the logs or whether it shall be silent * @return the embedded editor (or <b>null</b> if embedding failed) */ public static IEmbeddedEditor embedEditor(final java.net.URI file, Composite parent, final Injector injector, String[] content, boolean requireWorkspaceResource) { IEditedResourceProvider resourceProvider = new IEditedResourceProvider() { public XtextResource createResource() { XtextResourceSet rs = injector.getInstance(XtextResourceSet.class); rs.setClasspathURIContext(getClass()); IResourceFactory resourceFactory = injector.getInstance(IResourceFactory.class); URI uri = URI.createURI(file.toString()); XtextResource resource = (XtextResource) resourceFactory.createResource(uri); rs.getResources().add(resource); EcoreUtil.resolveAll(resource); if (!resource.getErrors().isEmpty()) { StringBuilder tmp = new StringBuilder(); for (Diagnostic d : resource.getErrors()) { if (tmp.length() > 0) { tmp.append(", "); } tmp.append(d.getMessage()); } EASyLoggerFactory.INSTANCE.getLogger(EmbeddingHelper.class, Activator.PLUGIN_ID).error( tmp.toString()); } return resource; } }; return embedEditor(parent, injector, resourceProvider, content, requireWorkspaceResource); }
/** * Embeds an xText editor for the given resource into the given <code>parent</code>. * * @param parent the parent UI component * @param injector the injector configured for the editor and the grammar of <code>file</code> * @param resourceProvider the resource to be displayed * @param content the actual editable content in terms of prefix not to be shown [0], content to be displayed [1] * and postfix not to be shown[2] * @param requireWorkspaceResource whether the editor requires a workspace resource and shall complain at least in * the logs or whether it shall be silent * @return the embedded editor (or <b>null</b> if embedding failed) */ private static IEmbeddedEditor embedEditor(Composite parent, Injector injector, IEditedResourceProvider resourceProvider, String[] content, boolean requireWorkspaceResource) { // processIssuesBy EmbeddedEditorFactory factory = injector.getInstance(EmbeddedEditorFactory.class); EmbeddedEditor embeddedEditor = factory.newEditor(resourceProvider).showErrorAndWarningAnnotations() .withParent(parent); // solution by: http://stackoverflow.com/questions/15324481/xtext-dsl-embedded-editor-in-a-dialog EmbeddedEditorModelAccess partialEditorModelAccess = embeddedEditor.createPartialEditor(content[0], content[1], content[2], false); XtextSourceViewer viewer = embeddedEditor.getViewer(); StyledText text = viewer.getTextWidget(); try { Font font = text.getFont(); if (null != font) { // TODO preliminary, use editor configuration instead FontData[] fontData = font.getFontData(); if (null != fontData && fontData.length > 0) { text.setFont(new Font(font.getDevice(), "Consolas", fontData[0].getHeight(), fontData[0].getStyle())); } } } catch (SWTError e) { // no font, ignore EASyLoggerFactory.INSTANCE.getLogger(EmbeddingHelper.class, Activator.PLUGIN_ID).error(e.getMessage()); } return new EmbeddedXtextSourceEditor(embeddedEditor, viewer, partialEditorModelAccess, requireWorkspaceResource); }