Disable Context-Sensitive Menu Items for Custom Author Actions

Question

Is there a way to disable menu items for custom Author actions depending on the cursor context?

Answer

By default Oxygen XML Editor does not toggle the enabled/disabled states for actions based on whether the activation XPath expressions for that certain Author action are fulfilled. This is done because the actions can be many and evaluating XPath expression on each caret move can lead to performance problems. But if you have your own ro.sync.ecss.extensions.api.ExtensionsBundle implementation you can overwrite the method:
ro.sync.ecss.extensions.api.ExtensionsBundle.createAuthorExtensionStateListener()
and when the extension state listener gets activated you can use the API like:
/**
 * @see ro.sync.ecss.extensions.api.AuthorExtensionStateListener#activated(ro.sync.ecss.extensions.api.AuthorAccess)
 */
public void activated(final AuthorAccess authorAccess) {

  //Add a caret listener to enable/disable extension actions:
  authorAccess.getEditorAccess().addAuthorCaretListener(new AuthorCaretListener() {
    @Override
    public void caretMoved(AuthorCaretEvent caretEvent) {
      try {
        Map<String, Object> authorExtensionActions = authorAccess.getEditorAccess().getActionsProvider().getAuthorExtensionActions();
        //Get the action used to insert a paragraph. It's ID is "paragraph"
        AbstractAction insertParagraph = (AbstractAction) authorExtensionActions.get("paragraph");
        //Evaluate an XPath expression in the context of the current node in which the caret is located
        Object[] evaluateXPath = authorAccess.getDocumentController().evaluateXPath(".[ancestor-or-self::p]", false, false, false, false);
        if(evaluateXPath != null && evaluateXPath.length > 0 && evaluateXPath[0] != null) {
          //We are inside a paragraph, disable the action.
          insertParagraph.setEnabled(false);
        } else {
          //Enable the action
          insertParagraph.setEnabled(true);
        }
      } catch (AuthorOperationException e) {
        e.printStackTrace();
      }
    }
  });
When the extension is deactivated you should remove the caret listener in order to avoid adding multiple caret listeners which perform the same functionality.