# This file is part of the Coral project.
# Please read the copyright notice in the LICENSE file.

import coral
DI = coral.lang.XMIDI20
L = coral.lang.DIML10
SMD = coral.lang.SMD01
from coral.modeler.diagram.ui import *

from coral import log
logger=log.getLogger("DIML10 UI")

import coral.modeler.diagram.di_support as DISUPP

## This is the correct place for DIML 1.0 specific user interfaces

class ReferencedUI(DiagramElementUI):

    def _isRefd(self):
        cnt = self.graphElement.container
        if cnt and cnt.semanticModel and cnt.semanticModel.oclIsKindOf(DI.SimpleSemanticModelElement) and cnt.semanticModel.typeInfo=="TransitionDescription":
            return False
        else:
            return True

        modelcontainer = DISUPP.findUpwardsModelElement(self.graphElement.container)
        if modelcontainer:
            return True
        else:
            return False
    
    def movable( self ):
        return self._isRefd()

    def resizable(self):
        return self._isRefd()


class ContainedUI(DiagramElementUI):

    def accepts(self, element):
        if element and (element.oclIsKindOf(L.InitialPart) or element.oclIsKindOf(L.GraphElementPart)):
            return True
        logger.debug("Element rejected by Contained")
        return False

    def relink(self, cont, part, end):
        """
        relink Contained between parts
        end is an integer 0 or 1 to tell witch end of the Contained should be relinked
        """
        if end==0 and part.oclIsKindOf(L.InitialPart):
            cont.parent = part
        elif end==1 and part.oclIsKindOf(L.GraphElementPart):
            cont.child = part

class SeparatorUI(DiagramElementUI):

    def accepts(self, element):
        if element and (element.oclIsKindOf(L.ConcretePart) or element.oclIsKindOf(L.Contained)):
            return True
        logger.debug("Element rejected by Contained")
        return False

    def relink(self, sep, part, end):
        """
        relink Separator between parts
        end is an integer 0 or 1 to tell witch end of the Separator should be relinked
        """
        if end==0 and part.oclIsKindOf(L.Contained):
            sep.contained = part
        elif end==1 and part.oclIsKindOf(L.ConcretePart):
            sep.subtree = part

class ELToDIMappingUI(DiagramElementUI):

    def accepts(self, element):
        if element and (element.oclIsKindOf(L.InitialPart) or element.oclIsKindOf(SMD.ElementDefinition)):
            return True
        logger.debug("Element rejected by ElementToXMIDIMapping")
        return False

    def relink(self, map, part, end):
        """
        relink ElementToXMIDIMapping
        end is an integer 0 or 1 to tell witch end of the ElementToXMIDIMapping should be relinked
        """
        if end==0 and part.oclIsKindOf(SMD.ElementDefinition):
            map.element = part
        elif end==1 and part.oclIsKindOf(L.InitialPart):
            map.root = part
