PageRenderTime 13ms CodeModel.GetById 2ms app.highlight 8ms RepoModel.GetById 2ms app.codeStats 0ms

/examples/dynamictable/SchoolCalendarWidget.py

http://pyjamas.googlecode.com/
Python | 84 lines | 63 code | 21 blank | 0 comment | 7 complexity | 7854799a385ef9bc518c8f9164352acd MD5 | raw file
 1from ui import Composite
 2from DeferredCommand import DeferredCommand
 3from SchoolCalendarService import SchoolCalendarService
 4from DynaTableWidget import DynaTableWidget
 5from Person import Person
 6from Student import Student
 7from Professor import Professor
 8
 9class CalendarProvider:
10
11    def __init__(self, owner):
12        self.owner = owner
13        self.calService = SchoolCalendarService()
14        self.lastStartRow = -1
15        self.lastMaxRows = -1
16        self.lastPeople = []
17        
18    def updateRowData(self, startRow, maxRows, acceptor):
19        if startRow == self.lastStartRow:
20            if maxRows == self.lastMaxRows:
21                self.pushResults(acceptor, startRow, self.lastPeople)
22                return
23        
24        self.calService.getPeople(startRow, maxRows, CalendarProviderHandler(self, acceptor, startRow, maxRows))
25
26    def pushResults(self, acceptor, startRow, people):
27        rows = []
28        for person in people:
29            rows.append([person.getName(), person.getDescription(), person.getSchedule(self.owner.daysFilter)])
30        acceptor.accept(startRow, rows)
31
32
33class CalendarProviderHandler:
34    def __init__(self, owner, acceptor, startRow, maxRows):
35        self.owner = owner
36        self.acceptor = acceptor
37        self.startRow = startRow
38        self.maxRows = maxRows
39    
40    def onRemoteResponse(self, response, requestInfo):
41        people = response
42        
43        self.owner.lastStartRow = self.startRow
44        self.owner.lastMaxRows = self.maxRows
45        self.owner.lastPeople = people
46        self.owner.pushResults(self.acceptor, self.startRow, people)
47        
48    def onRemoteError(self, code, message, request):
49        self.acceptor.failed(message)
50    
51
52class SchoolCalendarWidget(Composite):
53    
54    def __init__(self, visibleRows):
55        Composite.__init__(self)
56    
57        columns = ["Name", "Description", "Schedule"]
58        styles = ["name", "desc", "sched"]
59        self.calProvider = CalendarProvider(self)
60        self.daysFilter = [True, True, True, True, True, True, True]
61        self.pendingRefresh = False
62
63        self.dynaTable = DynaTableWidget(self.calProvider, columns, styles, visibleRows)
64        self.initWidget(self.dynaTable)
65        
66    def getDayIncluded(self, day):
67        return self.daysFilter[day]
68        
69    def onLoad(self):
70        self.dynaTable.refresh()
71
72    def setDayIncluded(self, day, included):
73        if (self.daysFilter[day] == included):
74            return
75
76        self.daysFilter[day] = included
77        
78        if not self.pendingRefresh:
79            self.pendingRefresh = True
80            DeferredCommand().add(self)
81
82    def execute(self):
83        self.pendingRefresh = False
84        self.dynaTable.refresh()