tags:

views:

33

answers:

2

I have textpane getting generated. i need to supercript the text when the text is selected and during click of superscript button, i need to superscript the text. if the text is already superscripted, it needs to unsuperscript the text. My problem is i am able to superscript the text, but unable to restore back. I am checking for the isSuperscript condition, but then every time it returns the value as true and sets the text as superscript. below is the code i am using, can anyone tell me how i can reset the superscripted text.

 SimpleAttributeSet sasText = new SimpleAttributeSet(parentTextPane.getCharacterAttributes());
 System.out.println("character set 1 " + sasText.toString());

 if ( StyleConstants.isSuperscript(sasText) ){ 
     System.out.println("already super"); 
     StyleConstants.setSuperscript(sasText, false);  
 } else { 
     System.out.println("needs super"); 
     StyleConstants.setSuperscript(sasText, true);     
 }

 int caretOffset = parentTextPane.getSelectionStart();

 parentTextPane.select(caretOffset, caretOffset + textLength);
 HTMLDoc.setCharacterAttributes(selStart,textLength,sasText, false);

 parentEkit.refreshOnUpdate();
A: 

It works fine for me. I do a quick test with code like:

SimpleAttributeSet green = new SimpleAttributeSet();
System.out.println( StyleConstants.isSuperscript(green) );
StyleConstants.setForeground(green, Color.GREEN);
StyleConstants.setSuperscript(green, true);
System.out.println( StyleConstants.isSuperscript(green) );
StyleConstants.setSuperscript(green, false);
System.out.println( StyleConstants.isSuperscript(green) );

and get the output:

false
true
false

which proves that the attribute is being reset properly. The text is also displayed properly.

If your "sasText" always returns true when you test for the superscript attribute then you must be resetting that attribute somewhere else in your code.

If you need more help post your SSCCE showing the problem.

camickr
+1  A: 

The problem is that parentTextPane.getCharacterAttributes() will return the character attributes for the character after the current caret position. As your selection encompasses your superscript text, the following character is normal. It is the attributes for that following char that you are testing, and the result will be false. You have the option of doing what getCharacterAttributes() (from JTextPane):

public AttributeSet getCharacterAttributes() {
    StyledDocument doc = getStyledDocument();
    Element run = doc.getCharacterElement(getCaretPosition());
    if (run != null) {
        return run.getAttributes();
    }
    return null;
}

except that you want to return the start of your selection:

public AttributeSet getMyCharacterAttributes() {
    StyledDocument doc = parentTextPane.getStyledDocument();
    Element run = doc.getCharacterElement(parentTextPane.getSelectionStart());
    if (run != null) {
        return run.getAttributes();
    }
    return null;
}

Your code would then change to do something like the following:

SimpleAttributeSet sasText = new SimpleAttributeSet(getMyCharacterAttributes());
//... the rest of your code
akf
Thanks a lot AKF. I had a feeling that it was about this getCharacterAttribute() but didn't have a proper skills to change it.