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

Source Code for Module library.pyjamas.ui.ListBox

  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   
 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
31 - def __init__(self, **kwargs):
32 if not kwargs.has_key('StyleName'): kwargs['StyleName']="gwt-ListBox" 33 self.INSERT_AT_END = -1 34 element = kwargs.pop('Element', None) or DOM.createSelect() 35 FocusWidget.__init__(self, element, **kwargs) 36 ChangeHandler.__init__(self)
37 38 @classmethod
39 - def _getProps(self):
40 return FocusWidget._getProps() + self._props
41
42 - def _setWeirdProps(self, props, builderstate):
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):
54 self.insertItem(item, value, self.INSERT_AT_END)
55
56 - def clear(self):
57 h = self.getElement() 58 while DOM.getChildCount(h) > 0: 59 DOM.removeChild(h, DOM.getChild(h, 0))
60
61 - def getItemCount(self):
62 return DOM.getChildCount(self.getElement())
63
64 - def getItemText(self, index):
65 child = DOM.getChild(self.getElement(), index) 66 return DOM.getInnerText(child)
67
68 - def getName(self):
69 return DOM.getAttribute(self.getElement(), "name")
70
71 - def getSelectedIndex(self):
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
78 - def getValue(self, index):
79 self.checkIndex(index) 80 81 option = DOM.getChild(self.getElement(), index) 82 return DOM.getAttribute(option, "value")
83
84 - def getVisibleItemCount(self):
85 return DOM.getIntAttribute(self.getElement(), "size")
86 87 # also callable as insertItem(item, index)
88 - def insertItem(self, item, value, index=None):
89 if index is None: 90 index = value 91 value = None 92 DOM.insertListItem(self.getElement(), item, value, index)
93
94 - def isItemSelected(self, index):
95 self.checkIndex(index) 96 option = DOM.getChild(self.getElement(), index) 97 return DOM.getBooleanAttribute(option, "selected")
98
99 - def removeItem(self, idx):
100 child = DOM.getChild(self.getElement(), idx) 101 DOM.removeChild(self.getElement(), child)
102
103 - def setItemSelected(self, index, selected):
104 self.checkIndex(index) 105 option = DOM.getChild(self.getElement(), index) 106 DOM.setIntAttribute(option, "selected", selected and 1 or 0)
107
108 - def isMultipleSelect(self):
109 """ this function is deprecated: use getMultipleSelect 110 """ 111 return self.getMultipleSelect()
112
113 - def getMultipleSelect(self):
114 return DOM.getBooleanAttribute(self.getElement(), "multiple")
115
116 - def setMultipleSelect(self, multiple):
117 DOM.setBooleanAttribute(self.getElement(), "multiple", multiple)
118
119 - def setName(self, name):
120 DOM.setAttribute(self.getElement(), "name", name)
121
122 - def setSelectedIndex(self, index):
123 DOM.setIntAttribute(self.getElement(), "selectedIndex", index)
124
125 - def selectValue(self, value):
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
136 - def selectItem(self, item):
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
154 - def setValue(self, index, value):
155 self.checkIndex(index) 156 157 option = DOM.getChild(self.getElement(), index) 158 DOM.setAttribute(option, "value", value)
159
160 - def setVisibleItemCount(self, visibleItems):
161 DOM.setIntAttribute(self.getElement(), "size", visibleItems)
162
163 - def checkIndex(self, index):
164 elem = self.getElement() 165 if (index < 0) or (index >= DOM.getChildCount(elem)): 166 #throw new IndexOutOfBoundsException(); 167 pass
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
180 - def getSelectedValues(self, ignore_first_value = False):
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
201 - def setValueSelection(self, values):
202 if not values: 203 values = [] 204 self.setSelectedIndex(0) 205 for i in range(0,self.getItemCount()): 206 if self.getValue(i) in values: 207 self.setItemSelected(i, "selected") 208 else: 209 self.setItemSelected(i, "")
210 211 Factory.registerClass('pyjamas.ui.ListBox', 'ListBox', ListBox) 212