| 1 18 19 package sync4j.syncclient.demo; 20 21 import java.awt.BorderLayout ; 22 import java.awt.CardLayout ; 23 import java.awt.Frame ; 24 import java.awt.Image ; 25 import java.awt.Panel ; 26 import java.awt.Toolkit ; 27 import java.awt.event.WindowAdapter ; 28 import java.awt.event.WindowEvent ; 29 30 import java.io.File ; 31 import java.io.FileInputStream ; 32 import java.io.FileOutputStream ; 33 import java.io.IOException ; 34 import java.io.PrintStream ; 35 36 import java.text.SimpleDateFormat ; 37 38 import java.util.Arrays ; 39 import java.util.Date ; 40 import java.util.HashMap ; 41 import java.util.Hashtable ; 42 import java.util.Properties ; 43 import java.util.Vector ; 44 45 import sync4j.foundation.pdi.contact.Contact; 46 import sync4j.foundation.pdi.converter.Converter; 47 import sync4j.foundation.pdi.converter.ConverterException; 48 import sync4j.foundation.pdi.event.Calendar; 49 import sync4j.foundation.pdi.parser.*; 50 51 import sync4j.syncclient.common.SourceUtils; 52 import sync4j.syncclient.common.logging.Logger; 53 54 import sync4j.syncclient.demo.logging.PanelHandler; 55 56 import sync4j.syncclient.spdm.DMException; 57 import sync4j.syncclient.spdm.ManagementNode; 58 import sync4j.syncclient.spdm.SimpleDeviceManager; 59 60 import sync4j.syncclient.spds.SyncManager; 61 62 import sync4j.syncclient.spds.event.SyncEvent; 63 import sync4j.syncclient.spds.event.SyncItemEvent; 64 import sync4j.syncclient.spds.event.SyncSourceEvent; 65 import sync4j.syncclient.spds.event.SyncStatusEvent; 66 import sync4j.syncclient.spds.event.SyncTransportEvent; 67 import sync4j.syncclient.spds.event.SyncListener; 68 import sync4j.syncclient.spds.event.SyncItemListener; 69 import sync4j.syncclient.spds.event.SyncSourceListener; 70 import sync4j.syncclient.spds.event.SyncStatusListener; 71 import sync4j.syncclient.spds.event.SyncTransportListener; 72 73 import com.funambol.commons.io.FileTools; 74 75 import org.xml.sax.SAXException ; 76 77 85 public class MainWindow 86 extends Frame  87 implements 88 ConfigurationParameters , 89 SyncItemListener , 90 SyncListener , 91 SyncSourceListener , 92 SyncStatusListener , 93 SyncTransportListener { 94 95 117 119 public static final String DIR_CONTACTS = "contacts" ; 120 public static final String DIR_CALENDAR = "calendar" ; 121 public static final String DM_VALUE_PATH = "spds/syncml" ; 122 public static final String DM_VALUE_CONTACT_PATH = "spds/sources/contact" ; 123 public static final String DM_VALUE_CALENDAR_PATH = "spds/sources/calendar" ; 124 125 public static final String PROP_CHARSET = "spds.charset" ; 126 public static final String PROP_WD = "wd" ; 127 128 public static final String DATE_FORMAT = "yyyyMMdd'T'HHmmss" ; 129 130 public static final String ROOT_DIRECTORY = "config" ; 131 132 public static final String VALUE_UTF8 = "UTF8" ; 133 134 public static final String XML_VERSION 135 = "<?xml version=\"1.0\" encoding=\"" + 136 System.getProperty("file.encoding") + 137 "\"?>" ; 138 139 141 144 protected Vector contacts ; 145 146 149 protected Vector calendars ; 150 151 153 156 private ManagementNode rootNode = null ; 157 158 161 private Hashtable syncmlValues = null ; 162 163 166 private Hashtable xmlContactValues = null ; 167 168 171 private Hashtable xmlCalendarValues = null ; 172 173 176 private Integer currentIndex = null ; 177 178 181 private String rootDirectory = null ; 182 183 186 private String sourceDirectory = "db" ; 187 188 private CardLayout cardLayout = null ; 189 private Panel mainPanel = null ; 190 private DemoMenuBar menubar = null ; 191 private CalendarNew calNew = null ; 192 private CalendarList calList = null ; 193 private CalendarModify calendarModify = null ; 194 private ContactNew contNew = null ; 195 private ContactList contList = null ; 196 private ContactModify contactModify = null ; 197 198 private PanelHandler panelHandler = null ; 199 private Configuration config = null ; 200 201 private Language ln = new Language() ; 202 private Logger logger = new Logger () ; 203 204 206 211 public MainWindow() { 212 213 super (); 214 setTitle (ln.getString("sync4j_sync_client_pim_demo")); 215 216 Image icon = Toolkit.getDefaultToolkit().getImage(FRAME_ICONNAME); 217 218 setIconImage(icon); 219 220 Panel bottomPanel = null ; 221 Properties props = null ; 222 223 contacts = new Vector () ; 224 calendars = new Vector () ; 225 226 rootDirectory = System.getProperty(PROP_WD); 227 228 props = System.getProperties(); 229 if (System.getProperty(SimpleDeviceManager.PROP_DM_DIR_BASE) == null) { 230 props.put(SimpleDeviceManager.PROP_DM_DIR_BASE , 231 buildPath(rootDirectory, ROOT_DIRECTORY)); 232 System.setProperties(props); 233 } 234 props = System.getProperties(); 235 props.put(PROP_CHARSET, VALUE_UTF8); 236 System.setProperties(props); 237 238 sourceDirectory = buildPath(rootDirectory,sourceDirectory); 239 240 rootNode = SimpleDeviceManager.getDeviceManager().getManagementTree(); 244 245 try { 246 syncmlValues = rootNode.getNodeValues ( DM_VALUE_PATH ) ; 247 xmlContactValues = rootNode.getNodeValues ( DM_VALUE_CONTACT_PATH ) ; 248 xmlCalendarValues = rootNode.getNodeValues ( DM_VALUE_CALENDAR_PATH ) ; 249 } catch (DMException exc) { 250 251 if (logger.isLoggable(Logger.DEBUG)) { 252 logger.debug(ln.getString ("cannot_find_devicemanager_values") + 253 " " + 254 exc.getMessage() ); 255 } 256 257 } 258 259 setSize(440,495); 263 setLocation(400,200); 264 265 setLayout(new BorderLayout ()); 266 mainPanel = new Panel (); 267 268 bottomPanel = new Panel (); 272 bottomPanel.setSize(240, 5); 273 274 cardLayout = new CardLayout (); 275 mainPanel.setLayout(cardLayout); 276 277 menubar = new DemoMenuBar(this) ; 278 contList = new ContactList(this) ; 279 280 refresh(); 281 282 contactModify = new ContactModify (this) ; 283 calendarModify = new CalendarModify (this) ; 284 contNew = new ContactNew (this) ; 285 panelHandler = new PanelHandler (this) ; 286 config = new Configuration (this) ; 287 288 calList = new CalendarList(this); 289 calNew = new CalendarNew(this); 290 291 setMenuBar(menubar); 295 296 mainPanel.add(contList , KEY_CONTACTLIST ) ; 297 mainPanel.add(contactModify , KEY_CONTACTMODIFY ) ; 298 mainPanel.add(calendarModify , KEY_CALENDARMODIFY ) ; 299 mainPanel.add(contNew , KEY_CONTACTNEW ) ; 300 mainPanel.add(panelHandler , KEY_SYNC ) ; 301 mainPanel.add(config , KEY_CONFIG ) ; 302 mainPanel.add(calList , KEY_CALENDARLIST ) ; 303 mainPanel.add(calNew , KEY_CALENDARNEW ) ; 304 305 add(mainPanel,BorderLayout.CENTER); 306 add(bottomPanel,BorderLayout.SOUTH); 307 308 cardLayout.show(mainPanel, KEY_CONTACTLIST); 310 311 addWindowListener(new WindowAdapter () { 313 public void windowClosing(WindowEvent evt) { 314 exit(); 315 } 316 } ); 317 } 318 319 322 public void refresh() { 323 contacts = getContacts(); 324 contList.fillList(); 325 show(KEY_CONTACTLIST); 326 } 327 328 329 334 public void syncBegin(SyncEvent event) { 335 if (logger.isLoggable(Logger.DEBUG)) { 336 logger.debug("SyncEvent - Sync begin - date: " + 337 event.getDate() ); 338 } 339 } 340 341 346 public void syncEnd(SyncEvent event) { 347 if (logger.isLoggable(Logger.DEBUG)) { 348 logger.debug("SyncEvent - Sync end - date: " + 349 event.getDate() ); 350 } 351 } 352 353 358 public void sendInitialization(SyncEvent event) { 359 if (logger.isLoggable(Logger.DEBUG)) { 360 logger.debug("SyncEvent - Send initialization - date: " + 361 event.getDate() ); 362 } 363 } 364 365 370 public void sendModification(SyncEvent event) { 371 if (logger.isLoggable(Logger.DEBUG)) { 372 logger.debug("SyncEvent - Send modification - date: " + 373 event.getDate() ); 374 } 375 } 376 377 382 public void sendFinalization(SyncEvent event) { 383 if (logger.isLoggable(Logger.DEBUG)) { 384 logger.debug("SyncEvent - Send finalization - date: " + 385 event.getDate() ); 386 } 387 } 388 389 394 public void syncError(SyncEvent event) { 395 if (logger.isLoggable(Logger.DEBUG)) { 396 logger.debug("SyncEvent - Sync error - date: " + 397 event.getDate () + 398 " - message: " + 399 event.getMessage () + 400 " - cause: " + 401 event.getCause().getMessage() ); 402 } 403 } 404 405 410 public void syncBegin(SyncSourceEvent event) { 411 if (logger.isLoggable(Logger.DEBUG)) { 412 logger.debug("SyncSourceEvent - Sync begin - date: " + 413 event.getDate () + 414 " - sourceUri: " + 415 event.getSourceUri () + 416 " - sync mode: " + 417 event.getSyncMode () ); 418 } 419 } 420 421 426 public void syncEnd(SyncSourceEvent event) { 427 if (logger.isLoggable(Logger.DEBUG)) { 428 logger.debug("SyncSourceEvent - Sync end - date: " + 429 event.getDate () + 430 " - sourceUri: " + 431 event.getSourceUri () + 432 " - sync mode: " + 433 event.getSyncMode () ); 434 } 435 } 436 437 442 public void sendDataBegin(SyncTransportEvent event) { 443 if (logger.isLoggable(Logger.DEBUG)) { 444 logger.debug 445 ("SyncTransportEvent - Send data begin - data length: " + 446 event.getData() ); 447 } 448 } 449 450 455 public void sendDataEnd(SyncTransportEvent event) { 456 if (logger.isLoggable(Logger.DEBUG)) { 457 logger.debug 458 ("SyncTransportEvent - Send data end - data length: " + 459 event.getData() ); 460 } 461 } 462 463 468 public void receiveDataBegin(SyncTransportEvent event) { 469 if (logger.isLoggable(Logger.DEBUG)) { 470 logger.debug 471 ("SyncTransportEvent - Receive data begin - data length: " + 472 event.getData() ); 473 } 474 } 475 476 481 public void dataReceived(SyncTransportEvent event) { 482 if (logger.isLoggable(Logger.DEBUG)) { 483 logger.debug 484 ("SyncTransportEvent - Data received - data length: " + 485 event.getData() ); 486 } 487 } 488 489 494 public void receiveDataEnd(SyncTransportEvent event) { 495 if (logger.isLoggable(Logger.DEBUG)) { 496 logger.debug 497 ("SyncTransportEvent - Received data end - data length: " + 498 event.getData() ); 499 } 500 } 501 502 507 public void itemAddedByServer(SyncItemEvent event) { 508 if (logger.isLoggable(Logger.DEBUG)) { 509 logger.debug 510 ("SyncItemEvent - Item added by server - sourceUri: " + 511 event.getSourceUri() + 512 " - key: " + 513 event.getItemKey().getKeyAsString() ); 514 } 515 } 516 517 522 public void itemDeletedByServer(SyncItemEvent event) { 523 if (logger.isLoggable(Logger.DEBUG)) { 524 logger.debug 525 ("SyncItemEvent - Item deleted by server - sourceUri: " + 526 event.getSourceUri() + 527 " - key: " + 528 event.getItemKey().getKeyAsString() ); 529 } 530 } 531 532 537 public void itemUpdatedByServer(SyncItemEvent event) { 538 if (logger.isLoggable(Logger.DEBUG)) { 539 logger.debug 540 ("SyncItemEvent - Item updated by server - sourceUri: " + 541 event.getSourceUri() + 542 " - key: " + 543 event.getItemKey().getKeyAsString() ); 544 } 545 } 546 547 552 public void itemAddedByClient(SyncItemEvent event) { 553 if (logger.isLoggable(Logger.DEBUG)) { 554 logger.debug 555 ("SyncItemEvent - Item added by client - sourceUri: " + 556 event.getSourceUri() + 557 " - key: " + 558 event.getItemKey().getKeyAsString() ); 559 } 560 } 561 562 567 public void itemDeletedByClient(SyncItemEvent event) { 568 if (logger.isLoggable(Logger.DEBUG)) { 569 logger.debug 570 ("SyncItemEvent - Item deleted by client - sourceUri: " + 571 event.getSourceUri() + 572 " - key: " + 573 event.getItemKey().getKeyAsString() ); 574 } 575 } 576 577 582 public void itemUpdatedByClient(SyncItemEvent event) { 583 if (logger.isLoggable(Logger.DEBUG)) { 584 logger.debug 585 ("SyncItemEvent - Item updated by client - sourceUri: " + 586 event.getSourceUri() + 587 " - key: " + 588 event.getItemKey().getKeyAsString() ); 589 } 590 } 591 592 597 public void statusReceived (SyncStatusEvent event) { 598 if (logger.isLoggable(Logger.DEBUG)) { 599 logger.debug 600 ("SyncStatusEvent - Received status - command: " + 601 event.getCommand() + 602 " - status: " + 603 event.getStatusCode() + 604 " - sourceUri: " + 605 event.getSourceUri() + 606 " - key: " + 607 event.getItemKey().getKeyAsString() ); 608 } 609 } 610 611 616 public void statusToSend (SyncStatusEvent event) { 617 if (logger.isLoggable(Logger.DEBUG)) { 618 logger.debug 619 ("SyncStatusEvent - Status to send - command: " + 620 event.getCommand() + 621 " - status: " + 622 event.getStatusCode() + 623 " - sourceUri: " + 624 event.getSourceUri() + 625 " - key: " + 626 event.getItemKey().getKeyAsString() ); 627 } 628 629 } 630 631 633 636 protected void exit() { 637 638 setVisible(false); 642 643 dispose(); 644 645 if (logger.isLoggable(Logger.INFO)) { 649 logger.info(ln.getString ("logging_stopped")); 650 } 651 652 System.exit(0); 653 } 654 655 659 public void synchronize() { 660 661 panelHandler.setButton(false); 662 663 try { 664 665 if (logger.isLoggable(Logger.INFO)) { 666 logger.info(ln.getString ("setting_up_sync_manager")); 667 } 668 669 Logger.setHandler(panelHandler); 670 SyncManager syncManager = SyncManager.getSyncManager(""); 671 672 syncManager.addSyncItemListener (this) ; 673 syncManager.addSyncListener (this) ; 674 syncManager.addSyncSourceListener (this) ; 675 syncManager.addSyncStatusListener (this) ; 676 syncManager.addSyncTransportListener (this) ; 677 678 if (logger.isLoggable(Logger.INFO)) { 679 logger.info(ln.getString ("synchronizing")); 680 } 681 682 syncManager.sync(); 683 684 if (logger.isLoggable(Logger.INFO)) { 685 logger.info(ln.getString ("done")); 686 } 687 688 panelHandler.setButton(true); 689 690 } catch (Exception exc) { 691 if (exc.getMessage()==null) { 692 693 if (logger.isLoggable(Logger.INFO)) { 694 logger.info(ln.getString ("synch_exception") + 695 " " + 696 exc ); 697 } 698 699 } else { 700 if (logger.isLoggable(Logger.INFO)) { 701 logger.info(exc.getMessage()); 702 } 703 } 704 705 if (logger.isLoggable(Logger.INFO)) { 706 logger.info(this.getClass().getName() + 707 " synchronize" + 708 exc.getMessage() ); 709 } 710 711 panelHandler.setButton(true); 712 } 713 } 714 715 718 protected void refreshCalendar() { 719 calendars = getCalendars(); 720 calList.fillList(); 721 } 722 723 726 protected void refreshContact() { 727 728 contacts = getContacts() ; 729 730 contacts = sortContacts(contacts); 731 732 contList.fillList(); 733 } 734 735 744 protected void show(String card) { 745 746 cardLayout.show(mainPanel,card); 747 748 if (card.equals (KEY_CALENDARLIST )) { 749 750 refreshCalendar(); 751 752 } else if (card.equals (KEY_CALENDARMODIFY )) { 753 754 calendarModify.fillFields(((DemoCalendar) calendars. 755 elementAt(getCurrentIndex().intValue())).getCalendar()); 756 757 } else if (card.equals (KEY_CALENDARNEW )) { 758 759 calNew.blankFields(); 760 761 } else if (card.equals (KEY_CONFIG )) { 762 763 config.setAllFields 764 (syncmlValues, xmlContactValues, xmlCalendarValues); 765 766 } else if (card.equals (KEY_CONTACTLIST )) { 767 768 refreshContact(); 769 770 } if (card.equals (KEY_CONTACTMODIFY )) { 771 772 contactModify.fillFields(((DemoContact) contacts. 773 elementAt(getCurrentIndex().intValue())).getContact()); 774 775 } else if (card.equals (KEY_CONTACTNEW )) { 776 777 contNew.blankFields(); 778 779 } else if (card.equals (KEY_SYNC )) { 780 781 synchronize(); 782 783 } 784 785 } 786 787 793 protected Vector getContacts() { 794 795 try { 796 797 File curFile = null ; 798 799 Vector existingFiles = null ; 800 Vector contacts = null ; 801 Contact contact = null ; 802 803 contacts = new Vector (); 804 805 existingFiles = getExistingFiles(DIR_CONTACTS); 806 807 for (int i=0, l = existingFiles.size(); i < l; i++) { 808 809 curFile = (File ) existingFiles.elementAt(i); 810 811 try { 812 813 if (logger.isLoggable(Logger.DEBUG)) { 814 logger.debug(ln.getString ("parsing_file") + 815 " " + 816 curFile ); 817 } 818 819 XMLContactParser xmlParser = 820 new XMLContactParser(new FileInputStream (curFile)); 821 contact = (Contact)xmlParser.parse(); 822 contacts.addElement 823 (new DemoContact(curFile.getName(),contact)); 824 825 } catch (SAXException e) { 826 827 if (logger.isLoggable(Logger.DEBUG)) { 828 logger.debug(ln.getString ("error_sax_exception") + 829 " " + 830 curFile + 831 ": " + 832 e.getMessage() ); 833 } 834 835 } 836 837 } 838 839 return contacts; 840 } catch (IOException exc) { 841 if (logger.isLoggable(Logger.DEBUG)) { 842 logger.debug(ln.getString ("error_reading_files") + 843 " " + 844 exc.getMessage() ); 845 } 846 847 } 848 849 return new Vector (); 850 } 851 852 858 protected Vector getCalendars() { 859 860 try { 861 862 File curFile = null ; 863 864 Vector existingFiles = null ; 865 Vector calendars = null ; 866 Calendar calendar = null ; 867 868 calendars = new Vector (); 869 870 existingFiles = getExistingFiles(DIR_CALENDAR); 871 for (int i=0, l = existingFiles.size(); i < l; i++) { 872 873 curFile = (File ) existingFiles.elementAt(i); 874 875 try { 876 877 if (logger.isLoggable(Logger.DEBUG)) { 878 logger.debug(ln.getString ("parsing_file") + 879 " " + 880 curFile ); 881 }<
|