]> code.citadel.org Git - citadel.git/blob - webcit/static/MultiUserChat102.java
fe620bad4d33a429991cbe650c80337eb5a2377b
[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, String n) {
31                 TheArea = t;
32                 serv = s;
33                 MyName = n;
34                 ParentMUC = muc;
35                 serv.AddClientThread(this);
36
37                 TheArea.setLayout(new GridLayout(25,1));
38
39                 for (a=0; a<25; ++a) {
40                         Linez[a] = new Label(" ");
41                         Linez[a].setBackground(Color.black);
42                         Linez[a].setForeground(Color.black);
43                         TheArea.add(Linez[a]);
44                         }
45
46                 TheArea.validate();
47
48                 }
49
50         private void ScrollIt(String TheNewLine, Color NewLineColor) {
51                 for (a=0; a<24; ++a) {
52                         Linez[a].setText(Linez[a+1].getText());
53                         Linez[a].setForeground(Linez[a+1].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                                 }
80                         else {
81                                 cUser = ":";
82                                 }
83                         if (ST.hasMoreTokens()) {
84                                 cText = ST.nextToken();
85                                 }
86                         else {
87                                 cText = " ";
88                                 }
89                         if (!cText.startsWith("NOOP")) {
90                                 if (!LastLineUser.equals(cUser)) {
91                                         ScrollIt("", Color.black);
92                                         ThisLine = cUser + ": ";
93                                         }
94                                 else {
95 ThisLine = "                                  ".substring(0, 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
118         extends Frame {
119
120 wcCitServer serv;
121 ReceiveChat MyReceive;
122 Panel AllUsers;
123 TextField SendBox;
124 wcchat ParentApplet;
125
126
127 MultiUserChat102(wcCitServer PrimaryServ, wcchat P) {
128         super ("Multiuser Chat");
129
130         String boof;
131
132         /* Set up a new server connection as a piggyback to the first. */
133         serv = PrimaryServ;
134         ParentApplet = P;
135
136         resize(600,400);
137         setLayout(new BorderLayout());
138
139         boof = "This is the buffer before the chat command.";
140         serv.ServPuts("CHAT");
141         boof = serv.ServGets();
142
143         if (boof.charAt(0) != '8') {
144                 add("Center", new Label("ERROR: " + boof) );
145                 show();
146                 }
147
148         else {
149                 DoChat(PrimaryServ.GetUserName());
150                 }
151
152         }
153
154
155 /*
156  * Do the actual chat stuff
157  */
158 private void DoChat(String MyName) {
159         String boof;
160
161         SendBox = new TextField(80);
162
163         AllUsers = new Panel();
164
165         add("Center", AllUsers);
166         add("South", SendBox);
167         show();
168
169         MyReceive = new ReceiveChat(this, AllUsers, serv, MyName);
170         MyReceive.start();
171
172         SendBox.requestFocus();
173         }
174
175
176 public boolean handleEvent(Event evt) {
177         int LastSpace;
178
179         if ( (evt.target == SendBox) && (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() + serv.GetUserName().length() > 78 ) {
186                         LastSpace = SendBox.getText().lastIndexOf(' ');
187                         if (LastSpace < 0) {
188                                 serv.ServPuts(SendBox.getText());
189                                 SendBox.setText("");
190                                 }
191                         else {
192                                 serv.ServPuts(SendBox.getText().substring(0,LastSpace));
193                                 SendBox.setText(SendBox.getText().substring(LastSpace));
194                                 if (SendBox.getText().charAt(0) == ' ') {
195                                         SendBox.setText(SendBox.getText().substring(1));
196                                         }
197                                 }
198                         }
199                 }
200
201                 return super.handleEvent(evt);
202         }
203
204
205 }