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

Source Code for Module library.pyjamas.ui.DockPanel

  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 Factory 
 16  from pyjamas import DOM 
 17   
 18  from pyjamas.ui.CellPanel import CellPanel 
 19  from pyjamas.ui import HasHorizontalAlignment 
 20  from pyjamas.ui import HasVerticalAlignment 
 21   
22 -class DockPanelTmpRow:
23 center = 0 24 tr = None
25
26 -class LayoutData:
27 - def __init__(self, direction):
28 self.direction = direction 29 self.hAlign = "left" 30 self.height = "" 31 self.td = None 32 self.vAlign = "top" 33 self.width = ""
34 35
36 -class DockPanel(CellPanel):
37 38 CENTER = "center" 39 EAST = "east" 40 NORTH = "north" 41 SOUTH = "south" 42 WEST = "west" 43 44 elem_props = [ 45 ("height", "Cell Height", "CellHeight", str, None), 46 ("width", "Cell Width", "CellWidth", str, None), 47 ("halign", "Cell Horizontal Alignment", 48 "CellHorizontalAlignment", None, "left"), 49 ("valign", "Cell Vertical Alignment", 50 "CellVerticalAlignment", None, "top"), 51 ] 52
53 - def _getElementProps(self):
55
56 - def __init__(self, **kwargs):
57 58 self.center = None 59 self.dock_children = [] # cannot use self.children 60 61 CellPanel.__init__(self, **kwargs)
62
63 - def add(self, widget, direction):
64 if direction == self.CENTER: 65 if self.center is not None: 66 raise Exception("Only one CENTER widget may be added") 67 self.center = widget 68 69 layout = LayoutData(direction) 70 widget.setLayoutData(layout) 71 self.setCellHorizontalAlignment(widget, self.horzAlign) 72 self.setCellVerticalAlignment(widget, self.vertAlign) 73 74 self.dock_children.append(widget) 75 self.realizeTable(widget)
76 77 # next three functions are part of the standard Builder API for panels
78 - def addIndexedItem(self, index, item):
79 self.add(item, index[1])
80
81 - def getWidgetIndex(self, widget):
82 index = self.dock_children.index(widget) 83 direction = self.getWidgetDirection(widget) 84 return (index, direction)
85
86 - def getIndexedChild(self, index):
87 index, direction = index 88 return self.dock_children[index]
89
90 - def getWidgetDirection(self, widget):
91 if widget.getParent() != self: 92 return None 93 return widget.getLayoutData().direction
94
95 - def __len__(self):
96 return len(self.dock_children)
97
98 - def __iter__(self):
99 return self.dock_children.__iter__()
100
101 - def remove(self, widget):
102 if widget == self.center: 103 self.center = None 104 105 ret = CellPanel.remove(self, widget) 106 if ret: 107 self.dock_children.remove(widget) 108 self.realizeTable(None) 109 return ret
110
111 - def setCellHeight(self, widget, height):
112 data = widget.getLayoutData() 113 data.height = height 114 if data.td is not None: 115 DOM.setStyleAttribute(data.td, "height", data.height)
116
117 - def setCellHorizontalAlignment(self, widget, align):
118 data = widget.getLayoutData() 119 data.hAlign = align 120 if data.td is not None: 121 DOM.setAttribute(data.td, "align", data.hAlign)
122
123 - def setCellVerticalAlignment(self, widget, align):
124 data = widget.getLayoutData() 125 data.vAlign = align 126 if data.td is not None: 127 DOM.setStyleAttribute(data.td, "verticalAlign", data.vAlign)
128
129 - def setCellWidth(self, widget, width):
130 data = widget.getLayoutData() 131 data.width = width 132 if data.td is not None: 133 DOM.setStyleAttribute(data.td, "width", data.width)
134
135 - def realizeTable(self, beingAdded):
136 bodyElement = self.getBody() 137 138 while DOM.getChildCount(bodyElement) > 0: 139 DOM.removeChild(bodyElement, DOM.getChild(bodyElement, 0)) 140 141 rowCount = 1 142 colCount = 1 143 for child in self.dock_children: 144 dir = child.getLayoutData().direction 145 if dir == self.NORTH or dir == self.SOUTH: 146 rowCount += 1 147 elif dir == self.EAST or dir == self.WEST: 148 colCount += 1 149 150 rows = [] 151 for i in range(rowCount): 152 rows.append(DockPanelTmpRow()) 153 rows[i].tr = DOM.createTR() 154 DOM.appendChild(bodyElement, rows[i].tr) 155 156 westCol = 0 157 eastCol = colCount - 1 158 northRow = 0 159 southRow = rowCount - 1 160 centerTd = None 161 162 for child in self.dock_children: 163 layout = child.getLayoutData() 164 165 td = DOM.createTD() 166 layout.td = td 167 DOM.setAttribute(layout.td, "align", layout.hAlign) 168 DOM.setStyleAttribute(layout.td, "verticalAlign", layout.vAlign) 169 DOM.setAttribute(layout.td, "width", layout.width) 170 DOM.setAttribute(layout.td, "height", layout.height) 171 172 if layout.direction == self.NORTH: 173 DOM.insertChild(rows[northRow].tr, td, rows[northRow].center) 174 self.appendAndMaybeAdopt(td, child.getElement(), beingAdded) 175 DOM.setIntAttribute(td, "colSpan", eastCol - westCol + 1) 176 northRow += 1 177 elif layout.direction == self.SOUTH: 178 DOM.insertChild(rows[southRow].tr, td, rows[southRow].center) 179 self.appendAndMaybeAdopt(td, child.getElement(), beingAdded) 180 DOM.setIntAttribute(td, "colSpan", eastCol - westCol + 1) 181 southRow -= 1 182 elif layout.direction == self.WEST: 183 row = rows[northRow] 184 DOM.insertChild(row.tr, td, row.center) 185 row.center += 1 186 self.appendAndMaybeAdopt(td, child.getElement(), beingAdded) 187 DOM.setIntAttribute(td, "rowSpan", southRow - northRow + 1) 188 westCol += 1 189 elif layout.direction == self.EAST: 190 row = rows[northRow] 191 DOM.insertChild(row.tr, td, row.center) 192 self.appendAndMaybeAdopt(td, child.getElement(), beingAdded) 193 DOM.setIntAttribute(td, "rowSpan", southRow - northRow + 1) 194 eastCol -= 1 195 elif layout.direction == self.CENTER: 196 centerTd = td 197 198 if self.center is not None: 199 row = rows[northRow] 200 DOM.insertChild(row.tr, centerTd, row.center) 201 self.appendAndMaybeAdopt(centerTd, self.center.getElement(), beingAdded)
202
203 - def appendAndMaybeAdopt(self, parent, child, beingAdded):
204 if beingAdded is not None: 205 if DOM.compare(child, beingAdded.getElement()): 206 CellPanel.add(self, beingAdded, parent) 207 return 208 DOM.appendChild(parent, child)
209 210 Factory.registerClass('pyjamas.ui.DockPanel', 'DockPanel', DockPanel) 211