KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > chatserver > client > ChatWindow


1 /*
2  * This file is part of the QuickServer library
3  * Copyright (C) 2003-2005 QuickServer.org
4  *
5  * Use, modification, copying and distribution of this software is subject to
6  * the terms and conditions of the GNU Lesser General Public License.
7  * You should have received a copy of the GNU LGP License along with this
8  * library; if not, you can download a copy from <http://www.quickserver.org/>.
9  *
10  * For questions, suggestions, bug-reports, enhancement-requests etc.
11  * visit http://www.quickserver.org
12  *
13  */

14
15 package chatserver.client;
16
17 import javax.swing.text.*;
18 import java.awt.Color JavaDoc;
19 import java.awt.Point JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.logging.*;
23 import javax.swing.*;
24 import javax.swing.event.ListSelectionEvent JavaDoc;
25 import javax.swing.event.ListSelectionListener JavaDoc;
26 import java.awt.Graphics JavaDoc;
27 import java.awt.event.*;
28
29 /**
30  *
31  * @author Akshathkumar Shetty
32  */

33 public class ChatWindow extends javax.swing.JFrame JavaDoc {
34     private static Logger logger = Logger.getLogger(ChatWindow.class.getName());
35
36     private ClassLoader JavaDoc classLoader = getClass().getClassLoader();
37     public final ImageIcon logo = new ImageIcon(
38         classLoader.getResource("chatserver/client/face-smile.gif"));
39     public final ImageIcon smile = new ImageIcon(
40         classLoader.getResource("chatserver/client/smile.gif"));
41     public final ImageIcon sad = new ImageIcon(
42         classLoader.getResource("chatserver/client/sad.gif"));
43     
44     private ChatRoom chatRoom;
45     private LoginDialog loginDialog;
46     private DefaultStyledDocument logDoc = null;
47     private DefaultStyledDocument chatDoc = null;
48     private DefaultListModel userListModel = null;
49     private UserListListener userListListener = null;
50     private Map JavaDoc styleMap = new HashMap JavaDoc();
51     
52     final String JavaDoc NORMALBLUE = "NormalBlue";
53     final String JavaDoc BOLDBLUE = "BoldBlue";
54     final String JavaDoc NORMALBLACK = "NormalBlack";
55     final String JavaDoc ITALICBLACK = "ITALICBLACK";
56     final String JavaDoc BOLDGREEN = "BoldGreen";
57     final String JavaDoc NORMALRED = "NormalRed";
58     final String JavaDoc ITALICRED = "ItalicRed";
59     
60     private InfiniteProgressPanel glassPane;
61     
62     /** Creates new form ChatWindow */
63     public ChatWindow(ChatRoom chatRoom, String JavaDoc args[]) {
64         this.chatRoom = chatRoom;
65         setLogDoc(new DefaultStyledDocument());
66         setChatDoc(new DefaultStyledDocument());
67         setUserListModel(new DefaultListModel());
68         prepareAllStyles();
69         initComponents();
70         userListListener = new UserListListener(userList);
71         userList.addListSelectionListener(userListListener);
72         loginDialog = new LoginDialog(this, args);
73     }
74     
75     private void initComponents() {
76         getContentPane().setLayout(new java.awt.BorderLayout JavaDoc(2, 2));
77         setTitle("QuickChat - Please Login");
78         
79         buildChatPanel();
80         buildSendMsg();
81         buildLogPanel();
82         buildUserListPanel();
83
84         //---
85
jPanel1 = new javax.swing.JPanel JavaDoc();
86         jPanel1.setLayout(new java.awt.BorderLayout JavaDoc());
87         jPanel1.add(jPanel2, java.awt.BorderLayout.SOUTH); //sendmsg
88

89         JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
90             chatRoomScrollPane, jScrollPane1);
91         splitPane.setOneTouchExpandable(true);
92         java.awt.Dimension JavaDoc minimumSize = new java.awt.Dimension JavaDoc(500, 20);
93         chatRoomScrollPane.setMinimumSize(minimumSize);
94         splitPane.setDividerLocation(500);
95         splitPane.setResizeWeight(0.9);
96
97         jPanel1.add(splitPane, java.awt.BorderLayout.CENTER); //chat
98

99         jTabbedPane1 = new javax.swing.JTabbedPane JavaDoc();
100         jTabbedPane1.setTabPlacement(javax.swing.JTabbedPane.BOTTOM);
101         jTabbedPane1.addTab("Chat Room", jPanel1);
102         jTabbedPane1.addTab("Logs", logTextScrollPane);
103
104         getContentPane().add(jTabbedPane1, java.awt.BorderLayout.CENTER);
105
106         buildMenu();
107         setIconImage(logo.getImage());
108
109         addWindowListener(new WindowAdapter() {
110              public void windowClosing(WindowEvent e) {
111                  glassPane.interrupt();
112                  System.exit(0);
113              }
114              public void windowOpened(WindowEvent e) {
115              }
116         });
117         pack();
118         setLocationRelativeTo(null);
119
120         glassPane = new InfiniteProgressPanel("Logging to server..");
121         setGlassPane(glassPane);
122     }
123
124     private void loginMenuItemActionPerformed(final java.awt.event.ActionEvent JavaDoc evt) {
125         Thread JavaDoc t = new Thread JavaDoc() {
126             public void run() {
127                 while(true) {
128                     if(login()==false) break;
129                     logger.info("Calling login()");
130                 }
131             }
132         };
133         t.setPriority(Thread.NORM_PRIORITY);
134         t.start();
135     }
136     
137     private boolean login() {
138         loginDialog.clearStatus();
139         loginDialog.show();
140         if(loginDialog.isOk()==true) {
141             String JavaDoc r[] = loginDialog.getValues();
142             glassPane.start();
143             try {
144                 boolean flag = chatRoom.doLogin(r[0], Integer.parseInt(r[1]),
145                     r[2], r[3]);
146
147                 if(flag==true) {
148                     enableChat(true);
149                 } else {
150                     enableChat(false);
151                     return true;//recall the login dialog
152
}
153             } catch(Exception JavaDoc ex) {
154                 enableChat(false);
155                 logger.warning("Error logging in : "+ex);
156                 return true;
157             }
158         }
159         return false;
160     }
161     
162     private void logoutMenuItemActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
163         Thread JavaDoc t = new Thread JavaDoc() {
164             public void run() {
165                 try {
166                     chatRoom.doLogout();
167                 } catch(Exception JavaDoc ex) {
168                     loginMenuItem.setEnabled(true);
169                     logoutMenuItem.setEnabled(false);
170                     logger.warning("Error logging in : "+ex);
171                 }
172             }
173         };
174         t.setPriority(Thread.NORM_PRIORITY);
175         t.start();
176     }
177     
178     private void sendTextActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
179        sendButtonActionPerformed(evt);
180     }
181     
182     private void sendPrivateButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
183         if(userListListener.getSelecteduser()==null) {
184             setResponse("-ERR No User is selected!");
185             return;
186         }
187         if(sendText.getText().length()==0) {
188             setResponse("-ERR No message to send!");
189             return;
190         }
191         chatRoom.sendPrivateMessage(userListListener.getSelecteduser()+"@"+chatRoom.getRoom(),
192                 sendText.getText());
193         sendText.setText("");
194     }
195     
196     private void sendButtonActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
197         if(sendText.getText().length()==0) {
198             setResponse("-ERR No message to send!");
199             return;
200         }
201         chatRoom.sendMessage(sendText.getText());
202         sendText.setText("");
203     }
204     
205     
206     // Variables declaration - do not modify
207
private javax.swing.JScrollPane JavaDoc chatRoomScrollPane;
208     private javax.swing.JTextPane JavaDoc chatRoomTextPane1;
209     private javax.swing.JMenu JavaDoc jMenu1, jMenu2;
210     private javax.swing.JMenuBar JavaDoc jMenuBar1;
211     private javax.swing.JPanel JavaDoc jPanel1;
212     private javax.swing.JPanel JavaDoc jPanel2;
213     private javax.swing.JScrollPane JavaDoc jScrollPane1;
214     private javax.swing.JTabbedPane JavaDoc jTabbedPane1;
215     private javax.swing.JTextPane JavaDoc logTextPane;
216     private javax.swing.JScrollPane JavaDoc logTextScrollPane;
217     private javax.swing.JMenuItem JavaDoc loginMenuItem;
218     private javax.swing.JMenuItem JavaDoc logoutMenuItem;
219     private javax.swing.JMenuItem JavaDoc exitMenuItem;
220     private javax.swing.JMenuItem JavaDoc changeRoomMenuItem;
221     private javax.swing.JMenuItem JavaDoc updateUserListMenuItem;
222     private javax.swing.JMenuItem JavaDoc clearMenuItem;
223     private javax.swing.JMenuItem JavaDoc aboutMenuItem;
224     private javax.swing.JButton JavaDoc sendButton;
225     private javax.swing.JButton JavaDoc sendPrivateButton;
226     private javax.swing.JTextField JavaDoc sendText;
227     private javax.swing.JList JavaDoc userList;
228     // End of variables declaration
229

