PageRenderTime 34ms CodeModel.GetById 15ms app.highlight 15ms RepoModel.GetById 2ms app.codeStats 0ms

/examples/dynamictable/DynaTableWidget.py

http://pyjamas.googlecode.com/
Python | 139 lines | 103 code | 36 blank | 0 comment | 12 complexity | 9cc39e3449ff1ee78ac2d237d4fbf1c5 MD5 | raw file
  1from ui import Composite, DockPanel, HTML, HorizontalPanel, DockPanel, HasAlignment, Button, Grid
  2
  3class NavBar(Composite):
  4
  5    def __init__(self, owner):
  6        self.owner = owner
  7        self.bar = DockPanel()
  8        self.gotoFirst = Button("<<", self)
  9        self.gotoNext = Button(">", self)
 10        self.gotoPrev = Button("<", self)
 11        self.status = HTML()
 12        
 13        self.initWidget(self.bar)
 14        self.bar.setStyleName("navbar")
 15        self.status.setStyleName("status")
 16        
 17        buttons = HorizontalPanel()
 18        buttons.add(self.gotoFirst)
 19        buttons.add(self.gotoPrev)
 20        buttons.add(self.gotoNext)
 21        self.bar.add(buttons, DockPanel.EAST)
 22        self.bar.setCellHorizontalAlignment(buttons, HasAlignment.ALIGN_RIGHT)
 23        self.bar.add(self.status, DockPanel.CENTER)
 24        self.bar.setVerticalAlignment(HasAlignment.ALIGN_MIDDLE)
 25        self.bar.setCellHorizontalAlignment(self.status, HasAlignment.ALIGN_RIGHT)
 26        self.bar.setCellVerticalAlignment(self.status, HasAlignment.ALIGN_MIDDLE)
 27        self.bar.setCellWidth(self.status, "100%")
 28        
 29        self.gotoPrev.setEnabled(False)
 30        self.gotoFirst.setEnabled(False)
 31        
 32    def onClick(self, sender):
 33        if sender == self.gotoNext:
 34            self.owner.startRow += self.owner.getDataRowCount()
 35            self.owner.refresh()
 36        elif sender == self.gotoPrev:
 37            self.owner.startRow -= self.owner.getDataRowCount()
 38            if self.owner.startRow < 0:
 39                self.owner.startRow = 0
 40            self.owner.refresh()
 41        elif sender == self.gotoFirst:
 42            self.owner.startRow = 0
 43            self.owner.refresh()
 44
 45
 46class RowDataAcceptorImpl:
 47
 48    def __init__(self, owner):
 49        self.owner = owner
 50
 51    def accept(self, startRow, data):
 52        destRowCount = self.owner.getDataRowCount()
 53        destColCount = self.owner.grid.getCellCount(0)
 54        
 55        srcRowIndex = 0
 56        srcRowCount = len(data)
 57        destRowIndex = 1
 58        
 59        while srcRowIndex < srcRowCount:
 60            
 61            srcRowData = data[srcRowIndex]
 62            
 63            for srcColIndex in range(destColCount):
 64                cellHTML = srcRowData[srcColIndex]
 65                self.owner.grid.setText(destRowIndex, srcColIndex, cellHTML)
 66            
 67            srcRowIndex += 1
 68            destRowIndex += 1
 69        
 70        isLastPage = False
 71        
 72        while destRowIndex < destRowCount + 1:
 73        
 74            isLastPage = True
 75            
 76            for destColIndex in range(destColCount):
 77                self.owner.grid.clearCell(destRowIndex, destColIndex)
 78        
 79            destRowIndex += 1
 80            
 81        self.owner.navbar.gotoNext.setEnabled(not isLastPage)
 82        self.owner.navbar.gotoFirst.setEnabled(startRow > 0)
 83        self.owner.navbar.gotoPrev.setEnabled(startRow > 0)
 84        
 85        self.owner.setStatusText(str(startRow + 1) + " - " + str(startRow + srcRowCount))
 86    
 87    def failed(self, message): 
 88        msg = "Failed to access data"
 89        if message:
 90            msg += ": " + message
 91        self.owner.setStatusText(msg)
 92
 93
 94
 95class DynaTableWidget(Composite):
 96
 97    def __init__(self, provider, columns, columnStyles, rowCount):
 98        Composite.__init__(self)
 99    
100        self.acceptor = RowDataAcceptorImpl(self)
101        self.outer = DockPanel()
102        self.startRow = 0
103        self.grid = Grid()
104        self.navbar = NavBar(self)
105        
106        self.provider = provider
107        self.initWidget(self.outer)
108        self.grid.setStyleName("table")
109        self.outer.add(self.navbar, DockPanel.NORTH)
110        self.outer.add(self.grid, DockPanel.CENTER)
111        self.initTable(columns, columnStyles, rowCount)
112        self.setStyleName("DynaTable-DynaTableWidget")
113        
114    def initTable(self, columns, columnStyles, rowCount):
115        self.grid.resize(rowCount + 1, len(columns))
116        for i in range(len(columns)):
117            self.grid.setText(0, i, columns[i])
118            if columnStyles:
119                self.grid.cellFormatter.setStyleName(0, i, columnStyles[i] + "header")
120
121    def setStatusText(self, text):
122        self.navbar.status.setText(text)
123        
124    def clearStatusText(self, text):
125        self.navbar.status.setHTML("&nbsp;")
126        
127    def refresh(self):
128        self.navbar.gotoFirst.setEnabled(False)
129        self.navbar.gotoPrev.setEnabled(False)
130        self.navbar.gotoNext.setEnabled(False)
131        
132        self.setStatusText("Please wait...")
133        self.provider.updateRowData(self.startRow, self.grid.getRowCount() - 1, self.acceptor)
134        
135    def setRowCount(self, rows):
136        self.grid.resizeRows(rows)
137        
138    def getDataRowCount(self):
139        return self.grid.getRowCount() - 1