Package pyjamas :: Package ui :: Module Grid
[hide private]
[frames] | no frames]

Source Code for Module pyjamas.ui.Grid

  1  # Copyright 2006 James Tauber and contributors 
  2  # Copyright (C) 2009 Luke Kenneth Casson Leighton <lkcl@lkcl.net> 
  3  # 
  4  # Licensed under the Apache License, Version 2.0 (the "License"); 
  5  # you may not use this file except in compliance with the License. 
  6  # You may obtain a copy of the License at 
  7  # 
  8  #     http://www.apache.org/licenses/LICENSE-2.0 
  9  # 
 10  # Unless required by applicable law or agreed to in writing, software 
 11  # distributed under the License is distributed on an "AS IS" BASIS, 
 12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 13  # See the License for the specific language governing permissions and 
 14  # limitations under the License. 
 15  from pyjamas import DOM 
 16  from pyjamas import Factory 
 17   
 18  from HTMLTable import HTMLTable 
 19  from CellFormatter import CellFormatter 
 20  from RowFormatter import RowFormatter 
 21   
22 -class Grid(HTMLTable):
23
24 - def __init__(self, rows=0, columns=0, **kwargs):
25 self.numColumns = 0 26 self.numRows = 0 27 HTMLTable.__init__(self, **kwargs) 28 if rows > 0 or columns > 0: 29 self.resize(rows, columns)
30
31 - def removeRow(self, row):
32 HTMLTable.removeRow(self, row) 33 self.numRows -= 1
34
35 - def resize(self, rows, columns):
36 self.resizeColumns(columns) 37 self.resizeRows(rows)
38
39 - def resizeColumns(self, columns):
40 if self.numColumns == columns: 41 return 42 43 if self.numColumns > columns: 44 for i in range(0, self.numRows): 45 for j in range(self.numColumns - 1, columns - 1, -1): 46 self.removeCell(i, j) 47 else: 48 for i in range(self.numRows): 49 for j in range(self.numColumns, columns): 50 self.insertCell(i, j) 51 self.numColumns = columns
52
53 - def resizeRows(self, rows):
54 if self.numRows == rows: 55 return 56 57 if self.numRows < rows: 58 self.addRows(self.getBodyElement(), rows - self.numRows, self.numColumns) 59 self.numRows = rows 60 else: 61 while self.numRows > rows: 62 self.removeRow(self.numRows - 1)
63
64 - def createCell(self):
65 td = HTMLTable.createCell(self) 66 DOM.setInnerHTML(td, "&nbsp;") 67 return td
68
69 - def clearCell(self, row, column):
70 td = self.cellFormatter.getElement(row, column) 71 b = HTMLTable.internalClearCell(self, td) 72 DOM.setInnerHTML(td, "&nbsp;") 73 return b
74
75 - def prepareCell(self, row, column):
76 pass
77
78 - def prepareRow(self, row):
79 pass
80
81 - def getCellCount(self, row):
82 return self.numColumns
83
84 - def getColumnCount(self):
85 return self.numColumns
86
87 - def getRowCount(self):
88 return self.numRows
89
90 - def addRows(self, table, numRows, columns):
91 td = DOM.createElement("td") 92 DOM.setInnerHTML(td, "&nbsp;") 93 row = DOM.createElement("tr") 94 for cellNum in range(columns): 95 cell = td.cloneNode(True) 96 row.appendChild(cell) 97 for rowNum in range(numRows): 98 table.appendChild(row.cloneNode(True))
99 100 Factory.registerClass('pyjamas.ui.Grid', Grid) 101