1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
26 - def __init__(self, tabBar=None, **kwargs):
27 self.children = []
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
40
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
68 self.tabListeners.append(listener)
69
72
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
89 for listener in self.tabListeners:
90 if not listener.onBeforeTabSelected(sender, tabIndex):
91 return False
92 return True
93
95 self.deck.showWidget(tabIndex)
96 for listener in self.tabListeners:
97 listener.onTabSelected(sender, tabIndex)
98
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
124 self.tabListeners.remove(listener)
125
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