Edit online

How to Use a Custom View with the Oxygen XML Author Eclipse plugin Distribution

Question

Is it possible to create a custom view in Eclipse that can insert certain XML fragments in the documents opened with the Oxygen XML Author Eclipse plugin?

Answer

Here you can find more information about the Eclipse part of the Oxygen SDK:

https://www.oxygenxml.com/oxygen_sdk.html#oXygen_Eclipse_plugin

Use the provided Oxygen XML Author Eclipse plugin sample project as a starting point. From any custom view/component you can have singleton access to the using the ro.sync.exml.workspace.api.PluginWorkspaceProvider.getPluginWorkspace() API.

The Java code for inserting a certain XML fragment in the currently open editor (either in the Text or Author editing modes) would look like this:
    WSEditor currentEditorAccess = PluginWorkspaceProvider.getPluginWorkspace().
getCurrentEditorAccess(PluginWorkspace.MAIN_EDITING_AREA);
    if(currentEditorAccess.getCurrentPage() instanceof WSXMLTextEditorPage) {
      //Editor opened in Text page
      WSXMLTextEditorPage tp = 
(WSXMLTextEditorPage) currentEditorAccess.getCurrentPage();

      //You can access an API to insert text in the XML content
//      tp.getDocument().insertString(tp.getCaretOffset(), "<testTag/>", null);
      //This is the internal StyledText implementation
//      tp.getTextComponent()
      //You can use this XPath API to find the range of an XML element.
//      tp.findElementsByXPath(xpathExpression)
 } else if(currentEditorAccess.getCurrentPage() instanceof WSAuthorEditorPage) {
      //Editor opened in Author page
//      try {
        WSAuthorEditorPage authPage = (WSAuthorEditorPage) 
currentEditorAccess.getCurrentPage();

        //Then you can do stuff like this to insert XML at the cursor position
//        authPage.getDocumentController().insertXMLFragment("<testTag/>", 
authPage.getCaretOffset());
//      } catch (AuthorOperationException e) {
//        // TODO Auto-generated catch block
//        e.printStackTrace();
//      }
    }