I'm trying to create a simple WYSIWYG editor that will allow users to select text and bold/underline/italicise it. Currently the user can select text, right-click it and select bold from a popup menu, which ends up applying the bold style to the selected text like so:
this.getStyledDocument().setCharacterAttributes(this.getSelectionStart(), this.getSelectionEnd()-this.getSelectionStart(), boldStyle, false);
The bold style is set up like so:
boldStyle = this.addStyle("Bold", null);
StyleConstants.setBold(boldStyle, true);
What I would like to know, is if it is possible to get the style for the currently selected text, so that if a user attempts to "bold" some text that is already bold, I can detect this and write code to un-bold this text instead of simply applying the bold style to it again?
Something like:
if(!this.getStyledDocument().getStyleForSelection(this.getSelectionStart(), this.getSelectionEnd()-this.getSelectionStart()).isBold()){
//do bold
}
else{
//un-bold
}
Would be a dream come true, but I have no hope for this. What I'm realistically hoping for is to either be told I'm doing it wrong and be shown "the way", or to be pointed in the direction of a round-a-bout method of achieving this.
Thank you very much for your time.