1 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 22 public class TalkApplet extends java.applet.Applet 23 { 24 25 26 public void init() 27 { 28 codeBase = getCodeBase(); 30 31 if (codeBase.getPort() >= 0) { 34 port = codeBase.getPort(); 35 } 36 37 String parm = getParameter("port"); 38 if (parm != null) 39 { 40 try 41 { 42 port = Integer.parseInt(parm); 43 } 44 catch (NumberFormatException 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 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 timer = new TalkApplet.TimerThread(); 160 timer.start(); 161 162 } 163 164 169 private void initComponents() { 171 172 setLayout(new java.awt.BorderLayout ()); 173 174 } 176 private CallingNameElement parseUserInfo(String parm) 177 { 178 String full_name = null; 179 String email = null; 180 StringBuffer additional_info = new StringBuffer (); 181 182 StringTokenizer tokens = new StringTokenizer(parm, "&"); 183 184 while (tokens.hasMoreTokens() == true) 185 { 186 String token = tokens.nextToken(); 187 188 StringTokenizer nv_pairs = new StringTokenizer(new String (token), 190 "="); 191 if (nv_pairs.countTokens() != 2) 192 { 193 continue; 195 } 196 197 String name = nv_pairs.nextToken(); 198 String 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 { 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 private String decodeValue(String value) 243 { 244 StringBuffer buffer = new StringBuffer (); 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 try 273 { 274 char[] encoded_value = null; 275 char v1 = array[++i]; 276 if (v1 == 'u') 277 { 278 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 (encoded_value)) 297 .intValue(); 298 299 char ch = (char)int_value; 300 buffer.append(ch); 301 } 302 catch (NumberFormatException 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); } 338 339 if (image == null) 340 { 341 System.err.println("The specified URL does not exist"); 342 drawTextButton(); return; 344 } 345 346 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 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 new TalkFrame(buttonText, codeBase.getHost(), port, 404 applicationId, TalkApplet.this, 405 unregistered, unregisteredCalled, null, 406 caption, logo); 407 } 408 } 409 } 410 } 411 else 412 { 414 if (TalkFrame.Instance() == null) 415 { 416 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 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 media, String param) 450 { 451 TalkFrame.Instance().userSendingMedia(media, param); 452 } 453 454 class ButtonClickedActionListener implements ActionListener 455 { 456 public void actionPerformed(java.awt.event.ActionEvent actionEvent) 457 { 458 handleButtonClick(); 459 } 460 } 461 462 class UserInformationListener implements UserInformationListenerInterface 463 { 464 public void actionPerformed(boolean submit, String name, String email, 465 String addnl) 466 { 467 userInfoFrame = null; 468 if (submit == true) 469 { 470 if (name.length() == 0) 472 { 473 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 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 521 { 522 public void run() 523 { 524 try 525 { 526 sleep(500); 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 parm = getParameter("open-on-init"); 544 if (parm != null) 545 { 546 if (parm.equalsIgnoreCase("yes") == true) 547 { 548 handleButtonClick(); } 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 ex) 576 { 577 ; 578 } 579 } 580 } 581 582 private String 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 unregisteredCalled = null; 595 596 private boolean infoRequired = false; 597 598 private UserInformationFrame userInfoFrame = null; 599 600 private String imageURL = null; 601 602 private String fontInfo = null; 603 604 private String colorInfo = null; 605 606 private TimerThread timer = null; 607 608 private CallingNameElement element = null; 609 610 private String caption = null; 611 612 private Image logo = null; 613 614 private String clientType = "JAVA"; 615 616 private ImageButton imageButton = null; 617 618 private Button textButton = null; 619 } | Popular Tags |