[oXygen-sdk] JTextArea#replaceRange in the editor does not seem to work

Alex Jitianu alex_jitianu at sync.ro
Fri Apr 8 03:10:16 CDT 2016


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/humanistika/oxygen/tei/authorizer/TeiAuthorizer.java#L150
>
> Any ideas why replaceRange doesn't work, or if there is a different
> API I should be using to do this?
>
> Cheers Adam.
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.oxygenxml.com/pipermail/oxygen-sdk/attachments/20160408/d342be6e/attachment.html>


More information about the oXygen-sdk mailing list