PageRenderTime 12ms CodeModel.GetById 8ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/mail/Contacts.py

http://pyjamas.googlecode.com/
Python | 76 lines | 57 code | 15 blank | 4 comment | 2 complexity | be8f4d774fb49da5be39cb7198e08f1e MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. from ui import ClickListener, Composite, HTML, HorizontalPanel, Image, PopupPanel, VerticalPanel, Widget, Label
  2. from Logger import Logger
  3. class Contact:
  4. def __init__(self, name, email):
  5. self.photo = "http://code.google.com/webtoolkit/documentation/examples/desktopclone/default_photo.jpg"
  6. self.name = name
  7. self.email = email
  8. class ContactPopup(PopupPanel):
  9. def __init__(self, contact):
  10. # The popup's constructor's argument is a boolean specifying that it
  11. # auto-close itself when the user clicks outside of it.
  12. PopupPanel.__init__(self, True)
  13. inner = VerticalPanel()
  14. nameLabel = Label(contact.name)
  15. emailLabel = Label(contact.email)
  16. inner.add(nameLabel)
  17. inner.add(emailLabel)
  18. panel = HorizontalPanel()
  19. panel.setSpacing(4)
  20. panel.add(Image(contact.photo))
  21. panel.add(inner)
  22. self.add(panel)
  23. self.setStyleName("mail-ContactPopup")
  24. nameLabel.setStyleName("mail-ContactPopupName")
  25. emailLabel.setStyleName("mail-ContactPopupEmail")
  26. class Contacts(Composite):
  27. def __init__(self):
  28. self.contacts = []
  29. self.contacts.append(Contact("Benoit Mandelbrot", "benoit@example.com"))
  30. self.contacts.append(Contact("Albert Einstein", "albert@example.com"))
  31. self.contacts.append(Contact("Rene Descartes", "rene@example.com"))
  32. self.contacts.append(Contact("Bob Saget", "bob@example.com"))
  33. self.contacts.append(Contact("Ludwig von Beethoven", "ludwig@example.com"))
  34. self.contacts.append(Contact("Richard Feynman", "richard@example.com"))
  35. self.contacts.append(Contact("Alan Turing", "alan@example.com"))
  36. self.contacts.append(Contact("John von Neumann", "john@example.com"))
  37. self.panel = VerticalPanel()
  38. # Add all the contacts to the list.
  39. i = 0
  40. while (i < len(self.contacts)):
  41. self.addContact(self.contacts[i])
  42. i = i + 1
  43. self.initWidget(self.panel)
  44. self.setStyleName("mail-Contacts")
  45. def addContact(self, contact):
  46. link = HTML("<a href='javascript:;'>" + contact.name + "</a>")
  47. self.panel.add(link)
  48. # Add a click listener that displays a ContactPopup when it is clicked.
  49. listener = ContactListener(contact, link)
  50. link.addClickListener(listener)
  51. class ContactListener:
  52. def __init__(self, contact, link):
  53. self.cont = contact
  54. self.link = link
  55. def onClick(self, sender):
  56. if (sender == self.link):
  57. popup = ContactPopup(self.cont)
  58. left = self.link.getAbsoluteLeft() + 32
  59. top = self.link.getAbsoluteTop() + 8
  60. popup.setPopupPosition(left, top)
  61. popup.show()