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

import coral

DI = coral.lang.XMIDI20

class DiagramElementUI: 

    def __init__( self, graphElement ):
        self.graphElement = graphElement
        graphElement.ui = self

        if not self.graphElement.oclIsKindOf(DI.DiagramElement):
            print "DiagramElementLayouter.__init__", self.graphElement

        assert self.graphElement.oclIsKindOf(DI.DiagramElement)

    def movable( self ):
        """ Returns True/False whether it should be possible to move the widget. """
        return self.graphElement.oclIsKindOf(DI.GraphNode)
    
    def selectable( self ):
        """ Returns True/False whether it should be possible to select the widget. """
        return self.graphElement.semanticModel.oclIsKindOf(DI.Uml1SemanticModelBridge) 

    def resizable( self ):
        """ Returns True/False whether it should be possible to resize the widget. """
        return self.graphElement.oclIsKindOf(DI.GraphNode)
    
    def relinkable( self ):
        """ Returns True/False whether it should be possible to relink the widget. """
        return self.graphElement.oclIsKindOf(DI.GraphEdge)

    def reparent(self, *args):
        """ Performs reparenting of element to parent. Metamodel specific"""
        pass

    def accepts(self, *args):
        """ Evaluates whether a target in the relink process is acceptable. """
        return False

    def acceptChild(self, *args):
        """ Evaluates whether a target in the reparenting process is acceptable. """
        return False

    def acceptDrop(self, element):
        """ Evaluates whether a target in the dropping-over process is acceptable. """
        return False

    def onDrop(self, element, parent):
        """ Performs the operations required when an element is dropped
        over another, and (typically) referenced under association. """
        pass
    


class MobileUI(DiagramElementUI):

    def selectable( self ):
        return True

    def movable( self ):
        return True

class MobileNoResizeUI(MobileUI):

    def resizable( self ):
        return False

class MobileIfLabelUI( DiagramElementUI ):

    def selectable( self ):
        return typeInContainerChain(DI.GraphEdge, self.graphElement)

    def movable( self ):
        return typeInContainerChain(DI.GraphEdge, self.graphElement)

def typeInContainerChain(type, graphElement):
    if not graphElement:
        return False
    while graphElement.container:
        if graphElement.container.oclIsKindOf(type):
            return True
        else:
            graphElement = graphElement.container
    return False

class MobileIfLabelNoResizeUI( MobileIfLabelUI ):

    def resizable( self ):
        return False

class ImmobileUI(DiagramElementUI):
    def movable(self):
        return False
    
    def selectable(self):
        return False

class ImmobileSelectableUI(ImmobileUI):
    
    def selectable(self):
        return True

class ImmobileNoResize(ImmobileUI):

    def resizable( self ):
        return False

class UnselectableUI(DiagramElementUI):
    def selectable(self):
        return False

class UnresizableUI(DiagramElementUI):
    def resizable(self):
        return False


## Not necessarily consistent
class LeafElementUI(DiagramElementUI):

    def __init__( self, graphElement ):
        DiagramElementUI.__init__(self, graphElement)

    def movable( self ):
        return True

    def relinkable( self ):
        return False

    def selectable( self ):
        return True

class DebugUserInterface( DiagramElementUI ):

    def selectable( self ):
        return True
    
##    def movable( self ):
##        return True
    
##    def resizable( self ):
##        return True
    

#
# Default User Interfaces for widgets
#
# NOTE Currently unused
#
