PageRenderTime 335ms CodeModel.GetById 161ms app.highlight 18ms RepoModel.GetById 153ms app.codeStats 0ms

/examples/mail/MailList.py

http://pyjamas.googlecode.com/
Python | 155 lines | 133 code | 14 blank | 8 comment | 5 complexity | 53f217e829ca7c6ed9e87c19437bbf03 MD5 | raw file
  1from ui import ClickListener, Composite, FlexTable, HTML, HorizontalPanel, SourcesTableEvents, TableListener, Widget, Label, HasAlignment
  2from MailItems import MailItems
  3from Logger import Logger
  4
  5class MailList(Composite):
  6
  7    VISIBLE_EMAIL_COUNT = 10
  8
  9    def __init__(self, mailObject):
 10        self.countLabel = HTML()
 11        self.newerButton = HTML("<a href='javascript:;'>&lt; newer</a>", True)
 12        self.olderButton = HTML("<a href='javascript:;'>older &gt;</a>", True)
 13        self.startIndex = 0
 14        self.selectedRow = -1
 15        self.table = FlexTable()
 16        self.navBar = HorizontalPanel()
 17        self.mailObject = mailObject
 18
 19        # Setup the table.
 20        self.table.setCellSpacing(0)
 21        self.table.setCellPadding(2)
 22        self.table.setWidth("100%")
 23
 24        # Hook up events.
 25        self.table.addTableListener(self)
 26        self.newerButton.addClickListener(self)
 27        self.olderButton.addClickListener(self)
 28
 29        # Create the 'navigation' bar at the upper-right.
 30        innerNavBar = HorizontalPanel()
 31        innerNavBar.setStyleName("mail-ListNavBar")
 32        innerNavBar.setSpacing(8)
 33        innerNavBar.add(self.newerButton)
 34        innerNavBar.add(self.countLabel)
 35        innerNavBar.add(self.olderButton)
 36
 37        self.navBar.setHorizontalAlignment(HasAlignment.ALIGN_RIGHT)
 38        self.navBar.add(innerNavBar)
 39        self.navBar.setWidth("100%")
 40
 41        self.initWidget(self.table)
 42        self.setStyleName("mail-List")
 43
 44        self.initTable()
 45        self.update()
 46
 47    def onCellClicked(self, sender, row, cell):
 48        # Select the row that was clicked (-1 to account for header row).
 49        if (row > 0):
 50            self.selectRow(row - 1)
 51
 52    def onClick(self, sender):
 53        if (sender == self.olderButton):
 54            # Move forward a page.
 55            self.startIndex = self.startIndex + MailList.VISIBLE_EMAIL_COUNT
 56            if (self.startIndex >= MailItems().getMailItemCount()):
 57                self.startIndex = self.startIndex - MailList.VISIBLE_EMAIL_COUNT
 58            else:
 59                self.styleRow(self.selectedRow, False)
 60                self.selectedRow = -1
 61                self.update()
 62    
 63        elif (sender == self.newerButton):
 64            # Move back a page.
 65            self.startIndex = self.startIndex - MailList.VISIBLE_EMAIL_COUNT
 66            if (self.startIndex < 0):
 67                self.startIndex = 0
 68            else:
 69                self.styleRow(self.selectedRow, False)
 70                self.selectedRow = -1
 71                self.update()
 72
 73    def initTable(self):
 74        # Create the header row.
 75        self.table.setText(0, 0, "sender")
 76        self.table.setText(0, 1, "email")
 77        self.table.setText(0, 2, "subject")
 78        self.table.setWidget(0, 3, self.navBar)
 79        self.table.getRowFormatter().setStyleName(0, "mail-ListHeader")
 80
 81        # Initialize the rest of the rows.
 82        i = 0
 83        while i < MailList.VISIBLE_EMAIL_COUNT:
 84            self.table.setText(i + 1, 0, "")
 85            self.table.setText(i + 1, 1, "")
 86            self.table.setText(i + 1, 2, "")
 87            self.table.getCellFormatter().setWordWrap(i + 1, 0, False)
 88            self.table.getCellFormatter().setWordWrap(i + 1, 1, False)
 89            self.table.getCellFormatter().setWordWrap(i + 1, 2, False)
 90            self.table.getFlexCellFormatter().setColSpan(i + 1, 2, 2)
 91            i = i + 1
 92
 93
 94    def selectRow(self, row):
 95        # When a row (other than the first one, which is used as a header) is
 96        # selected, display its associated MailItem.
 97        item = MailItems().getMailItem(self.startIndex + row)
 98        if (item == None):
 99            return
100                    
101        self.styleRow(self.selectedRow, False)
102        self.styleRow(row, True)
103
104        item.read = True
105        self.selectedRow = row
106        self.mailObject.get().displayItem(item)
107
108
109    def styleRow(self, row, selected):
110        if (row != -1):
111            if (selected):
112                self.table.getRowFormatter().addStyleName(row + 1, "mail-SelectedRow")
113            else:
114                self.table.getRowFormatter().removeStyleName(row + 1, "mail-SelectedRow")
115
116    def update(self):
117        # Update the older/newer buttons & label.
118        count = MailItems().getMailItemCount()
119        max = self.startIndex + MailList.VISIBLE_EMAIL_COUNT
120        if (max > count):
121            max = count
122
123        self.newerButton.setVisible(self.startIndex != 0)
124        self.olderButton.setVisible(self.startIndex + MailList.VISIBLE_EMAIL_COUNT < count)
125        startIndexPlusOne = self.startIndex + 1
126        self.countLabel.setText("" + startIndexPlusOne + " - " + max + " of " + count)
127
128        # Show the selected emails.
129        i = 0
130        while (i < MailList.VISIBLE_EMAIL_COUNT):
131            # Don't read past the end.
132            if (self.startIndex + i >= MailItems().getMailItemCount()):
133                break
134
135            item = MailItems().getMailItem(self.startIndex + i)
136
137            # Add a row to the table, then set each of its columns to the
138            # email's sender and subject values.
139
140            self.table.setText(i + 1, 0, item.sender)
141            self.table.setText(i + 1, 1, item.email)
142            self.table.setText(i + 1, 2, item.subject)
143
144            i = i + 1
145
146        # Clear any remaining slots.
147        while (i < MailList.VISIBLE_EMAIL_COUNT):
148            self.table.setHTML(i + 1, 0, "&nbsp;")
149            self.table.setHTML(i + 1, 1, "&nbsp;")
150            self.table.setHTML(i + 1, 2, "&nbsp;")
151            i = i + 1
152
153        # Select the first row if none is selected.
154        if (self.selectedRow == -1):
155            self.selectRow(0)