XML Author - Custom 'Learn Word' feature

Post here questions and problems related to oXygen frameworks/document types.
gergokozma
Posts: 1
Joined: Mon Apr 15, 2019 12:56 pm

XML Author - Custom 'Learn Word' feature

Post by gergokozma » Mon Apr 15, 2019 2:43 pm

Hello All,

I wish to expand the standard 'Learn Word' action in Oxygen Author 20.1 . I would like to include some additional steps, while still having the original behaviour as well. For example: when the user selects a word for learning, a custom dictionary/wordlist is updated (with this word). This dictionary/wordlist is probably not present on a local machine but on the server.

Is it possible to create a wrapper around this action? And if so, how?

Thanks,
Geri

alex_jitianu
Posts: 682
Joined: Wed Nov 16, 2005 11:11 am

Re: XML Author - Custom 'Learn Word' feature

Post by alex_jitianu » Wed Apr 17, 2019 3:34 pm

Hi Geri,

You can use an AuthorActionsProvider to intercept the "Learn word" action. It can be used from the framework level API as well as from the plugin level API.

1. From a framework
From within an ro.sync.ecss.extensions.api.AuthorExtensionStateListener you can do this:

Code: Select all

public abstract class AuthorExtensionStateAdapter implements AuthorExtensionStateListener {

  private ActionPerformedListener listener = new ActionPerformedListener() {
    @Override
    public boolean beforeActionPerformed(Object actionEvent) {
      return true;
    };
    @Override
    public void afterActionPerformed(Object actionEvent) {
      // TODO Perform the required action. The authorAccess instance can be used to detect the word at caret.
      int[] wordAtCaret = authorAccess.getEditorAccess().getWordAtCaret();
      try {
        String word = authorAccess.getDocumentController().getText(wordAtCaret[0], wordAtCaret[1]);
      } catch (BadLocationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    };
  };
  private AuthorAccess authorAccess;
  
  /**
   * @see ro.sync.ecss.extensions.api.AuthorExtensionStateListener#activated(ro.sync.ecss.extensions.api.AuthorAccess)
   */
  @Override
  public void activated(AuthorAccess authorAccess) {
    this.authorAccess = authorAccess;
    AuthorActionsProvider actionsProvider = authorAccess.getEditorAccess().getActionsProvider();
    Action object = (Action) actionsProvider.getAuthorCommonActions().get("Spell_Check/Learn_word");
    actionsProvider.addActionPerformedListener(object, listener);
  }

  /**
   * @see ro.sync.ecss.extensions.api.AuthorExtensionStateListener#deactivated(ro.sync.ecss.extensions.api.AuthorAccess)
   */
  @Override
  public void deactivated(AuthorAccess authorAccess) {
    AuthorActionsProvider actionsProvider = authorAccess.getEditorAccess().getActionsProvider();
    Action object = (Action) actionsProvider.getAuthorCommonActions().get("Spell_Check/Learn_word");
    actionsProvider.removeActionPerformedListener(object, listener);
  }

  /**
   * @see ro.sync.ecss.extensions.api.Extension#getDescription()
   */
  @Override
  public String getDescription() {
    return "Author extension state adapter implementation";
  }
}
2. From within a plugin, more precisely from a Workspace Access plugin

Code: Select all

/**
 * Plugin extension - workspace access extension.
 */
public class CustomWorkspaceAccessPluginExtension implements WorkspaceAccessPluginExtension {
  /**
   * @see ro.sync.exml.plugin.workspace.WorkspaceAccessPluginExtension#applicationStarted(ro.sync.exml.workspace.api.standalone.StandalonePluginWorkspace)
   */
  public void applicationStarted(final StandalonePluginWorkspace pluginWorkspaceAccess) {
	pluginWorkspaceAccess.addEditorChangeListener(new WSEditorChangeListener() {
		@Override
		public void editorOpened(URL editorLocation) {
			WSEditor editorAccess = pluginWorkspaceAccess.getEditorAccess(editorLocation, PluginWorkspace.MAIN_EDITING_AREA);
			
			WSEditorPage currentPage = editorAccess.getCurrentPage();
			if (currentPage instanceof WSAuthorEditorPage) {
				AuthorActionsProvider actionsProvider = ((WSAuthorEditorPage) currentPage).getActionsProvider();
				Action object = (Action) actionsProvider.getAuthorCommonActions().get("Spell_Check/Learn_word");
				actionsProvider.addActionPerformedListener(object, new ActionPerformedListener() {
					@Override
					public boolean beforeActionPerformed(Object actionEvent) {
						// TODO Custom code.
						return true;
					}
				});
			} else {
				// TODO You could add a listener to detect when the user switches to author page and run your code there. 
				editorAccess.addEditorListener(new WSEditorListener() {
					@Override
					public void editorPageChanged() {
						// Add the listener on the action, if it wasn't added already!
					}
				});
			}
		}
	}, PluginWorkspace.MAIN_EDITING_AREA);  
  }	
  
  /**
   * @see ro.sync.exml.plugin.workspace.WorkspaceAccessPluginExtension#applicationClosing()
   */
  public boolean applicationClosing() {
	  //You can reject the application closing here
    return true;
  }
}
Best regards,
Alex

Post Reply