]> code.citadel.org Git - citadel.git/blob - webcit/static/MultiUserChat102.java
* Replaced the Java chat with a new system based on IFRAME's and JavaScript
[citadel.git] / webcit / static / MultiUserChat102.java
1 /*
2  * MultiUserChat102.java
3  *
4  * Chat mode...
5  *
6  */
7
8 import java.awt.*;
9 import java.util.*;
10
11 /*
12  * ReceiveChat implements a thread which listens on the server socket and
13  * display anything which comes across the wire in the Panel specified
14  * to the constructor.
15  */
16 class ReceiveChat extends Thread {
17         Panel TheArea;
18         wcCitServer serv;
19         String boof;
20         String cUser;
21         String cText;
22         String ThisLine;
23         String LastLineUser;
24         String MyName;
25         StringTokenizer ST;
26         MultiUserChat102 ParentMUC;
27          Label[] Linez = new Label[25];
28         int a;
29
30          ReceiveChat(MultiUserChat102 muc, Panel t, wcCitServer s,
31                      String n) {
32                 TheArea = t;
33                 serv = s;
34                 MyName = n;
35                 ParentMUC = muc;
36                 serv.AddClientThread(this);
37
38                 TheArea.setLayout(new GridLayout(25, 1));
39
40                 for (a = 0; a < 25; ++a) {
41                         Linez[a] = new Label(" ");
42                         Linez[a].setBackground(Color.black);
43                         Linez[a].setForeground(Color.black);
44                         TheArea.add(Linez[a]);
45                 } TheArea.validate();
46
47         }
48
49         private void ScrollIt(String TheNewLine, Color NewLineColor) {
50                 for (a = 0; a < 24; ++a) {
51                         Linez[a].setText(Linez[a + 1].getText());
52                         Linez[a].setForeground(Linez[a + 1].
53                                                getForeground());
54                 }
55                 Linez[24].setText(TheNewLine);
56                 Linez[24].setForeground(NewLineColor);
57         }
58
59
60         public void run() {
61                 Color UserColor;
62                 int a;
63
64                 LastLineUser = "  ";
65                 while (true) {
66                         boof = serv.ServGets();
67
68                         if (boof.equals("000")) {
69                                 serv.ServPuts("QUIT");
70                                 ParentMUC.dispose();
71                                 stop();
72                                 destroy();
73                         }
74
75
76                         ST = new StringTokenizer(boof, "|");
77                         if (ST.hasMoreTokens()) {
78                                 cUser = ST.nextToken();
79                         } else {
80                                 cUser = ":";
81                         }
82                         if (ST.hasMoreTokens()) {
83                                 cText = ST.nextToken();
84                         } else {
85                                 cText = " ";
86                         }
87                         if (!cText.startsWith("NOOP")) {
88                                 if (!LastLineUser.equals(cUser)) {
89                                         ScrollIt("", Color.black);
90                                         ThisLine = cUser + ": ";
91                                 } else {
92                                         ThisLine =
93                                             "                                  ".
94                                             substring(0,
95                                                       cUser.length() + 2);
96                                 }
97                                 ThisLine = ThisLine + cText;
98                                 UserColor = Color.green;
99                                 if (cUser.equals(":")) {
100                                         UserColor = Color.red;
101                                 }
102                                 if (cUser.equalsIgnoreCase(MyName)) {
103                                         UserColor = Color.yellow;
104                                 }
105                                 ScrollIt(ThisLine, UserColor);
106                                 LastLineUser = cUser;
107                         }
108                 }
109         }
110
111 }
112
113
114
115
116
117 public class MultiUserChat102 extends Frame {
118
119         wcCitServer serv;
120         ReceiveChat MyReceive;
121         Panel AllUsers;
122         TextField SendBox;
123         wcchat ParentApplet;
124
125
126          MultiUserChat102(wcCitServer PrimaryServ, wcchat P) {
127                 super("Multiuser Chat");
128
129                 String boof;
130
131                 /* Set up a new server connection as a piggyback to the first. */
132                  serv = PrimaryServ;
133                  ParentApplet = P;
134
135                  resize(600, 400);
136                  setLayout(new BorderLayout());
137
138                  boof = "This is the buffer before the chat command.";
139                  serv.ServPuts("CHAT");
140                  boof = serv.ServGets();
141
142                 if (boof.charAt(0) != '8') {
143                         add("Center", new Label("ERROR: " + boof));
144                         show();
145                 }
146
147                 else {
148                         DoChat(PrimaryServ.GetUserName());
149                 }
150
151         }
152
153
154 /*
155  * Do the actual chat stuff
156  */
157         private void DoChat(String MyName) {
158                 String boof;
159
160                 SendBox = new TextField(80);
161
162                 AllUsers = new Panel();
163
164                 add("Center", AllUsers);
165                 add("South", SendBox);
166                 show();
167
168                 MyReceive = new ReceiveChat(this, AllUsers, serv, MyName);
169                 MyReceive.start();
170
171                 SendBox.requestFocus();
172         }
173
174
175         public boolean handleEvent(Event evt) {
176                 int LastSpace;
177
178                 if ((evt.target == SendBox)
179                     && (evt.id == Event.ACTION_EVENT)) {
180                         serv.ServPuts(SendBox.getText());
181                         SendBox.setText("");
182                 }
183
184                 else if (evt.target == SendBox) {
185                         if (SendBox.getText().length() +
186                             serv.GetUserName().length() > 78) {
187                                 LastSpace =
188                                     SendBox.getText().lastIndexOf(' ');
189                                 if (LastSpace < 0) {
190                                         serv.ServPuts(SendBox.getText());
191                                         SendBox.setText("");
192                                 } else {
193                                         serv.ServPuts(SendBox.getText().
194                                                       substring(0,
195                                                                 LastSpace));
196                                         SendBox.setText(SendBox.getText().
197                                                         substring
198                                                         (LastSpace));
199                                         if (SendBox.getText().charAt(0) ==
200                                             ' ') {
201                                                 SendBox.setText(SendBox.
202                                                                 getText().
203                                                                 substring
204                                                                 (1));
205                                         }
206                                 }
207                         }
208                 }
209
210                 return super.handleEvent(evt);
211         }
212
213
214 }