Deleting its connections when a node is deleted

3 posts, 1 answers
  1. Barış
    Barış avatar
    19 posts
    Member since:
    Jul 2015

    Posted 03 Sep 2015 Link to this post

    I am trying to delete the connection that comes or goes from a node when the node is deleting. Using MVVM, I override the below methods in GraphSource to achieve this. However, At the time RemoveItem is triggered, the connections have already updated (Source or Target property is null). 

     I also don't let the connection with null Source or Target property, so removing links with null target or source will solve my problem. But I wonder if there is a better way to achieve this?

    Below you can find my functions.

    /// <summary>
    /// Gets the node.
    /// </summary>
    /// <param name="id">The identifier.</param>
    /// <returns>Node with given id.</returns>
    public Node<T> GetNode(string id)
    {
        return this.InternalItems.FirstOrDefault(x => x.Entity.GetIdentifier() == id);
    }
     
    /// <summary>
    /// Adds the node.
    /// </summary>
    /// <param name="node">The node.</param>
    public override void AddNode(Node<T> node)
    {
        if (node == null)
        {
            throw new ArgumentNullException("node");
        }
     
        Node<T> foundNode = this.GetNode(node.Entity.GetIdentifier());
        if (foundNode == null)
        {
            base.AddNode(node);
        }
    }
     
    /// <summary>
    /// Adds the link.
    /// </summary>
    /// <param name="link">The link.</param>
    public override void AddLink(Link<T> link)
    {
        if (link == null)
        {
            throw new ArgumentNullException("link");
        }
     
        if (link.Source != null && link.Target != null)
        {
            var foundLink =
                this.InternalLinks.FirstOrDefault(x =>
                    (IsSameNode(x.Source, link.Source) && IsSameNode(x.Target, link.Target)) ||
                    (IsSameNode(x.Target, link.Source) && IsSameNode(x.Source, link.Target)));
            if (foundLink == null)
            {
                link.Source = this.GetNode(link.Source.Entity.GetIdentifier());
                link.Target = this.GetNode(link.Target.Entity.GetIdentifier());
                base.AddLink(link);
            }
        }
    }
     
    /// <summary>
    /// Removes the item.
    /// </summary>
    /// <param name="node">The node.</param>
    /// <returns>Removes the node and its connections.</returns>
    public override bool RemoveItem(Node<T> node)
    {
        this.InternalLinks.Remove(x => (IsSameNode(x.Source, node) && IsSameNode(x.Target, node)));
        return base.RemoveItem(node);
    }
     
    /// <summary>
    /// Determines whether [is same node] [the specified node1].
    /// </summary>
    /// <param name="node1">The node1.</param>
    /// <param name="node2">The node2.</param>
    /// <returns>True if the nodes have the same id.</returns>
    private static bool IsSameNode(Node<T> node1, Node<T> node2)
    {
        if (node1 == null)
        {
            return false;
        }
     
        if (node2 == null)
        {
            return false;
        }
     
        return node1.Entity.GetIdentifier() == node2.Entity.GetIdentifier();
    }

  2. Barış
    Barış avatar
    19 posts
    Member since:
    Jul 2015

    Posted 03 Sep 2015 in reply to Barış Link to this post

    I had an issue with AddLink function. When I drow a connection without source and target, it adds to diagram but does not add to InternalItems. So you cant delete or select it later. It stays as it is forever. What I need to is avoid orphaned connections to be added from diagram.

    Thank you in advance. 

    /// <summary>
    /// Adds the link.
    /// </summary>
    /// <param name="link">The link.</param>
    public override void AddLink(Link<T> link)
    {
        if (link == null)
        {
            throw new ArgumentNullException("link");
        }
      
        if (link.Source != null && link.Target != null)
        {
            var foundLink =
                this.InternalLinks.FirstOrDefault(x =>
                    (IsSameNode(x.Source, link.Source) && IsSameNode(x.Target, link.Target)) ||
                    (IsSameNode(x.Target, link.Source) && IsSameNode(x.Source, link.Target)));
            if (foundLink == null)
            {
                link.Source = this.GetNode(link.Source.Entity.GetIdentifier());
                link.Target = this.GetNode(link.Target.Entity.GetIdentifier());
                base.AddLink(link);
            }
        }
    }
  3. DevCraft banner
  4. Answer
    Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 08 Sep 2015 Link to this post

    Hi Barış,

    To delete a connection when its source / target shape is deleted you can override the delete command execution in RadDiagram or use code behind events. However, with your requirement that connection should be deleted if it has no source and target, these approaches are not better than yours.
    In MVVM manner, to detect that source or target is changed, you can use the PropertyChanged event of the LinkViewModelBase:
    public class Link : LinkViewModelBase<NodeViewModelBase>
        {
            public Link()
            {
                this.PropertyChanged += Link_PropertyChanged;
            }
     
            void Link_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
            {
                if (e.PropertyName == "Source")
                {
      
                }

    As for the AddLink function, you need to ensure base.AddLink() is invoked in order to have the added link in the InternalLinks collection. Could you please confirm this condition is fulfilled ?




    Regards,
    Petar Mladenov
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top