Ejemplo n.º 1
0
 public DsmElementModel()
 {
     _elementsById        = new Dictionary <int, DsmElement>();
     _elementsByName      = new Dictionary <string, DsmElement>();
     _deletedElementsById = new Dictionary <int, DsmElement>();
     _root = new DsmElement(0, "", "");
     Clear();
 }
Ejemplo n.º 2
0
        private void UnregisterElementNameHierarchy(DsmElement element)
        {
            _elementsByName.Remove(element.Fullname);

            foreach (DsmElement child in element.ExportedChildren)
            {
                UnregisterElementNameHierarchy(child);
            }
        }
Ejemplo n.º 3
0
        private void RegisterElementNameHierarchy(DsmElement element)
        {
            _elementsByName[element.Fullname] = element;

            foreach (DsmElement child in element.ExportedChildren)
            {
                RegisterElementNameHierarchy(child);
            }
        }
Ejemplo n.º 4
0
        public void ChangeElementType(IDsmElement element, string type)
        {
            DsmElement changedElement = element as DsmElement;

            if (changedElement != null)
            {
                changedElement.Type = type;
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Add a child to the element.
        /// </summary>
        /// <param name="child">The child to be added</param>
        public void AddChild(IDsmElement child)
        {
            _children.Add(child);
            DsmElement c = child as DsmElement;

            if (c != null)
            {
                c._parent = this;
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Remove a child from the element.
        /// </summary>
        /// <param name="child">The child to be added</param>
        public void RemoveChild(IDsmElement child)
        {
            _children.Remove(child);
            DsmElement c = child as DsmElement;

            if (c != null)
            {
                c._parent = null;
            }
        }
Ejemplo n.º 7
0
        public void UnremoveElement(int elementId)
        {
            Logger.LogDataModelMessage($"Restore element id={elementId}");

            if (_deletedElementsById.ContainsKey(elementId))
            {
                DsmElement element = _deletedElementsById[elementId];
                ReregisterElement(element);
            }
        }
Ejemplo n.º 8
0
        public void ChangeElementName(IDsmElement element, string name)
        {
            DsmElement changedElement = element as DsmElement;

            if (changedElement != null)
            {
                UnregisterElementNameHierarchy(changedElement);
                changedElement.Name = name;
                RegisterElementNameHierarchy(changedElement);
            }
        }
Ejemplo n.º 9
0
        private void CollapseIfNoChildrenLeft(IDsmElement element)
        {
            DsmElement parent = element.Parent as DsmElement;

            if (parent != null)
            {
                if (parent.Children.Count == 0)
                {
                    parent.IsExpanded = false;
                }
            }
        }
Ejemplo n.º 10
0
        public void RemoveElement(int elementId)
        {
            Logger.LogDataModelMessage($"Remove element id={elementId}");

            if (_elementsById.ContainsKey(elementId))
            {
                DsmElement element = _elementsById[elementId];
                UnregisterElement(element);

                CollapseIfNoChildrenLeft(element);
            }
        }
Ejemplo n.º 11
0
        private void ReregisterElement(DsmElement element)
        {
            foreach (IDsmElement child in element.ExportedChildren)
            {
                ReregisterElement(child as DsmElement);
            }

            _elementsById[element.Id] = element;
            _deletedElementsById.Remove(element.Id);
            element.IsDeleted = false;

            ReregisterElementRelations?.Invoke(this, element);
        }
Ejemplo n.º 12
0
        private void UnregisterElement(DsmElement element)
        {
            UnregisterElementRelations?.Invoke(this, element);

            element.IsDeleted = true;
            _deletedElementsById[element.Id] = element;
            _elementsById.Remove(element.Id);
            _elementsByName.Remove(element.Fullname);

            foreach (IDsmElement child in element.ExportedChildren)
            {
                UnregisterElement(child as DsmElement);
            }
        }
Ejemplo n.º 13
0
        private void AssignElementOrder(DsmElement element, ref int order)
        {
            element.Order = order;
            order++;

            foreach (IDsmElement child in element.Children)
            {
                DsmElement childElement = child as DsmElement;
                if (childElement != null)
                {
                    AssignElementOrder(childElement, ref order);
                }
            }
        }
Ejemplo n.º 14
0
        public void AssignElementOrder()
        {
            Logger.LogDataModelMessage("AssignElementOrder");

            int order = 1;

            foreach (IDsmElement root in _root.Children)
            {
                DsmElement rootElement = root as DsmElement;
                if (rootElement != null)
                {
                    AssignElementOrder(rootElement, ref order);
                }
            }
        }
Ejemplo n.º 15
0
        public bool Swap(IDsmElement element1, IDsmElement element2)
        {
            bool swapped = false;

            if (element1?.Parent == element2?.Parent)
            {
                DsmElement parent = element1?.Parent as DsmElement;
                if (parent != null)
                {
                    swapped = parent.Swap(element1, element2);
                }
            }

            return(swapped);
        }
Ejemplo n.º 16
0
        public IDsmElement NextSibling(IDsmElement element)
        {
            IDsmElement nextSibling = null;
            DsmElement  parent      = element?.Parent as DsmElement;

            if (parent != null)
            {
                int index = parent.Children.IndexOf(element);

                if (index < parent.Children.Count - 1)
                {
                    nextSibling = parent.Children[index + 1];
                }
            }
            return(nextSibling);
        }
Ejemplo n.º 17
0
        public IDsmElement PreviousSibling(IDsmElement element)
        {
            IDsmElement previousSibling = null;
            DsmElement  parent          = element?.Parent as DsmElement;

            if (parent != null)
            {
                int index = parent.Children.IndexOf(element);

                if (index > 0)
                {
                    previousSibling = parent.Children[index - 1];
                }
            }
            return(previousSibling);
        }
Ejemplo n.º 18
0
        private IDsmElement AddElement(int id, string name, string type, int order, bool expanded, int?parentId, bool deleted)
        {
            DsmElement element = new DsmElement(id, name, type)
            {
                Order = order, IsExpanded = expanded, IsDeleted = deleted
            };

            if (parentId.HasValue)
            {
                DsmElement parent = null;
                if (_elementsById.ContainsKey(parentId.Value))
                {
                    parent = _elementsById[parentId.Value];
                }

                if (_deletedElementsById.ContainsKey(parentId.Value))
                {
                    parent = _deletedElementsById[parentId.Value];
                }

                if (parent != null)
                {
                    parent.AddChild(element);
                }
                else
                {
                    Logger.LogError($"Parent not found id={id}");
                }
            }
            else
            {
                _root.AddChild(element);
                _root.IsExpanded = true;
            }

            if (deleted)
            {
                UnregisterElement(element);
            }
            else
            {
                RegisterElement(element);
            }

            return(element);
        }
Ejemplo n.º 19
0
        public void ChangeElementParent(IDsmElement element, IDsmElement parent)
        {
            Logger.LogDataModelMessage($"Change element parent name={element.Name} from {element.Parent.Fullname} to {parent.Fullname}");

            DsmElement changedElement = element as DsmElement;
            DsmElement currentParent  = element.Parent as DsmElement;
            DsmElement newParent      = parent as DsmElement;

            if ((currentParent != null) && (newParent != null))
            {
                BeforeElementChangeParent?.Invoke(this, element);
                UnregisterElementNameHierarchy(changedElement);
                currentParent.RemoveChild(element);
                newParent.AddChild(element);
                RegisterElementNameHierarchy(changedElement);
                AfterElementChangeParent?.Invoke(this, element);
            }
        }
Ejemplo n.º 20
0
        public void ReorderChildren(IDsmElement element, ISortResult sortResult)
        {
            DsmElement parent = element as DsmElement;

            if (parent != null)
            {
                List <IDsmElement> clonedChildren = new List <IDsmElement>(parent.Children);

                foreach (IDsmElement child in clonedChildren)
                {
                    parent.RemoveChild(child);
                }

                for (int i = 0; i < sortResult.GetNumberOfElements(); i++)
                {
                    parent.AddChild(clonedChildren[sortResult.GetIndex(i)]);
                }
            }
        }
Ejemplo n.º 21
0
        public int CompareTo(object obj)
        {
            DsmElement element = obj as DsmElement;

            return(Id.CompareTo(element?.Id));
        }
Ejemplo n.º 22
0
 private void RegisterElement(DsmElement element)
 {
     _elementsById[element.Id]         = element;
     _elementsByName[element.Fullname] = element;
 }