KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > chateverywhere > JChatPane


1 /* Chat
2  * Copyright (C) 1998-2004 Alexis de Bernis <alexis@bernis.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  *
18  */

19 /* JChatPane
20  * styled text area designed for Chat
21  */

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 JavaDoc my_nick;
48     private boolean smileys_as_pics;
49     private Hashtable smileys;
50
51     public JChatPane(JChatPaneScroller parent, String JavaDoc 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         /* Initialize the styles */
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 // StyleConstants.setItalic(set_action, true);
88

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 JavaDoc from, String JavaDoc msg)
103     {
104         SimpleAttributeSet symbols, nick;
105         
106         
107         if(my_nick.equals(from)) { /* We are talking */
108             symbols = set_my_symbols;
109             nick = set_my_nick;
110
111         } else if(msg.toLowerCase().indexOf(my_nick.toLowerCase()) != -1) {
112             /* Somebody is talking to us */
113             symbols = set_symbols;
114             nick = set_highlight_nick;
115         
116         } else {
117             /* normal message */
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             /* Do we need to pay attention to smileys */
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 JavaDoc 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 JavaDoc msg)
146     {
147         try {
148             doc.insertString(doc.getLength(), "** " + msg + "\n", set_msg);
149         } catch (Exception JavaDoc e) { }
150
151         parent.scroll_to_bottom();
152     }
153     
154     
155     public void disp_error(String JavaDoc msg)
156     {
157         try {
158             doc.insertString(doc.getLength(), "*** " + msg + "\n", set_error);
159         } catch (Exception JavaDoc e) { }
160
161         parent.scroll_to_bottom();
162     }
163     
164     public void disp_action(String JavaDoc from, String JavaDoc msg)
165     {
166         try {
167             doc.insertString(doc.getLength(), "* " + from + " " + msg + "\n", set_action);
168         } catch (Exception JavaDoc e) { }
169
170         parent.scroll_to_bottom();
171     }
172
173     public void disp_sent_pmsg(String JavaDoc to, String JavaDoc 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 JavaDoc e) { }
181
182         parent.scroll_to_bottom();
183     }
184
185     /********** print a nick completion proposal *********/
186     public void disp_proposal(String JavaDoc msg)
187     {
188         try {
189             doc.insertString(doc.getLength(), "++ " + msg + "\n", set_proposal);
190         } catch (Exception JavaDoc e) { }
191
192         parent.scroll_to_bottom();
193     }
194
195     /***************** clear the chat text ***************/
196     public void clear() { this.setText(""); }
197
198
199     /***************** clear the chat text ***************/
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     /******* recursively print a msg with smileys ********/
215     private void print_smiley_msg(String JavaDoc msg)
216      throws Exception JavaDoc
217     {
218         Enumeration smileys_txt = smileys.keys();
219         String JavaDoc smiley_cur;
220         int p;
221         
222         while(smileys_txt.hasMoreElements()) {
223
224             smiley_cur = (String JavaDoc) smileys_txt.nextElement();
225             
226             if((p = msg.indexOf(smiley_cur)) != -1) {
227
228                 print_smiley_msg(msg.substring(0, p));
229
230                 /* Brain-dead method to add an icon in a JTextPane... */
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         /* No smiley found */
243         doc.insertString(doc.getLength(), msg, set_talk);
244     }
245
246
247     /*****************************************************
248      * *
249      * Accessors *
250      * *
251      *****************************************************/

252     /**************** chat pane parameters ***************/
253     public void set_my_nick(String JavaDoc 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