Edit online

Dynamically Modify the Content Inserted by the Author

Use Case

You want to insert typographic quotation marks instead of double quotes.

Solution

By using the API you can set a document filter to change the text that is inserted in the document in Author mode. You can use this method to change the insertion of double quotes with the typographic quotes.

Here is some sample code:
    authorAccess.getDocumentController().setDocumentFilter
(new AuthorDocumentFilter() {
    /**
     * @see ro.sync.ecss.extensions.api.AuthorDocumentFilter#insertText
(ro.sync.ecss.extensions.api.AuthorDocumentFilterBypass, int, java.lang.String)
     */
    @Override
    public void insertText(AuthorDocumentFilterBypass filterBypass, 
int offset, String toInsert) {
      if(toInsert.length() == 1 && "\"".equals(toInsert)) {
        //User typed a quote but he actually needs a smart quote.
        //So we either have to add \u201E (start smart quote)
        //Or we add \u201C (end smart quote)
        //Depending on whether there's already a start smart quote inserted
in the current paragraph.
     
      try {
        AuthorNode currentNode = 
authorAccess.getDocumentController().getNodeAtOffset(offset);
        int startofTextInCurrentNode = currentNode.getStartOffset();
        if(offset > startofTextInCurrentNode) {
          Segment seg = new Segment();
        authorAccess.getDocumentController().getChars(startofTextInCurrentNode, 
offset - startofTextInCurrentNode, seg);
          String previosTextInNode = seg.toString();
          boolean insertStartQuote = true;
          for (int i = previosTextInNode.length() - 1; i >= 0; i--) {
            char ch = previosTextInNode.charAt(i);
            if('\u201C' == ch) {
              //Found end of smart quote, so yes, we should insert a start one
              break;
            } else if('\u201E' == ch) {
              //Found start quote, so we should insert an end one.
              insertStartQuote = false;
              break;
            }
          }
         
          if(insertStartQuote) {
            toInsert = "\u201E";
          } else {
            toInsert = "\u201C";
          }
        }
      } catch (BadLocationException e) {
        e.printStackTrace();
      }
    }
    System.err.println("INSERT TEXT |" + toInsert + "|");
    super.insertText(filterBypass, offset, toInsert);
  }
});

You can find the online Javadoc for AuthorDocumentFilter API here: https://www.oxygenxml.com/InstData/Editor/SDK/javadoc/ro/sync/ecss/extensions/api/AuthorDocumentFilter.html

An alternative to using a document filtering is the use of a ro.sync.ecss.extensions.api.AuthorSchemaAwareEditingHandlerAdapter, which has clear callbacks indicating the source from where the API is called (Paste, Drag and Drop, Typing).