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