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

Source Code for Module pyjamas.ui.StackPanel

  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   
 16  from pyjamas import DOM 
 17  from pyjamas import Factory 
 18   
 19  from ComplexPanel import ComplexPanel 
 20  from pyjamas.ui import Event 
 21   
22 -class StackPanel(ComplexPanel):
23
24 - def __init__(self, **kwargs):
25 self.body = None 26 self.visibleStack = -1 27 self.indices = {} 28 29 if kwargs.has_key('Element'): 30 table = kwargs.pop('Element') 31 fc = DOM.getFirstChild(table) 32 if fc: 33 self.body = fc 34 else: 35 self.body = DOM.createTBody() 36 DOM.appendChild(table, self.body) 37 else: 38 table = DOM.createTable() 39 self.body = DOM.createTBody() 40 DOM.appendChild(table, self.body) 41 42 self.setElement(table) 43 44 if not kwargs.has_key('Spacing'): kwargs['Spacing'] = 0 45 if not kwargs.has_key('Padding'): kwargs['Padding'] = 0 46 if not kwargs.has_key('StyleName'): kwargs['StyleName'] = "gwt-StackPanel" 47 48 DOM.sinkEvents(table, Event.ONCLICK) 49 50 ComplexPanel.__init__(self, **kwargs)
51
52 - def add(self, widget, stackText="", asHTML=False):
53 widget.removeFromParent() 54 index = self.getWidgetCount() 55 56 tr = DOM.createTR() 57 td = DOM.createTD() 58 DOM.appendChild(self.body, tr) 59 DOM.appendChild(tr, td) 60 self.setStyleName(td, "gwt-StackPanelItem", True) 61 self._setIndex(td, index) 62 DOM.setAttribute(td, "height", "1px") 63 64 tr = DOM.createTR() 65 td = DOM.createTD() 66 DOM.appendChild(self.body, tr) 67 DOM.appendChild(tr, td) 68 DOM.setAttribute(td, "height", "100%") 69 DOM.setAttribute(td, "vAlign", "top") 70 71 ComplexPanel.add(self, widget, td) 72 73 self.setStackVisible(index, False) 74 if self.visibleStack == -1: 75 self.showStack(0) 76 77 if stackText != "": 78 self.setStackText(self.getWidgetCount() - 1, stackText, asHTML)
79
80 - def onBrowserEvent(self, event):
81 if DOM.eventGetType(event) == "click": 82 index = self.getDividerIndex(DOM.eventGetTarget(event)) 83 if index != -1: 84 self.showStack(index)
85 86 # also callable as remove(child) and remove(index)
87 - def remove(self, child, index=None):
88 if index is None: 89 if isinstance(child, int): 90 index = child 91 child = self.getWidget(child) 92 else: 93 index = self.getWidgetIndex(child) 94 95 if child.getParent() != self: 96 return False 97 98 if self.visibleStack == index: 99 self.visibleStack = -1 100 elif self.visibleStack > index: 101 self.visibleStack -= 1 102 103 rowIndex = 2 * index 104 tr = DOM.getChild(self.body, rowIndex) 105 DOM.removeChild(self.body, tr) 106 tr = DOM.getChild(self.body, rowIndex) 107 DOM.removeChild(self.body, tr) 108 ComplexPanel.remove(self, child) 109 rows = self.getWidgetCount() * 2 110 111 #for (int i = rowIndex; i < rows; i = i + 2) { 112 for i in range(rowIndex, rows, 2): 113 childTR = DOM.getChild(self.body, i) 114 td = DOM.getFirstChild(childTR) 115 curIndex = self._getIndex(td) 116 self._setIndex(td, index) 117 index += 1 118 119 return True
120
121 - def _setIndex(self, td, index):
122 self.indices[td] = index
123
124 - def _getIndex(self, td):
125 return self.indices.get(td)
126
127 - def setStackText(self, index, text, asHTML=False):
128 if index >= self.getWidgetCount(): 129 return 130 131 td = DOM.getChild(DOM.getChild(self.body, index * 2), 0) 132 if asHTML: 133 DOM.setInnerHTML(td, text) 134 else: 135 DOM.setInnerText(td, text)
136
137 - def showStack(self, index):
138 if (index >= self.getWidgetCount()) or (index == self.visibleStack): 139 return 140 141 if self.visibleStack >= 0: 142 self.setStackVisible(self.visibleStack, False) 143 144 self.visibleStack = index 145 self.setStackVisible(self.visibleStack, True)
146
147 - def getDividerIndex(self, elem):
148 while (elem is not None) and not DOM.compare(elem, self.getElement()): 149 expando = self._getIndex(elem) 150 if expando is not None: 151 return int(expando) 152 153 elem = DOM.getParent(elem) 154 155 return -1
156
157 - def setStackVisible(self, index, visible):
158 tr = DOM.getChild(self.body, (index * 2)) 159 if tr is None: 160 return 161 162 td = DOM.getFirstChild(tr) 163 self.setStyleName(td, "gwt-StackPanelItem-selected", visible) 164 165 tr = DOM.getChild(self.body, (index * 2) + 1) 166 self.setVisible(tr, visible) 167 self.getWidget(index).setVisible(visible)
168
169 - def getSelectedIndex(self):
170 return self.visibleStack
171 172 Factory.registerClass('pyjamas.ui.StackPanel', StackPanel) 173