Implementing an Author Mode Action Event Handler
The AuthorActionEventHandler
extension point allows you to handle certain
Author mode actions in a special way. For example, a specific
use-case would be if you want to insert new lines when you press
Enter instead of it opening the
Content Completion Assistant.
How to Implement an AuthorActionEventHandler
To implement your own AuthorActionEventHandler
, follow this procedure:
- Implement the
ro.sync.ecss.extensions.api.AuthorActionEventHandler
interface. - To instruct Oxygen XML Editor plugin to use this newly created implementation, use
either of the following methods:
- If you have configured an extensions bundle, you can return the
AuthorActionEventHandler
implementation using thero.sync.ecss.extensions.api.ExtensionsBundle.getAuthorActionEventHandler()
method. - Specify the
AuthorActionEventHandler
in the Author action event handler individual extension in the Extensions tab of the Document Type configuration dialog box for your particular document type.
- If you have configured an extensions bundle, you can return the
Example
The following example illustrates the use-case mentioned in the introduction, that
is an
implementation for inserting a new line when the user presses
Enter in Author mode.
It uses the canHandleEvent
method to make sure the insertion will be
performed in an element that will preserve the new-line
character. Then
the handleEvent
method inserts the new line at the current cursor
position.
public class CustomAuthorActionEventHandler implements AuthorActionEventHandler { /** * @see ro.sync.ecss.extensions.api.AuthorActionEventHandler#canHandleEvent (AuthorAccess, AuthorActionEventType) */ @Override public boolean canHandleEvent(AuthorAccess authorAccess, AuthorActionEventType type) { boolean canHandle = false; if (type == AuthorActionEventType.ENTER) { AuthorDocumentController documentController = authorAccess.getDocumentController(); int caretOffset = authorAccess.getEditorAccess().getCaretOffset(); try { AuthorNode nodeAtOffset = documentController.getNodeAtOffset(caretOffset); if (nodeAtOffset instanceof AuthorElement) { AuthorElement elementAtOffset = (AuthorElement) nodeAtOffset; AttrValue xmlSpace = elementAtOffset.getAttribute("xml:space"); if (xmlSpace != null && xmlSpace.getValue().equals("preserve")) { canHandle = true; } } } catch (BadLocationException ex) { if (logger.isDebugEnabled()) { logger.error(ex.getMessage(), ex); } } } return canHandle; } /** * @see ro.sync.ecss.extensions.api.AuthorActionEventHandler#handleEvent (ro.sync.ecss.extensions.api.AuthorAccess, ro.sync.ecss.extensions.api.AuthorActionEventHandler.AuthorActionEventType) */ @Override public boolean handleEvent(AuthorAccess authorAccess, AuthorActionEventType eventType) { int caretOffset = authorAccess.getEditorAccess().getCaretOffset(); // Insert a new line authorAccess.getDocumentController().insertText(caretOffset, "\n"); return true; } /** * @see ro.sync.ecss.extensions.api.Extension#getDescription() */ @Override public String getDescription() { return "Insert a new line"; } }