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

import coral
from coral.modeler.diagram.degen import DiagramElementGenerator
DI = coral.lang.XMIDI20

from coral.modeler import extension


import string

def replaceCap(letter):
    if letter.isupper():
        return " "+letter
    else:
        return letter

def splitAtCaps(word):
    return string.join(map(replaceCap,word),"").strip()


empty_list = [None]

class __DiagramCreator:
    def __init__(self):
        self.extensions={}
        
    def updateExtensions(self,extensionRegistry):
        for entry in extensionRegistry.getRegisteredExtensionsByType(type="DiagramCreator"):
            ed=extension.fromFullElementName2AbstractElementDefinition(entry[0])
            dc = entry[1]#() # create an instance
            if self.extensions.has_key(ed):
                if dc not in self.extensions[ed]:
                    self.extensions[ed].append(dc)
            else:
                self.extensions[ed]=[dc]

    def getExtension(self, elementdefinition):
        global empty_list
        return self.extensions.get(elementdefinition, empty_list)[0]

    def getAllExtensions(self, elementdefinition):
        global empty_list
        return self.extensions.get(elementdefinition, empty_list)


def createDiagram( parent, owner_elem=None, typeinfo=""):
    diagram = DI.Diagram(isVisible=True, zoom=1.0)
    if typeinfo:
        diagram.semanticModel = DI.SimpleSemanticModelElement(typeInfo=typeinfo)
        diagram.name = "New "+splitAtCaps(typeinfo)
    else:
        diagram.name = "New Diagram"
    if owner_elem:
         diagram.owner = DI.Uml1SemanticModelBridge(element=owner_elem)

    if not isinstance(parent,coral.Project):
        if parent.oclIsKindOf(coral.lang.CQUERY10.Query):
            parent.diagram = diagram
        else:
            parent.diagram.append(diagram)
    else:
        diagram.__setProject__(parent)


    return diagram


DiagramCreator = __DiagramCreator()
