001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.chat.ui.conversation;
019:
020: import java.io.IOException;
021:
022: import javax.swing.JEditorPane;
023: import javax.swing.text.BadLocationException;
024: import javax.swing.text.html.HTMLDocument;
025: import javax.swing.text.html.HTMLEditorKit;
026: import javax.swing.text.html.StyleSheet;
027:
028: import org.columba.chat.MainInterface;
029: import org.columba.chat.model.api.IBuddyStatus;
030: import org.columba.chat.ui.conversation.api.IMessageViewer;
031: import org.jivesoftware.smack.packet.Message;
032:
033: /**
034: * HTML view.
035: *
036: * @author fdietz
037: *
038: */
039: public class HTMLViewer extends JEditorPane implements IMessageViewer {
040:
041: private HTMLEditorKit kit;
042:
043: private HTMLDocument doc;
044:
045: private StringBuffer buffer = new StringBuffer();
046:
047: /**
048: *
049: */
050: public HTMLViewer() {
051: super ();
052:
053: setContentType("text/html; enctype='UTF-8'");
054:
055: kit = (HTMLEditorKit) getEditorKit();
056: doc = (HTMLDocument) getDocument();
057:
058: /*
059: * Font font = UIManager.getFont("Label.font"); String name =
060: * font.getName(); String size = Integer.toString(font.getSize());
061: */
062:
063: StyleSheet myStyleSheet = new StyleSheet();
064: myStyleSheet.addRule("body { font-size: 12 }");
065: myStyleSheet
066: .addRule("body a { color: #5B62BC; text-decoration: underline }");
067:
068: kit.setStyleSheet(myStyleSheet);
069: doc = new HTMLDocument(myStyleSheet);
070: setDocument(doc);
071: setEditable(false);
072:
073: }
074:
075: protected void append(String html) {
076: html = html.replaceAll("\\n", "<br>");
077:
078: buffer.append(html);
079:
080: try {
081:
082: kit.insertHTML(doc, doc.getLength(), html, 0, 0, null);
083: } catch (BadLocationException e) {
084:
085: e.printStackTrace();
086: } catch (IOException e) {
087:
088: e.printStackTrace();
089: }
090:
091: }
092:
093: /**
094: * @see org.altura.ui.conversation.view.IViewer#displayReceivedMessage(org.jivesoftware.smack.packet.Message,
095: * org.altura.jabber.BuddyStatus)
096: */
097: public void displayReceivedMessage(Message message,
098: IBuddyStatus buddy) {
099: String body = message.getBody();
100: String from = message.getFrom();
101:
102: //DateFormat format = DateFormat.getTimeInstance(DateFormat.SHORT);
103: // green color
104: append("<font color='#348756'>" + from + "</font>: " + body);
105:
106: }
107:
108: /**
109: * @see org.altura.ui.conversation.view.IViewer#displaySendMessage(org.jivesoftware.smack.packet.Message,
110: * org.altura.jabber.BuddyStatus)
111: */
112: public void displaySendMessage(Message message, IBuddyStatus buddy) {
113: String body = message.getBody();
114: String to = MainInterface.config.getAccount().getId();
115:
116: //DateFormat format = DateFormat.getTimeInstance(DateFormat.SHORT);
117: append("<font color='#2B3780'>" + to + "</font>: " + body);
118: }
119:
120: }
|