001: /*
002: * This file is part of the QuickServer library
003: * Copyright (C) 2003-2005 QuickServer.org
004: *
005: * Use, modification, copying and distribution of this software is subject to
006: * the terms and conditions of the GNU Lesser General Public License.
007: * You should have received a copy of the GNU LGP License along with this
008: * library; if not, you can download a copy from <http://www.quickserver.org/>.
009: *
010: * For questions, suggestions, bug-reports, enhancement-requests etc.
011: * visit http://www.quickserver.org
012: *
013: */
014:
015: package chatserver.client;
016:
017: import javax.swing.text.*;
018: import java.awt.Color;
019: import java.awt.Point;
020: import java.util.HashMap;
021: import java.util.Map;
022: import java.util.logging.*;
023: import javax.swing.*;
024: import javax.swing.event.ListSelectionEvent;
025: import javax.swing.event.ListSelectionListener;
026: import java.awt.Graphics;
027: import java.awt.event.*;
028:
029: /**
030: *
031: * @author Akshathkumar Shetty
032: */
033: public class ChatWindow extends javax.swing.JFrame {
034: private static Logger logger = Logger.getLogger(ChatWindow.class
035: .getName());
036:
037: private ClassLoader classLoader = getClass().getClassLoader();
038: public final ImageIcon logo = new ImageIcon(classLoader
039: .getResource("chatserver/client/face-smile.gif"));
040: public final ImageIcon smile = new ImageIcon(classLoader
041: .getResource("chatserver/client/smile.gif"));
042: public final ImageIcon sad = new ImageIcon(classLoader
043: .getResource("chatserver/client/sad.gif"));
044:
045: private ChatRoom chatRoom;
046: private LoginDialog loginDialog;
047: private DefaultStyledDocument logDoc = null;
048: private DefaultStyledDocument chatDoc = null;
049: private DefaultListModel userListModel = null;
050: private UserListListener userListListener = null;
051: private Map styleMap = new HashMap();
052:
053: final String NORMALBLUE = "NormalBlue";
054: final String BOLDBLUE = "BoldBlue";
055: final String NORMALBLACK = "NormalBlack";
056: final String ITALICBLACK = "ITALICBLACK";
057: final String BOLDGREEN = "BoldGreen";
058: final String NORMALRED = "NormalRed";
059: final String ITALICRED = "ItalicRed";
060:
061: private InfiniteProgressPanel glassPane;
062:
063: /** Creates new form ChatWindow */
064: public ChatWindow(ChatRoom chatRoom, String args[]) {
065: this .chatRoom = chatRoom;
066: setLogDoc(new DefaultStyledDocument());
067: setChatDoc(new DefaultStyledDocument());
068: setUserListModel(new DefaultListModel());
069: prepareAllStyles();
070: initComponents();
071: userListListener = new UserListListener(userList);
072: userList.addListSelectionListener(userListListener);
073: loginDialog = new LoginDialog(this , args);
074: }
075:
076: private void initComponents() {
077: getContentPane().setLayout(new java.awt.BorderLayout(2, 2));
078: setTitle("QuickChat - Please Login");
079:
080: buildChatPanel();
081: buildSendMsg();
082: buildLogPanel();
083: buildUserListPanel();
084:
085: //---
086: jPanel1 = new javax.swing.JPanel();
087: jPanel1.setLayout(new java.awt.BorderLayout());
088: jPanel1.add(jPanel2, java.awt.BorderLayout.SOUTH); //sendmsg
089:
090: JSplitPane splitPane = new JSplitPane(
091: JSplitPane.HORIZONTAL_SPLIT, chatRoomScrollPane,
092: jScrollPane1);
093: splitPane.setOneTouchExpandable(true);
094: java.awt.Dimension minimumSize = new java.awt.Dimension(500, 20);
095: chatRoomScrollPane.setMinimumSize(minimumSize);
096: splitPane.setDividerLocation(500);
097: splitPane.setResizeWeight(0.9);
098:
099: jPanel1.add(splitPane, java.awt.BorderLayout.CENTER); //chat
100:
101: jTabbedPane1 = new javax.swing.JTabbedPane();
102: jTabbedPane1.setTabPlacement(javax.swing.JTabbedPane.BOTTOM);
103: jTabbedPane1.addTab("Chat Room", jPanel1);
104: jTabbedPane1.addTab("Logs", logTextScrollPane);
105:
106: getContentPane()
107: .add(jTabbedPane1, java.awt.BorderLayout.CENTER);
108:
109: buildMenu();
110: setIconImage(logo.getImage());
111:
112: addWindowListener(new WindowAdapter() {
113: public void windowClosing(WindowEvent e) {
114: glassPane.interrupt();
115: System.exit(0);
116: }
117:
118: public void windowOpened(WindowEvent e) {
119: }
120: });
121: pack();
122: setLocationRelativeTo(null);
123:
124: glassPane = new InfiniteProgressPanel("Logging to server..");
125: setGlassPane(glassPane);
126: }
127:
128: private void loginMenuItemActionPerformed(
129: final java.awt.event.ActionEvent evt) {
130: Thread t = new Thread() {
131: public void run() {
132: while (true) {
133: if (login() == false)
134: break;
135: logger.info("Calling login()");
136: }
137: }
138: };
139: t.setPriority(Thread.NORM_PRIORITY);
140: t.start();
141: }
142:
143: private boolean login() {
144: loginDialog.clearStatus();
145: loginDialog.show();
146: if (loginDialog.isOk() == true) {
147: String r[] = loginDialog.getValues();
148: glassPane.start();
149: try {
150: boolean flag = chatRoom.doLogin(r[0], Integer
151: .parseInt(r[1]), r[2], r[3]);
152:
153: if (flag == true) {
154: enableChat(true);
155: } else {
156: enableChat(false);
157: return true;//recall the login dialog
158: }
159: } catch (Exception ex) {
160: enableChat(false);
161: logger.warning("Error logging in : " + ex);
162: return true;
163: }
164: }
165: return false;
166: }
167:
168: private void logoutMenuItemActionPerformed(
169: java.awt.event.ActionEvent evt) {
170: Thread t = new Thread() {
171: public void run() {
172: try {
173: chatRoom.doLogout();
174: } catch (Exception ex) {
175: loginMenuItem.setEnabled(true);
176: logoutMenuItem.setEnabled(false);
177: logger.warning("Error logging in : " + ex);
178: }
179: }
180: };
181: t.setPriority(Thread.NORM_PRIORITY);
182: t.start();
183: }
184:
185: private void sendTextActionPerformed(java.awt.event.ActionEvent evt) {
186: sendButtonActionPerformed(evt);
187: }
188:
189: private void sendPrivateButtonActionPerformed(
190: java.awt.event.ActionEvent evt) {
191: if (userListListener.getSelecteduser() == null) {
192: setResponse("-ERR No User is selected!");
193: return;
194: }
195: if (sendText.getText().length() == 0) {
196: setResponse("-ERR No message to send!");
197: return;
198: }
199: chatRoom.sendPrivateMessage(userListListener.getSelecteduser()
200: + "@" + chatRoom.getRoom(), sendText.getText());
201: sendText.setText("");
202: }
203:
204: private void sendButtonActionPerformed(
205: java.awt.event.ActionEvent evt) {
206: if (sendText.getText().length() == 0) {
207: setResponse("-ERR No message to send!");
208: return;
209: }
210: chatRoom.sendMessage(sendText.getText());
211: sendText.setText("");
212: }
213:
214: // Variables declaration - do not modify
215: private javax.swing.JScrollPane chatRoomScrollPane;
216: private javax.swing.JTextPane chatRoomTextPane1;
217: private javax.swing.JMenu jMenu1, jMenu2;
218: private javax.swing.JMenuBar jMenuBar1;
219: private javax.swing.JPanel jPanel1;
220: private javax.swing.JPanel jPanel2;
221: private javax.swing.JScrollPane jScrollPane1;
222: private javax.swing.JTabbedPane jTabbedPane1;
223: private javax.swing.JTextPane logTextPane;
224: private javax.swing.JScrollPane logTextScrollPane;
225: private javax.swing.JMenuItem loginMenuItem;
226: private javax.swing.JMenuItem logoutMenuItem;
227: private javax.swing.JMenuItem exitMenuItem;
228: private javax.swing.JMenuItem changeRoomMenuItem;
229: private javax.swing.JMenuItem updateUserListMenuItem;
230: private javax.swing.JMenuItem clearMenuItem;
231: private javax.swing.JMenuItem aboutMenuItem;
232: private javax.swing.JButton sendButton;
233: private javax.swing.JButton sendPrivateButton;
234: private javax.swing.JTextField sendText;
235: private javax.swing.JList userList;
236:
237: // End of variables declaration
238:
239: public void log(String msg) {
240: logger.fine("Got: " + msg);
241: try {
242: AttributeSet style = (AttributeSet) styleMap
243: .get(NORMALBLACK);
244: getLogDoc().insertString(getLogDoc().getLength(),
245: msg + "\n", style);
246:
247: //Point pt1=logTextPane.getLocation();
248: Point pt2 = new Point((int) (0), (int) (logTextPane
249: .getBounds().getHeight()));
250: logTextScrollPane.getViewport().setViewPosition(pt2);
251: } catch (Exception e) {
252: logger.warning("Error: " + e);
253: }
254: }
255:
256: public DefaultStyledDocument getLogDoc() {
257: return logDoc;
258: }
259:
260: public void setLogDoc(DefaultStyledDocument logDoc) {
261: this .logDoc = logDoc;
262: }
263:
264: public void prepareAllStyles() {
265: SimpleAttributeSet aset = new SimpleAttributeSet();
266: StyleConstants.setForeground(aset, Color.blue);
267: StyleConstants.setFontSize(aset, 12);
268: StyleConstants.setFontFamily(aset, "Verdana");
269: styleMap.put(NORMALBLUE, aset);
270:
271: aset = new SimpleAttributeSet();
272: StyleConstants.setForeground(aset, Color.blue);
273: StyleConstants.setFontSize(aset, 12);
274: StyleConstants.setFontFamily(aset, "Verdana");
275: StyleConstants.setBold(aset, true);
276: styleMap.put(BOLDBLUE, aset);
277:
278: aset = new SimpleAttributeSet();
279: StyleConstants.setForeground(aset, Color.black);
280: StyleConstants.setFontSize(aset, 12);
281: StyleConstants.setFontFamily(aset, "Verdana");
282: styleMap.put(NORMALBLACK, aset);
283:
284: aset = new SimpleAttributeSet();
285: StyleConstants.setForeground(aset, Color.black);
286: StyleConstants.setFontSize(aset, 12);
287: StyleConstants.setFontFamily(aset, "Verdana");
288: StyleConstants.setItalic(aset, true);
289: styleMap.put(ITALICBLACK, aset);
290:
291: aset = new SimpleAttributeSet();
292: StyleConstants.setForeground(aset, new Color(0, 128, 0));
293: StyleConstants.setFontSize(aset, 12);
294: StyleConstants.setFontFamily(aset, "Verdana");
295: StyleConstants.setBold(aset, true);
296: styleMap.put(BOLDGREEN, aset);
297:
298: aset = new SimpleAttributeSet();
299: StyleConstants.setForeground(aset, Color.red);
300: StyleConstants.setFontSize(aset, 12);
301: StyleConstants.setFontFamily(aset, "Verdana");
302: styleMap.put(NORMALRED, aset);
303:
304: aset = new SimpleAttributeSet();
305: StyleConstants.setForeground(aset, Color.red);
306: StyleConstants.setFontSize(aset, 12);
307: StyleConstants.setFontFamily(aset, "Verdana");
308: StyleConstants.setItalic(aset, true);
309: styleMap.put(ITALICRED, aset);
310: }
311:
312: public void addChatMessage(String message) {
313: logger.fine("Got: " + message);
314: if (message.startsWith("{system.help} ")) {
315: return;
316: } else if (message.startsWith("{system.debug} ")) {
317: //already logged
318: return;
319: }
320:
321: AttributeSet style = null;
322: String fromid = null;
323: String toid = null;
324:
325: String msgType = null;
326: try {
327: if (message.startsWith("{system.msg} ")) {
328: msgType = "{system.msg}";
329: message = message.substring(13);
330: style = (AttributeSet) styleMap.get(BOLDBLUE);
331: } else if (message.startsWith("{system.error} ")) {
332: msgType = "{system.error}";
333: message = "Error: " + message.substring(15);
334: style = (AttributeSet) styleMap.get(NORMALRED);
335: } else if (message.startsWith("{user.msg:")) {
336: msgType = "{user.msg}";
337: int j = message.indexOf(":", 10);//end of from
338: int i = message.indexOf("} ", 10);
339: if (j != -1) {
340: toid = message.substring(j + 1, i);
341: } else {
342: j = i;
343: }
344: fromid = message.substring(10, j);
345: message = message.substring(i + 2);
346: style = (AttributeSet) styleMap.get(NORMALBLUE);
347: } else if (message.startsWith("{msg.user:")) { //gui command
348: msgType = "{msg.user}";
349: int i = message.indexOf("} ", 10);
350: toid = message.substring(10, i);
351: message = message.substring(i + 2);
352: style = (AttributeSet) styleMap.get(NORMALBLUE);
353: } else if (message.startsWith("{user.info:")) {
354: msgType = "{user.info}";
355: int i = message.indexOf("} ", 11);
356: fromid = message.substring(11, i);
357: message = message.substring(i + 2);
358: if (message.equals("LoggedIn")) {
359: addToUserList(fromid);
360: message = "joined the room";
361: } else if (message.equals("LoggedOut")) {
362: removeFromUserList(fromid);
363: message = "left the room";
364: } else
365: System.out.println("Unknown ->" + message + "<-");
366: style = (AttributeSet) styleMap.get(ITALICBLACK);
367: } else {
368: msgType = "{unknown}";
369: style = (AttributeSet) styleMap.get(NORMALBLACK);
370: }
371:
372: if (msgType.equals("{user.msg}")) {
373: toid = removeRoom(toid);
374: fromid = removeRoom(fromid);
375: if (toid != null && toid.length() == 0) {//to group
376: getChatDoc().insertString(getChatDoc().getLength(),
377: fromid + ": ",
378: (AttributeSet) styleMap.get(NORMALRED));
379: } else if (toid != null) {
380: getChatDoc().insertString(getChatDoc().getLength(),
381: "PrvMsg From " + fromid + ": ",
382: (AttributeSet) styleMap.get(BOLDBLUE));
383: }
384: } else if (msgType.equals("{msg.user}")) {
385: toid = removeRoom(toid);
386: getChatDoc().insertString(getChatDoc().getLength(),
387: "PrvMsg To " + toid + ": ",
388: (AttributeSet) styleMap.get(BOLDBLUE));
389: } else if (msgType.equals("{user.info}")) {
390: fromid = removeRoom(fromid);
391: getChatDoc().insertString(getChatDoc().getLength(),
392: fromid + ": ",
393: (AttributeSet) styleMap.get(NORMALRED));
394: }
395:
396: if (message.indexOf(":-)") == -1
397: && message.indexOf(":-(") == -1) {
398: getChatDoc().insertString(getChatDoc().getLength(),
399: message + "\n", style);
400: } else {
401: checkForSmile(message, style);
402: getChatDoc().insertString(getChatDoc().getLength(),
403: "\n", style);
404: }
405:
406: Point pt1 = chatRoomTextPane1.getLocation();
407: Point pt2 = new Point((int) (0), (int) (chatRoomTextPane1
408: .getBounds().getHeight()));
409: chatRoomScrollPane.getViewport().setViewPosition(pt2);
410: } catch (Exception e) {
411: logger.warning("Error: " + e);
412: }
413:
414: toFront();
415: }
416:
417: protected void setEndSelection() {
418: int len = chatRoomTextPane1.getDocument().getLength();
419: chatRoomTextPane1.setSelectionStart(len);
420: chatRoomTextPane1.setSelectionEnd(len);
421: }
422:
423: public DefaultStyledDocument getChatDoc() {
424: return chatDoc;
425: }
426:
427: public void setChatDoc(DefaultStyledDocument chatDoc) {
428: this .chatDoc = chatDoc;
429: }
430:
431: public void setResponse(String res) {
432: int msgType = JOptionPane.PLAIN_MESSAGE;
433: if (res.startsWith("+OK"))
434: msgType = JOptionPane.INFORMATION_MESSAGE;
435: if (res.startsWith("-ERR"))
436: msgType = JOptionPane.ERROR_MESSAGE;
437: toFront();
438: JOptionPane.showMessageDialog(this , res.substring(res
439: .indexOf(" ") + 1), "Response", msgType);
440: }
441:
442: public void enableChat(boolean flag) {
443: sendText.setEnabled(flag);
444: sendButton.setEnabled(flag);
445: sendPrivateButton.setEnabled(flag);
446: loginMenuItem.setEnabled(!flag);
447: logoutMenuItem.setEnabled(flag);
448: changeRoomMenuItem.setEnabled(flag);
449: updateUserListMenuItem.setEnabled(flag);
450: if (flag == false) {
451: userListModel.clear();
452: setTitle("QuickChat - Not Connected");
453: } else {
454: chatRoomTextPane1.setText("");
455: chatRoom.processReceivedMsg();
456: }
457: glassPane.stop();
458: glassPane.setVisible(false);
459: }
460:
461: public void addToUserList(String id) {
462: logger.fine("Got: " + id);
463: id = removeRoom(id);
464: getUserListModel().addElement(id);
465: }
466:
467: public void removeFromUserList(String id) {
468: logger.fine("Got: " + id);
469: id = removeRoom(id);
470: getUserListModel().removeElement(id);
471: }
472:
473: public DefaultListModel getUserListModel() {
474: return userListModel;
475: }
476:
477: public void setUserListModel(DefaultListModel userListModel) {
478: this .userListModel = userListModel;
479: }
480:
481: private String removeRoom(String id) {
482: if (id == null)
483: return id;
484: if (id.endsWith("@" + chatRoom.getRoom())) {
485: id = id.substring(0, id.indexOf("@" + chatRoom.getRoom()));
486: }
487: return id;
488: }
489:
490: private void about() {
491: JOptionPane
492: .showMessageDialog(
493: this ,
494: "QuickChat v 1.0\n\n"
495: + "GUI Client for ChatServer example of QuickServer.\n"
496: + "This is compliant with QuickServer v1.4.5 release.\n\n"
497: + "Copyright (C) 2005 QuickServer.org\n"
498: + "http://www.quickserver.org",
499: "About QuickChat",
500: JOptionPane.INFORMATION_MESSAGE, logo);
501: }
502:
503: private void changeRoom() {
504: String newRoom = (String) JOptionPane.showInputDialog(this ,
505: "Chat Room:", "Change Room", JOptionPane.PLAIN_MESSAGE,
506: logo, null, chatRoom.getRoom());
507: if (newRoom == null)
508: return;
509: chatRoom.changeRoom(newRoom);
510: userListModel.clear();
511: chatRoom.updateUserList();
512: }
513:
514: //-- build gui
515: private void buildChatPanel() {
516: chatRoomTextPane1 = new JTextPane(getChatDoc());
517: //chatRoomTextPane1.setDocument(getChatDoc());
518: chatRoomTextPane1.setEditable(false);
519: chatRoomTextPane1.setMinimumSize(new java.awt.Dimension(500,
520: 200));
521: chatRoomTextPane1.setPreferredSize(new java.awt.Dimension(500,
522: 300));
523:
524: chatRoomScrollPane = new JScrollPane(chatRoomTextPane1);
525: chatRoomScrollPane
526: .setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
527: }
528:
529: private void buildSendMsg() {
530: jPanel2 = new javax.swing.JPanel();
531: jPanel2.setLayout(new java.awt.GridBagLayout());
532: jPanel2.setMinimumSize(new java.awt.Dimension(373, 40));
533: jPanel2.setPreferredSize(new java.awt.Dimension(373, 50));
534:
535: sendText = new javax.swing.JTextField();
536: sendText.setEnabled(false);
537: sendText.addActionListener(new java.awt.event.ActionListener() {
538: public void actionPerformed(
539: final java.awt.event.ActionEvent evt) {
540: java.awt.EventQueue.invokeLater(new Runnable() {
541: public void run() {
542: sendTextActionPerformed(evt);
543: }
544: });
545: }
546: });
547:
548: java.awt.GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints();
549: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
550: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
551: gridBagConstraints.weightx = 1.0;
552: gridBagConstraints.insets = new java.awt.Insets(0, 2, 0, 2);
553: jPanel2.add(sendText, gridBagConstraints);
554:
555: sendButton = new javax.swing.JButton();
556: sendButton.setText("Send");
557: sendButton.setEnabled(false);
558: sendButton
559: .addActionListener(new java.awt.event.ActionListener() {
560: public void actionPerformed(
561: final java.awt.event.ActionEvent evt) {
562: java.awt.EventQueue.invokeLater(new Runnable() {
563: public void run() {
564: sendButtonActionPerformed(evt);
565: }
566: });
567: }
568: });
569:
570: gridBagConstraints = new java.awt.GridBagConstraints();
571: gridBagConstraints.insets = new java.awt.Insets(0, 2, 0, 2);
572: jPanel2.add(sendButton, gridBagConstraints);
573:
574: sendPrivateButton = new javax.swing.JButton();
575: sendPrivateButton.setText("Private Mesage");
576: sendPrivateButton.setEnabled(false);
577: sendPrivateButton
578: .addActionListener(new java.awt.event.ActionListener() {
579: public void actionPerformed(
580: final java.awt.event.ActionEvent evt) {
581: java.awt.EventQueue.invokeLater(new Runnable() {
582: public void run() {
583: sendPrivateButtonActionPerformed(evt);
584: }
585: });
586: }
587: });
588:
589: gridBagConstraints = new java.awt.GridBagConstraints();
590: gridBagConstraints.insets = new java.awt.Insets(0, 2, 0, 2);
591: jPanel2.add(sendPrivateButton, gridBagConstraints);
592: }
593:
594: private void buildLogPanel() {
595: logTextScrollPane = new javax.swing.JScrollPane();
596: logTextScrollPane
597: .setMinimumSize(new java.awt.Dimension(24, 50));
598: logTextScrollPane.setPreferredSize(new java.awt.Dimension(11,
599: 50));
600:
601: logTextPane = new JTextPane(getLogDoc());
602: //logTextPane.setDocument(getLogDoc());
603: logTextPane.setEditable(false);
604: logTextScrollPane.setViewportView(logTextPane);
605: }
606:
607: private void buildUserListPanel() {
608: jScrollPane1 = new javax.swing.JScrollPane();
609: jScrollPane1.setPreferredSize(new java.awt.Dimension(70, 132));
610:
611: userList = new javax.swing.JList(getUserListModel());
612: //userList.setModel(getUserListModel());
613: userList
614: .setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
615: jScrollPane1.setViewportView(userList);
616: }
617:
618: private void buildMenu() {
619: jMenuBar1 = new javax.swing.JMenuBar();
620: jMenu1 = new javax.swing.JMenu();
621: jMenu1.setText("Chat");
622:
623: loginMenuItem = new JMenuItem("Login...");
624: loginMenuItem
625: .addActionListener(new java.awt.event.ActionListener() {
626: public void actionPerformed(
627: java.awt.event.ActionEvent evt) {
628: loginMenuItemActionPerformed(evt);
629: }
630: });
631:
632: jMenu1.add(loginMenuItem);
633:
634: JMenu optionsjMenu = new javax.swing.JMenu();
635: optionsjMenu.setText("Options");
636:
637: updateUserListMenuItem = new JMenuItem("Update UserList");
638: updateUserListMenuItem.setEnabled(false);
639: updateUserListMenuItem
640: .addActionListener(new java.awt.event.ActionListener() {
641: public void actionPerformed(
642: java.awt.event.ActionEvent evt) {
643: java.awt.EventQueue.invokeLater(new Runnable() {
644: public void run() {
645: updateUserList();
646: }
647: });
648: }
649: });
650: optionsjMenu.add(updateUserListMenuItem);
651:
652: changeRoomMenuItem = new JMenuItem("Change Room...");
653: changeRoomMenuItem.setEnabled(false);
654: changeRoomMenuItem
655: .addActionListener(new java.awt.event.ActionListener() {
656: public void actionPerformed(
657: java.awt.event.ActionEvent evt) {
658: java.awt.EventQueue.invokeLater(new Runnable() {
659: public void run() {
660: changeRoom();
661: }
662: });
663: }
664: });
665: optionsjMenu.add(changeRoomMenuItem);
666:
667: logoutMenuItem = new JMenuItem("Logout");
668: logoutMenuItem.setEnabled(false);
669: logoutMenuItem
670: .addActionListener(new java.awt.event.ActionListener() {
671: public void actionPerformed(
672: java.awt.event.ActionEvent evt) {
673: logoutMenuItemActionPerformed(evt);
674: }
675: });
676: jMenu1.add(logoutMenuItem);
677:
678: exitMenuItem = new JMenuItem("Exit");
679: exitMenuItem
680: .addActionListener(new java.awt.event.ActionListener() {
681: public void actionPerformed(
682: java.awt.event.ActionEvent evt) {
683: System.exit(0);
684: }
685: });
686: jMenu1.add(exitMenuItem);
687:
688: clearMenuItem = new JMenuItem("Clear Chat");
689: clearMenuItem.setEnabled(true);
690: clearMenuItem
691: .addActionListener(new java.awt.event.ActionListener() {
692: public void actionPerformed(
693: java.awt.event.ActionEvent evt) {
694: chatRoomTextPane1.setText("");
695: logTextPane.setText("");
696: }
697: });
698: optionsjMenu.add(clearMenuItem);
699:
700: jMenu2 = new javax.swing.JMenu();
701: jMenu2.setText("Help");
702:
703: aboutMenuItem = new JMenuItem("About...");
704: aboutMenuItem.setEnabled(true);
705: aboutMenuItem
706: .addActionListener(new java.awt.event.ActionListener() {
707: public void actionPerformed(
708: java.awt.event.ActionEvent evt) {
709: java.awt.EventQueue.invokeLater(new Runnable() {
710: public void run() {
711: about();
712: }
713: });
714: }
715: });
716: jMenu2.add(aboutMenuItem);
717:
718: jMenuBar1.add(jMenu1);
719: jMenuBar1.add(optionsjMenu);
720: jMenuBar1.add(jMenu2);
721:
722: setJMenuBar(jMenuBar1);
723: }
724:
725: private boolean checkForSmile(String message, AttributeSet style)
726: throws BadLocationException {
727: if (message.length() == 0)
728: return false;
729: int loc = message.indexOf(":-)");
730:
731: int start = 0;
732: String temp = null;
733: while (loc != -1) {
734: if (loc != start) {
735: temp = message.substring(start, loc);
736: if (checkForSad(temp, style) == false) {
737: getChatDoc().insertString(getChatDoc().getLength(),
738: temp, style);
739: }
740: }
741: setEndSelection();
742: chatRoomTextPane1.insertIcon(smile);
743: loc = loc + 3;
744: start = loc;
745: if (loc >= message.length())
746: break;
747: loc = message.indexOf(":-)", start);
748: }
749: if (start < message.length()) {
750: temp = message.substring(start, message.length());
751: if (checkForSad(temp, style) == false) {
752: getChatDoc().insertString(getChatDoc().getLength(),
753: temp, style);
754: }
755: }
756: return true;
757: }
758:
759: private boolean checkForSad(String message, AttributeSet style)
760: throws BadLocationException {
761: int loc = message.indexOf(":-(");
762: if (message.length() == 0)
763: return false;
764:
765: int start = 0;
766: String temp = null;
767: while (loc != -1) {
768: if (loc != start) {
769: temp = message.substring(start, loc);
770: getChatDoc().insertString(getChatDoc().getLength(),
771: temp, style);
772: }
773: setEndSelection();
774: chatRoomTextPane1.insertIcon(sad);
775: loc = loc + 3;
776: start = loc;
777: if (loc >= message.length())
778: break;
779: loc = message.indexOf(":-(", start);
780: }
781: if (start < message.length()) {
782: temp = message.substring(start, message.length());
783: getChatDoc().insertString(getChatDoc().getLength(), temp,
784: style);
785: }
786: return true;
787: }
788:
789: private void updateUserList() {
790: java.awt.EventQueue.invokeLater(new Runnable() {
791: public void run() {
792: chatRoom.updateUserList();
793: userListModel.clear();
794: }
795: });
796: }
797: }
798:
799: class UserListListener implements ListSelectionListener {
800: private String userSelected = null;
801: private JList list;
802:
803: public UserListListener(JList list) {
804: this .list = list;
805: }
806:
807: public String getSelecteduser() {
808: return userSelected;
809: }
810:
811: public void valueChanged(ListSelectionEvent e) {
812: if (e.getValueIsAdjusting() == false) {
813: int index = list.getSelectedIndex();
814: if (index == -1)
815: userSelected = null;
816: else
817: userSelected = list.getSelectedValue().toString();
818: }
819: }
820: }
|