Xtext DSL embedded editor in a dialog

匆匆过客 提交于 2020-07-18 05:28:20

问题


I am new to xtext, and i have created a DSL using xtext and i have generated the artifacts, which has generated the editor which has many features like content assist and syntax coloring now the problem is i want to embed the editor inside a dialog.

For achieving this im using EmbeddedEditor, i am able to get the embedded editor and place it in the dialog, but the embedded editor is not displaying the contents of the file.

The file C:/Eclipse_Work/workspace/runtime_workspace/apa/ex.mydsl contains:

import com.ex.test;
entity{
 element a;
}

The code in the createcontrol() of dialog is :

    IEditedResourceProvider resourceProvider=new IEditedResourceProvider() {
        
        @Override
        public XtextResource createResource() {
            try {

                Resource resource = resourceSet.createResource(URI.createURI("C:/Eclipse_Work/workspace/runtime_workspace/apa/ex.mydsl"));
                XtextResource resource2=(XtextResource)resource;
                
                return (XtextResource) resource;
            } catch (Exception e) {
                return null;
            }
        }
    };
    
    MyDslActivator activator = MyDslActivator.getInstance();
    Injector injector = activator
            .getInjector(MyDslActivator.COM_APAMA_STUDIO_QUERY_EXT_MYDSL);
    
    @SuppressWarnings("restriction")
    EmbeddedEditorFactory factory = injector.getInstance(EmbeddedEditorFactory.class);
   EmbeddedEditor handle= factory.newEditor(resourceProvider).withParent(
            composite);
   
   EmbeddedEditorModelAccess partialEditor= handle.createPartialEditor();

   
   handle.getViewer().getControl().setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true, 2, 0));  

When i run the project the dialog opens with a editor area but it is not displaying the code present in ex.mydsl, the editor is empty.

Please tell me how to show the code in the embedded editor


回答1:


You have to specify the editor's initial contents as the editablePart parameter of createPartialEditor(String prefix, String editablePart, String suffix, boolean insertLineBreaks). To obtain your XtextResource's contents as text, save it to a ByteArrayOutputStream, then convert it to a string using toString.



来源:https://stackoverflow.com/questions/15324481/xtext-dsl-embedded-editor-in-a-dialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!