I wrote a two panes diff class using two DefaultStyledDocument.
Now I would like to let the user change, in place, the colours (and, say, boldness) used to show the deletions and insertions, without re-diffing nor keeping the intermediate diff results.
The problem I am facing is that insertString copies the attributes, so I can not just update the Style instance afterward.
I tried DefaultStyledDocument.setLogicalStyle(int, Style) but nothing changed visually.
Do you see a (simple/cheap) way to do the style update without recreating the whole document ?
Logical styles apply to whole paragraphs. From what I can tell, there is no way to apply a Style to characters within a paragraph and have Swing automatically listen to changes in the Style.
What you can do is use the Style as a regular AttributeSet, then use an ElementIterator to scan for the styled elements, since a Style’s name is stored under the AttributeSet.NameAttribute attribute key:
Whether that qualifies as “simple/cheap” is up to you to decide.
Here’s a demo: