1 6 7 package com.quikj.application.web.talk.client; 8 import java.util.*; 9 import java.awt.*; 10 import com.quikj.client.beans.*; 11 import com.quikj.application.web.talk.messaging.*; 12 import com.quikj.client.framework.*; 13 14 25 public class XMLFormUser implements XMLFormListenerInterface 26 { 27 28 private static String [] awtClasses = 29 { "Checkbox", "Choice", "List", 30 "TextArea", "TextField" }; 31 32 private static final int AWTINDEX_CHECKBOX = 0, 33 AWTINDEX_CHOICE = AWTINDEX_CHECKBOX + 1, 34 AWTINDEX_LIST = AWTINDEX_CHOICE + 1, 35 AWTINDEX_TEXTAREA = AWTINDEX_LIST + 1, 36 AWTINDEX_TEXTFIELD = AWTINDEX_TEXTAREA + 1, 37 AWTINDEX_BUTTON = AWTINDEX_TEXTFIELD + 1; 38 39 private StringBuffer awtString = new StringBuffer (); 40 private String formString; 41 private boolean submitSuccess; 42 private String failString; 43 private TalkSession session; 44 private CallInfo cinfo; 45 private CallPartyElement clngPartyInfo; 46 private ServerCommunications com; 47 48 private Locale locale = Locale.US; 49 50 51 public XMLFormUser(TalkSession s, 52 CallInfo c, 53 CallPartyElement clng, 54 ServerCommunications c2, 55 Locale locale) 56 { 57 super(); 58 59 if (locale != null) 60 { 61 this.locale = locale; 62 } 63 64 session = s; 65 cinfo = c; 66 clngPartyInfo = clng; 67 com = c2; 68 failString = ""; 69 submitSuccess = true; 70 } 71 72 public boolean submitSuccessful() 73 { 74 return submitSuccess; 75 } 76 77 public String getMsgString() 78 { 79 return failString; 80 } 81 82 public void clearMsgString() 83 { 84 failString = ""; 85 } 86 87 97 public void resetActionPerformed(java.awt.event.ActionEvent evt, 98 XMLArrayList awtItems) 99 { 100 int i, awtType; 101 Object obj; 102 103 for ( i=0; i<awtItems.size(); i++ ) 104 { 105 obj = getItem(i, awtItems); 110 awtType = convertObject(obj); 111 switch ( awtType ) 112 { 113 case AWTINDEX_CHECKBOX: 114 clearCheckbox((Checkbox)obj); 115 break; 116 case AWTINDEX_CHOICE: 117 clearChoice((Choice)obj); 118 break; 119 case AWTINDEX_LIST: 120 clearList((java.awt.List )obj); 121 break; 122 case AWTINDEX_TEXTAREA: 123 clearTextarea((TextArea)obj); 124 break; 125 case AWTINDEX_TEXTFIELD: 126 clearTextfield((TextField)obj); 127 break; 128 default: 129 break; 130 } 131 } 132 } 133 134 135 136 137 148 void clearCheckbox(Checkbox cb) 149 { 150 cb.setState(false); 151 CheckboxGroup cbg; 152 cbg = cb.getCheckboxGroup(); 153 if ( cbg != null ) 154 cbg.setSelectedCheckbox(null); 155 } 156 157 158 166 void clearChoice(Choice c) 167 { 168 c.select(0); 169 } 170 171 180 void clearList(java.awt.List l) 181 { 182 int [] indexes = l.getSelectedIndexes(); 183 for ( int i=0; i<indexes.length; i++ ) 184 l.deselect(indexes[i]); 185 } 186 187 188 196 void clearTextarea(TextArea ta) 197 { 198 ta.setText(""); 199 } 200 201 209 void clearTextfield(TextField tf) 210 { 211 tf.setText(""); 212 } 213 214 215 216 227 public void submitActionPerformed(java.awt.event.ActionEvent evt, 228 XMLArrayList awtItems, 229 String formName) 230 { 231 int i, awtType; 232 Object obj; 233 boolean required; 234 235 submitSuccess = true; 236 awtString = new StringBuffer (); 237 awtString.append("<form"); 238 if ( formName != "" ) 239 { 240 awtString.append(" name=\"" 241 + TalkMessageParser.encodeXMLString(formName) + "\""); 242 } 243 244 awtString.append(" type=\"response\">\n"); 245 246 for ( i=0; i < awtItems.size(); i++ ) 247 { 248 obj = getItem(i, awtItems); 249 required = getRequired(i, awtItems); 250 awtType = convertObject(obj); 251 switch ( awtType ) 252 { 253 case AWTINDEX_CHECKBOX: 254 getCheckboxInput((Checkbox)obj); 255 break; 256 case AWTINDEX_CHOICE: 257 getChoiceInput((Choice)obj); 258 break; 259 case AWTINDEX_LIST: 260 getListInput((java.awt.List )obj); 261 break; 262 case AWTINDEX_TEXTAREA: 263 getTextareaInput((TextArea)obj, required); 264 break; 265 case AWTINDEX_TEXTFIELD: 266 getTextfieldInput((TextField)obj, required); 267 break; 268 default: 269 break; 270 } 271 } 272 awtString.append("</form>"); 273 274 if (submitSuccess == true ) 275 { 276 XMLFormResponseElement userResponse = new XMLFormResponseElement(); 277 userResponse.setXmlForm(awtString.toString()); 278 formString = userResponse.format(); 279 if (cinfo != null) 280 { 281 if (cinfo.isConnected() == true) 282 { 283 String from_name = clngPartyInfo.getUserName(); 284 if (clngPartyInfo.getFullName() != null) 285 { 286 if (clngPartyInfo.getFullName().length() > 0) 287 { 288 from_name = clngPartyInfo.getFullName(); 289 } 290 } 291 292 RTPMessage message = new RTPMessage(); 293 message.setSessionId(cinfo.getSessionId()); 294 message.setFrom(from_name); 295 MediaElements elements = new MediaElements(); 296 elements.addMediaElement(userResponse); 297 message.setMediaElements(elements); 298 com.sendRequestMessage("text/xml", message.format()); 299 300 session.appendToConversation(from_name + ": ", Color.red, Font.BOLD); 301 session.appendToConversation("Form data sent.\n", null, -1); 302 } 303 } 304 } 305 306 return; 307 308 } 309 310 void getCheckboxInput(Checkbox cb) 311 { 312 String s; 313 CheckboxGroup cbg; 314 315 awtString.append("<checkbox"); 316 s = cb.getName(); 317 if ( s.length() != 0 ) 318 { 319 awtString.append(" name=\"" 320 + TalkMessageParser.encodeXMLString(s) + "\""); 321 } 322 awtString.append(">"); 323 324 if (cb.getState() == true ) 325 { 326 awtString.append("yes"); 327 } 328 else 329 { 330 awtString.append("no"); 331 } 332 333 awtString.append("</checkbox>\n"); 334 } 335 336 337 void getChoiceInput(Choice c) 338 { 339 String s; 340 341 if ( c.getSelectedIndex() != -1 ) 342 { 343 awtString.append("<choice"); 344 s = c.getName(); 345 if ( s.length() != 0 ) 346 { 347 awtString.append(" name=\"" 348 + TalkMessageParser.encodeXMLString(s) + "\""); 349 } 350 351 awtString.append(">" 352 + c.getSelectedItem() + "</choice>\n"); 353 } 354 } 355 356 void getListInput(java.awt.List l) 357 { 358 String [] listItems = l.getSelectedItems(); 359 String s; 360 361 if ( listItems.length > 0 ) 362 { 363 for (int i = 0; i < listItems.length; i++ ) 364 { 365 awtString.append("<list"); 366 s = l.getName(); 367 if ( s.length() != 0 ) 368 { 369 awtString.append(" name=\"" 370 + TalkMessageParser.encodeXMLString(s) + "\""); 371 } 372 awtString.append(">" 373 + TalkMessageParser.encodeXMLString(listItems[i]) 374 + "</list>\n"); 375 } 376 } 377 } 378 379 void getTextareaInput(TextArea ta, boolean required) 380 { 381 String s, name; 382 awtString.append("<textarea"); 383 name = ta.getName(); 384 if ( name.length() != 0 ) 385 { 386 awtString.append(" name=\"" 387 + TalkMessageParser.encodeXMLString(name) + "\""); 388 } 389 awtString.append(">"); 390 391 s = ta.getText(); 392 awtString.append(TalkMessageParser.encodeXMLString(s) + "</textarea>\n"); 393 394 if ( (s.length() == 0) & (required == true) ) 395 { 396 submitSuccess = false; 397 398 String newline = ""; 399 if (failString.length() > 0) 400 { 401 newline = "\n"; 402 } 403 404 failString = failString 405 + name + ": " 406 + java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.client.language", 407 locale).getString("cannot_be_empty") 408 + newline; 409 } 410 } 411 412 void getTextfieldInput(TextField tf, boolean required) 413 { 414 String s, name; 415 awtString.append("<textfield"); 416 name = tf.getName(); 417 if ( name.length() != 0 ) 418 { 419 awtString.append(" name=\"" 420 + TalkMessageParser.encodeXMLString(name) + "\""); 421 } 422 423 awtString.append(">"); 424 s = tf.getText(); 425 awtString.append(TalkMessageParser.encodeXMLString(s) 426 + "</textfield>\n"); 427 428 if ( (s.length() == 0) & (required == true) ) 429 { 430 submitSuccess = false; 431 String newline = ""; 432 if (failString.length() > 0) 433 { 434 newline = "\n"; 435 } 436 failString = failString 437 + name + ": " 438 + java.util.ResourceBundle.getBundle("com.quikj.application.web.talk.client.language", 439 locale).getString("cannot_be_empty") 440 + newline; 441 } 442 } 443 444 456 Object getItem( int index, XMLArrayList awtItems) 457 { 458 return(awtItems.get(index)); 459 } 460 461 470 boolean getRequired( int index, XMLArrayList awtItems) 471 { 472 return(awtItems.isRequired(index)); 473 } 474 475 476 490 int convertObject(Object obj) 491 { 492 int returnValue = -1, count; 493 StringTokenizer awtType; 494 String objString, classString; 495 496 objString = obj.getClass().toString(); 497 awtType = new StringTokenizer(objString, ". "); 498 count = awtType.countTokens(); 499 for ( int i=0; i<count-1; i++) 500 awtType.nextToken(); 501 classString = awtType.nextToken(); 502 503 for ( int i=0; i<awtClasses.length; i++ ) 504 { 505 if ( classString.equals(awtClasses[i]) ) 506 { 507 returnValue = i; 508 break; 509 } 510 } 511 512 return (returnValue); 513 } 514 515 516 } 517 | Popular Tags |