JTextArea#replaceRange in the editor does not seem to work

We have created a plugin for Oxygen that provides suggestions for the values of various attributes. However we want the ability to add a new value to our list of suggestions from within the UI. I have been able to do this, by making one of the suggested attribute values in the CIValue list "Add New..." and then displaying a dialog box to the user when they select this (i.e. detecting when it is written into the attribute value in the document). However, I then want to replace the attribute value (currently "Add New...") with the actual value they entered into the document, I believe that I am doing this correctly by calling JTextArea#replaceRange, however after that call the attribute value in the Oxygen editor window does not seem to change. Here is the code where I call `replaceRange`: https://github.com/adamretter/TEI-Authorizer/blob/master/src/main/java/org/h... Any ideas why replaceRange doesn't work, or if there is a different API I should be using to do this? Cheers Adam. -- Adam Retter skype: adam.retter tweet: adamretter http://www.adamretter.org.uk

Hi Adam, As far as I can say, I see no reason why JTextArea#replaceRange wouldn't work. Have you added some log to make sure it really gets executed, with the correct values? JTextArea#replaceRange implementation uses the javax.swing.text.Document API to do the actual work but I don't think you'll see any differences if you use the document API instead. It might be a problem with the offsets given to the method. At a first glance I would say that the interval to replace should be (to cover the case when start > 0): jTextArea.replaceRange(suggestion, *start + *lastIndex, *start + *lastIndex + ADD_NEW.length()); Perhaps you can also resolve the use case without a timer. CIValue#getInsertString() gets called after the user chooses a value in the content completion window, at the moment when we have to decide what to insert in the document. I suggest overriding this method and getting the value from the user then. I've tested it in Oxygen v17.1 and it worked well. public List<CIValue> filterAttributeValues(List<CIValue> attributeValues, WhatPossibleValuesHasAttributeContext context) { // If the element from the current context is the "image" element and the current context // attribute is "href" then add a custom URL value to the list of default content completion // proposals. if (context != null) { ContextElement currentElement = context.getParentElement(); String attributeName = context.getAttributeName(); if("image".equals(currentElement.getQName()) && "href".equals(attributeName)) { CIValue newValue = new CIValue("Add new...") { String insertValue = null; @Override public String getInsertString() { if (insertValue == null) { // This call might come multiple times. Make sure we present the dialog only once. insertValue = getValue(); String valueFromUser = JOptionPane.showInputDialog("Give me a value: ", ""); if (valueFromUser != null) { insertValue = valueFromUser; } } return insertValue; } }; if (attributeValues == null) { attributeValues = new ArrayList<CIValue>(); } // Add the new value. attributeValues.add(newValue); } } return attributeValues; } Best regards, Alex -- Alex Jitianu <oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger http://www.oxygenxml.com On 4/8/2016 7:32 AM, Adam Retter wrote:
We have created a plugin for Oxygen that provides suggestions for the values of various attributes.
However we want the ability to add a new value to our list of suggestions from within the UI. I have been able to do this, by making one of the suggested attribute values in the CIValue list "Add New..." and then displaying a dialog box to the user when they select this (i.e. detecting when it is written into the attribute value in the document).
However, I then want to replace the attribute value (currently "Add New...") with the actual value they entered into the document, I believe that I am doing this correctly by calling JTextArea#replaceRange, however after that call the attribute value in the Oxygen editor window does not seem to change.
Here is the code where I call `replaceRange`: https://github.com/adamretter/TEI-Authorizer/blob/master/src/main/java/org/h...
Any ideas why replaceRange doesn't work, or if there is a different API I should be using to do this?
Cheers Adam.

Hey Alex, Thanks for your reply. It was very helpful, my comments are inline below:
It might be a problem with the offsets given to the method. At a first glance I would say that the interval to replace should be (to cover the case when start > 0):
jTextArea.replaceRange(suggestion, start + lastIndex, start + lastIndex + ADD_NEW.length());
Doh! Yes, of course you are correct, my offsets should be from `start`. When I make that change, then replaceRange does indeed work.
Perhaps you can also resolve the use case without a timer. CIValue#getInsertString() gets called after the user chooses a value in the content completion window, at the moment when we have to decide what to insert in the document. I suggest overriding this method and getting the value from the user then. I've tested it in Oxygen v17.1 and it worked well.
I was hoping that you might suggest something like that :-) I just used `Timer` as it enabled me to get something working quickly without having to dig into the Oxygen API in too much detail. I have now switched to overriding CIValue#getInsertString() and it is working well. Thanks again. Adam. -- Adam Retter skype: adam.retter tweet: adamretter http://www.adamretter.org.uk
participants (2)
-
Adam Retter
-
Alex Jitianu