#!/usr/bin/env python
import qt

from coral.modeler.property.propertyeditor import ElementPE,PropertyEditorManager
import coral
metamodel=coral.lang.UML14

s=coral.Project()
app=qt.QApplication([])
c=metamodel.Class(name="Customer")
c.__setProject__(s)


dlg=ElementPE()
dlg.show()
dlg.focusOn(c)
dlg.apply()
dlg.focusOn(None)
dlg.focusOn(c)

# Adding the dialog to a PropertyEditorManager.
pem = PropertyEditorManager(None)
pem.focusOn(c)
pem.pages=[dlg]
pem.subEditors=[dlg]


a=metamodel.Attribute(name="Name")

# Check that the widget updates correctly.
coral.getTransactionManager().beginModifyCmd()
try:
    getattr(c,"feature").append(a)
finally:
    coral.getTransactionManager().endModifyCmd()

for eti in dlg.itemsW:
    if eti.property.getName()=="feature":
        visibleText=eti.text()

# Assert that the name of the Attribute is shown
# in the ElementPE
assert (visibleText=="[ Name ]")


# Check that the widget responds to deleted elements
coral.getTransactionManager().beginModifyCmd()
try:
    a.__resetAttrs__()
finally:
    coral.getTransactionManager().endModifyCmd()

for eti in dlg.itemsW:
    if eti.property.getName()=="feature":
        visibleText=eti.text()

# Assert that the name of the Attribute is removed
# from the ElementPE when it has been deleted
assert (visibleText=="[  ]")
