I am developing a netbeans platform application and I have a custom xml editor. The xml document looks similar to this one (This is an example):
<paragraph id="1">
<sentence id="1">
<word id="1">Hello</word>
<word id="2">World</word>
<word id="3">.</word>
</sentence>
<sentence id="2">
<word id="1">This</word>
<word id="2">is</word>
<word id="3">another</word>
<word id="4">sentence</word>
<word id="5">.</word>
</sentence>
</paragraph>
<paragraph id="1">
<sentence id="1">
<word id="1">New</word>
<word id="2">Sentence</word>
<word id="3">.</word>
</sentence>
<sentence id="2">
<word id="1">Test</word>
<word id="2">two</word>
<word id="3">.</word>
</sentence>
</paragraph>
Now the user wants to select the second sentence of the first paragraph. Here he clicks on the line of the second sentence and with a right click the "implemented" action will be executed.
I have the following code so far: In the first code snippet, I get the xml structure and the complete DomDocument. But I have no access to the selected line.
private final EditorCookie context;
The following is executed in the actionPerformed-Method:
CloneableEditorSupport editorSupport = (CloneableEditorSupport )context;
try {
InputStream inputStream = editorSupport.getInputStream();
InputSource inputSource = new InputSource(inputStream);
Document xmlDoc = XMLUtil.parse(inputSource, true, true, null, new RegisterCatalog());
NodeList nodeList = xmlDoc.getElementsByTagName("sentence");
...
And in the second code snippet, I have access to the selected line:
JEditorPane[] panes = context.getOpenedPanes();
if (panes != null) {
if (panes.length > 0) {
JEditorPane pane = panes[0];
AccessibleContext ac = pane.getAccessibleContext();
AccessibleText accessibleText = ac.getAccessibleText();
int caretPosition = accessibleText.getCaretPosition();
StyledDocument document = context.getDocument();
Element characterElement = document.getCharacterElement(caretPosition);
int start = characterElement.getStartOffset();
int end = characterElement.getEndOffset();
}
}
With the start and the end position I get:
<sentence id="2">
But what I want is with a right click on the line of the second sentence
I want to get:
<sentence id="2">
<word id="1">This</word>
<word id="2">is</word>
<word id="3">another</word>
<word id="4">sentence</word>
<word id="5">.</word>
</sentence>
Is there a possibility to access the whole tag including its closing tag? Another problem is the comparability of the lines because with
<sentence id="2">
I cannot uniquely identify which one is selected because there are two sentences with the id="2" and I need to distinguish which one is selected. I hope you can help me :)
No comments:
Post a Comment