KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > chat > ui > conversation > HTMLViewer


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.chat.ui.conversation;
19
20 import java.io.IOException JavaDoc;
21
22 import javax.swing.JEditorPane JavaDoc;
23 import javax.swing.text.BadLocationException JavaDoc;
24 import javax.swing.text.html.HTMLDocument JavaDoc;
25 import javax.swing.text.html.HTMLEditorKit JavaDoc;
26 import javax.swing.text.html.StyleSheet JavaDoc;
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 /**
34  * HTML view.
35  *
36  * @author fdietz
37  *
38  */

39 public class HTMLViewer extends JEditorPane JavaDoc implements IMessageViewer {
40
41     private HTMLEditorKit JavaDoc kit;
42
43     private HTMLDocument JavaDoc doc;
44
45     private StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
46
47     /**
48      *
49      */

50     public HTMLViewer() {
51         super();
52
53         setContentType("text/html; enctype='UTF-8'");
54
55         kit = (HTMLEditorKit JavaDoc) getEditorKit();
56         doc = (HTMLDocument JavaDoc) getDocument();
57
58         /*
59          * Font font = UIManager.getFont("Label.font"); String name =
60          * font.getName(); String size = Integer.toString(font.getSize());
61          */

62
63         StyleSheet JavaDoc myStyleSheet = new StyleSheet JavaDoc();
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 JavaDoc(myStyleSheet);
70         setDocument(doc);
71         setEditable(false);
72
73     }
74
75     protected void append(String JavaDoc 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 JavaDoc e) {
84
85             e.printStackTrace();
86         } catch (IOException JavaDoc e) {
87
88             e.printStackTrace();
89         }
90
91     }
92
93     /**
94      * @see org.altura.ui.conversation.view.IViewer#displayReceivedMessage(org.jivesoftware.smack.packet.Message,
95      * org.altura.jabber.BuddyStatus)
96      */

97     public void displayReceivedMessage(Message message, IBuddyStatus buddy) {
98         String JavaDoc body = message.getBody();
99         String JavaDoc from = message.getFrom();
100
101         //DateFormat format = DateFormat.getTimeInstance(DateFormat.SHORT);
102
// green color
103
append("<font color='#348756'>" + from + "</font>: " + body);
104
105     }
106
107     /**
108      * @see org.altura.ui.conversation.view.IViewer#displaySendMessage(org.jivesoftware.smack.packet.Message,
109      * org.altura.jabber.BuddyStatus)
110      */

111     public void displaySendMessage(Message message, IBuddyStatus buddy) {
112         String JavaDoc body = message.getBody();
113         String JavaDoc to = MainInterface.config.getAccount().getId();
114
115         //DateFormat format = DateFormat.getTimeInstance(DateFormat.SHORT);
116
append("<font color='#2B3780'>" + to + "</font>: " + body);
117     }
118
119 }
Popular Tags