Пример #1
0
        // Public methods
        //
        public T Add <T>(T element, bool addToUndoRedo) where T : IGraphElement
        {
            System.Action redo = () =>
            {
                if (element is IVertex v)
                {
                    Execute.OnUIThread(() => ObservableVertices.Add(v));
                }
                else if (element is IEdge e)
                {
                    Execute.OnUIThread(() => ObservableEdges.Add(e));
                }
            };
            System.Action undo = () =>
            {
                Remove(element, false);
            };

            if (addToUndoRedo)
            {
                UndoRedoManager.Instance.AddAndExecute(new SimpleOperation(redo, undo));
            }
            else
            {
                redo();
            }
            return(element);
        }
Пример #2
0
        public IEdge AddEdge(IVertex source, IVertex target, bool isDirected, bool makeNotDirectedIfreversedExisted = false)
        {
            if (source == target)
            {
                return(null);
            }

            var existingEdge = ObservableEdges.FirstOrDefault(x => x.Source == source && x.Target == target);

            if (existingEdge == null)
            {
                var reversedEdge = ObservableEdges.FirstOrDefault(x => x.Source == target && x.Target == source);
                if (reversedEdge != null)
                {
                    if (makeNotDirectedIfreversedExisted && reversedEdge.IsDirected)
                    {
                        reversedEdge.IsDirected = false;
                        return(reversedEdge);
                    }
                    return(reversedEdge);
                }

                var newEdge = new Edge(source, target, isDirected);
                return(Add(newEdge, true));
            }
            return(null);
        }
Пример #3
0
        public void Remove(IGraphElement element, bool useUndoRedo = true)
        {
            if (element is IVertex)
            {
                var          vertex = element as IVertex;
                List <IEdge> edges  = null;

                System.Action redo = () =>
                {
                    Execute.OnUIThread(() => ObservableVertices.Remove(vertex));
                    edges = ObservableEdges.Where(x => x.Source == vertex || x.Target == vertex)
                            .ToList();
                    edges.ForEach(x => ObservableEdges.Remove(x));
                };
                System.Action undo = () =>
                {
                    AddVertex(vertex);
                    edges.ForEach(x => Add(x, false));
                };

                if (useUndoRedo)
                {
                    UndoRedoManager.Instance.AddAndExecute(new SimpleOperation(redo, undo));
                }
                else
                {
                    redo();
                }
            }
            else if (element is IEdge)
            {
                var           edge = element as IEdge;
                System.Action redo = () =>
                {
                    Execute.OnUIThread(() => ObservableEdges.Remove(edge));
                };
                System.Action undo = () =>
                {
                    Add(edge, false);
                };

                if (useUndoRedo)
                {
                    UndoRedoManager.Instance.AddAndExecute(new SimpleOperation(redo, undo));
                }
                else
                {
                    redo();
                }
            }
        }
Пример #4
0
 public void Clear()
 {
     Execute.OnUIThread(() => ObservableEdges.Clear());
     Execute.OnUIThread(() => ObservableVertices.Clear());
     UndoRedoManager.Instance.Clear();
 }