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

Source Code for Module pyjamas.ui.TabBar

  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.ui.Composite import Composite 
 18  from pyjamas.ui import Event 
 19  from pyjamas.ui.HTML import HTML 
 20  from pyjamas.ui.Label import Label 
 21  from pyjamas.ui.HorizontalPanel import HorizontalPanel 
 22  from pyjamas.ui.ClickDelegatePanel import ClickDelegatePanel 
 23  from pyjamas.ui import HasAlignment 
 24   
25 -class TabBar(Composite):
26 27 STYLENAME_DEFAULT = "gwt-TabBarItem" 28
29 - def __init__(self, **kwargs):
30 31 if not kwargs.has_key('StyleName'): kwargs['StyleName']="gwt-TabBar" 32 33 # this is awkward: HorizontalPanel is the composite, 34 # so we either the element here, and pass it in to HorizontalPanel. 35 element = None 36 if kwargs.has_key('Element'): 37 element = kwargs.pop('Element') 38 39 self.panel = HorizontalPanel(Element=element) 40 self.selectedTab = None 41 self.tabListeners = [] 42 43 self.panel.setVerticalAlignment(HasAlignment.ALIGN_BOTTOM) 44 45 first = HTML("&nbsp;", True) 46 rest = HTML("&nbsp;", True) 47 first.setStyleName("gwt-TabBarFirst") 48 rest.setStyleName("gwt-TabBarRest") 49 first.setHeight("100%") 50 rest.setHeight("100%") 51 52 self.panel.add(first) 53 self.panel.add(rest) 54 first.setHeight("100%") 55 self.panel.setCellHeight(first, "100%") 56 self.panel.setCellWidth(rest, "100%") 57 58 Composite.__init__(self, self.panel, **kwargs) 59 self.sinkEvents(Event.ONCLICK)
60
61 - def addTab(self, text, asHTML=False):
62 self.insertTab(text, asHTML, self.getTabCount())
63
64 - def addTabListener(self, listener):
65 self.tabListeners.append(listener)
66
67 - def getSelectedTab(self):
68 if self.selectedTab is None: 69 return -1 70 return self.panel.getWidgetIndex(self.selectedTab) - 1
71
72 - def getTabCount(self):
73 return self.panel.getWidgetCount() - 2
74
75 - def getTabWidget(self, index):
76 if index >= self.getTabCount(): 77 return None 78 delPanel = self.panel.getWidget(index + 1) 79 focusablePanel = delPanel.getFocusablePanel() 80 widget = focusablePanel.getWidget() 81 return widget
82
83 - def getTabHTML(self, index):
84 widget = self.getTabWidget(index) 85 if hasattr(widget, "getHTML"): 86 return widget.getHTML() 87 elif hasattr(widget, "getText"): # assume it's a Label if it has getText 88 return widget.getText() 89 else: 90 fpe = DOM.getParent(self.focusablePanel.getElement()) 91 return DOM.getInnerHTML(fpe)
92
93 - def createTabTextWrapper(self):
94 return None
95
96 - def insertTab(self, text, asHTML, beforeIndex=None):
97 """ 1st arg can, instead of being 'text', be a widget. 98 99 1st arg can also be None, which results in a blank 100 space between tabs. Use this to push subsequent 101 tabs out to the right hand end of the TabBar. 102 (the "blank" tab, by not being focussable, is not 103 clickable). 104 """ 105 if beforeIndex is None: 106 beforeIndex = asHTML 107 asHTML = False 108 109 if (beforeIndex < 0) or (beforeIndex > self.getTabCount()): 110 #throw new IndexOutOfBoundsException(); 111 pass 112 113 if text is None: 114 text = HTML("&nbsp;", True) 115 text.setWidth("100%") 116 text.setStyleName("gwt-TabBarRest") 117 self.panel.insert(text, beforeIndex + 1) 118 self.panel.setCellWidth(text, "100%") 119 return 120 121 istext = isinstance(text, basestring) 122 123 if istext: 124 if asHTML: 125 item = HTML(text) 126 else: 127 item = Label(text) 128 item.setWordWrap(False) 129 else: 130 # passing in a widget, it's expected to have its own style 131 item = text 132 133 self.insertTabWidget(item, beforeIndex)
134
135 - def insertTabWidget(self, widget, beforeIndex):
136 137 delWidget = ClickDelegatePanel(self, widget, self, self) 138 delWidget.setStyleName(self.STYLENAME_DEFAULT) 139 140 focusablePanel = delWidget.getFocusablePanel() 141 self.panel.insert(delWidget, beforeIndex + 1) 142 143 self.setStyleName(DOM.getParent(delWidget.getElement()), 144 self.STYLENAME_DEFAULT + "-wrapper", True)
145 146 #print "insertTabWidget", DOM.getParent(delWidget.getElement()), DOM.getAttribute(DOM.getParent(delWidget.getElement()), "className") 147 148
149 - def onClick(self, sender=None):
150 for i in range(1, self.panel.getWidgetCount() - 1): 151 if DOM.isOrHasChild(self.panel.getWidget(i).getElement(), 152 sender.getElement()): 153 return self.selectTab(i - 1) 154 return False
155
156 - def removeTab(self, index):
157 self.checkTabIndex(index) 158 159 toRemove = self.panel.getWidget(index + 1) 160 if toRemove == self.selectedTab: 161 self.selectedTab = None 162 self.panel.remove(toRemove)
163
164 - def removeTabListener(self, listener):
165 self.tabListeners.remove(listener)
166
167 - def selectTab(self, index):
168 self.checkTabIndex(index) 169 170 for listener in self.tabListeners: 171 if not listener.onBeforeTabSelected(self, index): 172 return False 173 174 self.setSelectionStyle(self.selectedTab, False) 175 if index == -1: 176 self.selectedTab = None 177 return True 178 179 self.selectedTab = self.panel.getWidget(index + 1) 180 self.setSelectionStyle(self.selectedTab, True) 181 182 for listener in self.tabListeners: 183 listener.onTabSelected(self, index) 184 185 return True
186
187 - def checkTabIndex(self, index):
188 if (index < -1) or (index >= self.getTabCount()): 189 #throw new IndexOutOfBoundsException(); 190 pass
191
192 - def setSelectionStyle(self, item, selected):
193 if item is not None: 194 if selected: 195 item.addStyleName("gwt-TabBarItem-selected") 196 self.setStyleName(DOM.getParent(item.getElement()), 197 "gwt-TabBarItem-wrapper-selected", True) 198 199 else: 200 item.removeStyleName("gwt-TabBarItem-selected") 201 self.setStyleName(DOM.getParent(item.getElement()), 202 "gwt-TabBarItem-wrapper-selected", False)
203 204 Factory.registerClass('pyjamas.ui.TabBar', 'TabBar', TabBar) 205