#!/usr/bin/env python
from qt import *
try:
    from qttable import *
except:
    pass
from coral.modeler.property.propertyeditor import CompositePropertyEditor
import coral
metamodel=coral.lang.UML14
s=coral.Project()

app=QApplication([])

class MyPE(CompositePropertyEditor):
    def __init__(self):
        CompositePropertyEditor.__init__(self,None,None)
        
    def getName(self):
        return "TestPE"

    def canEdit(self,e):
        for element in [metamodel.Classifier]:
            if e.oclIsKindOf(element):
                return 1
        return 0

    def filterAssociation(self,e):
        if e.oclIsKindOf(metamodel.Attribute):
            return 1
        return 0

    def getSubEditors(self):
        return []

    def getAssociationName(self):
        return "feature"

    def elementFactory(self):
        name=createUniqueName(self.focus.feature,'attr')
        return Attribute(name=name)

c=metamodel.Class()
c.__setProject__(s)


o1=metamodel.Operation()
o2=metamodel.Operation()
o3=metamodel.Operation()

c.feature.append(o1)
c.feature.append(o2)
c.feature.append(o3)

a1=metamodel.Attribute()
a2=metamodel.Attribute()
a3=metamodel.Attribute()


dlg=MyPE()
dlg.show()
dlg.focusOn(c)

dlg.add(a1)
dlg.add(a2)
dlg.add(a3)

# Check that the attributes have been succesfully added.
assert (len(c.feature)==6)

# Check that the attribute we added last is last in the ordered list c.feature
assert (c.feature[5]==a3)

# Select an listelement and move it
dlg.itable.clearSelection()
dlg.itable.setCurrentCell(2,0)
cellSelect=QTableSelection()
cellSelect.init(2,0)
cellSelect.expandTo(2,0)
dlg.itable.addSelection(cellSelect)
dlg.moveUp()

#Check that the attribute is in a new position and that the class still has all its features.
assert (c.feature[4]==a3)
assert (len(c.feature)==6)



