class SimpleEditor ( EditorWithList, IValueChangeListener ): """ Simple style of editor for checklists, which displays a combo box. """ #--------------------------------------------------------------------------- # Trait definitions: #--------------------------------------------------------------------------- # Checklist item names names = List( Unicode ) # Checklist item values values = List #--------------------------------------------------------------------------- # Finishes initializing the editor by creating the underlying toolkit # widget: #--------------------------------------------------------------------------- def init ( self, parent ): """ Finishes initializing the editor by creating the underlying toolkit widget. """ self.create_control( parent ) super( SimpleEditor, self ).init( parent ) self.set_tooltip() #--------------------------------------------------------------------------- # Creates the initial editor control: #--------------------------------------------------------------------------- def create_control ( self, parent ): """ Creates the initial editor control. """ self.control = ComboBox() self.control.setMultiSelect(False) self.control.setNullSelectionAllowed(True) self.control.setImmediate(True) self.control.addListener(self, IValueChangeListener) #--------------------------------------------------------------------------- # Handles the list of legal check list values being updated: #--------------------------------------------------------------------------- def list_updated ( self, values ): """ Handles updates to the list of legal checklist values. """ sv = self.string_value if (len( values ) > 0) and isinstance( values[0], basestring ): values = [ ( x, sv( x, capitalize ) ) for x in values ] self.values = valid_values = [ x[0] for x in values ] self.names = [ x[1] for x in values ] # Make sure the current value is still legal: modified = False cur_value = parse_value( self.value ) for i in range( len( cur_value ) - 1, -1, -1 ): if cur_value[i] not in valid_values: try: del cur_value[i] modified = True except TypeError: logger.warn('Unable to remove non-current value [%s] from ' 'values %s', cur_value[i], values) if modified: if isinstance( self.value, basestring ): cur_value = ','.join( cur_value ) self.value = cur_value self.rebuild_editor() #--------------------------------------------------------------------------- # Rebuilds the editor after its definition is modified: #--------------------------------------------------------------------------- def rebuild_editor ( self ): """ Rebuilds the editor after its definition is modified. """ control = self.control c = IndexedContainer() for name in self.names: c.addItem(name) control.setContainerDataSource(c) self.update_editor() #--------------------------------------------------------------------------- # Handles the user selecting a new value from the combo box: #--------------------------------------------------------------------------- def valueChange(self, event): v = str(event.getProperty()) self.update_object(v) def update_object ( self, text ): """ Handles the user selecting a new value from the combo box. """ if unicode(text) in self.names: value = self.values[self.names.index(unicode(text))] if not isinstance(self.value, basestring): value = [value] elif not isinstance(self.value, basestring): value = [] else: value = '' self.value = value #--------------------------------------------------------------------------- # Updates the editor when the object trait changes external to the editor: #--------------------------------------------------------------------------- def update_editor ( self ): """ Updates the editor when the object trait changes externally to the editor. """ try: self.control.select( parse_value(self.value)[0] ) except: pass