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

Source Code for Module pyjamas.ui.TabPanel

  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  from __pyjamas__ import console 
 18   
 19  from Composite import Composite 
 20  from DeckPanel import DeckPanel 
 21  from VerticalPanel import VerticalPanel 
 22  from TabBar import TabBar 
 23   
24 -class TabPanel(Composite):
25 - def __init__(self, tabBar=None, **kwargs):
26 self.tab_children = [] # TODO: can self.children be used instead? 27 self.deck = DeckPanel(StyleName="gwt-TabPanelBottom") 28 if tabBar is None: 29 self.tabBar = TabBar() 30 else: 31 self.tabBar = tabBar 32 self.tabListeners = [] 33 34 # this is awkward: VerticalPanel is the composite, 35 # so we get the element here, and pass it in to VerticalPanel. 36 element = None 37 if kwargs.has_key('Element'): 38 element = kwargs.pop('Element') 39 40 panel = VerticalPanel(Element=element) 41 panel.add(self.tabBar) 42 panel.add(self.deck) 43 44 panel.setCellHeight(self.deck, "100%") 45 self.tabBar.setWidth("100%") 46 self.tabBar.addTabListener(self) 47 48 if not kwargs.has_key('StyleName'): kwargs['StyleName']="gwt-TabPanel" 49 50 Composite.__init__(self, panel, **kwargs)
51
52 - def add(self, widget, tabText=None, asHTML=False):
53 """ tabText=None now means insert a spacer, pushed out at 100% 54 width so that any subsequent tabs added will be pushed to 55 the right hand side 56 """ 57 self.insert(widget, tabText, asHTML, self.getWidgetCount())
58
59 - def addTabListener(self, listener):
60 self.tabListeners.append(listener)
61
62 - def clear(self):
63 while self.getWidgetCount() > 0: 64 self.remove(self.getWidget(0))
65
66 - def getDeckPanel(self):
67 return self.deck
68
69 - def getTabBar(self):
70 return self.tabBar
71
72 - def getWidget(self, index):
73 return self.tab_children[index]
74
75 - def getWidgetCount(self):
76 return len(self.tab_children)
77
78 - def getWidgetIndex(self, child):
79 return self.tab_children.index(child)
80
81 - def insert(self, widget, tabText, asHTML=False, beforeIndex=None):
82 if beforeIndex is None: 83 beforeIndex = asHTML 84 asHTML = False 85 86 self.tab_children.insert(beforeIndex, widget) 87 self.tabBar.insertTab(tabText, asHTML, beforeIndex) 88 self.deck.insert(widget, beforeIndex)
89
90 - def __iter__(self):
91 return self.tab_children.__iter__()
92
93 - def onBeforeTabSelected(self, sender, tabIndex):
94 for listener in self.tabListeners: 95 if not listener.onBeforeTabSelected(sender, tabIndex): 96 return False 97 return True
98
99 - def onTabSelected(self, sender, tabIndex):
100 self.deck.showWidget(tabIndex) 101 for listener in self.tabListeners: 102 listener.onTabSelected(sender, tabIndex)
103
104 - def remove(self, widget):
105 if isinstance(widget, int): 106 widget = self.getWidget(widget) 107 108 index = self.getWidgetIndex(widget) 109 if index == -1: 110 return False 111 112 self.tab_children.remove(widget) 113 self.tabBar.removeTab(index) 114 self.deck.remove(widget) 115 return True
116
117 - def removeTabListener(self, listener):
118 self.tabListeners.remove(listener)
119
120 - def selectTab(self, index):
121 self.tabBar.selectTab(index)
122 123 Factory.registerClass('pyjamas.ui.TabPanel', TabPanel) 124