KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ChatPrivate


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 /* ChatPrivate
20  * manages one-to-one private chat in a separate window
21  */

22
23
24 import java.awt.*;
25 import java.awt.event.*;
26 import java.util.*;
27 import org.chateverywhere.*;
28
29 public class ChatPrivate extends Frame implements KeyListener, WindowListener
30 {
31     private Chat parent;
32     private String JavaDoc nick, my_nick;
33     private TextField usr_input;
34     private TextArea main_text;
35     private Font txt_fnt;
36     private boolean msg_indicator;
37     private InputStack input_stack;
38     private boolean tab_pressed = false;
39     private ResourceBundle captions;
40
41
42     ChatPrivate(Chat parent, String JavaDoc nick)
43     {
44         super();
45
46         // Variables
47
this.parent = parent;
48         captions = parent.get_translated_messages();
49         this.setTitle(captions.getString("MSG_CHAT")+" " + nick);
50         this.my_nick = parent.get_my_nick();
51         this.nick = nick;
52         input_stack = new InputStack();
53         txt_fnt = parent.get_txt_font();
54
55         // Creating components
56
setForeground(parent.getForeground());
57         setBackground(parent.getBackground());
58         usr_input = new TextField(30);
59             usr_input.setFont(txt_fnt);
60             usr_input.setForeground(parent.get_fginput_color());
61             usr_input.setBackground(parent.get_bginput_color());
62             usr_input.addKeyListener(this);
63         main_text = new TextArea("", 10, 30, TextArea.SCROLLBARS_VERTICAL_ONLY);
64             main_text.setEditable(false);
65             main_text.setFont(txt_fnt);
66             main_text.setForeground(parent.get_fgtext_color());
67             main_text.setBackground(parent.get_bgtext_color());
68
69         // Build the UI
70
setLayout(new BorderLayout());
71         add("Center", main_text);
72         add("South", usr_input);
73         setSize(new Dimension(300, 200));
74         doLayout();
75         addWindowListener(this);
76         parent.center_window(this);
77         setVisible(true);
78     }
79
80     public void disp_pmsg(String JavaDoc msg)
81     {
82         display_from(nick, msg);
83
84         if(getFocusOwner() == null) { // Window is not active
85
setTitle("!! "+captions.getString("MSG_CHAT")+" " + nick + " !!");
86             msg_indicator = true;
87         }
88     }
89
90     public void display_from(String JavaDoc f_nick, String JavaDoc msg)
91     {
92         main_text.append("<" + f_nick + "> " + msg + "\n");
93         main_text.setCaretPosition(30000);
94     }
95
96     public void change_font(Font new_fnt) {
97         main_text.setFont(new_fnt);
98     }
99
100
101     public void child_terminate()
102     {
103         parent.pchat_closed(nick);
104         dispose();
105     }
106
107
108     private void send_user_text()
109     {
110         String JavaDoc temp, orig = usr_input.getText();
111         int nlp;
112
113         temp = orig.trim();
114         if(temp.length() > 0) {
115             if(temp.startsWith("/")) {
116                 parent.proceed_command(temp.substring(1));
117                 input_stack.add(temp);
118             } else {
119                 // Cut the message if multiple lines
120
while((nlp = orig.indexOf("\n")) != -1) {
121                     String JavaDoc n = orig.substring(0, nlp);
122                     parent.send_pmsg(nick, n);
123                     display_from(my_nick, n);
124                     input_stack.add(n);
125                     orig = orig.substring(nlp + 1);
126                 }
127                 parent.send_pmsg(nick, orig);
128                 display_from(my_nick, orig);
129                 input_stack.add(orig);
130             }
131         }
132         usr_input.setText("");
133     }
134
135     /******* stop private chat if correspond quit ********/
136     public void correspondant_has_quit(String JavaDoc byemsg)
137     {
138         main_text.append("**** " + nick + " "+captions.getString("MSG_USER_QUIT")+" (" + byemsg +")\n");
139         usr_input.setEnabled(false);
140     }
141
142
143     /*****************************************************
144      * *
145      * Event Handlers *
146      * *
147      *****************************************************/

148     /******************** Keys input *********************/
149     public void keyPressed(KeyEvent e) {
150         int k_code = e.getKeyCode();
151         
152         if(k_code == KeyEvent.VK_ENTER) {
153             send_user_text();
154         } else if(k_code == KeyEvent.VK_UP) {
155             String JavaDoc n = input_stack.up();
156
157             usr_input.setText(n);
158             usr_input.setCaretPosition(n.length() + 1);
159         } else if(k_code == KeyEvent.VK_DOWN) {
160             String JavaDoc n = input_stack.down();
161
162             if(n != null) {
163                 usr_input.setText(n);
164                 usr_input.setCaretPosition(n.length() + 1);
165             }
166         } else if(k_code == KeyEvent.VK_ESCAPE) {
167             child_terminate();
168         }
169     }
170
171
172     /************ the user closed the window *************/
173     public void windowClosing(WindowEvent e)
174     {
175         child_terminate();
176     }
177
178     /************ the user gave us the focus *************/
179     public void windowActivated(WindowEvent e)
180     {
181         if(msg_indicator) {
182             setTitle(captions.getString("MSG_CHAT") + " " + nick);
183             msg_indicator = false;
184         }
185     }
186
187     /**************** unused event handlers **************/
188     public void keyReleased(KeyEvent e) {}
189     public void keyTyped(KeyEvent e) {}
190     public void windowClosed(WindowEvent e) {}
191     public void windowDeactivated(WindowEvent e) {}
192     public void windowDeiconified(WindowEvent e) {}
193     public void windowIconified(WindowEvent e) {}
194     public void windowOpened(WindowEvent e) {}
195
196 }
197
Popular Tags