1 22 23 package client; 24 25 import javax.swing.JTextPane ; 26 import javax.swing.text.html.HTMLEditorKit ; 27 import javax.swing.text.html.StyleSheet ; 28 import javax.swing.text.html.HTMLDocument ; 29 import javax.swing.text.html.HTML ; 30 import java.lang.String ; 31 import java.lang.StringBuffer ; 32 import java.nio.CharBuffer ; 33 import java.util.regex.Pattern ; 34 import java.awt.Color ; 35 36 37 42 public class ChatPane extends JTextPane { 43 44 String icoSad; 45 String icoSmily; 46 String icoTongue; 47 String icoWinking; 48 String icoOh; 49 LlamaChat llamaChat; 50 51 ChatPane(LlamaChat lc) { 52 super(); 53 54 llamaChat = lc; 55 56 setEditable(false); 57 setContentType("text/html"); 58 HTMLEditorKit kit = new HTMLEditorKit (); 59 StyleSheet css = new StyleSheet (); 60 css.addRule("BODY{ margin : 0;}"); 61 css.addRule("P{ margin : 0;}"); 62 css.addRule("A{ color:#0000FF; text-decoration:underline;}"); 63 kit.setStyleSheet(css); 64 setEditorKit(kit); 65 setBackground(new Color (249, 249, 250)); 66 67 icoSad = "<img SRC=\"" + lc.locationURL + "images/sad.gif\" height=\"14\" width=\"14\">"; 68 icoSmily = "<img SRC=\"" + lc.locationURL + "images/smiley.gif\" height=\"14\" width=\"14\">"; 69 icoOh = "<img SRC=\"" + lc.locationURL + "images/oh.gif\" height=\"14\" width=\"14\">"; 70 icoTongue = "<img SRC=\"" + lc.locationURL + "images/tongue.gif\" height=\"14\" width=\"14\">"; 71 icoWinking = "<img SRC=\"" + lc.locationURL + "images/winking.gif\" height=\"14\" width=\"14\">"; 72 73 addHyperlinkListener(lc.myHyperlinkListener); 74 } 75 76 84 public void sendText(String un, String message) { 85 sendText(un,message,false); 86 } 87 88 94 public void sendText(String un, String message, boolean whisper) { 95 StringBuffer buff = new StringBuffer (); 96 HTMLDocument doc = (HTMLDocument ) getDocument(); 97 HTMLEditorKit kit = (HTMLEditorKit ) getEditorKit(); 98 99 if (un == null || llamaChat.username == null) { 100 return; 101 } 102 103 if (!"server".equals(un)) { 104 message = message.replaceAll("<", "<"); 105 message = message.replaceAll(">", ">"); 106 } 107 message = message.replaceAll("\n", "<br>"); 108 109 if (llamaChat.username.equals(un)) { 110 buff.append("<font color=#009900><b>"); 111 } else if (un.equals("server")) { 112 buff.append("<font color=#990000><b>"); 113 } else { 114 buff.append("<font color=#000099><b>"); 115 } 116 buff.append(un); 117 buff.append("</b></font>"); 118 119 buff.append(" : "); 120 if (whisper) { 121 buff.append("<i>"); 122 } 123 char[] tmp = message.toCharArray(); 124 int start, end; 125 for (start = 0, end = 0; end < (tmp.length-1); ++end) { 126 if (tmp[end] == ';') { 127 if (tmp[end+1] == ')') { 128 if (end-start > 0) { 129 buff.append(tmp,start,end-start); 130 } 131 buff.append(icoWinking); 132 start = end += 2; 133 end--; 134 } 135 } else if (tmp[end] == ':') { 136 switch(tmp[end+1]) { 137 case ')': 138 if (end-start > 0) { 139 buff.append(tmp,start,end-start); 140 } 141 buff.append(icoSmily); 142 start = end += 2; 143 end--; 144 break; 145 case '(': 146 if (end-start > 0) { 147 buff.append(tmp,start,end-start); 148 } 149 buff.append(icoSad); 150 start = end += 2; 151 end--; 152 break; 153 case 'P': 154 if (end-start > 0) { 155 buff.append(tmp,start,end-start); 156 } 157 buff.append(icoTongue); 158 start = end += 2; 159 end--; 160 break; 161 case 'o': 162 if (end-start > 0) { 163 buff.append(tmp,start,end-start); 164 } 165 buff.append(icoOh); 166 start = end += 2; 167 end--; 168 break; 169 } 170 } else if (end + 10 < tmp.length && tmp[end] == 'h' && 171 tmp[end+1] == 't' && 172 tmp[end+2] == 't' && 173 tmp[end+3] == 'p' && 174 tmp[end+4] == ':' && 175 tmp[end+5] == '/' && 176 tmp[end+6] == '/') { 177 if (end-start > 0) { 178 buff.append(tmp,start,end-start); 179 } 180 int index; 181 for (index = 7; index < tmp.length && 182 Pattern.matches("[\\w.:/\\?\\=&%_\\-~]", CharBuffer.wrap(tmp, end+index, 1)); 183 index++) { } 184 185 buff.append("<a HREF=\""); 186 buff.append(tmp, end, index); 187 buff.append("\">"); 188 buff.append(tmp, end, index); 189 buff.append("</a>"); 190 start = end += index; 191 end--; 192 } 193 } 194 if (start < tmp.length) { 195 buff.append(tmp, start, tmp.length - start); 196 } 197 if (whisper) { 198 buff.append("</i>"); 199 } 200 if (buff.length() > 0) { 201 try { 202 buff.append("<br>"); 203 kit.insertHTML(doc,doc.getLength(), buff.toString(), 0,0,HTML.Tag.FONT); 204 setCaretPosition(doc.getLength()); 205 } catch (Throwable t) { t.printStackTrace(); } 206 } 207 if (message.startsWith("afk") || message.startsWith("brb")) { 208 llamaChat.afks.add(un); 209 llamaChat.updateList(); 210 } else { 211 if (llamaChat.afks.contains(un)) { 212 llamaChat.afks.remove(un); 213 llamaChat.updateList(); 214 } 215 } 216 } 217 218 222 public void error(String s) { 223 HTMLDocument doc = (HTMLDocument ) getDocument(); 224 HTMLEditorKit kit = (HTMLEditorKit ) getEditorKit(); 225 try { 226 kit.insertHTML(doc, doc.getLength(), "<font color=#CC0000><b>ERROR : " + s + "<b><br></font>", 0,0,HTML.Tag.FONT); 227 setCaretPosition(doc.getLength()); 228 } catch (Throwable t) { } 229 } 230 } 231 | Popular Tags |