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.
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";
}
}