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

Source Code for Module 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 FocusWidget import FocusWidget 
 19  from pyjamas.ui import Event 
 20  from __pyjamas__ import console 
 21   
22 -class ListBox(FocusWidget):
23 - def __init__(self, **kwargs):
24 if not kwargs.has_key('StyleName'): kwargs['StyleName']="gwt-ListBox" 25 self.changeListeners = [] 26 self.INSERT_AT_END = -1 27 if kwargs.has_key('Element'): 28 element = kwargs.pop('Element') 29 else: 30 element = DOM.createSelect() 31 FocusWidget.__init__(self, element, **kwargs) 32 self.sinkEvents(Event.ONCHANGE)
33
34 - def addChangeListener(self, listener):
35 self.changeListeners.append(listener)
36
37 - def addItem(self, item, value = None):
38 self.insertItem(item, value, self.INSERT_AT_END)
39
40 - def clear(self):
41 h = self.getElement() 42 while DOM.getChildCount(h) > 0: 43 DOM.removeChild(h, DOM.getChild(h, 0))
44
45 - def getItemCount(self):
46 return DOM.getChildCount(self.getElement())
47
48 - def getItemText(self, index):
49 child = DOM.getChild(self.getElement(), index) 50 return DOM.getInnerText(child)
51
52 - def getName(self):
53 return DOM.getAttribute(self.getElement(), "name")
54
55 - def getSelectedIndex(self):
56 """ returns the selected item's index on a single-select 57 listbox. returns -1 if no item is selected. 58 for multi-select, use repeated calls to isItemSelected. 59 """ 60 return DOM.getIntAttribute(self.getElement(), "selectedIndex")
61
62 - def getValue(self, index):
63 self.checkIndex(index) 64 65 option = DOM.getChild(self.getElement(), index) 66 return DOM.getAttribute(option, "value")
67
68 - def getVisibleItemCount(self):
69 return DOM.getIntAttribute(self.getElement(), "size")
70 71 # also callable as insertItem(item, index)
72 - def insertItem(self, item, value, index=None):
73 if index is None: 74 index = value 75 value = None 76 DOM.insertListItem(self.getElement(), item, value, index)
77
78 - def isItemSelected(self, index):
79 self.checkIndex(index) 80 option = DOM.getChild(self.getElement(), index) 81 return DOM.getBooleanAttribute(option, "selected")
82
83 - def isMultipleSelect(self):
84 return DOM.getBooleanAttribute(self.getElement(), "multiple")
85
86 - def onBrowserEvent(self, event):
87 if DOM.eventGetType(event) == "change": 88 for listener in self.changeListeners: 89 if hasattr(listener, 'onChange'): 90 listener.onChange(self) 91 else: 92 listener(self) 93 else: 94 FocusWidget.onBrowserEvent(self, event)
95
96 - def removeChangeListener(self, listener):
97 self.changeListeners.remove(listener)
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 setMultipleSelect(self, multiple):
109 DOM.setBooleanAttribute(self.getElement(), "multiple", multiple)
110
111 - def setName(self, name):
112 DOM.setAttribute(self.getElement(), "name", name)
113
114 - def setSelectedIndex(self, index):
115 DOM.setIntAttribute(self.getElement(), "selectedIndex", index)
116
117 - def selectValue(self, value):
118 """ selects the ListBox according to a value. 119 to select by item, see selectItem. 120 # http://code.google.com/p/pyjamas/issues/detail?id=63 121 """ 122 for n in range(self.getItemCount()): 123 if self.getValue(n) == value: 124 self.setSelectedIndex(n) 125 return n 126 return None
127
128 - def selectItem(self, item):
129 """ selects the ListBox according to an item's text 130 to select by value, see selectValue. 131 # http://code.google.com/p/pyjamas/issues/detail?id=63 132 """ 133 for n in range(self.getItemCount()): 134 if self.getItemText(n) == item: 135 self.setSelectedIndex(n) 136 return n 137 return None
138
139 - def setItemText(self, index, text):
140 self.checkIndex(index) 141 if text is None: 142 console.error("Cannot set an option to have null text") 143 return 144 DOM.setOptionText(self.getElement(), text, index)
145
146 - def setValue(self, index, value):
147 self.checkIndex(index) 148 149 option = DOM.getChild(self.getElement(), index) 150 DOM.setAttribute(option, "value", value)
151
152 - def setVisibleItemCount(self, visibleItems):
153 DOM.setIntAttribute(self.getElement(), "size", visibleItems)
154
155 - def checkIndex(self, index):
156 elem = self.getElement() 157 if (index < 0) or (index >= DOM.getChildCount(elem)): 158 #throw new IndexOutOfBoundsException(); 159 pass
160
161 - def getSelectedItemText(self, ignore_first_value = False):
162 selected = [] 163 if ignore_first_value: 164 start_idx = 1 165 else: 166 start_idx = 0 167 for i in range(start_idx,self.getItemCount()): 168 if self.isItemSelected(i): 169 selected.append(self.getItemText(i)) 170 return selected
171
172 - def getSelectedValues(self, ignore_first_value = False):
173 selected = [] 174 if ignore_first_value: 175 start_idx = 1 176 else: 177 start_idx = 0 178 for i in range(start_idx,self.getItemCount()): 179 if self.isItemSelected(i): 180 selected.append(self.getValue(i)) 181 return selected
182
183 - def setItemTextSelection(self, values):
184 if not values: 185 values = [] 186 self.setSelectedIndex(0) 187 for i in range(0,self.getItemCount()): 188 if self.getItemText(i) in values: 189 self.setItemSelected(i, "selected") 190 else: 191 self.setItemSelected(i, "")
192
193 - def setValueSelection(self, values):
194 if not values: 195 values = [] 196 self.setSelectedIndex(0) 197 for i in range(0,self.getItemCount()): 198 if self.getValue(i) in values: 199 self.setItemSelected(i, "selected") 200 else: 201 self.setItemSelected(i, "")
202 203 Factory.registerClass('pyjamas.ui.ListBox', ListBox) 204