Exemplo n.º 1
0
        /// <summary>
        /// Tells the command which elements to delete. The method uses
        /// interactive dialogs to specify the initial set of delted elements and
        /// to ask whether to delete unused elements from the model.
        /// </summary>
        /// <param name="deletedElements">set of deleted elements</param>
        public bool InitializeCommand(IEnumerable <Element> deletedElements)
        {
            List <Element>                _deleted     = new List <Element>(deletedElements);
            ElementDependencies           dependencies = ElementDependencies.FindElementDependenciesInDiagram(_deleted, Controller.Diagram);
            DeleteDependentElementsDialog dialog       = new DeleteDependentElementsDialog(dependencies);

            if (dependencies.Count == 0 || dialog.ShowDialog() == true)
            {
                if (dependencies.Count == 0)
                {
                    dialog.Close();
                }
                // add all selected dependent elements, remove those that were not selected
                foreach (KeyValuePair <Element, bool> _flag in dependencies.Flags)
                {
                    if (_flag.Value == false)
                    {
                        _deleted.Remove(_flag.Key);
                    }
                }
                foreach (KeyValuePair <Element, bool> _flag in dependencies.Flags)
                {
                    if (_flag.Value)
                    {
                        foreach (Element element in dependencies[_flag.Key])
                        {
                            if (!_deleted.Contains(element))
                            {
                                _deleted.Add(element);
                            }
                        }
                    }
                }

                DeleteFromDiagramCommand command = (DeleteFromDiagramCommand)DeleteFromDiagramCommandFactory.Factory().Create(Controller);
                command.DeletedElements = _deleted;
                Commands.Add(command);

                //
                // Elements that are not used in any diagrams are offered to be
                // deleted from model.
                //
                List <Element>      deleteFromModelCandidates = new List <Element>();
                ElementDependencies modelDependencies         = ElementDependencies.FindElementDependenciesInModel(deleteFromModelCandidates);
                foreach (Element element in _deleted)
                {
                    if (!Controller.ModelController.IsElementUsedInDiagrams(element, Controller.Diagram) &&
                        (!modelDependencies.ContainsKey(element) || modelDependencies[element].All(dependentElement => !Controller.ModelController.IsElementUsedInDiagrams(dependentElement))))
                    {
                        deleteFromModelCandidates.Add(element);
                        if (!modelDependencies.ContainsKey(element))
                        {
                            modelDependencies[element] = new List <Element>();
                        }
                    }
                }

                if (deleteFromModelCandidates.Count > 0)
                {
                    DeleteDependentElementsDialog modelDialog = new DeleteDependentElementsDialog(modelDependencies);
                    modelDialog.tbShort.Content = "Some elements remained unused in model";
                    modelDialog.tbLong.Text     =
                        "Some elements were removed from diagram and it was their only usage. Select elements you wish to remove also from model from the following list.";
                    modelDialog.Title           = "Delete from model";
                    modelDialog.bCancel.Content = "Cancel";
                    if (modelDialog.ShowDialog() == true) //User clicked on Delete
                    {
                        // add all selected dependent elements, remove those that were not selected
                        foreach (KeyValuePair <Element, bool> _flag in modelDependencies.Flags)
                        {
                            if (_flag.Value == false)
                            {
                                deleteFromModelCandidates.Remove(_flag.Key);
                            }
                        }
                        if (deleteFromModelCandidates.Count > 0)
                        {
                            DeleteFromModelCommand deleteFromModelCommand = (DeleteFromModelCommand)DeleteFromModelCommandFactory.Factory().Create(Controller.ModelController);
                            deleteFromModelCommand.DeletedElements = deleteFromModelCandidates;
                            deleteFromModelCommand.CallingDiagram  = Controller.Diagram;
                            Commands.Add(deleteFromModelCommand);
                        }
                        // both dialog were closed with OK
                        return(true);
                    }
                    else
                    {
                        // user clicked Cancel in the second dialog
                        return(false);
                    }
                }
                else
                {
                    // first dialog closed OK, second dialog was not shown
                    return(true);
                }
            }
            else
            {
                // first dialog closed OK, but second closed Cancel
                return(false);
            }
        }
