private void btnRedo_Click(object sender, RoutedEventArgs e)
        {
            btnSelect.IsEnabled = true;
            if (Command.CanceledCommands.Count != 0)
            {
                Command lastCommand = Command.CanceledCommands.Pop();
                if (lastCommand is AddFigureCommand)
                {
                    PetriNetNode currentFigure = (lastCommand as AddFigureCommand).newFigure;
                    Shape        shape         = (lastCommand as AddFigureCommand).shape;

                    if (currentFigure.IsSelect)
                    {
                        currentFigure.IsSelect = false;
                    }
                    if ((lastCommand as AddFigureCommand).newFigure is VPlace)
                    {
                        //Place.places.Add(currentFigure as Place);
                        Net.places.Add(currentFigure as VPlace);
                    }
                    else
                    {
                        //Transition.transitions.Add(currentFigure as Transition);
                        Net.transitions.Add(currentFigure as VTransition);
                    }
                    //SetOfFigures.Figures.Add(currentFigure);

                    Canvas.SetLeft(shape, currentFigure.CoordX);
                    Canvas.SetTop(shape, currentFigure.CoordY);
                    MainModelCanvas.Children.Add(shape);

                    _allFiguresObjectReferences.Add(shape, currentFigure);

                    AddFigureCommand newCommand = new AddFigureCommand(currentFigure, shape);
                    Command.ExecutedCommands.Push(newCommand);
                    btnUndo.IsEnabled = true;
                }
                else if (lastCommand is AddArcCommand)
                {
                    VArc newArc = (lastCommand as AddArcCommand).newArc;
                    if (newArc.IsSelect)
                    {
                        newArc.IsSelect = false;
                    }
                    Net.arcs.Add(newArc);
                    newArc.AddToThisArcsLists();

                    DrawArc(newArc);
                    RedrawArrowHeads(newArc);

                    AddArcCommand newCommand = new AddArcCommand(newArc);
                    Command.ExecutedCommands.Push(newCommand);
                }
                else if (lastCommand is AddTokensCommand)
                {
                    PetriNetNode figure = (lastCommand as AddTokensCommand).markedPlace;

                    VPlace place = (VPlace)figure;
                    place.NumberOfTokens = (lastCommand as AddTokensCommand).oldNumber;

                    if (place.NumberOfTokens == 0)
                    {
                        RemoveTokens(place);
                    }
                    if (place.NumberOfTokens >= 0 && place.NumberOfTokens < 5)
                    {
                        MainModelCanvas.Children.Remove(place.NumberOfTokensLabel);
                    }
                    AddTokens(place);

                    int temp = (lastCommand as AddTokensCommand).oldNumber;
                    (lastCommand as AddTokensCommand).oldNumber = (lastCommand as AddTokensCommand).newNumber;
                    (lastCommand as AddTokensCommand).newNumber = temp;

                    AddTokensCommand newCommand = new AddTokensCommand(place, (lastCommand as AddTokensCommand).oldNumber,
                                                                       (lastCommand as AddTokensCommand).newNumber);
                    Command.ExecutedCommands.Push(newCommand);
                }
                else if (lastCommand is ChangeNameCommand)
                {
                    PetriNetNode namedFigure = (lastCommand as ChangeNameCommand).namedFigure;
                    namedFigure.Label = (lastCommand as ChangeNameCommand).oldName;

                    ChangeLabel(namedFigure, (lastCommand as ChangeNameCommand).oldName);

                    string temp = (lastCommand as ChangeNameCommand).oldName;
                    (lastCommand as ChangeNameCommand).oldName = (lastCommand as ChangeNameCommand).newName;
                    (lastCommand as ChangeNameCommand).newName = temp;

                    ChangeNameCommand newCommand = new ChangeNameCommand(namedFigure, (lastCommand as ChangeNameCommand).oldName,
                                                                         (lastCommand as ChangeNameCommand).newName);
                    Command.ExecutedCommands.Push(newCommand);
                }
                else if (lastCommand is DragCommand)
                {
                    foreach (PetriNetNode figure in Net.Nodes)
                    {
                        foreach (PetriNetNode afterDragFigure in (lastCommand as DragCommand).figuresAfterDrag)
                        {
                            if (figure.Id == afterDragFigure.Id)
                            {
                                figure.CoordX = afterDragFigure.CoordX;
                                figure.CoordY = afterDragFigure.CoordY;
                            }
                        }
                    }

                    //List<PetriNetNode> newList = SetOfFigures.Figures;

                    foreach (PetriNetNode figure in Net.Nodes)//newList)
                    {
                        MoveFigure(figure);
                    }

                    DragCommand newCommand = new DragCommand((lastCommand as DragCommand).figuresBeforeDrag,
                                                             (lastCommand as DragCommand).figuresAfterDrag);
                    Command.ExecutedCommands.Push(newCommand);
                }
                else if (lastCommand is DeleteCommand)
                {
                    List <PetriNetNode> figures = (lastCommand as DeleteCommand).deletedFigures;
                    List <VArc>         arcs    = (lastCommand as DeleteCommand).deletedArcs;
                    DeleteFigures(figures, arcs);
                    DeleteCommand newCommand = new DeleteCommand(figures, arcs);
                    Command.ExecutedCommands.Push(newCommand);
                }
                else if (lastCommand is ChangeWeightCommand)
                {
                    VArc arc = (lastCommand as ChangeWeightCommand).arc;
                    MainModelCanvas.Children.Remove((lastCommand as ChangeWeightCommand).oldWeightLabel);

                    arc.Weight = (lastCommand as ChangeWeightCommand).newWeight;
                    (lastCommand as ChangeWeightCommand).newWeightLabel.Content = arc.Weight;

                    Canvas.SetLeft((lastCommand as ChangeWeightCommand).newWeightLabel, (arc.From.CoordX + arc.To.CoordX) / 2);
                    Canvas.SetTop((lastCommand as ChangeWeightCommand).newWeightLabel, (arc.From.CoordY + arc.To.CoordY) / 2 - 5);
                    MainModelCanvas.Children.Add((lastCommand as ChangeWeightCommand).newWeightLabel);

                    arc.WeightLabel = (lastCommand as ChangeWeightCommand).newWeightLabel;

                    ChangeWeightCommand newCommand = new ChangeWeightCommand(arc, (lastCommand as ChangeWeightCommand).oldWeight,
                                                                             arc.Weight, (lastCommand as ChangeWeightCommand).oldWeightLabel, arc.WeightLabel);
                    Command.ExecutedCommands.Push(newCommand);
                }
                else if (lastCommand is CutCommand)
                {
                    cuttedOrCopiedArcs.Clear();
                    cuttedOrCopiedFigures.Clear();

                    List <PetriNetNode> figures  = (lastCommand as CutCommand).cuttedFigures;
                    List <PetriNetNode> old      = (lastCommand as CutCommand).oldFigures;
                    List <VArc>         expArcs  = (lastCommand as CutCommand).explicitlyCuttedArcs;
                    List <VArc>         implArcs = (lastCommand as CutCommand).implicitlyCuttedArcs;

                    cuttedOrCopiedFigures.AddRange(figures);
                    cuttedOrCopiedArcs.AddRange(expArcs);

                    List <PetriNetNode> cutF = new List <PetriNetNode>();
                    List <VArc>         cutA = new List <VArc>();
                    cutF.AddRange(cuttedOrCopiedFigures);
                    cutA.AddRange(implArcs);

                    CutFigures(cutF, cutA);
                    _selectedFigures.Clear();
                    _selectedArcs.Clear();
                    ReassignSelectedProperties();

                    CutCommand newCommand = new CutCommand(figures, old, expArcs, implArcs);
                    Command.ExecutedCommands.Push(newCommand);
                }
                else if (lastCommand is PasteCommand)
                {
                    if (nothingToPaste == false)
                    {
                        List <PetriNetNode> figuresToPaste = (lastCommand as PasteCommand).pastedfigures;
                        List <VArc>         arcsToPaste    = (lastCommand as PasteCommand).pastedArcs;

                        double minX = figuresToPaste[0].CoordX, minY = figuresToPaste[0].CoordY;

                        foreach (PetriNetNode figure in figuresToPaste)
                        {
                            if (figure.CoordX < minX)
                            {
                                minX = figure.CoordX;
                            }
                            if (figure.CoordY < minY)
                            {
                                minY = figure.CoordY;
                            }
                        }

                        if (cutOrCopy == cutOrCopyMode.copy)
                        {
                            UnselectFigures();//(selectedFigures, selectedArcs);
                        }

                        foreach (PetriNetNode figure in figuresToPaste)
                        {
                            if (cutOrCopy == cutOrCopyMode.copy)
                            {
                                figure.DetectIdMatches(Net.Nodes);
                            }
                            figure.CoordX += -minX + ScrollViewerForMainModelCanvas.HorizontalOffset;
                            figure.CoordY += -minY + ScrollViewerForMainModelCanvas.VerticalOffset;
                        }
                        PasteCommand newCommand = new PasteCommand(figuresToPaste, arcsToPaste);
                        Command.ExecutedCommands.Push(newCommand);

                        if (cutOrCopy == cutOrCopyMode.cut)
                        {
                            arcsToPaste.RemoveAll(ShouldBeDeleted);
                        }

                        PasteFiguresAndArcs(figuresToPaste, arcsToPaste);
                    }
                }
            }

            EnableUndoRedoButtons();
            ReassignSelectedProperties();
            if (Command.CanceledCommands.Count == 0)
            {
                btnRedo.IsEnabled = false;
            }
            if (Command.ExecutedCommands.Count > 0)
            {
                btnUndo.IsEnabled = true;
            }
            TurnOnSelectMode();
        }
