KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quikj > application > web > talk > client > TalkApplet


1 /*
2  * TalkApplet.java
3  *
4  * Created on February 5, 2002, 2:51 AM
5  */

6
7 package com.quikj.application.web.talk.client;
8
9 import java.awt.*;
10 import java.net.*;
11 import java.awt.event.*;
12 import java.util.*;
13
14 import com.quikj.application.web.talk.messaging.*;
15 import com.quikj.client.beans.*;
16 import com.quikj.client.framework.*;
17
18 /**
19  *
20  * @author amit
21  */

22 public class TalkApplet extends java.applet.Applet JavaDoc
23 {
24
25     /** Initializes the applet TalkApplet */
26     public void init()
27     {
28         // initialize code base
29
codeBase = getCodeBase();
30
31         if (codeBase.getPort() >= 0) // a port is
32
// specified
33
{
34             port = codeBase.getPort();
35         }
36
37         String JavaDoc parm = getParameter("port");
38         if (parm != null)
39         {
40             try
41             {
42                 port = Integer.parseInt(parm);
43             }
44             catch (NumberFormatException JavaDoc ex)
45             {
46                 ;
47             }
48         }
49
50         parm = getParameter("plugin");
51         if (parm != null)
52         {
53             try
54             {
55                 applicationId = Integer.parseInt(parm);
56             }
57             catch (NumberFormatException JavaDoc ex)
58             {
59                 ;
60             }
61         }
62
63         parm = getParameter("client-type");
64         if (parm != null)
65         {
66             clientType = parm;
67         }
68
69         parm = getParameter("button");
70         if (parm != null)
71         {
72             buttonText = parm;
73         }
74
75         parm = getParameter("font");
76         if (parm != null)
77         {
78             fontInfo = parm;
79         }
80
81         parm = getParameter("color");
82         if (parm != null)
83         {
84             colorInfo = parm;
85         }
86
87         parm = getParameter("image");
88         if (parm != null)
89         {
90             imageURL = parm;
91         }
92
93         parm = getParameter("unregistered");
94         if (parm != null)
95         {
96             if (parm.equalsIgnoreCase("yes") == true)
97             {
98                 unregistered = true;
99             }
100             else
101             {
102                 unregistered = false;
103             }
104         }
105
106         parm = getParameter("called");
107         if (parm != null)
108         {
109             unregisteredCalled = parm;
110         }
111
112         element = null;
113         parm = getParameter("info");
114         if (parm != null)
115         {
116             if (parm.equalsIgnoreCase("yes") == true)
117             {
118                 infoRequired = true;
119             }
120             else if (parm.equalsIgnoreCase("no") == true)
121             {
122                 infoRequired = false;
123             }
124             else
125             {
126                 element = parseUserInfo(parm);
127             }
128         }
129
130         parm = getParameter("caption");
131         if (parm != null)
132         {
133             caption = parm;
134         }
135
136         parm = getParameter("logo");
137         if (parm != null)
138         {
139             URL url = URLUtils.formatURL(this, parm);
140             if (url != null)
141             {
142                 logo = getImage(url);
143             }
144         }
145
146         initComponents();
147
148         if (imageURL == null)
149         {
150             drawTextButton();
151         }
152         else
153         {
154             drawImageButton();
155         }
156
157         // start a timer because a delay is needed for registered users (modal
158
// dialog problem)
159
timer = new TalkApplet.TimerThread();
160         timer.start();
161
162     }
163
164     /**
165      * This method is called from within the init() method to initialize the
166      * form. WARNING: Do NOT modify this code. The content of this method is
167      * always regenerated by the Form Editor.
168      */

169     private void initComponents()//GEN-BEGIN:initComponents
170
{
171
172         setLayout(new java.awt.BorderLayout JavaDoc());
173
174     }//GEN-END:initComponents
175

176     private CallingNameElement parseUserInfo(String JavaDoc parm)
177     {
178         String JavaDoc full_name = null;
179         String JavaDoc email = null;
180         StringBuffer JavaDoc additional_info = new StringBuffer JavaDoc();
181
182         StringTokenizer tokens = new StringTokenizer(parm, "&");
183
184         while (tokens.hasMoreTokens() == true)
185         {
186             String JavaDoc token = tokens.nextToken();
187
188             // break up the token into name value pair
189
StringTokenizer nv_pairs = new StringTokenizer(new String JavaDoc(token),
190                     "=");
191             if (nv_pairs.countTokens() != 2)
192             {
193                 // ignore this token
194
continue;
195             }
196
197             String JavaDoc name = nv_pairs.nextToken();
198             String JavaDoc value = nv_pairs.nextToken();
199
200             if (name.equals("full-name") == true)
201             {
202                 full_name = decodeValue(value);
203             }
204             else if (name.equals("email") == true)
205             {
206                 email = decodeValue(value);
207             }
208             else
209             // other information
210
{
211                 additional_info
212                         .append(name + " : " + decodeValue(value) + "\n");
213             }
214         }
215
216         CallingNameElement element = new CallingNameElement();
217         CallPartyElement cp_element = new CallPartyElement();
218         cp_element.setName("unregistered");
219
220         cp_element.setFullName(full_name);
221
222         if (email != null)
223         {
224             if (email.length() > 0)
225             {
226                 cp_element.setEmail(email);
227             }
228         }
229
230         if (additional_info.length() > 0)
231         {
232             cp_element.setComment(additional_info.toString());
233         }
234
235         element.setCallParty(cp_element);
236
237         return element;
238     }
239
240     // our own decoder since we cannot assume that everyone is using JDK 1.2
241
// URLDecoder
242
private String JavaDoc decodeValue(String JavaDoc value)
243     {
244         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
245         char[] array = value.toCharArray();
246
247         for (int i = 0; i < array.length; i++)
248         {
249             char c = array[i];
250
251             if (Character.isLetterOrDigit(c) == true)
252             {
253                 buffer.append(c);
254                 continue;
255             }
256
257             switch (c)
258             {
259             case '.':
260             case '-':
261             case '*':
262             case '_':
263                 buffer.append(c);
264                 break;
265
266             case '+':
267                 buffer.append(' ');
268                 break;
269
270             case '%':
271                 // I am sure there is a better way
272
try
273                 {
274                     char[] encoded_value = null;
275                     char v1 = array[++i];
276                     if (v1 == 'u')
277                     {
278                         // unicode formatting
279
encoded_value = new char[6];
280                         encoded_value[0] = '0';
281                         encoded_value[1] = 'x';
282                         encoded_value[2] = array[++i];
283                         encoded_value[3] = array[++i];
284                         encoded_value[4] = array[++i];
285                         encoded_value[5] = array[++i];
286                     }
287                     else
288                     {
289                         encoded_value = new char[4];
290                         encoded_value[0] = '0';
291                         encoded_value[1] = 'x';
292                         encoded_value[2] = v1;
293                         encoded_value[3] = array[++i];
294                     }
295
296                     int int_value = Integer.decode(new String JavaDoc(encoded_value))
297                             .intValue();
298
299                     char ch = (char)int_value;
300                     buffer.append(ch);
301                 }
302                 catch (NumberFormatException JavaDoc ex)
303                 {
304                     break;
305                 }
306                 break;
307             }
308
309         }
310
311         return buffer.toString();
312     }
313
314     public void destroy()
315     {
316         if (timer != null)
317         {
318             if (timer.isAlive() == true)
319             {
320                 timer.interrupt();
321             }
322         }
323
324         if (TalkFrame.Instance() != null)
325         {
326             TalkFrame.Instance().dispose();
327         }
328     }
329
330     private void drawImageButton()
331     {
332         Image image = null;
333         URL url = URLUtils.formatURL(this, imageURL);
334         if (url != null)
335         {
336             image = getImage(url); // download the image
337
}
338
339         if (image == null)
340         {
341             System.err.println("The specified URL does not exist");
342             drawTextButton(); // draw a text button instead
343
return;
344         }
345
346         // next, draw the image
347
imageButton = new ImageButton();
348         imageButton.setImage(image);
349         imageButton.setLabel(buttonText);
350         add(imageButton, BorderLayout.CENTER);
351         imageButton.invalidate();
352     }
353
354     private void drawTextButton()
355     {
356         textButton = new Button(buttonText);
357         add(textButton, BorderLayout.CENTER);
358     }
359
360     private void handleButtonClick()
361     {
362         try
363         {
364             if (unregistered == true)
365             {
366                 if (element != null)
367                 {
368                     if (TalkFrame.Instance() == null)
369                     {
370                         new TalkFrame(buttonText, codeBase.getHost(), port,
371                                 applicationId, TalkApplet.this, unregistered,
372                                 unregisteredCalled, element, caption, logo);
373                     }
374                 }
375                 else
376                 {
377                     if (infoRequired == true)
378                     {
379                         if (TalkFrame.Instance() == null)
380                         {
381                             if (userInfoFrame == null)
382                             {
383                                 // popup the UserInformationDialog
384
userInfoFrame = new UserInformationFrame(
385                                         TalkFrame.getLocale(this
386                                                 .getParameter("language")),
387                                         caption, logo);
388                                 userInfoFrame
389                                         .setListener(new UserInformationListener());
390                                 userInfoFrame.show();
391                             }
392                             else
393                             {
394                                 userInfoFrame.setVisible(true);
395                             }
396                         }
397                     }
398                     else
399                     {
400                         if (TalkFrame.Instance() == null)
401                         {
402                             // popup the talk frame
403
new TalkFrame(buttonText, codeBase.getHost(), port,
404                                     applicationId, TalkApplet.this,
405                                     unregistered, unregisteredCalled, null,
406                                     caption, logo);
407                         }
408                     }
409                 }
410             }
411             else
412             // registered user
413
{
414                 if (TalkFrame.Instance() == null)
415                 {
416                     // popup the talk frame
417
new TalkFrame(buttonText, codeBase.getHost(), port,
418                             applicationId, TalkApplet.this, unregistered,
419                             unregisteredCalled, null, caption, logo);
420                 }
421                 else
422                 {
423                     TalkFrame.Instance().popup();
424                 }
425
426             }
427         }
428         catch (TalkClientException ex)
429         {
430             return;
431         }
432     }
433
434     public void userSendingMessage(String JavaDoc message)
435     {
436         TalkFrame.Instance().userSendingMessage(message);
437     }
438
439     public void userTypingMessage()
440     {
441         TalkFrame.Instance().userTypingMessage();
442     }
443
444     public void userClickedActionButton()
445     {
446         TalkFrame.Instance().userClickedActionButton();
447     }
448
449     public void userSendingMedia(String JavaDoc media, String JavaDoc param)
450     {
451         TalkFrame.Instance().userSendingMedia(media, param);
452     }
453
454     class ButtonClickedActionListener implements ActionListener
455     {
456         public void actionPerformed(java.awt.event.ActionEvent JavaDoc actionEvent)
457         {
458             handleButtonClick();
459         }
460     }
461
462     class UserInformationListener implements UserInformationListenerInterface
463     {
464         public void actionPerformed(boolean submit, String JavaDoc name, String JavaDoc email,
465                 String JavaDoc addnl)
466         {
467             userInfoFrame = null;
468             if (submit == true)
469             {
470                 // check if the mandatory information is entered
471
if (name.length() == 0)
472                 {
473                     // popup the UserInformationDialog
474
userInfoFrame = new UserInformationFrame(
475                             TalkFrame.getLocale(TalkApplet.this
476                                     .getParameter("language")), caption, logo);
477                     userInfoFrame.setListener(new UserInformationListener());
478                     userInfoFrame.setTitle(java.util.ResourceBundle.getBundle(
479                             "com.quikj.application.web.talk.client.language",
480                             TalkFrame.getLocale(TalkApplet.this
481                                     .getParameter("language"))).getString(
482                             "User_name_has_not_been_entered_") + ' ');
483                     userInfoFrame.show();
484                     return;
485                 }
486
487                 CallingNameElement element = new CallingNameElement();
488                 CallPartyElement cp_element = new CallPartyElement();
489                 cp_element.setName("unregistered");
490                 cp_element.setFullName(name);
491
492                 if (email.length() > 0)
493                 {
494                     cp_element.setEmail(email);
495                 }
496
497                 if (addnl.length() > 0)
498                 {
499                     cp_element.setComment(addnl);
500                 }
501
502                 element.setCallParty(cp_element);
503
504                 try
505                 {
506                     // popup the talk frame
507
new TalkFrame(buttonText, codeBase.getHost(), port,
508                             applicationId, TalkApplet.this, unregistered,
509                             unregisteredCalled, element, caption, logo);
510                 }
511                 catch (TalkClientException ex)
512                 {
513                     return;
514                 }
515
516             }
517         }
518     }
519
520     class TimerThread extends Thread JavaDoc
521     {
522         public void run()
523         {
524             try
525             {
526                 sleep(500); // 500ms
527

528                 if (element != null)
529                 {
530                     try
531                     {
532                         new TalkFrame(buttonText, codeBase.getHost(), port,
533                                 applicationId, TalkApplet.this, unregistered,
534                                 unregisteredCalled, element, caption, logo);
535                     }
536                     catch (TalkClientException ex)
537                     {
538                         return;
539                     }
540                 }
541                 else
542                 {
543                     String JavaDoc parm = getParameter("open-on-init");
544                     if (parm != null)
545                     {
546                         if (parm.equalsIgnoreCase("yes") == true)
547                         {
548                             handleButtonClick(); // just
549
// treat
550
// this as
551
// if
552
// someone
553
// has
554
// clicked
555
// the
556
// button
557
}
558                     }
559                 }
560
561                 if (clientType.equals("JAVA") == true)
562                 {
563                     if (imageButton != null)
564                     {
565                         imageButton
566                                 .addActionListener(new TalkApplet.ButtonClickedActionListener());
567                     }
568                     else if (textButton != null)
569                     {
570                         textButton
571                                 .addActionListener(new TalkApplet.ButtonClickedActionListener());
572                     }
573                 }
574             }
575             catch (InterruptedException JavaDoc ex)
576             {
577                 ;
578             }
579         }
580     }
581
582     // Variables declaration - do not modify//GEN-BEGIN:variables
583
// End of variables declaration//GEN-END:variables
584
private String JavaDoc buttonText = " Talk ";
585
586     private int port = 80;
587
588     private int applicationId = 2;
589
590     private URL codeBase;
591
592     private boolean unregistered = false;
593
594     private String JavaDoc unregisteredCalled = null;
595
596     private boolean infoRequired = false;
597
598     private UserInformationFrame userInfoFrame = null;
599
600     private String JavaDoc imageURL = null;
601
602     private String JavaDoc fontInfo = null;
603
604     private String JavaDoc colorInfo = null;
605
606     private TimerThread timer = null;
607
608     private CallingNameElement element = null;
609
610     private String JavaDoc caption = null;
611
612     private Image logo = null;
613
614     private String JavaDoc clientType = "JAVA";
615
616     private ImageButton imageButton = null;
617
618     private Button textButton = null;
619 }
Popular Tags