Get multiple selected AuthorNodes

Post here questions and problems related to oXygen frameworks/document types.
cditcher
Posts: 29
Joined: Thu Jun 21, 2012 6:47 pm
Location: Victoria BC, Canada

Get multiple selected AuthorNodes

Post by cditcher »

Hi I am attempting to get multiple selected nodes (something like List<AuthorNode>) from AuthorAccess object in our custom framework. Consider the scenario of multiple cells selected in a table (we are using oasis:table). You can see from the attached screenshot the user has selected 4 cells across 2 columns and see the corresponding selected elements in the outline view to the left.
Image
I have attempted the following test code:

Code: Select all

List<ContentInterval> selections = aa.getEditorAccess().getAuthorSelectionModel().getSelectionIntervals();
for (ContentInterval contentInterval : selections) {
log.debug(aa.getDocumentController().getNodeAtOffset(contentInterval.getStartOffset()).getDisplayName());
}
The log output shows that only the trow parent elements are selected by this:

Code: Select all

7004548 DEBUG[ AWT-EventQueue-0 ] ca.bc.gov.qp.civix.municipal.action.TableOperation - oasis:trow
7005643 DEBUG [ AWT-EventQueue-0 ] ca.bc.gov.qp.civix.municipal.action.TableOperation - oasis:trow
I am trying to get a List<AuthorNode> of the selected (in this case) <oasis:entry/> elements. Any thoughts?
Radu
Posts: 9041
Joined: Fri Jul 09, 2004 5:18 pm

Re: Get multiple selected AuthorNodes

Post by Radu »

Hi,

The internal hierarchy of AuthorNode nodes looks like the one in this image:

https://www.oxygenxml.com/InstData/Edit ... gment.html

Each node has an inclusive start offset and an inclusive end offset.
When you are calling this method:

Code: Select all

aa.getDocumentController().getNodeAtOffset(contentInterval.getStartOffset())
the API considers that the offset represents a caret position, so the actual node which will be returned will not be the node containing the start offset but the node which is its parent. So in this case asking for the node at "contentInterval.getStartOffset() + 1" would return what you expected.
Also the problem is a little bit more complicated, a single selection interval may contain one or more consecutive nodes, so if you look only for the node containing the start of the selection interval, you may not take into consideration nodes which are fully engulfed in the selection.

There is a special API which you could also call for each selection interval:

Code: Select all

ro.sync.ecss.extensions.api.AuthorDocumentController.getNodesToSelect(int, int)
and the merge all returned nodes in a list/set.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
cditcher
Posts: 29
Joined: Thu Jun 21, 2012 6:47 pm
Location: Victoria BC, Canada

Re: Get multiple selected AuthorNodes

Post by cditcher »

After looking at your answer it seems I was almost there. Combining the .getNodesToSelect() method with results returned from .getSelectionIntervals() gave me what i was looking for. The following code snippet works, even when there is no selection:

Code: Select all

List<ContentInterval> selections = aa.getEditorAccess().getAuthorSelectionModel().getSelectionIntervals();
for (ContentInterval contentInterval : selections) {
List<AuthorNode> selected = aa.getDocumentController().getNodesToSelect(
contentInterval.getStartOffset(),
contentInterval.getEndOffset()
);
for (AuthorNode authorNode : selected) {
log.info(authorNode.getDisplayName());
}
}
Thanks again for your help, Radu!
Post Reply