/** * 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); }
/** * Creates an editor representation instance. * * @param embeddedEditor the embedded editor * @param viewer the underlying source viewer * @param partialEditorModelAccess the model access * @param requireWorkspaceResource whether the editor requires a workspace resource and shall complain at least in * the logs or whether it shall be silent */ public EmbeddedXtextSourceEditor(EmbeddedEditor embeddedEditor, XtextSourceViewer viewer, EmbeddedEditorModelAccess partialEditorModelAccess, boolean requireWorkspaceResource) { this.embeddedEditor = embeddedEditor; this.viewer = viewer; this.partialEditorModelAccess = partialEditorModelAccess; try { URI uri = embeddedEditor.getDocument().getResourceURI(); java.net.URI netURI = new java.net.URI(uri.toString()); IFile[] files = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocationURI(netURI); if (null != files && files.length > 0) { resource = files[0]; } else { if (requireWorkspaceResource) { getLogger().error("No resource for " + uri); } } } catch (URISyntaxException e) { getLogger().exception(e); } viewer.getAnnotationModel().addAnnotationModelListener(new IAnnotationModelListener() { @Override public void modelChanged(IAnnotationModel model) { if (!skipNextUpdateEvent) { // loading / updating the model dirty = true; } skipNextUpdateEvent = false; } }); }
private void createEditor() { EmbeddedEditor editor = editorFactory.newEditor(this::createTempResource).withParent(this); editor.getViewer().getControl().setLayoutData(GridDataFactory.fillDefaults().grab(true, true).create()); // Initialize the document editor.createPartialEditor(true); editorDocument = editor.getDocument(); sourceViewer = editor.getViewer(); configureSourceViewer(sourceViewer); // Clear content editorDocument.set(""); }