import traceback
import coral
coral.lang.UML14.__import__(globals())
DI = coral.lang.XMIDI20
from coral.modeler.diagram import layout
from coral.modeler.diagram.degen import DiagramElementGenerator as DEGEN
from coral.log import getLogger

logger=getLogger("coral.addins.UML14.labellayoutobserver")

class labellayoutobserver:
    def __init__(self):
        coral.getTransactionManager().addObserver(self.onModelChanged)

    def onModelChanged(self, direc):
        """
        """
        logger.debug("Begin LabelLayoutObserver.onModelChangedLayoutLabels DIR=%d",direc)

        if direc != coral.TRANSACTION_ORDINARY:
            return

        try:
            tm=coral.getTransactionManager()
            tr=tm.getCurrentTransaction()

            labelsToLayout={}

            for c in tr:
                if not isinstance(c,coral.ElementChangeSlotCommand):
                    continue

                element = c.getSlot().getElement()
                
                # AssociationEnd
                if element.oclIsKindOf(AssociationEnd):
                    if c.getSlot().getProperty().getName() in ["name","visibility","multiplicity"]:
                        elements=element.xmidi_get_graphelements()
                        for e in elements:
                            if e.contained:
                                for cntd in e.contained:
                                    labelsToLayout[cntd]=True
                # Association, name
                if element.oclIsKindOf(Association):
                    if c.getSlot().getProperty().getName() == "name":
                        elements=element.xmidi_get_graphelements()
                        for e in elements:
                            if len(e.contained)==3:
                                labelsToLayout[e.contained[2]]=True

                # Dependency, name
                if element.oclIsKindOf(Dependency):
                    if c.getSlot().getProperty().getName() == "name":
                        elements=element.xmidi_get_graphelements()
                        for e in elements:
                            if len(e.contained)==1:
                                labelsToLayout[e.contained[0]]=True

                # Transition, name
                if element.oclIsKindOf(Transition):
                    if c.getSlot().getProperty().getName() in ["name"]: # add transitiondescription
                        elements=element.xmidi_get_graphelements()
                        for e in elements:
                            if len(e.contained)==2:
                                labelsToLayout[e.contained[1]]=True

                # Generalization ?

            # BUG could use same sizeHinter
            sz = layout._SizeHinter()
            for e in labelsToLayout.keys():
                l = e.layouter.__getPyValue__()
                if l:
                    l.layout(sz, toMinimal=True)

        except Exception,e:
            traceback.print_exc()
            raise e
        logger.debug("End LabelLayoutObserver.onModelChangedLayoutLabels") 
