Adding custom styles / behaviors to text in a StyledDocument
What I want to make:
A character in a JTextPane that appears bold and italicized, and displays a tool-tip on mouse-over. The character should behave otherwise like a normal character, but deleting it and undoing the delete, or copying it and pasting it elsewhere should preserve the bold/italics/tooltip.
What I have made (in a very complicated manner that I'd like to refactor):
A character that appears bold and italicized, and displays a tool-tip on mouse-over. Tool-tip text can be changed, removed, style removed, undone, redone, etc.
How I made it:
I just put a Position at the character and changed its style (while not reporting style changes to the UndoManager) and added a MouseMotionListener to change the JTextPane's tool-tip text every time the character is moused-over. However, this method requires a lot of ugly hacking, including a DocumentFilter to undo everything when the associated character is deleted, and is inherently buggy.
What I'm thinking:I have studied some of Stas's tutorials/samples on overriding JEditorPane and writing a custom ViewFactory, but can't exactly figure out from this the best way of actually having this behavior be implemented directly into the model. I think if it were built in to the model, then a lot of existing methods could be used to take care of what I've been trying to hack on from the outside. Unfortunately, not having a very deep (intuitive) understanding of the Swing document model, I am floundering just parsing the examples I've found.
My question:
Does anyone have any examples (preferably simple, well-commented ones) or tutorials of any sort where someone has successfully added a new character-like object or character-attribute to a StyledDocument or its editor?
[1833 byte] By [
Quintopiaa] at [2007-11-27 8:55:38]

# 1
What I would suggest is the same approach based on Style (an AttributeSet) all you need is to add bold and italic atribute and an additional custom attribute.
It can be done in single doc.insertString(offset, strChar, attrs) method.
When the mouse is over you can get caret position by viewToModel method and get attributes from character element. IF there is the custom attribute we defined show tooltip.
If you have more specific questions I'm ready to answer.
Regards,
Stas
# 3
1) use the addAttribute(...) method
2) How do you do this an any other editor, like Word for example? Editors assume you want to extend the attributes unless you assign a new set off attributes.
So say you Use the EditorKit.BOLD_ACTION. You click the button and start typing and all the text is bold. If you want non-bold text now, you click on the button again and the attributes is toggled off.
The editor can't read the mind of the user and randomly stop extending the attributes. So its up to the user to tell the edtitor that a new set of attributes is to be used.
# 4
1) Oh, forgot it inherited that method :(
2) What I meant was, how do I prevent it from extending the attribute by default. I don't want it to ever extend the attribute to other characters for any user, because I will be the main user of this software.
3) New question: Has anyone successfully solved the problem of styles not being copied when calling copy() or cut()? Is there any source code for reference?
# 5
2) It's rather simple. You can either override insertString() method. In the method you recreate attributeSet from passed parameter removing unnecessary on and call super.insertString with the new attribute set.
Or you can add a caret listener. On each caret update get input attributes from you kit (it's mutable) and remove the attribute(s) there.
3) In fact it's part of kit. You have to correct write() method of your kit (I guess it calls a Writer and the read(0 emthod.
Writer must write some info about the attribute and Reader must restore the attribute.
Regards,
Stas
# 6
4 stars to Stas for being mostly correct and very useful.
Unfortunately, copying, cutting, and pasting call neither of the read or write methods in StyledEditorKit (I tested this notion thoroughly). As it turns out, a bit more research revealed the correct way of implementing custom transfers (via drag,drop,copy,cut,paste). The java tutorial is here:
http://java.sun.com/docs/books/tutorial/uiswing/dnd/intro.html