1 package org.columba.chat.ui.conversation; 19 20 import java.io.IOException ; 21 22 import javax.swing.JEditorPane ; 23 import javax.swing.text.BadLocationException ; 24 import javax.swing.text.html.HTMLDocument ; 25 import javax.swing.text.html.HTMLEditorKit ; 26 import javax.swing.text.html.StyleSheet ; 27 28 import org.columba.chat.MainInterface; 29 import org.columba.chat.model.api.IBuddyStatus; 30 import org.columba.chat.ui.conversation.api.IMessageViewer; 31 import org.jivesoftware.smack.packet.Message; 32 33 39 public class HTMLViewer extends JEditorPane implements IMessageViewer { 40 41 private HTMLEditorKit kit; 42 43 private HTMLDocument doc; 44 45 private StringBuffer buffer = new StringBuffer (); 46 47 50 public HTMLViewer() { 51 super(); 52 53 setContentType("text/html; enctype='UTF-8'"); 54 55 kit = (HTMLEditorKit ) getEditorKit(); 56 doc = (HTMLDocument ) getDocument(); 57 58 62 63 StyleSheet myStyleSheet = new StyleSheet (); 64 myStyleSheet.addRule("body { font-size: 12 }"); 65 myStyleSheet 66 .addRule("body a { color: #5B62BC; text-decoration: underline }"); 67 68 kit.setStyleSheet(myStyleSheet); 69 doc = new HTMLDocument (myStyleSheet); 70 setDocument(doc); 71 setEditable(false); 72 73 } 74 75 protected void append(String html) { 76 html = html.replaceAll("\\n", "<br>"); 77 78 buffer.append(html); 79 80 try { 81 82 kit.insertHTML(doc, doc.getLength(), html, 0, 0, null); 83 } catch (BadLocationException e) { 84 85 e.printStackTrace(); 86 } catch (IOException e) { 87 88 e.printStackTrace(); 89 } 90 91 } 92 93 97 public void displayReceivedMessage(Message message, IBuddyStatus buddy) { 98 String body = message.getBody(); 99 String from = message.getFrom(); 100 101 append("<font color='#348756'>" + from + "</font>: " + body); 104 105 } 106 107 111 public void displaySendMessage(Message message, IBuddyStatus buddy) { 112 String body = message.getBody(); 113 String to = MainInterface.config.getAccount().getId(); 114 115 append("<font color='#2B3780'>" + to + "</font>: " + body); 117 } 118 119 } | Popular Tags |