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

Source Code for Module library.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 pyjamas.ui.Composite import Composite 
 20  from pyjamas.ui.DeckPanel import DeckPanel 
 21  from pyjamas.ui.VerticalPanel import VerticalPanel 
 22  from pyjamas.ui.Panel import PanelBase 
 23  from pyjamas.ui.TabBar import TabBar 
 24   
25 -class TabPanel(PanelBase, Composite):
26 - def __init__(self, tabBar=None, **kwargs):
27 self.children = [] # TODO: can self.children be used instead? 28 self.tab_names = {} 29 self.deck = kwargs.pop('Deck', None) 30 floatingtab = kwargs.pop('FloatingTab', False) 31 if self.deck is None: 32 self.deck = DeckPanel(StyleName="gwt-TabPanelBottom") 33 if tabBar is None: 34 self.tabBar = TabBar() 35 else: 36 self.tabBar = tabBar 37 self.tabListeners = [] 38 39 # this is awkward: VerticalPanel is the composite, 40 # so we get the element here, and pass it in to VerticalPanel. 41 element = kwargs.pop('Element', None) 42 43 panel = VerticalPanel(Element=element) 44 if not floatingtab: 45 panel.add(self.tabBar) 46 if self.deck.getParent() is None: 47 panel.add(self.deck) 48 panel.setCellHeight(self.deck, "100%") 49 self.tabBar.setWidth("100%") 50 self.tabBar.addTabListener(self) 51 52 kwargs['StyleName'] = kwargs.get('StyleName', "gwt-TabPanel") 53 54 PanelBase.__init__(self) 55 Composite.__init__(self, panel, **kwargs)
56
57 - def add(self, widget, tabText=None, asHTML=False, name=None):
58 """ tabText=None now means insert a spacer, pushed out at 100% 59 width so that any subsequent tabs added will be pushed to 60 the right hand side. 61 62 name refers to an optional name (string) where the tab can 63 be removed by name, if desired, using TabBar.remove. 64 """ 65 self.insert(widget, tabText, asHTML, self.getWidgetCount(), name)
66
67 - def addTabListener(self, listener):
68 self.tabListeners.append(listener)
69
70 - def getDeckPanel(self):
71 return self.deck
72
73 - def getTabBar(self):
74 return self.tabBar
75
76 - def insert(self, widget, tabText, asHTML=False, beforeIndex=None, 77 name=None):
78 if beforeIndex is None: 79 beforeIndex = asHTML 80 asHTML = False 81 82 self.children.insert(beforeIndex, widget) 83 if name is not None: 84 self.tab_names[name] = widget 85 self.tabBar.insertTab(tabText, asHTML, beforeIndex) 86 self.deck.insert(widget, beforeIndex)
87
88 - def onBeforeTabSelected(self, sender, tabIndex):
89 for listener in self.tabListeners: 90 if not listener.onBeforeTabSelected(sender, tabIndex): 91 return False 92 return True
93
94 - def onTabSelected(self, sender, tabIndex):
95 self.deck.showWidget(tabIndex) 96 for listener in self.tabListeners: 97 listener.onTabSelected(sender, tabIndex)
98
99 - def remove(self, widget):
100 """ widget to remove can be by integer, a widget in the panel, 101 or by name (string). if by name, the name has to be one 102 which was given to add or insert 103 """ 104 if isinstance(widget, basestring): 105 widget = self.tab_names[widget] 106 elif isinstance(widget, int): 107 widget = self.getWidget(widget) 108 109 for (k, w) in self.tab_names.items(): 110 if widget == w: 111 self.tab_names.pop(k) 112 break 113 114 index = self.getWidgetIndex(widget) 115 if index == -1: 116 return False 117 118 self.children.remove(widget) 119 self.tabBar.removeTab(index) 120 self.deck.remove(widget) 121 return True
122
123 - def removeTabListener(self, listener):
124 self.tabListeners.remove(listener)
125
126 - def selectTab(self, index):
127 """ manual tab selection. tab can be selected: 128 * by index, 129 * by tab name (matching TabPanel.insert name arg if given) 130 * or by widget. 131 """ 132 if isinstance(index, basestring): 133 index = self.tab_names[index] 134 if not isinstance(index, int): 135 index = self.getWidgetIndex(index) 136 137 self.tabBar.selectTab(index)
138 139 Factory.registerClass('pyjamas.ui.TabPanel', 'TabPanel', TabPanel) 140