Beispiel #1
0
 def quit(self):
 
   BasePopup.destroy(self)  
Beispiel #2
0
class SelectObjectPopup(BasePopup):

  def __init__(self, parent, objects, key=None, labels=None, objectName=None, 
               refClassName=None, **kw):
    
    self.parent = parent
    
    self.resetObjectType(objects, key, labels, objectName, refClassName)
    
    self.doRegisters()
    
    BasePopup.__init__(self, parent, title= "Object Selection", borderwidth=5,
                       **kw)
    self.geometry("+50+50")
  
  def resetObjectType(self, objects, key=None, labels=None, objectName=None, 
                      refClassName=None, **kw):
    if refClassName:
      apiClass = Implementation.getClassFromFullName(refClassName)
      refClass = apiClass._metaclass
    else:
      apiClass = objects[0].__class__
      refClass = objects[0].metaclass
      refClassName = refClass.qualifiedName()
    self.refClassName = refClassName
    self.apiClass = apiClass

    for obj in objects:
      if not isinstance(obj, self.apiClass):
        raise ApiError("Object %s is not instance of %s" % (obj, refClassName))
    
    self.objects = objects
    
    self.key    = key
    if labels is None:
      labels = {}
    self.labels = labels.copy()
    self.objectName = objectName or refClass.name 
        
    self.headingList = self.getAttributeHeadings(refClass)
    
    # add 'fullKey to labels
    self.labels['fullKey'] = [str(getattr(obj, 'getFullKey')()) 
           for obj in self.objects]
    
    if refClass.isAbstract:
      # add extra column to define the type
      self.labels['Class'] = [obj.__class__.__name__ for obj in self.objects]
      self.headingList.append('Class')
    
  
  def body(self, master):
    
    textMatrix = self.getTextMatrix(self.objects)
        
    self.label = Label(self,text='Select %s ' % self.objectName)
    self.label.grid(row=0,column=0,sticky='nw')
    
    self.scrolledMatrix = ScrolledMatrix(self, initialRows=10,
                            headingList=self.headingList, 
                            callback=self.selectCell,
                            objectList=self.objects, textMatrix=textMatrix)
    self.scrolledMatrix.grid(row=1,column=0,sticky='nsew')
    self.grid_rowconfigure   (0, weight=0)
    self.grid_rowconfigure   (1, weight=1)
    self.grid_columnconfigure(0, weight=1)
  
    self.protocol('WM_DELETE_WINDOW', self.close)
  
  def getAttributeHeadings(self, refClass):
  
    attrNames, keyNames = getDisplayAttrNames(refClass, self.labels)
  
    # set up for key parameter and fullkey
    # fullKey will be used as key if none othe is present,
    # and will be put at the end otherwise
    if self.key is None:
      if keyNames:
        attrNames.append('fullKey')
      else:
        # key in class not available, no key passed in. Use fullKekey = 'fullKey'
        attrNames.insert(0,'fullKey')
    else:
      try:
        attrNames.remove(self.key)
      except ValueError:
        raise ApiError("key %s not in attributes or labels: %s"
                        % (self.key, attrNames))
      attrNames.insert(0,self.key)
      attrNames.append('fullKey')
     
    return attrNames
      
  def getTextMatrix(self,objects):
    
    headings = self.headingList
    nHeadings = len(headings)
    matrix = []
    
    for ii,object in enumerate(objects):
      line = nHeadings * ['None']
      for jj,attrName in enumerate(headings):
      
        if self.labels.get(attrName) is None:
          
          if hasattr(object,attrName):
            val = getattr(object,attrName)
            if isinstance(val, frozenset):
              val = list(val)
              val.sort()
            ss = str(val)
            if len(ss) > 32:
              ss = ss[:32] + '...'
            line[jj] = ss
        
        else:
          line[jj] = self.labels[attrName][ii]
                        
      matrix.append(line)
    
    return matrix
    
    self.resetObjectType(objects, key, labels, objectName, refClassName)
  
  def update(self, objects, key=None, labels=None, objectName=None, 
             refClassName=None, remove=None, reset=True):
    
    if reset:
      self.doUnregisters()
      self.resetObjectType(objects, key, labels, objectName, refClassName)
      self.doRegisters()
    
    if remove:
      ii = self.objects.index(remove)
      del self.objects[ii]
      for ll in self.labels.values():
        del ll[ii]
    
    if not self.objects:
      self.selectCell(None)
    
    if len(objects) == 1:
      return objects[0]

    self.label.set('Select %s ' % self.objectName)
    textMatrix = self.getTextMatrix(self.objects)
    self.scrolledMatrix.update(objectList=self.objects, 
                               headingList=self.headingList, 
                               textMatrix=textMatrix)

  def close(self):
  
    self.doUnregisters()
    self.parent.argumentVar.set(1)
 
    BasePopup.close(self)

  def destroy(self):
  
    self.doUnregisters()
    try:
      self.parent.argumentVar.set(1)
    except AttributeError, e:
      pass
      
    BasePopup.destroy(self)