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
18 from pyjamas.ui.FocusWidget import FocusWidget
19 from pyjamas.ui.ChangeListener import ChangeHandler
20 from pyjamas.ui import Event
21 from __pyjamas__ import console
22
23 -class ListBox(FocusWidget, ChangeHandler):
24
25 _props = [("visible", "Visible Count", "VisibleItemCount", None),
26 ("name", "Name", "Name", None),
27 ("selectedIndex", "Selected Index", "SelectedIndex", None),
28 ("multiple", "Multiple Select", "MultiplSelect", None),
29 ]
30
37
38 @classmethod
41
43 items = {}
44 for (k, v) in props.items():
45 if not isinstance(k, int):
46 continue
47 items[int(k)] = v
48 items = items.items()
49 items.sort()
50 for (k, v) in items:
51 self.addItem(*v)
52
53 - def addItem(self, item, value = None):
55
60
63
64 - def getItemText(self, index):
65 child = DOM.getChild(self.getElement(), index)
66 return DOM.getInnerText(child)
67
70
72 """ returns the selected item's index on a single-select
73 listbox. returns -1 if no item is selected.
74 for multi-select, use repeated calls to isItemSelected.
75 """
76 return DOM.getIntAttribute(self.getElement(), "selectedIndex")
77
83
86
87
93
98
102
107
109 """ this function is deprecated: use getMultipleSelect
110 """
111 return self.getMultipleSelect()
112
115
118
121
124
126 """ selects the ListBox according to a value.
127 to select by item, see selectItem.
128 # http://code.google.com/p/pyjamas/issues/detail?id=63
129 """
130 for n in range(self.getItemCount()):
131 if self.getValue(n) == value:
132 self.setSelectedIndex(n)
133 return n
134 return None
135
137 """ selects the ListBox according to an item's text
138 to select by value, see selectValue.
139 # http://code.google.com/p/pyjamas/issues/detail?id=63
140 """
141 for n in range(self.getItemCount()):
142 if self.getItemText(n) == item:
143 self.setSelectedIndex(n)
144 return n
145 return None
146
147 - def setItemText(self, index, text):
148 self.checkIndex(index)
149 if text is None:
150 console.error("Cannot set an option to have null text")
151 return
152 DOM.setOptionText(self.getElement(), text, index)
153
159
162
168
169 - def getSelectedItemText(self, ignore_first_value = False):
170 selected = []
171 if ignore_first_value:
172 start_idx = 1
173 else:
174 start_idx = 0
175 for i in range(start_idx,self.getItemCount()):
176 if self.isItemSelected(i):
177 selected.append(self.getItemText(i))
178 return selected
179
181 selected = []
182 if ignore_first_value:
183 start_idx = 1
184 else:
185 start_idx = 0
186 for i in range(start_idx,self.getItemCount()):
187 if self.isItemSelected(i):
188 selected.append(self.getValue(i))
189 return selected
190
191 - def setItemTextSelection(self, values):
192 if not values:
193 values = []
194 self.setSelectedIndex(0)
195 for i in range(0,self.getItemCount()):
196 if self.getItemText(i) in values:
197 self.setItemSelected(i, "selected")
198 else:
199 self.setItemSelected(i, "")
200
210
211 Factory.registerClass('pyjamas.ui.ListBox', 'ListBox', ListBox)
212