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 56 57 58 59 60 61 62 63 64 65 66
| public class SmiliesDocument extends DefaultStyledDocument {
Style happySmiley;
Style unHappySmiley;
Style tongueSmiley;
Style winkSmiley;
Style regular;
public SmiliesDocument() {
Style def = StyleContext.getDefaultStyleContext().
getStyle(StyleContext.DEFAULT_STYLE);
regular = addStyle("text", def);
happySmiley = addStyle("happy", regular);
StyleConstants.setAlignment(happySmiley, StyleConstants.ALIGN_CENTER);
ImageIcon happySmileyIcon = new ImageIcon("emoticon_smile.png");
if (happySmileyIcon != null) {
StyleConstants.setIcon(happySmiley, happySmileyIcon);
}
unHappySmiley = addStyle("unhappy", regular);
StyleConstants.setAlignment(unHappySmiley, StyleConstants.ALIGN_CENTER);
ImageIcon unHappySmileyIcon = new ImageIcon("emoticon_unhappy.png");
if (unHappySmileyIcon != null) {
StyleConstants.setIcon(unHappySmiley, unHappySmileyIcon);
}
tongueSmiley = addStyle("tongue", regular);
StyleConstants.setAlignment(happySmiley, StyleConstants.ALIGN_CENTER);
ImageIcon tongueSmileyIcon = new ImageIcon("emoticon_tongue.png");
if (tongueSmileyIcon != null) {
StyleConstants.setIcon(tongueSmiley, tongueSmileyIcon);
}
winkSmiley = addStyle("wink", regular);
StyleConstants.setAlignment(unHappySmiley, StyleConstants.ALIGN_CENTER);
ImageIcon winkSmileyIcon = new ImageIcon("emoticon_wink.png");
if (winkSmileyIcon != null) {
StyleConstants.setIcon(winkSmiley, winkSmileyIcon);
}
}
public void insertString(final int offs, String str, AttributeSet a) throws BadLocationException {
String text = getText(0, getLength());
if(")".equals(str)) {
if(text.charAt(offs - 1) == ':') {
replace(offs - 1, 1, " ", happySmiley);
return;
} else if(text.charAt(offs - 1) == ';') {
replace(offs - 1, 1, " ", winkSmiley);
return;
}
} else if("(".equals(str)) {
if(text.charAt(offs - 1) == ':') {
replace(offs - 1, 1, " ", unHappySmiley);
return;
}
} else if("p".equals(str)) {
if(text.charAt(offs - 1) == ':') {
replace(offs - 1, 1, " ", tongueSmiley);
return;
}
}
super.insertString(offs, str, a);
}
} |