#!/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()
storage=m.openProject("AssociationWithLabels.xmi")
m.main(loop=0)


diagram = None
for r in storage.contents():
    if r.oclIsKindOf(XMIDI.Diagram) and r.semanticModel.typeInfo == "ClassDiagram":
        diagram = r
        break

assert diagram #this should be the diagram with the two classes

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

m.processEvents()

aclass_di = None
bclass_di = None
for e in diagram.contained:
    if e.semanticModel.element.__type__() == UML14.Class and e.semanticModel.element.name == "Class_1":
        aclass_di = e
    if e.semanticModel.element.__type__() == UML14.Class and e.semanticModel.element.name == "Class_2":
        bclass_di = e

assert aclass_di
assert bclass_di

print "3??:",aclass_di.anchorage[0].graphEdge[0].contained
label_graphnode_before_y=aclass_di.anchorage[0].graphEdge[0].contained[0].position.y

#this one doesn't seem to have a widget???
label_widget_before_y=aclass_di.anchorage[0].graphEdge[0].contained[0].widget.__getPyValue__().x

coral.getTransactionManager().beginModifyCmd()
try:
    aclass_di.position.y=200
finally:
    coral.getTransactionManager().endModifyCmd()

#Check that the widgets position has changed
assert label_widget_before_y!=aclass_di.anchorage[0].graphEdge[0].contained[0].widget.__getPyValue__().x

#Check that the graphnode containing the labels has moved
assert label_graphnode_before_y != aclass_di.anchorage[0].graphEdge[0].contained[0].position.y

