1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
class MyOwnStyledDocument extends DefaultStyledDocument {
MutableAttributeSet coloredText;
private DefaultStyledDocument doc;
private Element rootElement;
private Style anIconStyle = null;
private Style aComponent = null;
public SyntaxDocument() {
doc = this;
rootElement = doc.getDefaultRootElement();
coloredText = new SimpleAttributeSet();
StyleConstants.setForeground(coloredText, Color.red);
doc.addStyle("anIconStyle", null);
StyleConstants.setIcon(aIconStyle, new ImageIcon("path to your smiley.png"));
aComponent = doc.addStyle("aComponent", null);
JLabel aLink = new JLabel(); // JButton,
aLink.bindToWhatEverAction(); // this is up to you.
StyleConstants.setComponent(aComponent, aLink);
}
/* overrided method */
public void insertString(int offset, String s ..) throws BadLocationException {
super.insertString(offset, s, ...);
// now apply you own styles
applyStyles(offset, s);
}
/* overrided method */
public void remove(...) throws BadLocationException {
super.remove(.....);
...
}
private void applySttles() {
... test which style match to you changes.
// here a colored text in red
doc.setCharacterAttributes(fromOffset, ToOffset, coloredText, false);
// here a smiley
doc.insertString(offset, "what ever (it will be ignored)", anIconStyle);
// here a link.
doc.insertString(offset, "what ever (it will be ignored)", aComponent);
}
} |
Partager