1

I would like to have a document editor where certain document lines are uneditable, unclickable, etc.

I currently am using a JTextPane for my editor and extensions of DefaultStyledDocument and LeafElement, StyledEditorKit, and ParagraphView, to parse the document text into the appropriate elements and assign them to the appropriate extensions of ParagraphView. Everything is peachy up to this point.

So how to take the next step? How to designate and enforce certain extended ParagraphViews are "read-only"? My current approach is to override getNextVisualPositionFrom in the Leaf and Section ParagraphViews to prevent arrow keys from moving the cursor into a restricted ParagraphView, but preventing the mouse from clicking inside a restricted ParagraphView has to be handled separately.

Is there a simpler, more comprehensive approach to this? Thanks!

4

1 に答える 1

1

After a bit more research, a different approach that seems to fit more elegantly is to use a ComponentView for the uneditable views, instead of ParagraphView. I can then use something as simple as a JTextArea in the ComponentView in its createComponent() method and setEditable to false.

It automatically ignores mouse clicks in that view. I still have to include my code for nicely skipping the caret over the view when the user presses up, down, left, right. Otherwise, the caret will invisibly move through underlying model text. I will also have to prevent backspacing through the protected view. Even though the view is uneditable, the model data underneath can still be affected by the user.

于 2012-11-20T22:12:00.207 に答える