I want to implement a text editor in java where the model and the view of the text area are completely seperate and some parts of the text are "calculated". I went through the documentation and some examples of the Java editor kit, Document and DocumentFilter, but I'm still lost on this subject.
In short what I want to do is a text editor where certain words matching a pattern (e.g.: ID123456) would be swapped with another property of the object referenced by the id in the matching text. This swap should only happen in the view though, when the text area is modified or saved then the original text should be saved not the one projected for the view. Of course in this case the projected text can't be changed, but this can easily be handled with a DocumentFilter.
An example to make the task clearer:
The model behind the text editor:
Is it true that ID123 has the largest bauxite production of the world?
No, it's not true, ID44 has the largest bauxite production.
What the user should see:
Is it true that Belgium has the largest bauxite production of the world?
No, it's not true, Australia has the largest bauxite production.
I'm currently thinking about writing a parser that does this conversion every time it's needed, but I don't really like this solution. Any help would be appreciated on what would be the optimal way to do this.
I think you need to override EditorKit's read/write methods and add your own reader and writer (exact ones depend on EditorKit you extend).
On read place the IDs in an attribute of the character element e.g. MyCustomId=ID123 and store it in the model. Text of the element should be actually replaced with corresponding value (country in our case).
On write do opposite. Check whether text element has MyCustomId attribute specified. If the attribute exists write the value instead of the element text.