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

Source Code for Module library.pyjamas.ui.StackPanel

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