/// <summary> /// Prepares the command that include hidden elements in the diagram /// </summary> private void IncludeDependentElements() { List <Element> candidates = ElementDependencies.FindHiddenElements(ElementRepresentations.PresentElements, Controller.ModelController.Model); IncludeElementsDialog dialog = new IncludeElementsDialog { NoElementsContent = "There are no hidden dependent elements on the diagram", Items = candidates }; if (dialog.ShowDialog() == true && dialog.SelectedElements.Count > 0) { IncludeElementsCommand command = (IncludeElementsCommand)IncludeElementsCommandFactory.Factory().Create(Controller); foreach (Element element in dialog.SelectedElements) { if (ElementRepresentations.CanRepresentElement(element)) { command.IncludedElements.Add(element, ElementRepresentations.CreateNewViewHelper(element, Diagram)); } } command.Execute(); } }
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 } }