Example #2
0
        private void btnPaste_Click(object sender, RoutedEventArgs e)
        {
            btnUndo.IsEnabled = true;
            _copies.Clear();
            if (cuttedOrCopiedFigures.Count == 0)
            {
                nothingToPaste = true;
            }
            if (nothingToPaste == false)
            {
                double minX = cuttedOrCopiedFigures[0].CoordX, minY = cuttedOrCopiedFigures[0].CoordY;

                foreach (PetriNetNode figure in cuttedOrCopiedFigures)
                {
                    if (figure.CoordX < minX)
                    {
                        minX = figure.CoordX;
                    }
                    if (figure.CoordY < minY)
                    {
                        minY = figure.CoordY;
                    }
                }

                if (cutOrCopy == cutOrCopyMode.copy)
                {
                    var copiedF = new List <PetriNetNode>();
                    var copiedA = new List <VArc>();

                    UnselectFigures();//(cuttedOrCopiedFigures, cuttedOrCopiedArcs);

                    foreach (PetriNetNode copiedFigure in cuttedOrCopiedFigures)
                    {
                        var x = copiedFigure.CoordX - minX + ScrollViewerForMainModelCanvas.HorizontalOffset + 40;
                        var y = copiedFigure.CoordY - minY + ScrollViewerForMainModelCanvas.VerticalOffset + 40;

                        PetriNetNode newFigure;
                        if (copiedFigure is VPlace)
                        {
                            newFigure = VPlace.Create(x, y);
                            Net.places.Add((VPlace)newFigure);
                        }
                        else
                        {
                            newFigure = VTransition.Create(x, y);
                            Net.transitions.Add((VTransition)newFigure);
                        }
                        //SetOfFigures.Figures.Add(newFigure);
                        copiedF.Add(newFigure);

                        newFigure.Label = copiedFigure.Label;
                        VPlace place = newFigure as VPlace;
                        if (place != null)
                        {
                            place.NumberOfTokens = (copiedFigure as VPlace).NumberOfTokens;
                        }
                        newFigure.IsSelect = true;

                        DrawFigure(newFigure);
                        _copies.Add(copiedFigure, newFigure);
                        MakeSelected(newFigure);
                    }


                    foreach (PetriNetNode fig in cuttedOrCopiedFigures)
                    {
                        foreach (var arc in fig.ThisArcs)
                        {
                            if (!cuttedOrCopiedArcs.Contains(arc))
                            {
                                continue;
                            }

                            PetriNetNode fromF;
                            _copies.TryGetValue(arc.From, out fromF);
                            PetriNetNode toF;
                            _copies.TryGetValue(arc.To, out toF);
                            var arcs = from ar in Net.arcs where ar.From == fromF && ar.To == toF select ar;

                            if (arcs.Any())
                            {
                                continue;
                            }

                            var newArc = new VArc(fromF, toF)
                            {
                                IsDirected = arc.IsDirected,
                                Weight     = arc.Weight
                            };
                            newArc.AddToThisArcsLists();
                            DrawArc(newArc);

                            if (newArc.IsDirected)
                            {
                                var lineVisible = GetKeyByValueForArcs(newArc, DictionaryForArcs);
                                DrawArrowHeads(lineVisible);
                                GetKeyByValueForArcs(newArc, DictionaryForArrowHeads1).MouseDown += MouseArcDown;
                                GetKeyByValueForArcs(newArc, DictionaryForArrowHeads2).MouseDown += MouseArcDown;
                            }

                            newArc.IsSelect = true;
                            _selectedArcs.Add(newArc);
                            ColorArrow(newArc);
                            Net.arcs.Add(newArc);
                            copiedA.Add(newArc);
                        }
                    }
                    var newCommand = new PasteCommand(copiedF, copiedA);
                    Command.ExecutedCommands.Push(newCommand);
                }
                else if (cutOrCopy == cutOrCopyMode.cut && _numberOfPastes == 0)
                {
                    _numberOfPastes++;
                    foreach (var figure in cuttedOrCopiedFigures)
                    {
                        foreach (var arc in figure.ThisArcs)
                        {
                            if (!cuttedOrCopiedArcs.Contains(arc))
                            {
                                cuttedOrCopiedArcs.Add(arc);
                            }
                        }
                        figure.CoordX += -minX + ScrollViewerForMainModelCanvas.HorizontalOffset;
                        figure.CoordY += -minY + ScrollViewerForMainModelCanvas.VerticalOffset;
                    }

                    var toDelete = PasteFiguresAndArcs(cuttedOrCopiedFigures, cuttedOrCopiedArcs);
                    foreach (var arc in toDelete)
                    {
                        cuttedOrCopiedArcs.Remove(arc);
                        arc.IsSelect = false;
                        _selectedArcs.Remove(arc);
                        Net.arcs.Remove(arc);
                    }

                    var figures = new List <PetriNetNode>();
                    var arcs1   = new List <VArc>();
                    figures.AddRange(cuttedOrCopiedFigures);
                    arcs1.AddRange(cuttedOrCopiedArcs);

                    var newCommand = new PasteCommand(figures, arcs1);
                    Command.ExecutedCommands.Push(newCommand);
                }
                Command.CanceledCommands.Clear();
            }
            _figuresBeforeDrag = CopyListOfFigures(Net.Nodes);
            ReassignSelectedProperties();
            TurnOnSelectMode();
            btnPaste.Focusable = false;
        }