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 Composite import Composite 
 18  from pyjamas.ui import Event 
 19  from HTML import HTML 
 20  from Label import Label 
 21  from HorizontalPanel import HorizontalPanel 
 22  from 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 getTabHTML(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 if hasattr(widget, "getHTML"): 82 return widget.getHTML() 83 elif hasattr(widget, "getText"): # assume it's a Label if it has getText 84 return widget.getText() 85 else: 86 fpe = DOM.getParent(self.focusablePanel.getElement()) 87 return DOM.getInnerHTML(fpe)
88
89 - def createTabTextWrapper(self):
90 return None
91
92 - def insertTab(self, text, asHTML, beforeIndex=None):
93 """ 1st arg can, instead of being 'text', be a widget. 94 95 1st arg can also be None, which results in a blank 96 space between tabs. Use this to push subsequent 97 tabs out to the right hand end of the TabBar. 98 (the "blank" tab, by not being focussable, is not 99 clickable). 100 """ 101 if beforeIndex is None: 102 beforeIndex = asHTML 103 asHTML = False 104 105 if (beforeIndex < 0) or (beforeIndex > self.getTabCount()): 106 #throw new IndexOutOfBoundsException(); 107 pass 108 109 if text is None: 110 text = HTML("&nbsp;", True) 111 text.setWidth("100%") 112 text.setStyleName("gwt-TabBarRest") 113 self.panel.insert(text, beforeIndex + 1) 114 self.panel.setCellWidth(text, "100%") 115 return 116 117 try: 118 istext = isinstance(text, str) or isinstance(text, unicode) 119 except: 120 istext = isinstance(text, str) 121 122 if istext: 123 if asHTML: 124 item = HTML(text) 125 else: 126 item = Label(text) 127 item.setWordWrap(False) 128 else: 129 # passing in a widget, it's expected to have its own style 130 item = text 131 132 self.insertTabWidget(item, beforeIndex)
133
134 - def insertTabWidget(self, widget, beforeIndex):
135 136 delWidget = ClickDelegatePanel(self, widget, self, self) 137 delWidget.setStyleName(self.STYLENAME_DEFAULT) 138 139 focusablePanel = delWidget.getFocusablePanel() 140 self.panel.insert(delWidget, beforeIndex + 1) 141 142 self.setStyleName(DOM.getParent(delWidget.getElement()), 143 self.STYLENAME_DEFAULT + "-wrapper", True)
144 145 #print "insertTabWidget", DOM.getParent(delWidget.getElement()), DOM.getAttribute(DOM.getParent(delWidget.getElement()), "className") 146 147
148 - def onClick(self, sender=None):
149 for i in range(1, self.panel.getWidgetCount() - 1): 150 if DOM.isOrHasChild(self.panel.getWidget(i).getElement(), 151 sender.getElement()): 152 return self.selectTab(i - 1) 153 return False
154
155 - def removeTab(self, index):
156 self.checkTabIndex(index) 157 158 toRemove = self.panel.getWidget(index + 1) 159 if toRemove == self.selectedTab: 160 self.selectedTab = None 161 self.panel.remove(toRemove)
162
163 - def removeTabListener(self, listener):
164 self.tabListeners.remove(listener)
165
166 - def selectTab(self, index):
167 self.checkTabIndex(index) 168 169 for listener in self.tabListeners: 170 if not listener.onBeforeTabSelected(self, index): 171 return False 172 173 self.setSelectionStyle(self.selectedTab, False) 174 if index == -1: 175 self.selectedTab = None 176 return True 177 178 self.selectedTab = self.panel.getWidget(index + 1) 179 self.setSelectionStyle(self.selectedTab, True) 180 181 for listener in self.tabListeners: 182 listener.onTabSelected(self, index) 183 184 return True
185
186 - def checkTabIndex(self, index):
187 if (index < -1) or (index >= self.getTabCount()): 188 #throw new IndexOutOfBoundsException(); 189 pass
190
191 - def setSelectionStyle(self, item, selected):
192 if item is not None: 193 if selected: 194 item.addStyleName("gwt-TabBarItem-selected") 195 self.setStyleName(DOM.getParent(item.getElement()), 196 "gwt-TabBarItem-wrapper-selected", True) 197 198 else: 199 item.removeStyleName("gwt-TabBarItem-selected") 200 self.setStyleName(DOM.getParent(item.getElement()), 201 "gwt-TabBarItem-wrapper-selected", False)
202 203 Factory.registerClass('pyjamas.ui.TabBar', TabBar) 204