001: /*- ChatPane.java -------------------------------------------------+
002: | |
003: | Copyright (C) 2002-2003 Joseph Monti, LlamaChat |
004: | countjoe@users.sourceforge.net |
005: | http://www.42llamas.com/LlamaChat/ |
006: | |
007: | This program is free software; you can redistribute it and/or |
008: | modify it under the terms of the GNU General Public License |
009: | as published by the Free Software Foundation; either version 2 |
010: | of the License, or (at your option) any later version |
011: | |
012: | This program is distributed in the hope that it will be useful, |
013: | but WITHOUT ANY WARRANTY; without even the implied warranty of |
014: | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
015: | GNU General Public License for more details. |
016: | |
017: | A copy of the GNU General Public License may be found in the |
018: | installation directory named "GNUGPL.txt" |
019: | |
020: +-----------------------------------------------------------------+
021: */
022:
023: package client;
024:
025: import javax.swing.*;
026: import java.awt.*;
027: import java.awt.event.*;
028: import java.net.*;
029: import java.util.Hashtable;
030: import java.util.Enumeration;
031:
032: /* -------------------- JavaDoc Information ----------------------*/
033: /**
034: * This handles private messages
035: * @author Joseph Monti <a href="mailto:countjoe@users.sourceforge.net">countjoe@users.sourceforge.net</a>
036: * @version 0.8
037: */
038: public class PrivateMsg {
039: LlamaChat llamaChat;
040: private Hashtable privates;
041:
042: PrivateMsg(LlamaChat lc) {
043: llamaChat = lc;
044: privates = new Hashtable();
045: }
046:
047: public void recievePrivate(String username, String message) {
048: MsgWindow msg = newPrivate(username);
049: if (msg != null) {
050: msg.cp.sendText(username, message);
051: }
052: }
053:
054: public MsgWindow newPrivate(String username) {
055: if (!llamaChat.users.contains(username)) {
056: llamaChat.error(username + " does not exist");
057: return null;
058: }
059: if (llamaChat.ignores.contains(username)) {
060: llamaChat.error(username
061: + " is ignored, cannot private message");
062: return null;
063: }
064: MsgWindow msg = null;
065: if (privates.containsKey(username)) {
066: msg = (MsgWindow) privates.get(username);
067: } else {
068: msg = new MsgWindow(username);
069: privates.put(username, msg);
070: }
071: return msg;
072: }
073:
074: public void serverMessage(String username, String message) {
075: if (privates.containsKey(username)) {
076: MsgWindow msg = (MsgWindow) privates.get(username);
077: msg.cp.sendText("server", message);
078: }
079: }
080:
081: private class MsgWindow extends JFrame {
082: ChatPane cp;
083: JTextField messageText;
084: String username;
085:
086: MsgWindow(String un) {
087: super ("Private Message: " + un);
088: username = un;
089: setSize(new Dimension(350, 200));
090: setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
091: addWindowListener(new WindowAdapter() {
092: public void windowClosed(WindowEvent e) {
093: MsgWindow cur = (MsgWindow) e.getWindow();
094: Enumeration enum = privates.keys();
095: while (enum.hasMoreElements()) {
096: String user = (String) enum.nextElement();
097: MsgWindow tmpFra = (MsgWindow) privates.get(user);
098: if (cur.equals(tmpFra)) {
099: privates.remove(user);
100: break;
101: }
102: }
103: }
104: });
105: cp = new ChatPane(llamaChat);
106: messageText = new JTextField();
107:
108: messageText.addKeyListener(new KeyListener() {
109: public void keyTyped(KeyEvent ke) {
110: if (ke.getKeyChar() == KeyEvent.VK_ENTER) {
111: String txt = new String(messageText.getText());
112: if (!txt.equals("")) {
113: if (!llamaChat.users.contains(username)) {
114: cp.error(username + " does not exist");
115: } else {
116: llamaChat.sendPrivate(username, txt);
117: cp.sendText(llamaChat.username, txt);
118: }
119: messageText.setText("");
120: }
121: }
122: }
123: public void keyPressed(KeyEvent ke) {
124: //
125: }
126: public void keyReleased(KeyEvent ke) {
127: //
128: }
129: });
130: getContentPane().add(new JScrollPane(cp,
131: JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
132: JScrollPane.HORIZONTAL_SCROLLBAR_NEVER), BorderLayout.CENTER);
133: getContentPane().add(messageText, BorderLayout.SOUTH);
134: setVisible(true);
135: cp.sendText("server", "Private message session started with " + un);
136: }
137: }
138: }
|