Insert indexterm action

Are you missing a feature? Request its implementation here.
dcramer
Posts: 161
Joined: Sat Aug 28, 2010 1:23 am

Insert indexterm action

Post by dcramer »

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: 9049
Joined: Fri Jul 09, 2004 5:18 pm

Re: Insert indexterm action

Post by Radu »

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: 161
Joined: Sat Aug 28, 2010 1:23 am

Re: Insert indexterm action

Post by dcramer »

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 »

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: 9049
Joined: Fri Jul 09, 2004 5:18 pm

Re: Insert indexterm action

Post by Radu »

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
opeongo
Posts: 14
Joined: Mon Jun 21, 2021 7:07 pm

Re: Insert indexterm action

Post by opeongo »

I would like to have this kind of feature also. Is is possible for SyncRO Soft to add this to the factory installed OxygenXML Author so that us newbies do not have to learn the API and how to build custom extensions? Or perhaps make an example that we can download?

Thanks!
Radu
Posts: 9049
Joined: Fri Jul 09, 2004 5:18 pm

Re: Insert indexterm action

Post by Radu »

Hi,

What XML vocabulary are you using? The current thread was started by someone using the DocBook XML vocabulary. Are you using DITA, DocBook, or something else?
How should a custom indexterm insertion action behave in order to be useful to you?
If you want to select text and have it wrapped in an index term you can already define code templates in Oxygen, and inside code templates you can use editor variables to expand the current selection, there is an example here:
https://blog.oxygenxml.com/topics/dita_ ... ricks.html

around this paragraph:
Code templates - Define small fragments of XML content that can be inserted either by defining a shortcut key or by pressing ENTER in the editing area.
Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
opeongo
Posts: 14
Joined: Mon Jun 21, 2021 7:07 pm

Re: Insert indexterm action

Post by opeongo »

I am using DocBook.

Thank you for your suggestion. The code template works quite well for this.

I have defined my code template content as:

${selection}<indexterm><primary>${selection}</primary></indexterm>

Oxygen is a great product!
Radu
Posts: 9049
Joined: Fri Jul 09, 2004 5:18 pm

Re: Insert indexterm action

Post by Radu »

Hi,

Great, thanks for the kind words and for posting the XML fragment you ended up using.

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