Insert indexterm action

Are you missing a feature? Request it's implementation here.
dcramer
Posts: 154
Joined: Sat Aug 28, 2010 1:23 am

Insert indexterm action

Post by dcramer » Mon Feb 14, 2011 5:06 am

Hi there,
I would like to create an "Insert indexterm" action that operates on the current selected text. Say the user has highlighted the word "whatever" in the editor. This action would insert <indexterm><primary>whatever</primary></indexterm> at the end of the nearest ancestor block element (e.g. <para>) but otherwise leave the cursor unmoved. Would that be possible? I think an action like this would be useful for several of the frameworks with only the specific indexterm markup changed.

Thanks,
David

Radu
Posts: 6811
Joined: Fri Jul 09, 2004 5:18 pm

Re: Insert indexterm action

Post by Radu » Mon Feb 14, 2011 12:30 pm

Hi David,

The operation needs a custom Java implementation.
Please see this example:

Code: Select all

package ro.sync.ecss.extensions.docbook;

import javax.swing.text.BadLocationException;

import ro.sync.ecss.extensions.api.ArgumentDescriptor;
import ro.sync.ecss.extensions.api.ArgumentsMap;
import ro.sync.ecss.extensions.api.AuthorAccess;
import ro.sync.ecss.extensions.api.AuthorOperation;
import ro.sync.ecss.extensions.api.AuthorOperationException;
import ro.sync.ecss.extensions.api.node.AuthorNode;

/**
* Insert an index term.
*/
public class InsertIndexTermOperation implements AuthorOperation {

/**
* @see ro.sync.ecss.extensions.api.AuthorOperation#doOperation(ro.sync.ecss.extensions.api.AuthorAccess, ro.sync.ecss.extensions.api.ArgumentsMap)
*/
public void doOperation(AuthorAccess authorAccess, ArgumentsMap args)
throws IllegalArgumentException, AuthorOperationException {
String selectedText = authorAccess.getEditorAccess().getSelectedText();
if(selectedText != null && !selectedText.isEmpty()) {
//We can make an index term from it.

//Keep the caret position, we'll come back to it
int currentCaretPos = authorAccess.getEditorAccess().getCaretOffset();

//Find the insert position.
try {
//The end of the current element
AuthorNode toInsertIn = authorAccess.getDocumentController().getNodeAtOffset(currentCaretPos);
//But maybe it is an inline
//Go up to find a paragraph
while(! "para".equals(toInsertIn.getName())) {
toInsertIn = toInsertIn.getParent();
}
if(toInsertIn != null) {
authorAccess.getDocumentController().insertXMLFragment("<indexterm xmlns=\"http://docbook.org/ns/docbook\"><primary>" + selectedText + "</primary></indexterm>", toInsertIn.getEndOffset());
//And move the cursor back
authorAccess.getEditorAccess().setCaretPosition(currentCaretPos);
}
} catch (BadLocationException e) {
e.printStackTrace();
}
}
}

/**
* @see ro.sync.ecss.extensions.api.AuthorOperation#getArguments()
*/
public ArgumentDescriptor[] getArguments() {
return null;
}

/**
* @see ro.sync.ecss.extensions.api.Extension#getDescription()
*/
public String getDescription() {
return "Insert index term";
}
}
Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com

dcramer
Posts: 154
Joined: Sat Aug 28, 2010 1:23 am

Re: Insert indexterm action

Post by dcramer » Mon Feb 14, 2011 8:00 pm

Thanks Radu. That gives me something to work with. I'll have to handle some other situations (like when the word in question is in a <title> or <entry>). I'll let you know if I need more help with that.

David

DrRoot
Posts: 1
Joined: Thu May 19, 2011 3:54 pm

Re: Insert indexterm action

Post by DrRoot » Thu May 19, 2011 4:16 pm

I am looking for the same action, but as I am completely new to oXygen, where do I put this action code?

Cheers, Fons.

Radu
Posts: 6811
Joined: Fri Jul 09, 2004 5:18 pm

Re: Insert indexterm action

Post by Radu » Fri May 20, 2011 9:27 am

Hi Fons,

Creating your own Author operations implies some knowledge of Java as all our SDK is Java based:
http://www.oxygenxml.com/developer.html ... horing_SDK

You will need the libraries which come with the SDK to build the new operation. The SDK also comes with a tutorial for customizing a document type.

Once you create a new jar library with the new operation class you can modify the Docbook(4 or 5) document type from the Preferences->Document Type Association page, in the Classpath tab add the path to your jar library and then in the Author->Actions tab create an action which uses the operation. Then the action can be mounted to the main menu, contextual menu or to the toolbar.

Maybe David can share with you the jar library of the operation he implemented.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com

Post Reply