Guest User

Untitled

a guest
Feb 20th, 2012
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.39 KB | None | 0 0
  1. import pyjd # this is dummy in pyjs.
  2. from pyjamas.ui.RootPanel import RootPanel
  3. from pyjamas.ui.Button import Button
  4. from pyjamas.ui.HTML import HTML
  5. from pyjamas.ui.Label import Label
  6. from pyjamas import Window
  7.  
  8. from  pyjamas.ui.Grid import Grid
  9. from  pyjamas.ui.Hyperlink import Hyperlink
  10. from  pyjamas.ui.TextBox import TextBox
  11. import pygwt
  12.  
  13.  
  14.  
  15. # Constants
  16. CONTACT_LISTING_ROOT_PANEL = "contactListing"
  17. CONTACT_FORM_ROOT_PANEL = "contactForm"
  18. CONTACT_STATUS_ROOT_PANEL = "contactStatus"
  19. CONTACT_TOOL_BAR_ROOT_PANEL = "contactToolBar"
  20. EDIT_LINK = 3
  21. REMOVE_LINK = 4
  22.  
  23.  
  24. class Contact:
  25.     def __init__(self, name="", email="", phone=""):
  26.         self.name = name
  27.         self.email = email
  28.         self.phone = phone
  29.  
  30.    
  31.  
  32. class ContactService:
  33.     def __init__(self, callback):
  34.         self.callback = callback
  35.         self.contacts = []
  36.  
  37.     def addContact(self, contact):
  38.         self.contacts.append(contact)
  39.         self.callback.service_eventAddContactSuccessful()
  40.    
  41.     def updateContact(self, contact):
  42.         self.callback.service_eventUpdateContactSuccessful()
  43.  
  44.     def removeContact(self, contact):
  45.         self.contacts.remove(contact)
  46.         self.callback.service_eventRemoveContactSuccessful()
  47.        
  48.     def listContacts(self):
  49.         self.callback.service_eventListRetrievedFromService(self.contacts)
  50.  
  51.  
  52. class ContactListGUI:
  53.  
  54.  
  55.     def __init__(self):
  56.         self.contactService = ContactService(self)
  57.         self.currentContact = Contact("Rick", "[email protected]", "555-555-5555")
  58.         self.addButton = Button("Add contact", self.gui_eventAddButtonClicked)
  59.         self.addNewButton = Button("Add new contact", self.gui_eventAddNewButtonClicked)
  60.         self.updateButton = Button("Update contact", self.gui_eventUpdateButtonClicked)
  61.  
  62.         self.nameField = TextBox()
  63.         self.emailField = TextBox()
  64.         self.phoneField = TextBox()
  65.         self.status = Label()
  66.         self.contactGrid = Grid(2,5)
  67.         self.contactGrid.addTableListener(self)
  68.  
  69.         self.buildForm()
  70.         self.placeWidgets()
  71.  
  72.         self.contactService.listContacts()
  73.  
  74.    
  75.     def onCellClicked(self, sender, row, cell):
  76.         print "sender=%s row=%s cell=%s" % (sender, row, cell)
  77.         self.gui_eventContactGridClicked(row, cell)
  78.  
  79.     def onClick(self, sender):
  80.         if sender == self.addButton:
  81.             self.gui_eventAddButtonClicked()
  82.         elif sender == self.addNewButton:
  83.             self.gui_eventAddNewButtonClicked()
  84.         elif sender == self.updateButton:
  85.             self.gui_eventUpdateButtonClicked()
  86.                
  87.     def buildForm(self):
  88.         formGrid = Grid(4,3)
  89.         formGrid.setVisible(False)
  90.        
  91.         formGrid.setWidget(0, 0, Label("Name"))
  92.         formGrid.setWidget(0, 1, self.nameField);
  93.  
  94.         formGrid.setWidget(1, 0, Label("email"))
  95.         formGrid.setWidget(1, 1, self.emailField)
  96.        
  97.         formGrid.setWidget(2, 0, Label("phone"))
  98.         formGrid.setWidget(2, 1, self.phoneField)
  99.        
  100.         formGrid.setWidget(3, 0, self.updateButton)
  101.         formGrid.setWidget(3, 1, self.addButton)
  102.  
  103.         self.formGrid = formGrid
  104.        
  105.  
  106.  
  107.     def placeWidgets(self):
  108.         RootPanel(CONTACT_LISTING_ROOT_PANEL).add(self.contactGrid)
  109.         RootPanel(CONTACT_FORM_ROOT_PANEL).add(self.formGrid)
  110.         RootPanel(CONTACT_STATUS_ROOT_PANEL).add(self.status)
  111.         RootPanel(CONTACT_TOOL_BAR_ROOT_PANEL).add(self.addNewButton)
  112.  
  113.     def loadForm(self, contact):
  114.         self.formGrid.setVisible(True)
  115.         self.currentContact = contact
  116.         self.emailField.setText(contact.email)
  117.         self.phoneField.setText(contact.phone)
  118.         self.nameField.setText(contact.name)
  119.    
  120.     def copyFieldDateToContact(self):
  121.         self.currentContact.email = self.emailField.getText()
  122.         self.currentContact.name = self.nameField.getText()
  123.         self.currentContact.phone = self.phoneField.getText()
  124.    
  125.  
  126.     def gui_eventContactGridClicked(self, row, col):
  127.        
  128.          contact = self.contacts[row]
  129.  
  130.          self.status.setText("Name was " + contact.name + " clicked ")
  131.        
  132.          if col==EDIT_LINK:
  133.              self.addNewButton.setVisible(False)
  134.              self.updateButton.setVisible(True)
  135.              self.addButton.setVisible(False)
  136.              self.loadForm(contact)
  137.          elif (col==REMOVE_LINK):
  138.              self.contactService.removeContact(contact)
  139.  
  140.  
  141.     def gui_eventAddButtonClicked(self, sender):
  142.         self.addNewButton.setVisible(True)
  143.         self.formGrid.setVisible(False)
  144.         self.copyFieldDateToContact()
  145.         self.contactService.addContact(self.currentContact)
  146.  
  147.     def gui_eventUpdateButtonClicked(self, sender):
  148.         self.addNewButton.setVisible(True)
  149.         self.formGrid.setVisible(False)
  150.         self.copyFieldDateToContact()
  151.         self.contactService.updateContact(self.currentContact)
  152.  
  153.  
  154.     def gui_eventAddNewButtonClicked(self, sender):
  155.         self.addNewButton.setVisible(False)
  156.         self.updateButton.setVisible(False)
  157.         self.addButton.setVisible(True)
  158.         self.loadForm(Contact())
  159.  
  160.  
  161.  
  162.     def service_eventListRetrievedFromService(self, results):
  163.         self.status.setText("Retrieved contact list")
  164.         self.contacts = results;
  165.         self.contactGrid.clear();
  166.         self.contactGrid.resizeRows(len(self.contacts))
  167.         row = 0
  168.        
  169.         for contact in results:
  170.             self.contactGrid.setWidget(row, 0, Label(contact.name))
  171.             self.contactGrid.setWidget(row, 1, Label (contact.phone))
  172.             self.contactGrid.setWidget(row, 2, Label (contact.email))
  173.             self.contactGrid.setWidget(row, EDIT_LINK, Hyperlink("Edit", None))
  174.             self.contactGrid.setWidget(row, REMOVE_LINK, Hyperlink("Remove", None))
  175.             row += 1
  176.  
  177.  
  178.     def service_eventAddContactSuccessful(self):
  179.         self.status.setText("Contact was successfully added");
  180.         self.contactService.listContacts();
  181.  
  182.  
  183.     def service_eventUpdateContactSuccessful(self):
  184.         self.status.setText("Contact was successfully updated");
  185.         self.contactService.listContacts();
  186.  
  187.     def service_eventRemoveContactSuccessful(self):
  188.         self.status.setText("Contact was removed");
  189.         self.contactService.listContacts();
  190.        
  191.  
  192.  
  193. if __name__ == '__main__':
  194.     pyjd.setup("public/Contacts.html")
  195.     contacts = ContactListGUI()
  196.     pyjd.run()
Advertisement
Add Comment
Please, Sign In to add comment