KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > client > ChatPane


1 /*- ChatPane.java -------------------------------------------------+
2  | |
3  | Copyright (C) 2002-2003 Joseph Monti, LlamaChat |
4  | countjoe@users.sourceforge.net |
5  | http://www.42llamas.com/LlamaChat/ |
6  | |
7  | This program is free software; you can redistribute it and/or |
8  | modify it under the terms of the GNU General Public License |
9  | as published by the Free Software Foundation; either version 2 |
10  | of the License, or (at your option) any later version |
11  | |
12  | This program is distributed in the hope that it will be useful, |
13  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15  | GNU General Public License for more details. |
16  | |
17  | A copy of the GNU General Public License may be found in the |
18  | installation directory named "GNUGPL.txt" |
19  | |
20  +-----------------------------------------------------------------+
21  */

22
23 package client;
24
25 import javax.swing.JTextPane JavaDoc;
26 import javax.swing.text.html.HTMLEditorKit JavaDoc;
27 import javax.swing.text.html.StyleSheet JavaDoc;
28 import javax.swing.text.html.HTMLDocument JavaDoc;
29 import javax.swing.text.html.HTML JavaDoc;
30 import java.lang.String JavaDoc;
31 import java.lang.StringBuffer JavaDoc;
32 import java.nio.CharBuffer JavaDoc;
33 import java.util.regex.Pattern JavaDoc;
34 import java.awt.Color JavaDoc;
35
36 /* -------------------- JavaDoc Information ----------------------*/
37 /**
38  * This represents the chat pane where chat dialoge takes place
39  * @author Joseph Monti <a HREF="mailto:countjoe@users.sourceforge.net">countjoe@users.sourceforge.net</a>
40  * @version 0.8
41  */

42 public class ChatPane extends JTextPane JavaDoc {
43     
44     String JavaDoc icoSad;
45     String JavaDoc icoSmily;
46     String JavaDoc icoTongue;
47     String JavaDoc icoWinking;
48     String JavaDoc 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 JavaDoc kit = new HTMLEditorKit JavaDoc();
59         StyleSheet JavaDoc css = new StyleSheet JavaDoc();
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 JavaDoc(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     /**
77      * Sends a text to the chat window. Parses the message to pick
78      * out emoticons and links.
79      * @param un the name of the user sending the message
80      * @param message the message to be sent
81      * @param whisper indicates the message was a wisper and makes the
82      * message italic
83      */

84     public void sendText(String JavaDoc un, String JavaDoc message) {
85         sendText(un,message,false);
86     }
87     
88     /**
89      * Sends a text to the chat window. Parses the message to pick
90      * out emoticons and links.
91      * @param un the name of the user sending the message
92      * @param message the message to be sent
93      */

94     public void sendText(String JavaDoc un, String JavaDoc message, boolean whisper) {
95         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
96         HTMLDocument JavaDoc doc = (HTMLDocument JavaDoc) getDocument();
97         HTMLEditorKit JavaDoc kit = (HTMLEditorKit JavaDoc) getEditorKit();
98
99         if (un == null || llamaChat.username == null) {
100             return;
101         }
102         
103         if (!"server".equals(un)) {
104             message = message.replaceAll("<", "&lt;");
105             message = message.replaceAll(">", "&gt;");
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("&nbsp;&nbsp;:&nbsp;");
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 JavaDoc 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     /**
219      * signifies an error and reports it to the user
220      * @param s the error message
221      */

222     public void error(String JavaDoc s) {
223         HTMLDocument JavaDoc doc = (HTMLDocument JavaDoc) getDocument();
224         HTMLEditorKit JavaDoc kit = (HTMLEditorKit JavaDoc) 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 JavaDoc t) { }
229     }
230 }
231
Popular Tags