<s> from pyjamas import DOM <EOL> from pyjamas import Factory <EOL> from pyjamas . ui . FocusWidget import FocusWidget <EOL> from pyjamas . ui . ChangeListener import ChangeHandler <EOL> from pyjamas . ui import Event <EOL> from __pyjamas__ import console <EOL> class ListBox ( FocusWidget , ChangeHandler ) : <EOL> _props = [ ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , None ) , <EOL> ( "<STR_LIT:name>" , "<STR_LIT:Name>" , "<STR_LIT:Name>" , None ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , None ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , None ) , <EOL> ] <EOL> def __init__ ( self , ** kwargs ) : <EOL> if not kwargs . has_key ( '<STR_LIT>' ) : kwargs [ '<STR_LIT>' ] = "<STR_LIT>" <EOL> self . INSERT_AT_END = - <NUM_LIT:1> <EOL> element = kwargs . pop ( '<STR_LIT>' , None ) or DOM . createSelect ( ) <EOL> FocusWidget . __init__ ( self , element , ** kwargs ) <EOL> ChangeHandler . __init__ ( self ) <EOL> @ classmethod <EOL> def _getProps ( self ) : <EOL> return FocusWidget . _getProps ( ) + self . _props <EOL> def _setWeirdProps ( self , props , builderstate ) : <EOL> items = { } <EOL> for ( k , v ) in props . items ( ) : <EOL> if not isinstance ( k , int ) : <EOL> continue <EOL> items [ int ( k ) ] = v <EOL> items = items . items ( ) <EOL> items . sort ( ) <EOL> for ( k , v ) in items : <EOL> self . addItem ( * v ) <EOL> def addItem ( self , item , value = None ) : <EOL> self . insertItem ( item , value , self . INSERT_AT_END ) <EOL> def clear ( self ) : <EOL> h = self . getElement ( ) <EOL> while DOM . getChildCount ( h ) > <NUM_LIT:0> : <EOL> DOM . removeChild ( h , DOM . getChild ( h , <NUM_LIT:0> ) ) <EOL> def getItemCount ( self ) : <EOL> return DOM . getChildCount ( self . getElement ( ) ) <EOL> def getItemText ( self , index ) : <EOL> child = DOM . getChild ( self . getElement ( ) , index ) <EOL> return DOM . getInnerText ( child ) <EOL> def getName ( self ) : <EOL> return DOM . getAttribute ( self . getElement ( ) , "<STR_LIT:name>" ) <EOL> def getSelectedIndex ( self ) : <EOL> """<STR_LIT>""" <EOL> return DOM . getIntAttribute ( self . getElement ( ) , "<STR_LIT>" ) <EOL> def getValue ( self , index ) : <EOL> self . checkIndex ( index ) <EOL> option = DOM . getChild ( self . getElement ( ) , index ) <EOL> return DOM . getAttribute ( option , "<STR_LIT:value>" ) <EOL> def getVisibleItemCount ( self ) : <EOL> return DOM . getIntAttribute ( self . getElement ( ) , "<STR_LIT:size>" ) <EOL> def insertItem ( self , item , value , index = None ) : <EOL> if index is None : <EOL> index = value <EOL> value = None <EOL> DOM . insertListItem ( self . getElement ( ) , item , value , index ) <EOL> def isItemSelected ( self , index ) : <EOL> self . checkIndex ( index ) <EOL> option = DOM . getChild ( self . getElement ( ) , index ) <EOL> return DOM . getBooleanAttribute ( option , "<STR_LIT>" ) <EOL> def removeItem ( self , idx ) : <EOL> child = DOM . getChild ( self . getElement ( ) , idx ) <EOL> DOM . removeChild ( self . getElement ( ) , child ) <EOL> def setItemSelected ( self , index , selected ) : <EOL> self . checkIndex ( index ) <EOL> option = DOM . getChild ( self . getElement ( ) , index ) <EOL> DOM . setIntAttribute ( option , "<STR_LIT>" , selected and <NUM_LIT:1> or <NUM_LIT:0> ) <EOL> def isMultipleSelect ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . getMultipleSelect ( ) <EOL> def getMultipleSelect ( self ) : <EOL> return DOM . getBooleanAttribute ( self . getElement ( ) , "<STR_LIT>" ) <EOL> def setMultipleSelect ( self , multiple ) : <EOL> DOM . setBooleanAttribute ( self . getElement ( ) , "<STR_LIT>" , multiple ) <EOL> def setName ( self , name ) : <EOL> DOM . setAttribute ( self . getElement ( ) , "<STR_LIT:name>" , name ) <EOL> def setSelectedIndex ( self , index ) : <EOL> DOM . setIntAttribute ( self . getElement ( ) , "<STR_LIT>" , index ) <EOL> def selectValue ( self , value ) : <EOL> """<STR_LIT>""" <EOL> for n in range ( self . getItemCount ( ) ) : <EOL> if self . getValue ( n ) == value : <EOL> self . setSelectedIndex ( n ) <EOL> return n <EOL> return None <EOL> def selectItem ( self , item ) : <EOL> """<STR_LIT>""" <EOL> for n in range ( self . getItemCount ( ) ) : <EOL> if self . getItemText ( n ) == item : <EOL> self . setSelectedIndex ( n ) <EOL> return n <EOL> return None <EOL> def setItemText ( self , index , text ) : <EOL> self . checkIndex ( index ) <EOL> if text is None : <EOL> console . error ( "<STR_LIT>" ) <EOL> return <EOL> DOM . setOptionText ( self . getElement ( ) , text , index ) <EOL> def setValue ( self , index , value ) : <EOL> self . checkIndex ( index ) <EOL> option = DOM . getChild ( self . getElement ( ) , index ) <EOL> DOM . setAttribute ( option , "<STR_LIT:value>" , value ) <EOL> def setVisibleItemCount ( self , visibleItems ) : <EOL> DOM . setIntAttribute ( self . getElement ( ) , "<STR_LIT:size>" , visibleItems ) <EOL> def checkIndex ( self , index ) : <EOL> elem = self . getElement ( ) <EOL> if ( index < <NUM_LIT:0> ) or ( index >= DOM . getChildCount ( elem ) ) : <EOL> pass <EOL> def getSelectedItemText ( self , ignore_first_value = False ) : <EOL> selected = [ ] <EOL> if ignore_first_value : <EOL> start_idx = <NUM_LIT:1> <EOL> else : <EOL> start_idx = <NUM_LIT:0> <EOL> for i in range ( start_idx , self . getItemCount ( ) ) : <EOL> if self . isItemSelected ( i ) : <EOL> selected . append ( self . getItemText ( i ) ) <EOL> return selected <EOL> def getSelectedValues ( self , ignore_first_value = False ) : <EOL> selected = [ ] <EOL> if ignore_first_value : <EOL> start_idx = <NUM_LIT:1> <EOL> else : <EOL> start_idx = <NUM_LIT:0> <EOL> for i in range ( start_idx , self . getItemCount ( ) ) : <EOL> if self . isItemSelected ( i ) : <EOL> selected . append ( self . getValue ( i ) ) <EOL> return selected <EOL> def setItemTextSelection ( self , values ) : <EOL> if not values : <EOL> values = [ ] <EOL> self . setSelectedIndex ( <NUM_LIT:0> ) <EOL> for i in range ( <NUM_LIT:0> , self . getItemCount ( ) ) : <EOL> if self . getItemText ( i ) in values : <EOL> self . setItemSelected ( i , "<STR_LIT>" ) <EOL> else : <EOL> self . setItemSelected ( i , "<STR_LIT>" ) <EOL> def setValueSelection ( self , values ) : <EOL> if not values : <EOL> values = [ ] <EOL> self . setSelectedIndex ( <NUM_LIT:0> ) <EOL> for i in range ( <NUM_LIT:0> , self . getItemCount ( ) ) : <EOL> if self . getValue ( i ) in values : <EOL> self . setItemSelected ( i , "<STR_LIT>" ) <EOL> else : <EOL> self . setItemSelected ( i , "<STR_LIT>" ) <EOL> Factory . registerClass ( '<STR_LIT>' , '<STR_LIT>' , ListBox ) </s>
