#!/usr/bin/env python
import os
from coral.modeler import modeler
import coral
import time

XMIDI = coral.lang.XMIDI20
UML14 = coral.lang.UML14

m=modeler.ModelerWindow()
prj = m.openProject("oneclass.xmi")
m.main(loop=0)

diagram = prj.contents().select(lambda x: x.oclIsKindOf(XMIDI.Diagram))[0]
model = prj.contents().select(lambda x: x.oclIsKindOf(UML14.Model))[0]


assert model
assert diagram

print "DIAGRAM", diagram
m.getDiagramEditorManager().editorSet.notifyFocusOn(diagram)

cls = model.ownedElement.select(lambda x: x.oclIsKindOf(UML14.Class))[0]

x_cls = cls.xmidi_get_graphelements()[0]

assert cls
assert x_cls
m.processEvents()

print "Widget before ", x_cls.widget
assert x_cls.widget

m.processEvents()

print "Widget pyvalue before ", x_cls.widget.__getPyValue__()
assert x_cls.widget.__getPyValue__()

m.processEvents()

print "Canvas before ", diagram.widget
assert diagram.widget
print "Canvas pyvalue before ", diagram.widget.__getPyValue__()
assert diagram.widget.__getPyValue__()


# Executing UNDO
print "EXECUTING UNDO COMMAND ON NEWLY LOADED DIAGRAM"
coral.getTransactionManager().undo()

m.processEvents()

print "Widget after ", x_cls.widget
assert x_cls.widget

m.processEvents()

print "Widget pyvalue after ", x_cls.widget.__getPyValue__()
assert x_cls.widget.__getPyValue__()

m.processEvents()

print "Canvas after ", diagram.widget
assert diagram.widget
print "Canvas pyvalue after ", diagram.widget.__getPyValue__()
assert diagram.widget.__getPyValue__()

