adding user defined tags to JTextPane
Good day, everyone!
I use JTextPane as the main editor. When a user writes some text
for example:
<author>Smith
<title>Hello world
<text>1 paragraph bla bla bla
<text>2 paragraph bla bla bla
I need to make <author>, <title>, <text> tags invinsible after they
were written by user and change the color of the text after the tag
until I find next tag. All tags and text colors will be defined by user.
The main problem is that I can't understand how Document,
EditorKit, ParagraphView and LabelView interacts with each
other. I am trying to create this editor for two days now and still
without any luck.
Can you give me some hint or advise where and how to start or small example?
Thanks. Andrey
[838 byte] By [
Andriuwkoa] at [2007-11-26 20:35:10]

# 1
> <author>Smith
> <title>Hello world
> <text>1 paragraph bla bla bla
> <text>2 paragraph bla bla bla
Hi there,
Some questions :
1. Is the tag always fix ? I mean will always "author", "title", and "text" ?
2. Is the color for every tag always the same ?
If yes, why don't you just play with if-else ?
# 2
No, tags and colors can be changed. For example
<author>Smith
<title>Hello world
<text>1 paragraph bla bla bla
<text>2 paragraph bla bla bla
can be changed to
<author>Smith
<title>Hello world
<text>1 paragraph bla bla bla
<comment>2 paragraph bla bla bla
by special controls. And colors will be defined thru settings.
But even if I play with if-else, I don't know where :D
I tried to extend ParagraphView and parse text there, but I got
very strange JTextPanes functioning as a result.
# 3
Well, I'm starting to move forward.
I created a code, which does something...
public DefaultVKDocument() {
addDocumentListener(new DocumentListener() {
public void insertUpdate(DocumentEvent e) {
final StyledDocument doc = (StyledDocument) e.getDocument();
final int start = 0;
final int end = doc.getLength();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
int length = end - start;
String text;
try {
text = doc.getText(start, length);
VKTokenizer st = new VKTokenizer(text);
while (st.hasToken()) {
VKTag tag = st.nextToken();
Color c;
if (tag.getTag().equals("<text>"))
c = Color.red;
else {
c = Color.BLUE;
}
String tagsText = tag.getText();
int len = tagsText.length();
SimpleAttributeSet style = new SimpleAttributeSet();
style.addAttribute(StyleConstants.Foreground, c);
doc.setParagraphAttributes(
tag.getStart(),
tag.getEnd() + len,
style,
false
);
}
} catch (BadLocationException e1) {
e1.printStackTrace();
}
}
});
}
public void removeUpdate(DocumentEvent e) {
}
public void changedUpdate(DocumentEvent e) {
}
});
}
but I'm not shure if is it right to use DocumentListener to parse text. But for now it is the only thing that moved me forward.