Exemplo n.º 2
0
        public bool Set(IEnumerable <Element> deleted, DiagramController activeDiagramController)
        {
            ActiveDiagramController = activeDiagramController;
            Deleted = new List <Element>(deleted.Count());
            foreach (Element deletable in deleted)
            {
                Deleted.Add(deletable);
            }

            ElementDependencies           dependencies = ElementDependencies.FindElementDependenciesInModel(Deleted);
            DeleteDependentElementsDialog dialog       = new DeleteDependentElementsDialog(dependencies);

            dialog.DeleteLevel = DeleteDependentElementsDialog.EDeleteLevel.Model;

            if (dependencies.Count == 0 || dialog.ShowDialog() == true)
            {
                if (dependencies.Count == 0)
                {
                    dialog.Close();
                }
                // add all selected dependent elements, remove those that were not selected
                foreach (KeyValuePair <Element, bool> _flag in dependencies.Flags)
                {
                    if (_flag.Value == false)
                    {
                        Deleted.Remove(_flag.Key);
                    }
                    else
                    {
                        foreach (Element element in dependencies[_flag.Key])
                        {
                            if (!Deleted.Contains(element))
                            {
                                Deleted.Add(element);
                            }
                        }
                    }
                }

                // test whether elements are not used in other diagrams
                ElementDiagramDependencies dependentDiagrams;
                if (ActiveDiagramController != null)
                {
                    dependentDiagrams = ElementDiagramDependencies.FindElementDiagramDependencies(Controller.Project, Deleted, ActiveDiagramController.Diagram);
                }
                else
                {
                    dependentDiagrams = ElementDiagramDependencies.FindElementDiagramDependencies(Controller.Project, Deleted, null);
                }

                if (dependentDiagrams.Count > 0 &&
                    !(dependentDiagrams.Count == 1 &&
                      ActiveDiagramController != null &&
                      dependentDiagrams.Single().Value.Count == 1 &&
                      dependentDiagrams.Single().Value.Single() == ActiveDiagramController.Diagram
                      )
                    )
                {
                    ReferencingDiagramsDialog referencingDiagramsDialog = new ReferencingDiagramsDialog(dependentDiagrams);
                    referencingDiagramsDialog.ShowDialog();
                    if (referencingDiagramsDialog.cbOpen.IsChecked == true)
                    {
                        foreach (KeyValuePair <Element, List <Diagram> > pair in dependentDiagrams)
                        {
                            foreach (Diagram diagram in pair.Value)
                            {
                                ActivateDiagramCommand c = ActivateDiagramCommandFactory.Factory().Create(Controller) as ActivateDiagramCommand;
                                c.Set(diagram);
                                c.Execute();
                            }
                        }
                    }
                }
                else
                {
                    deleteFromModelCommand =
                        (DeleteFromModelCommand)DeleteFromModelCommandFactory.Factory().Create(Controller);
                    deleteFromModelCommand.DeletedElements = Deleted;
                    if (ActiveDiagramController != null)
                    {
                        deleteFromModelCommand.CallingDiagram = ActiveDiagramController.Diagram;
                    }
                    else
                    {
                        deleteFromModelCommand.CallingDiagram = null;
                    }

                    if (ActiveDiagramController != null && ActiveDiagramController.Diagram != null)
                    {
                        deleteFromDiagramCommand =
                            (DeleteFromDiagramCommand)DeleteFromDiagramCommandFactory.Factory().Create(ActiveDiagramController);
                        deleteFromDiagramCommand.DeletedElements = Deleted.Where(e => ActiveDiagramController.Diagram.IsElementPresent(e)).ToList();
                        Commands.Add(deleteFromDiagramCommand);
                    }

                    Commands.Add(deleteFromModelCommand);
                    return(true);
                }
            }
            return(false);
        }