from coral.modeler.extension import ContextMenuAction
import coral
import qt
from coral.modeler.coralmodeler import CoralModeler
from coral.modeler import modeler
DI = coral.lang.XMIDI20
import coral.modeler.diagram.xmidi_diagrams as diagrams
import UML14_to_SMD01

import SMD01_to_UML14

class Menu(ContextMenuAction):
    def createMenuItem(self,menu,lElement,lPresentation=[], diagram=None, position=None):

        self.diagram = diagram
        self.elements = lElement
        self.position = position
        self.menu=menu
        if lElement:
            self.element=lElement[0]
        else:
            self.element=None
            
        self.presentation=lPresentation
        
        if self.element and (type(self.element) != coral.kernel.ProjectPtr):
            
            if self.element.oclIsKindOf(coral.lang.UML14.Model):
                convertMenu=qt.QPopupMenu(menu,"UML to SMD")
                convertMenu.insertItem("Convert to SMD",self.convert2SMD)
                menu.insertItem("UML to SMD",convertMenu)

            if self.element.oclIsKindOf(coral.lang.SMD01.Language):
                convertMenu=qt.QPopupMenu(menu,"SMD to UML")
                convertMenu.insertItem("Convert to UML",self.convert2UML)
                menu.insertItem("SMD to UML",convertMenu)

              
                
    def convert2SMD(self):
        coral.getTransactionManager().beginTransaction()
        try:
            lang=UML14_to_SMD01.convert(self.element)
            prj=coral.Project()
            prj.setFilename(str(self.element.name)+str(lang.version)+str(lang.revision)+".xmi")
            lang.__setProject__(prj)
        finally:
            coral.getTransactionManager().endTransaction()

    def convert2UML(self):
        coral.getTransactionManager().beginTransaction()
        try:
            prj=coral.Project()
            prj.setFilename(str(self.element.name)+".xmi")            
            for lang in SMD01_to_UML14.convert(self.element):
                lang.__setProject__(prj)            
        finally:
            coral.getTransactionManager().endTransaction()
