Example #1
0
        /// <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);
        }
Example #2
0
        internal override void CommandOperation()
        {
            IList <Element> _deleted;

            // try to order elements
            if (PSMTree.ReturnElementsInPSMOrder(DeletedElements, out _deleted, true))
            {
                safeOrdering   = true;
                _deletionOrder = _deleted.Reverse();
            }
            else
            {
                // this will forbid undo
                safeOrdering   = false;
                _deletionOrder = DeletedElements;
            }

            foreach (Element element in _deletionOrder)
            {
                if (DeletedElements.Contains(element))
                {
                    DeletedElementsViewHelpers[element] = Diagram.DiagramElements[element];
                    element.RemoveMeFromModel();
                    Diagram.RemoveModelElement(element);
                }
                //AssociatedElements.Add(element);
            }
        }
Example #3
0
        public void BindToDiagram(Diagram diagram, ModelController modelController)
        {
            PropertyPath propertyPath = new PropertyPath("Caption");
            Binding      titleBinding = new Binding {
                Source = diagram, Path = propertyPath
            };

            SetBinding(TitleProperty, titleBinding);

            // bound view to controller and controller to model
            DiagramController = new DiagramController(diagram, modelController);
            ModelController   = modelController;

            xCaseDrawComponent.Canvas.Controller = DiagramController;
            try
            {
                Mouse.SetCursor(Cursors.Wait);

                #region load items on the diagram

                List <Element>  alreadyProcessed = new List <Element>();
                RegistrationSet registrationSet;
                if (diagram is PIMDiagram)
                {
                    registrationSet = MainWindow.PIMRepresentantsSet;
                    /* 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 diagram.DiagramElements)
                        {
                            if (!alreadyProcessed.Contains(pair.Key) &&
                                registration.ModelElementType.IsInstanceOfType(pair.Key))
                            {
                                diagram.NotifyElementAdded(this, pair.Key, pair.Value);
                                alreadyProcessed.Add(pair.Key);
                            }
                        }
                    }
                }
                else
                {
                    /* order of the elements in PSM diagram is more complex, an ordering function
                     * is called to order the elements (basically BFS)
                     */
                    TreeLayout.SwitchOff();
                    IList <Element> ordered;
                    if (PSMTree.ReturnElementsInPSMOrder(((PSMDiagram)diagram).Roots, out ordered, true))
                    {
                        foreach (Element element in ordered)
                        {
                            diagram.NotifyElementAdded(this, element, diagram.DiagramElements[element]);
                        }
                        foreach (Comment comment in diagram.DiagramElements.Keys.OfType <Comment>())
                        {
                            diagram.NotifyElementAdded(this, comment, diagram.DiagramElements[comment]);
                        }
                        foreach (PSMDiagramReference psmDiagramReference in diagram.DiagramElements.Keys.OfType <PSMDiagramReference>())
                        {
                            diagram.NotifyElementAdded(this, psmDiagramReference, diagram.DiagramElements[psmDiagramReference]);
                        }
                    }
                    xCaseDrawComponent.Canvas.Loaded += Canvas_Loaded;
                }

                #endregion
            }
            finally
            {
                Mouse.SetCursor(Cursors.Arrow);
            }
        }
Example #4
0
        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
            }
        }