Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pyjd # this is dummy in pyjs.
- from pyjamas.ui.RootPanel import RootPanel
- from pyjamas.ui.Button import Button
- from pyjamas.ui.HTML import HTML
- from pyjamas.ui.Label import Label
- from pyjamas import Window
- from pyjamas.ui.Grid import Grid
- from pyjamas.ui.Hyperlink import Hyperlink
- from pyjamas.ui.TextBox import TextBox
- import pygwt
- # Constants
- CONTACT_LISTING_ROOT_PANEL = "contactListing"
- CONTACT_FORM_ROOT_PANEL = "contactForm"
- CONTACT_STATUS_ROOT_PANEL = "contactStatus"
- CONTACT_TOOL_BAR_ROOT_PANEL = "contactToolBar"
- EDIT_LINK = 3
- REMOVE_LINK = 4
- class Contact:
- def __init__(self, name="", email="", phone=""):
- self.name = name
- self.email = email
- self.phone = phone
- class ContactService:
- def __init__(self, callback):
- self.callback = callback
- self.contacts = []
- def addContact(self, contact):
- self.contacts.append(contact)
- self.callback.service_eventAddContactSuccessful()
- def updateContact(self, contact):
- self.callback.service_eventUpdateContactSuccessful()
- def removeContact(self, contact):
- self.contacts.remove(contact)
- self.callback.service_eventRemoveContactSuccessful()
- def listContacts(self):
- self.callback.service_eventListRetrievedFromService(self.contacts)
- class ContactListGUI:
- def __init__(self):
- self.contactService = ContactService(self)
- self.addButton = Button("Add contact", self.gui_eventAddButtonClicked)
- self.addNewButton = Button("Add new contact", self.gui_eventAddNewButtonClicked)
- self.updateButton = Button("Update contact", self.gui_eventUpdateButtonClicked)
- self.nameField = TextBox()
- self.emailField = TextBox()
- self.phoneField = TextBox()
- self.status = Label()
- self.contactGrid = Grid(2,5)
- self.contactGrid.addTableListener(self)
- self.buildForm()
- self.placeWidgets()
- self.contactService.listContacts()
- def onCellClicked(self, sender, row, cell):
- print "sender=%s row=%s cell=%s" % (sender, row, cell)
- self.gui_eventContactGridClicked(row, cell)
- def onClick(self, sender):
- if sender == self.addButton:
- self.gui_eventAddButtonClicked()
- elif sender == self.addNewButton:
- self.gui_eventAddNewButtonClicked()
- elif sender == self.updateButton:
- self.gui_eventUpdateButtonClicked()
- def buildForm(self):
- formGrid = Grid(4,3)
- formGrid.setVisible(False)
- formGrid.setWidget(0, 0, Label("Name"))
- formGrid.setWidget(0, 1, self.nameField);
- formGrid.setWidget(1, 0, Label("email"))
- formGrid.setWidget(1, 1, self.emailField)
- formGrid.setWidget(2, 0, Label("phone"))
- formGrid.setWidget(2, 1, self.phoneField)
- formGrid.setWidget(3, 0, self.updateButton)
- formGrid.setWidget(3, 1, self.addButton)
- self.formGrid = formGrid
- def placeWidgets(self):
- RootPanel(CONTACT_LISTING_ROOT_PANEL).add(self.contactGrid)
- RootPanel(CONTACT_FORM_ROOT_PANEL).add(self.formGrid)
- RootPanel(CONTACT_STATUS_ROOT_PANEL).add(self.status)
- RootPanel(CONTACT_TOOL_BAR_ROOT_PANEL).add(self.addNewButton)
- def loadForm(self, contact):
- self.formGrid.setVisible(True)
- self.currentContact = contact
- self.emailField.setText(contact.email)
- self.phoneField.setText(contact.phone)
- self.nameField.setText(contact.name)
- def copyFieldDateToContact(self):
- self.currentContact.email = self.emailField.getText()
- self.currentContact.name = self.nameField.getText()
- self.currentContact.phone = self.phoneField.getText()
- def gui_eventContactGridClicked(self, row, col):
- contact = self.contacts[row]
- self.status.setText("Name was " + contact.name + " clicked ")
- if col==EDIT_LINK:
- self.addNewButton.setVisible(False)
- self.updateButton.setVisible(True)
- self.addButton.setVisible(False)
- self.loadForm(contact)
- elif (col==REMOVE_LINK):
- self.contactService.removeContact(contact)
- def gui_eventAddButtonClicked(self, sender):
- self.addNewButton.setVisible(True)
- self.formGrid.setVisible(False)
- self.copyFieldDateToContact()
- self.contactService.addContact(self.currentContact)
- def gui_eventUpdateButtonClicked(self, sender):
- self.addNewButton.setVisible(True)
- self.formGrid.setVisible(False)
- self.copyFieldDateToContact()
- self.contactService.updateContact(self.currentContact)
- def gui_eventAddNewButtonClicked(self, sender):
- self.addNewButton.setVisible(False)
- self.updateButton.setVisible(False)
- self.addButton.setVisible(True)
- self.loadForm(Contact())
- def service_eventListRetrievedFromService(self, results):
- self.status.setText("Retrieved contact list")
- self.contacts = results;
- self.contactGrid.clear();
- self.contactGrid.resizeRows(len(self.contacts))
- row = 0
- for contact in results:
- self.contactGrid.setWidget(row, 0, Label(contact.name))
- self.contactGrid.setWidget(row, 1, Label (contact.phone))
- self.contactGrid.setWidget(row, 2, Label (contact.email))
- self.contactGrid.setWidget(row, EDIT_LINK, Hyperlink("Edit", None))
- self.contactGrid.setWidget(row, REMOVE_LINK, Hyperlink("Remove", None))
- row += 1
- def service_eventAddContactSuccessful(self):
- self.status.setText("Contact was successfully added");
- self.contactService.listContacts();
- def service_eventUpdateContactSuccessful(self):
- self.status.setText("Contact was successfully updated");
- self.contactService.listContacts();
- def service_eventRemoveContactSuccessful(self):
- self.status.setText("Contact was removed");
- self.contactService.listContacts();
- if __name__ == '__main__':
- pyjd.setup("public/Contacts.html")
- contacts = ContactListGUI()
- pyjd.run()
Advertisement
Add Comment
Please, Sign In to add comment