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

import coral
DI = coral.lang.XMIDI20
from coral.modeler.extension import *
from coral.modeler import iconcache
from coral.modeler.diagram.deaction import *
import coral.modeler
import coral.modeler.modeler

class CreateManagedModelAction(CreateAction):

     def createModelElement(self,model,parent):
 
          language = coral.modeler.modeler.chooseLanguage()
          
          if not language:
               return None

          kl=coral.Language.__getLanguage__(str(language.name),language.version,language.revision)
            
          assert(kl)
          new=kl.getRoot()()

          mm=coral.lang.Manage10.ManagedModel()
          mm.name="Managed Model %d" % self.perpetualEnumerator()
          mm.model=new
          model.managedModel.insert(mm)
          
          return mm

     def getLabel(self):
          return "ManagedModel"
         
     def getIcon( cls ):
          return iconcache.getIcon("icons/managedtb","Manage10")
     
     getIcon = classmethod(getIcon)
    

class StepAction(CreateAction):

     def createModelElement(self,model,parent):
          
          return coral.lang.Manage10.Step(manager=model,name="tr")
     

     def getIcon( cls ):
          return iconcache.getIcon("icons/transformtb","Manage10")
     
     getIcon = classmethod(getIcon)

     def getLabel(self):
          return "Step"
    

class CreateParameterAction(CreateLinkAction):

     def check(self,e1,e2):
          return True

     def createModelElement(self, model, parent, ge1, ge2):

          if ge1.semanticModel.element.oclIsKindOf(coral.lang.Manage10.ManagedModel):
               if  ge2.semanticModel.element.oclIsKindOf(coral.lang.Manage10.ManagedModel):              
                    dep=coral.lang.Manage10.ModelRelationship(name="?",manager=model)
                    dep.source=ge1.semanticModel.element
                    dep.target=ge2.semanticModel.element
               else:
                    dep=coral.lang.Manage10.InputParameter(name="?",manager=model)
                    dep.targetTransformation=ge2.semanticModel.element
                    dep.source=ge1.semanticModel.element
          else:
               dep=coral.lang.Manage10.OutputParameter(name="?",manager=model)
               dep.sourceTransformation=ge1.semanticModel.element
               dep.target=ge2.semanticModel.element

          dep.sourceHasChanged=True
          dep.targetHasChanged=True
          
          return dep 

     def getLabel(self):
          return "Parameter"
     

     def getIcon( cls ):
          return iconcache.getIcon("icons/managedLinktb","Manage10")
     
     getIcon = classmethod(getIcon)
      
         
class CreateManagementLinkAction(CreateLinkAction):

     def check(self,e1,e2):
          return True
     
     def createModelElement(self, model, parent, ge1, ge2):

         dep = coral.lang.UML14.Dependency(name="NewDependency_%d"%self.perpetualEnumerator())
         for a in [["type","ManagedModelRelationship"],["sourceHasChanged","False"],["targetHasChanged","False"]]:
              dep.taggedValue.append(coral.lang.UML14.TaggedValue(name=a[0],dataValue=a[1]))
         dep.client.append(ge1.semanticModel.element)
         dep.supplier.append(ge2.semanticModel.element)
         model.ownedElement.insert(dep)
         return dep

     def getIcon( cls ):
         return iconcache.getIcon("icons/managedLinktb","Manage10")

     getIcon = classmethod(getIcon)
    

     def getLabel(self):
          return "ManagementLink"
    