230     public void log(String JavaDoc msg) {
231         logger.fine("Got: "+msg);
232         try {
233             AttributeSet style = (AttributeSet)styleMap.get(NORMALBLACK);
234             getLogDoc().insertString(getLogDoc().getLength(),
235                     msg + "\n" ,style);
236             
237             //Point pt1=logTextPane.getLocation();
238
Point JavaDoc pt2 = new Point JavaDoc((int)(0),
239                     (int)(logTextPane.getBounds().getHeight()));
240             logTextScrollPane.getViewport().setViewPosition(pt2);
241         } catch(Exception JavaDoc e) {
242             logger.warning("Error: "+e);
243         }
244     }
245     
246     public DefaultStyledDocument getLogDoc() {
247         return logDoc;
248     }
249     
250     public void setLogDoc(DefaultStyledDocument logDoc) {
251         this.logDoc = logDoc;
252     }
253     
254     
255     
256     public void prepareAllStyles() {
257         SimpleAttributeSet aset = new SimpleAttributeSet();
258         StyleConstants.setForeground(aset,Color.blue);
259         StyleConstants.setFontSize(aset,12);
260         StyleConstants.setFontFamily(aset,"Verdana");
261         styleMap.put(NORMALBLUE,aset);
262
263         aset = new SimpleAttributeSet();
264         StyleConstants.setForeground(aset,Color.blue);
265         StyleConstants.setFontSize(aset,12);
266         StyleConstants.setFontFamily(aset,"Verdana");
267         StyleConstants.setBold(aset, true);
268         styleMap.put(BOLDBLUE,aset);
269
270         aset = new SimpleAttributeSet();
271         StyleConstants.setForeground(aset,Color.black);
272         StyleConstants.setFontSize(aset,12);
273         StyleConstants.setFontFamily(aset,"Verdana");
274         styleMap.put(NORMALBLACK,aset);
275
276         aset = new SimpleAttributeSet();
277         StyleConstants.setForeground(aset,Color.black);
278         StyleConstants.setFontSize(aset,12);
279         StyleConstants.setFontFamily(aset,"Verdana");
280         StyleConstants.setItalic(aset, true);
281         styleMap.put(ITALICBLACK,aset);
282
283         aset = new SimpleAttributeSet();
284         StyleConstants.setForeground(aset, new Color JavaDoc(0, 128, 0));
285         StyleConstants.setFontSize(aset,12);
286         StyleConstants.setFontFamily(aset,"Verdana");
287         StyleConstants.setBold(aset, true);
288         styleMap.put(BOLDGREEN,aset);
289
290         aset = new SimpleAttributeSet();
291         StyleConstants.setForeground(aset,Color.red);
292         StyleConstants.setFontSize(aset,12);
293         StyleConstants.setFontFamily(aset,"Verdana");
294         styleMap.put(NORMALRED,aset);
295
296         aset = new SimpleAttributeSet();
297         StyleConstants.setForeground(aset,Color.red);
298         StyleConstants.setFontSize(aset,12);
299         StyleConstants.setFontFamily(aset,"Verdana");
300         StyleConstants.setItalic(aset, true);
301         styleMap.put(ITALICRED,aset);
302     }
303     
304     public void addChatMessage(String JavaDoc message) {
305         logger.fine("Got: "+message);
306         if(message.startsWith("{system.help} ")) {
307             return;
308         } else if(message.startsWith("{system.debug} ")) {
309             //already logged
310
return;
311         }
312         
313         AttributeSet style = null;
314         String JavaDoc fromid = null;
315         String JavaDoc toid = null;
316         
317         String JavaDoc msgType = null;
318         try {
319             if(message.startsWith("{system.msg} ")) {
320                 msgType = "{system.msg}";
321                 message = message.substring(13);
322                 style = (AttributeSet)styleMap.get(BOLDBLUE);
323             } else if(message.startsWith("{system.error} ")) {
324                 msgType = "{system.error}";
325                 message = "Error: "+message.substring(15);
326                 style = (AttributeSet)styleMap.get(NORMALRED);
327             } else if(message.startsWith("{user.msg:")) {
328                 msgType = "{user.msg}";
329                 int j = message.indexOf(":", 10);//end of from
330
int i = message.indexOf("} ", 10);
331                 if(j!=-1) {
332                     toid = message.substring(j+1, i);
333                 } else {
334                     j = i;
335                 }
336                 fromid = message.substring(10, j);
337                 message = message.substring(i+2);
338                 style = (AttributeSet)styleMap.get(NORMALBLUE);
339             } else if(message.startsWith("{msg.user:")) { //gui command
340
msgType = "{msg.user}";
341                 int i = message.indexOf("} ", 10);
342                 toid = message.substring(10, i);
343                 message = message.substring(i+2);
344                 style = (AttributeSet)styleMap.get(NORMALBLUE);
345             } else if(message.startsWith("{user.info:")) {
346                 msgType = "{user.info}";
347                 int i = message.indexOf("} ", 11);
348                 fromid = message.substring(11, i);
349                 message = message.substring(i+2);
350                 if(message.equals("LoggedIn")) {
351                     addToUserList(fromid);
352                     message = "joined the room";
353                 } else if(message.equals("LoggedOut")) {
354                     removeFromUserList(fromid);
355                     message = "left the room";
356                 } else
357                     System.out.println("Unknown ->"+message+"<-");
358                 style = (AttributeSet)styleMap.get(ITALICBLACK);
359             } else {
360                 msgType = "{unknown}";
361                 style = (AttributeSet)styleMap.get(NORMALBLACK);
362             }
363             
364
365             if(msgType.equals("{user.msg}")) {
366                 toid = removeRoom(toid);
367                 fromid = removeRoom(fromid);
368                 if(toid!=null && toid.length()==0) {//to group
369
getChatDoc().insertString(getChatDoc().getLength(),
370                         fromid+": ", (AttributeSet)styleMap.get(NORMALRED));
371                 } else if(toid!=null) {
372                     getChatDoc().insertString(getChatDoc().getLength(),
373                         "PrvMsg From "+fromid+": ", (AttributeSet)styleMap.get(BOLDBLUE));
374                 }
375             } else if(msgType.equals("{msg.user}")) {
376                 toid = removeRoom(toid);
377                 getChatDoc().insertString(getChatDoc().getLength(),
378                         "PrvMsg To "+toid+": ", (AttributeSet)styleMap.get(BOLDBLUE));
379             } else if(msgType.equals("{user.info}")) {
380                 fromid = removeRoom(fromid);
381                 getChatDoc().insertString(getChatDoc().getLength(),
382                         fromid+": ", (AttributeSet)styleMap.get(NORMALRED));
383             }
384             
385             if(message.indexOf(":-)")==-1 && message.indexOf(":-(")==-1) {
386                 getChatDoc().insertString(getChatDoc().getLength(), message+ "\n", style);
387             } else {
388                 checkForSmile(message, style);
389                 getChatDoc().insertString(getChatDoc().getLength(), "\n", style);
390             }
391             
392             Point JavaDoc pt1 = chatRoomTextPane1.getLocation();
393             Point JavaDoc pt2 = new Point JavaDoc((int)(0),
394                     (int)(chatRoomTextPane1.getBounds().getHeight()));
395             chatRoomScrollPane.getViewport().setViewPosition(pt2);
396         } catch(Exception JavaDoc e) {
397             logger.warning("Error: "+e);
398         }
399
400         toFront();
401     }
402
403     protected void setEndSelection() {
404         int len = chatRoomTextPane1.getDocument().getLength();
405         chatRoomTextPane1.setSelectionStart(len);
406         chatRoomTextPane1.setSelectionEnd(len);
407     }
408     
409     public DefaultStyledDocument getChatDoc() {
410         return chatDoc;
411     }
412     
413     public void setChatDoc(DefaultStyledDocument chatDoc) {
414         this.chatDoc = chatDoc;
415     }
416     
417     public void setResponse(String JavaDoc res) {
418         int msgType = JOptionPane.PLAIN_MESSAGE ;
419         if(res.startsWith("+OK"))
420             msgType = JOptionPane.INFORMATION_MESSAGE;
421         if(res.startsWith("-ERR"))
422             msgType = JOptionPane.ERROR_MESSAGE;
423         toFront();
424         JOptionPane.showMessageDialog(this,
425                 res.substring(res.indexOf(" ")+1), "Response", msgType);
426     }
427     
428     public void enableChat(boolean flag) {
429         sendText.setEnabled(flag);
430         sendButton.setEnabled(flag);
431         sendPrivateButton.setEnabled(flag);
432         loginMenuItem.setEnabled(!flag);
433         logoutMenuItem.setEnabled(flag);
434         changeRoomMenuItem.setEnabled(flag);
435         updateUserListMenuItem.setEnabled(flag);
436         if(flag==false) {
437             userListModel.clear();
438             setTitle("QuickChat - Not Connected");
439         } else {
440             chatRoomTextPane1.setText("");
441             chatRoom.processReceivedMsg();
442         }
443         glassPane.stop();
444         glassPane.setVisible(false);
445     }
446     
447     public void addToUserList(String JavaDoc id) {
448         logger.fine("Got: "+id);
449         id = removeRoom(id);
450         getUserListModel().addElement(id);
451     }
452     
453     public void removeFromUserList(String JavaDoc id) {
454         logger.fine("Got: "+id);
455         id = removeRoom(id);
456         getUserListModel().removeElement(id);
457     }
458     
459     public DefaultListModel getUserListModel() {
460         return userListModel;
461     }
462     
463     public void setUserListModel(DefaultListModel userListModel) {
464         this.userListModel = userListModel;
465     }
466     
467     private String JavaDoc removeRoom(String JavaDoc id) {
468         if(id==null) return id;
469         if( id.endsWith("@"+chatRoom.getRoom()) ) {
470             id = id.substring(0, id.indexOf("@"+chatRoom.getRoom()) );
471         }
472         return id;
473     }
474
475     private void about() {
476         JOptionPane.showMessageDialog(this,
477             "QuickChat v 1.0\n\n"+
478             "GUI Client for ChatServer example of QuickServer.\n"+
479             "This is compliant with QuickServer v1.4.5 release.\n\n"+
480             "Copyright (C) 2005 QuickServer.org\n"+
481             "http://www.quickserver.org",
482             "About QuickChat",
483             JOptionPane.INFORMATION_MESSAGE,
484             logo);
485     }
486
487     private void changeRoom() {
488         String JavaDoc newRoom = (String JavaDoc) JOptionPane.showInputDialog(this,
489             "Chat Room:",
490             "Change Room", JOptionPane.PLAIN_MESSAGE, logo,
491                     null, chatRoom.getRoom());
492         if(newRoom==null) return;
493         chatRoom.changeRoom(newRoom);
494         userListModel.clear();
495         chatRoom.updateUserList();
496     }
497
498     //-- build gui
499
private void buildChatPanel() {
500         chatRoomTextPane1 = new JTextPane(getChatDoc());
501         //chatRoomTextPane1.setDocument(getChatDoc());
502
chatRoomTextPane1.setEditable(false);
503         chatRoomTextPane1.setMinimumSize(new java.awt.Dimension JavaDoc(500, 200));
504         chatRoomTextPane1.setPreferredSize(new java.awt.Dimension JavaDoc(500, 300));
505
506         chatRoomScrollPane = new JScrollPane(chatRoomTextPane1);
507         chatRoomScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
508     }
509
510     private void buildSendMsg() {
511         jPanel2 = new javax.swing.JPanel JavaDoc();
512         jPanel2.setLayout(new java.awt.GridBagLayout JavaDoc());
513         jPanel2.setMinimumSize(new java.awt.Dimension JavaDoc(373, 40));
514         jPanel2.setPreferredSize(new java.awt.Dimension JavaDoc(373, 50));
515
516         sendText = new javax.swing.JTextField JavaDoc();
517         sendText.setEnabled(false);
518         sendText.addActionListener(new java.awt.event.ActionListener JavaDoc() {
519             public void actionPerformed(final java.awt.event.ActionEvent JavaDoc evt) {
520                 java.awt.EventQueue.invokeLater(new Runnable JavaDoc() {
521                     public void run() {
522                         sendTextActionPerformed(evt);
523                     }
524                 });
525             }
526         });
527
528         java.awt.GridBagConstraints JavaDoc gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
529         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
530         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
531         gridBagConstraints.weightx = 1.0;
532         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 2, 0, 2);
533         jPanel2.add(sendText, gridBagConstraints);
534
535         sendButton = new javax.swing.JButton JavaDoc();
536         sendButton.setText("Send");
537         sendButton.setEnabled(false);
538         sendButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
539             public void actionPerformed(final java.awt.event.ActionEvent JavaDoc evt) {
540                 java.awt.EventQueue.invokeLater(new Runnable JavaDoc() {
541                     public void run() {
542                         sendButtonActionPerformed(evt);
543                     }
544                 });
545             }
546         });
547
548         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
549         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 2, 0, 2);
550         jPanel2.add(sendButton, gridBagConstraints);
551
552         sendPrivateButton = new javax.swing.JButton JavaDoc();
553         sendPrivateButton.setText("Private Mesage");
554         sendPrivateButton.setEnabled(false);
555         sendPrivateButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
556             public void actionPerformed(final java.awt.event.ActionEvent JavaDoc evt) {
557                 java.awt.EventQueue.invokeLater(new Runnable JavaDoc() {
558                     public void run() {
559                         sendPrivateButtonActionPerformed(evt);
560                     }
561                 });
562             }
563         });
564
565         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
566         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 2, 0, 2);
567         jPanel2.add(sendPrivateButton, gridBagConstraints);
568     }
569
570     private void buildLogPanel() {
571         logTextScrollPane = new javax.swing.JScrollPane JavaDoc();
572         logTextScrollPane.setMinimumSize(new java.awt.Dimension JavaDoc(24, 50));
573         logTextScrollPane.setPreferredSize(new java.awt.Dimension JavaDoc(11, 50));
574
575         logTextPane = new JTextPane(getLogDoc());
576         //logTextPane.setDocument(getLogDoc());
577
logTextPane.setEditable(false);
578         logTextScrollPane.setViewportView(logTextPane);
579     }
580
581     private void buildUserListPanel() {
582         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
583         jScrollPane1.setPreferredSize(new java.awt.Dimension JavaDoc(70, 132));
584
585         userList = new javax.swing.JList JavaDoc(getUserListModel());
586         //userList.setModel(getUserListModel());
587
userList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
588         jScrollPane1.setViewportView(userList);
589     }
590
591     private void buildMenu() {
592         jMenuBar1 = new javax.swing.JMenuBar JavaDoc();
593         jMenu1 = new javax.swing.JMenu JavaDoc();
594         jMenu1.setText("Chat");
595
596         loginMenuItem = new JMenuItem("Login...");
597         loginMenuItem.addActionListener(new java.awt.event.ActionListener JavaDoc() {
598             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
599                 loginMenuItemActionPerformed(evt);
600             }
601         });
602
603         jMenu1.add(loginMenuItem);
604
605
606         JMenu optionsjMenu = new javax.swing.JMenu JavaDoc();
607         optionsjMenu.setText("Options");
608
609         updateUserListMenuItem = new JMenuItem("Update UserList");
610         updateUserListMenuItem.setEnabled(false);
611         updateUserListMenuItem.addActionListener(new java.awt.event.ActionListener JavaDoc() {
612             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
613                 java.awt.EventQueue.invokeLater(new Runnable JavaDoc() {
614                     public void run() {
615                          updateUserList();
616                     }
617                 });
618             }
619         });
620         optionsjMenu.add(updateUserListMenuItem);
621
622         changeRoomMenuItem = new JMenuItem("Change Room...");
623         changeRoomMenuItem.setEnabled(false);
624         changeRoomMenuItem.addActionListener(new java.awt.event.ActionListener JavaDoc() {
625             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
626                 java.awt.EventQueue.invokeLater(new Runnable JavaDoc() {
627                     public void run() {
628                         changeRoom();
629                     }
630                 });
631             }
632         });
633         optionsjMenu.add(changeRoomMenuItem);
634     
635         
636         logoutMenuItem = new JMenuItem("Logout");
637         logoutMenuItem.setEnabled(false);
638         logoutMenuItem.addActionListener(new java.awt.event.ActionListener JavaDoc() {
639             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
640                 logoutMenuItemActionPerformed(evt);
641             }
642         });
643         jMenu1.add(logoutMenuItem);
644
645         exitMenuItem = new JMenuItem("Exit");
646         exitMenuItem.addActionListener(new java.awt.event.ActionListener JavaDoc() {
647             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
648                 System.exit(0);
649             }
650         });
651         jMenu1.add(exitMenuItem);
652
653         clearMenuItem = new JMenuItem("Clear Chat");
654         clearMenuItem.setEnabled(true);
655         clearMenuItem.addActionListener(new java.awt.event.ActionListener JavaDoc() {
656             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
657                 chatRoomTextPane1.setText("");
658                 logTextPane.setText("");
659             }
660         });
661         optionsjMenu.add(clearMenuItem);
662
663         jMenu2 = new javax.swing.JMenu JavaDoc();
664         jMenu2.setText("Help");
665
666         aboutMenuItem = new JMenuItem("About...");
667         aboutMenuItem.setEnabled(true);
668         aboutMenuItem.addActionListener(new java.awt.event.ActionListener JavaDoc() {
669             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
670                 java.awt.EventQueue.invokeLater(new Runnable JavaDoc() {
671                     public void run() {
672                         about();
673                     }
674                 });
675             }
676         });
677         jMenu2.add(aboutMenuItem);
678
679         jMenuBar1.add(jMenu1);
680         jMenuBar1.add(optionsjMenu);
681         jMenuBar1.add(jMenu2);
682
683         setJMenuBar(jMenuBar1);
684     }
685
686     private boolean checkForSmile(String JavaDoc message, AttributeSet style) throws BadLocationException {
687         if(message.length()==0) return false;
688         int loc = message.indexOf(":-)");
689
690         int start = 0;
691         String JavaDoc temp = null;
692         while(loc!=-1) {
693             if(loc!=start) {
694                 temp = message.substring(start, loc);
695                 if(checkForSad(temp, style)==false) {
696                     getChatDoc().insertString(getChatDoc().getLength(), temp, style);
697                 }
698             }
699             setEndSelection();
700             chatRoomTextPane1.insertIcon(smile);
701             loc = loc+3;
702             start = loc;
703             if(loc>=message.length()) break;
704             loc = message.indexOf(":-)", start);
705         }
706         if(start<message.length()) {
707             temp = message.substring(start, message.length());
708             if(checkForSad(temp, style)==false) {
709                 getChatDoc().insertString(getChatDoc().getLength(), temp, style);
710             }
711         }
712         return true;
713     }
714
715     private boolean checkForSad(String JavaDoc message, AttributeSet style) throws BadLocationException {
716         int loc = message.indexOf(":-(");
717         if(message.length()==0) return false;
718
719         int start = 0;
720         String JavaDoc temp = null;
721         while(loc!=-1) {
722             if(loc!=start) {
723                 temp = message.substring(start, loc);
724                 getChatDoc().insertString(getChatDoc().getLength(), temp, style);
725             }
726             setEndSelection();
727             chatRoomTextPane1.insertIcon(sad);
728             loc = loc+3;
729             start = loc;
730             if(loc>=message.length()) break;
731             loc = message.indexOf(":-(", start);
732         }
733         if(start<message.length()) {
734             temp = message.substring(start, message.length());
735             getChatDoc().insertString(getChatDoc().getLength(), temp, style);
736         }
737         return true;
738     }
739     
740     private void updateUserList() {
741         java.awt.EventQueue.invokeLater(new Runnable JavaDoc() {
742             public void run() {
743                 chatRoom.updateUserList();
744                 userListModel.clear();
745             }
746         });
747     }
748 }
749
750 class UserListListener implements ListSelectionListener JavaDoc {
751     private String JavaDoc userSelected = null;
752     private JList list;
753     
754     public UserListListener(JList list) {
755         this.list = list;
756     }
757     public String JavaDoc getSelecteduser() {
758         return userSelected;
759     }
760     public void valueChanged(ListSelectionEvent JavaDoc e) {
761         if(e.getValueIsAdjusting() == false) {
762             int index = list.getSelectedIndex();
763             if(index==-1)
764                 userSelected = null;
765             else
766                 userSelected = list.getSelectedValue().toString();
767         }
768     }
769 }
770
Popular Tags