/// <summary> /// Executive function of a command /// </summary> /// <seealso cref="UndoOperation"/> internal override void CommandOperation() { DiagramClone = Diagram.Clone(); if (Diagram is PIMDiagram) { Dictionary <Element, ViewHelper> viewHelperCopies = new Dictionary <Element, ViewHelper>(); /* Elements in PIM diagram are loaded in the order of their LoadPriority in registration set */ foreach (Type ModelElementType in PIMElementsOrder) { foreach (KeyValuePair <Element, ViewHelper> pair in Diagram.DiagramElements) { Element element = pair.Key; ViewHelper viewHelper = pair.Value; if (!viewHelperCopies.ContainsKey(element) && ModelElementType.IsInstanceOfType(element)) { ViewHelper copiedViewHelper = viewHelper.CreateCopy(DiagramClone, null); viewHelperCopies.Add(element, copiedViewHelper); } } } Diagram.FillCopy(DiagramClone, Controller.Model, null, viewHelperCopies); } else { IList <Element> ordered; // order PSMTree.ReturnElementsInPSMOrder(((PSMDiagram)Diagram).Roots.Cast <Element>(), out ordered, true); // clone the selection ElementCopiesMap createdCopies = new ElementCopiesMap(); foreach (Element element in ordered) { Element copy = element.CreateCopy(Controller.Model, createdCopies); createdCopies[element] = copy; } // representants must be handled separately after all copies are created PSMTree.CopyRepresentantsRelations(createdCopies); // clone viewhelpers Dictionary <Element, ViewHelper> createdViewHelpers = new Dictionary <Element, ViewHelper>(); foreach (Element element in ordered) { ViewHelper viewHelper = Diagram.DiagramElements[element]; ViewHelper copiedViewHelper = viewHelper.CreateCopy(DiagramClone, createdCopies); createdViewHelpers.Add(element, copiedViewHelper); } Diagram.FillCopy(DiagramClone, Controller.Model, createdCopies, createdViewHelpers); } DiagramClone.Caption += " (copy)"; Controller.Project.AddDiagram(DiagramClone); }
public static void PasteContentToDiagram(XCaseCanvas targetDiagram, RegistrationSet registrationSet) { if (targetDiagram == null) { return; } if (State == EState.ContainsPIM) { List <Element> alreadyProcessed = new List <Element>(); Dictionary <Element, ViewHelper> createdCopies = new Dictionary <Element, ViewHelper>(); IncludeElementsCommand includeElementsCommand = (IncludeElementsCommand)IncludeElementsCommandFactory.Factory().Create(targetDiagram.Controller); /* Elements in PIM diagram are loaded in the order of their LoadPriority in registration set */ foreach (RepresentantRegistration registration in registrationSet.OrderBy(reg => reg.LoadPriority)) { foreach (KeyValuePair <Element, ViewHelper> pair in content) { Element element = pair.Key; ViewHelper viewHelper = pair.Value; if (!alreadyProcessed.Contains(element) && registration.ModelElementType.IsInstanceOfType(element)) { if (!targetDiagram.Diagram.IsElementPresent(element) && element.CanBePutToDiagram(targetDiagram.Diagram, includeElementsCommand.IncludedElements.Keys)) { ViewHelper copiedViewHelper = viewHelper.CreateCopy(targetDiagram.Diagram, null); createdCopies.Add(element, copiedViewHelper); includeElementsCommand.IncludedElements.Add(element, copiedViewHelper); } alreadyProcessed.Add(element); } } } includeElementsCommand.Execute(); } else if (State == EState.ContainsPSM) { IList <Element> ordered; PSMDiagram sourceDiagram = (PSMDiagram)content.First().Value.Diagram; if (!content.Keys.All(element => sourceDiagram.DiagramElements.Keys.Contains(element))) { throw new XCaseException("Cannot paste content. Source diagram was modified.") { ExceptionTitle = "Cannot paste content." }; } // identify tree roots IEnumerable <Element> roots = PSMTree.IdentifySubtreeRoots(content.Keys); List <AddPSMClassToRootsCommand> addToRootsCommands = new List <AddPSMClassToRootsCommand>(); // order PSMTree.ReturnElementsInPSMOrder(content.Keys.Union(roots), out ordered, false); IncludeElementsCommand includeElementsCommand = (IncludeElementsCommand)IncludeElementsCommandFactory.Factory().Create(targetDiagram.Controller); // clone the selection ElementCopiesMap createdCopies = new ElementCopiesMap(); foreach (Element element in ordered) { Element copy = element.CreateCopy(targetDiagram.Controller.ModelController.Model, createdCopies); createdCopies[element] = copy; } // representants must be handled separately after all copies are created PSMTree.CopyRepresentantsRelations(createdCopies); foreach (Element root in roots) { PSMClass _root = createdCopies[root] as PSMClass; if (_root != null) { AddPSMClassToRootsCommand command = (AddPSMClassToRootsCommand)AddPSMClassToRootsCommandFactory.Factory().Create(targetDiagram.Controller); command.Set(new ElementHolder <PSMClass>(_root)); addToRootsCommands.Add(command); } } // clone viewhelpers Dictionary <Element, ViewHelper> createdViewHelpers = new Dictionary <Element, ViewHelper>(); foreach (Element element in ordered) { ViewHelper viewHelper = sourceDiagram.DiagramElements[element]; ViewHelper copiedViewHelper = viewHelper.CreateCopy(targetDiagram.Diagram, createdCopies); createdViewHelpers.Add(element, copiedViewHelper); includeElementsCommand.IncludedElements.Add(createdCopies[element], copiedViewHelper); } IMacroCommand macro = targetDiagram.Controller.BeginMacro(); macro.CheckFirstOnlyInCanExecute = true; // put to diagram includeElementsCommand.Execute(); // new roots foreach (AddPSMClassToRootsCommand command in addToRootsCommands) { command.Execute(); } targetDiagram.Controller.CommitMacro(); #if DEBUG Tests.ModelIntegrity.ModelConsistency.CheckEverything(targetDiagram.Controller.ModelController.Project); #endif } }