1 19 22 23 24 package org.chateverywhere; 25 26 import javax.swing.*; 27 import javax.swing.text.*; 28 import java.awt.*; 29 import java.util.*; 30 31 32 public class JChatPane extends JTextPane 33 { 34 private SimpleAttributeSet set_default; 35 private SimpleAttributeSet set_nick; 36 private SimpleAttributeSet set_my_nick; 37 private SimpleAttributeSet set_highlight_nick; 38 private SimpleAttributeSet set_symbols; 39 private SimpleAttributeSet set_my_symbols; 40 private SimpleAttributeSet set_talk; 41 private SimpleAttributeSet set_action; 42 private SimpleAttributeSet set_msg; 43 private SimpleAttributeSet set_error; 44 private SimpleAttributeSet set_proposal; 45 private Document doc; 46 private JChatPaneScroller parent; 47 private String my_nick; 48 private boolean smileys_as_pics; 49 private Hashtable smileys; 50 51 public JChatPane(JChatPaneScroller parent, String my_nick, boolean smileys_as_pics) 52 { 53 super(); 54 this.parent = parent; 55 doc = getStyledDocument(); 56 this.my_nick = my_nick; 57 this.smileys_as_pics = smileys_as_pics; 58 59 60 set_default = new SimpleAttributeSet(); 61 StyleConstants.setForeground(set_default, Color.black); 62 StyleConstants.setItalic(set_default, false); 63 StyleConstants.setBold(set_default, false); 64 65 set_nick = new SimpleAttributeSet(set_default); 66 StyleConstants.setForeground(set_nick, Color.black); 67 StyleConstants.setBold(set_nick, true); 68 69 set_my_nick = new SimpleAttributeSet(set_default); 70 StyleConstants.setForeground(set_my_nick, Color.decode("#C00000")); 71 StyleConstants.setBold(set_my_nick, true); 72 73 set_highlight_nick = new SimpleAttributeSet(set_default); 74 StyleConstants.setForeground(set_highlight_nick, Color.decode("#EEEE00")); 75 StyleConstants.setBold(set_highlight_nick, true); 76 77 set_symbols = new SimpleAttributeSet(set_nick); 78 StyleConstants.setBold(set_symbols, true); 79 80 set_my_symbols = new SimpleAttributeSet(set_nick); 81 StyleConstants.setForeground(set_my_symbols, Color.gray); 82 83 set_talk = new SimpleAttributeSet(set_default); 84 85 set_action = new SimpleAttributeSet(set_default); 86 StyleConstants.setForeground(set_action, Color.green.darker()); 87 89 set_msg = new SimpleAttributeSet(set_default); 90 StyleConstants.setForeground(set_msg, Color.blue); 91 92 set_error = new SimpleAttributeSet(set_default); 93 StyleConstants.setForeground(set_error, Color.red); 94 StyleConstants.setBold(set_error, true); 95 96 set_proposal = new SimpleAttributeSet(set_default); 97 StyleConstants.setForeground(set_proposal, Color.lightGray); 98 99 } 100 101 102 public void disp_talk(String from, String msg) 103 { 104 SimpleAttributeSet symbols, nick; 105 106 107 if(my_nick.equals(from)) { 108 symbols = set_my_symbols; 109 nick = set_my_nick; 110 111 } else if(msg.toLowerCase().indexOf(my_nick.toLowerCase()) != -1) { 112 113 symbols = set_symbols; 114 nick = set_highlight_nick; 115 116 } else { 117 118 symbols = set_symbols; 119 nick = set_nick; 120 } 121 122 123 try { 124 doc.insertString(doc.getLength(), "<", symbols); 125 doc.insertString(doc.getLength(), from, nick); 126 doc.insertString(doc.getLength(), "> ", symbols); 127 128 129 if(smileys_as_pics) { 130 if(smileys == null) 131 load_smileys(); 132 print_smiley_msg(msg + "\n"); 133 } else { 134 doc.insertString(doc.getLength(), msg + "\n", set_talk); 135 } 136 } catch (Exception e) { 137 System.out.println("doc.insertString error !"); 138 System.out.println(e); 139 } 140 141 parent.scroll_to_bottom(); 142 } 143 144 145 public void disp_message(String msg) 146 { 147 try { 148 doc.insertString(doc.getLength(), "** " + msg + "\n", set_msg); 149 } catch (Exception e) { } 150 151 parent.scroll_to_bottom(); 152 } 153 154 155 public void disp_error(String msg) 156 { 157 try { 158 doc.insertString(doc.getLength(), "*** " + msg + "\n", set_error); 159 } catch (Exception e) { } 160 161 parent.scroll_to_bottom(); 162 } 163 164 public void disp_action(String from, String msg) 165 { 166 try { 167 doc.insertString(doc.getLength(), "* " + from + " " + msg + "\n", set_action); 168 } catch (Exception e) { } 169 170 parent.scroll_to_bottom(); 171 } 172 173 public void disp_sent_pmsg(String to, String msg) 174 { 175 try { 176 doc.insertString(doc.getLength(), ">", set_symbols); 177 doc.insertString(doc.getLength(), to, set_nick); 178 doc.insertString(doc.getLength(), "< ", set_symbols); 179 doc.insertString(doc.getLength(), msg + "\n", set_talk); 180 } catch (Exception e) { } 181 182 parent.scroll_to_bottom(); 183 } 184 185 186 public void disp_proposal(String msg) 187 { 188 try { 189 doc.insertString(doc.getLength(), "++ " + msg + "\n", set_proposal); 190 } catch (Exception e) { } 191 192 parent.scroll_to_bottom(); 193 } 194 195 196 public void clear() { this.setText(""); } 197 198 199 200 private void load_smileys() 201 { 202 smileys = new Hashtable(); 203 204 smileys.put(":-)", new ImageIcon(getClass().getResource( 205 "resources/pics/happy.gif"))); 206 smileys.put(":->", new ImageIcon(getClass().getResource( 207 "resources/pics/very_happy.gif"))); 208 smileys.put(":-|", new ImageIcon(getClass().getResource( 209 "resources/pics/neutral.gif"))); 210 smileys.put(";-)", new ImageIcon(getClass().getResource( 211 "resources/pics/wink.gif"))); 212 } 213 214 215 private void print_smiley_msg(String msg) 216 throws Exception 217 { 218 Enumeration smileys_txt = smileys.keys(); 219 String smiley_cur; 220 int p; 221 222 while(smileys_txt.hasMoreElements()) { 223 224 smiley_cur = (String ) smileys_txt.nextElement(); 225 226 if((p = msg.indexOf(smiley_cur)) != -1) { 227 228 print_smiley_msg(msg.substring(0, p)); 229 230 231 setCaretPosition(doc.getLength()); 232 setEditable(true); 233 insertIcon((Icon) smileys.get(smiley_cur)); 234 setEditable(false); 235 236 print_smiley_msg(msg.substring(p + smiley_cur.length())); 237 238 return; 239 } 240 } 241 242 243 doc.insertString(doc.getLength(), msg, set_talk); 244 } 245 246 247 252 253 public void set_my_nick(String nick) { my_nick = nick; } 254 public void set_smileys_as_pics(boolean s) { smileys_as_pics = s; } 255 public boolean get_smileys_as_pics() { return smileys_as_pics; } 256 } 257 | Popular Tags |