From 565ab9489d4468d81aa047086664763a87b3730e Mon Sep 17 00:00:00 2001 From: Tim Young Date: Thu, 15 Mar 2018 17:12:20 -0500 Subject: [PATCH] initial ctrl-z working --- EduNetworkBuilder/AnimationClass.cs | 1 + EduNetworkBuilder/LanguageString.cs | 2 + EduNetworkBuilder/NB.cs | 4 +- EduNetworkBuilder/NetShape.cs | 1 + EduNetworkBuilder/Network.cs | 1 + EduNetworkBuilder/NetworkBuilder.cs | 91 ++++++++++++++++++++++++++-- EduNetworkBuilder/SchoolworkClass.cs | 1 + EduNetworkBuilder/TraversalClass.cs | 1 + 8 files changed, 97 insertions(+), 5 deletions(-) diff --git a/EduNetworkBuilder/AnimationClass.cs b/EduNetworkBuilder/AnimationClass.cs index db65b04..eead2d9 100644 --- a/EduNetworkBuilder/AnimationClass.cs +++ b/EduNetworkBuilder/AnimationClass.cs @@ -9,6 +9,7 @@ using System.Windows.Forms; namespace EduNetworkBuilder { //Holds a little bit of information pertaining to the current animation. + [Serializable] public class AnimationClass { Point ImageStartPoint = new Point(); diff --git a/EduNetworkBuilder/LanguageString.cs b/EduNetworkBuilder/LanguageString.cs index 15a73e6..38f3645 100644 --- a/EduNetworkBuilder/LanguageString.cs +++ b/EduNetworkBuilder/LanguageString.cs @@ -8,6 +8,7 @@ using System.Text.RegularExpressions; namespace EduNetworkBuilder { + [Serializable] public class LanguageString { @@ -58,6 +59,7 @@ namespace EduNetworkBuilder } } + [Serializable] public class LanguageStrings { List TheStrings = new List(); diff --git a/EduNetworkBuilder/NB.cs b/EduNetworkBuilder/NB.cs index f12ec46..d48bd68 100644 --- a/EduNetworkBuilder/NB.cs +++ b/EduNetworkBuilder/NB.cs @@ -189,7 +189,7 @@ namespace EduNetworkBuilder internalIP = internal_ip; } } - + [Serializable] public class PingTestStatus { public string Source; @@ -211,6 +211,7 @@ namespace EduNetworkBuilder } } + [Serializable] public class PuzzleInfo { public string PuzzleName; @@ -320,6 +321,7 @@ namespace EduNetworkBuilder } } + [Serializable] public class HelpURL { public string URL = ""; diff --git a/EduNetworkBuilder/NetShape.cs b/EduNetworkBuilder/NetShape.cs index ef907e4..526e571 100644 --- a/EduNetworkBuilder/NetShape.cs +++ b/EduNetworkBuilder/NetShape.cs @@ -11,6 +11,7 @@ namespace EduNetworkBuilder /// /// For drawing shapes on a network. Rectangles, circles. Filled or not /// + [Serializable] public class NetShape { public NetShapeType MyShape = NetShapeType.none; diff --git a/EduNetworkBuilder/Network.cs b/EduNetworkBuilder/Network.cs index 4da3008..f1e0e43 100644 --- a/EduNetworkBuilder/Network.cs +++ b/EduNetworkBuilder/Network.cs @@ -45,6 +45,7 @@ namespace EduNetworkBuilder Image TheNetImage = new Bitmap(1024, 1024); Image TheNetImageBackground = new Bitmap(1024, 1024); public int itemsize = 100; //The size of network components + [NonSerialized] PictureBox myPBox = null; private int UniqueIdentifier = 100; //This gets used for all sorts of things. is auto-incremented every time someone asks for one private List myPackets = new List(); diff --git a/EduNetworkBuilder/NetworkBuilder.cs b/EduNetworkBuilder/NetworkBuilder.cs index e7eec29..873ead7 100644 --- a/EduNetworkBuilder/NetworkBuilder.cs +++ b/EduNetworkBuilder/NetworkBuilder.cs @@ -61,6 +61,7 @@ namespace EduNetworkBuilder public PersonClass CurrentUser; private List HelpURLs = new List(); + private List storedNetworkStates = new List(); //for ctrl-z going back in time to past state public BuilderWindow(string FirstArg="") { @@ -231,11 +232,55 @@ namespace EduNetworkBuilder } if (e.KeyCode == Keys.Z && e.Modifiers == Keys.Control) { - //ctrl-z + if(storedNetworkStates.Count > 0) + { + ChangeToPastState(storedNetworkStates[0]); + storedNetworkStates.RemoveAt(0); + } } //MessageBox.Show(e.KeyCode.ToString()); } + public void StoreNetworkState(Network toStore) + { + //We should verify something has changed before storing it. Oh well... + storedNetworkStates.Insert(0,Network.DeepClone(toStore)); + int maxCount = 30; + if(storedNetworkStates.Count > maxCount) + { + storedNetworkStates.RemoveRange(maxCount, storedNetworkStates.Count - maxCount); + } + } + + /// + /// We do this every time we load a new network or create a network from scratch + /// + public void ClearStoredNetworkStates() + { + storedNetworkStates.Clear(); + } + + /// + /// Store the new state after a change has been made. + /// Do this before every change (add, delete, move) + /// + public void ProcessChange() + { + StoreNetworkState(myNetwork); + } + + public void ChangeToPastState(Network OldNet) + { + if (OldNet != null) + { + myNetwork = OldNet; + myNetwork.RegisterDisplayArea(pbNetworkView); + UpdateMenu(); + UpdateForm(); + } + } + + /// /// Return the control with the given name. Used primarily to color help buttons /// @@ -1059,6 +1104,8 @@ namespace EduNetworkBuilder { if (ItemClickedOn != null) { + ProcessChange(); + ItemClickedOn.Hide(); UpdateLinks(); UpdateVisuals(); @@ -1069,6 +1116,7 @@ namespace EduNetworkBuilder { if (ItemClickedOn != null) { + ProcessChange(); ItemClickedOn.PowerOff = false; bool BlowUpOnce = myNetwork.ItemHasTest(ItemClickedOn.hostname, NetTestType.DeviceBlowsUpWithPower) && !myNetwork.ItemTestIsComplete(ItemClickedOn.hostname, NetTestType.DeviceBlowsUpWithPower); bool BlowUpMultiple = myNetwork.ItemHasTest(ItemClickedOn.hostname, NetTestType.DeviceNeedsUPS) && !myNetwork.ItemTestIsComplete(ItemClickedOn.hostname, NetTestType.DeviceNeedsUPS); @@ -1106,6 +1154,7 @@ namespace EduNetworkBuilder { if (ItemClickedOn != null) { + ProcessChange(); ItemClickedOn.PowerOff = true; //Mark the replace test as "done" myNetwork.RegisterDeviceReset(ItemClickedOn.hostname); @@ -1117,6 +1166,7 @@ namespace EduNetworkBuilder //We will still do this for devices that are spraying the network private void pbNetworkView_Replace_Click(object sender, EventArgs e) { + ProcessChange(); if (ItemClickedOn != null) { if (ItemClickedOn == null) return; @@ -1135,6 +1185,8 @@ namespace EduNetworkBuilder { if (ItemClickedOn != null) { + ProcessChange(); + if (ItemClickedOn == null) return; //Changing a UPS makes sure the power is off when done. ItemClickedOn.PowerOff = true; @@ -1182,6 +1234,7 @@ namespace EduNetworkBuilder private void pbNetworkView_RemoveLink_Click(object sender, EventArgs e) { + ProcessChange(); ToolStripItem thing = (ToolStripItem)sender; string released = thing.Text; released = Regex.Replace(released, ".* ", ""); @@ -1200,6 +1253,8 @@ namespace EduNetworkBuilder ToolStripItem thing = (ToolStripItem)sender; if (thing.Tag != null) { + ProcessChange(); + NetworkLink NL = (NetworkLink)thing.Tag; //This may delete the old link and make a new one LinkEditor LE = new LinkEditor(NL); @@ -1212,6 +1267,7 @@ namespace EduNetworkBuilder private void pbNetworkView_ReplaceNetLink_Click(object sender, EventArgs e) { + ProcessChange(); ToolStripItem thing = (ToolStripItem)sender; if (thing.Tag != null) { @@ -1234,6 +1290,7 @@ namespace EduNetworkBuilder private void pbNetworkView_RemoveNetLink_Click(object sender, EventArgs e) { + ProcessChange(); ToolStripItem thing = (ToolStripItem)sender; if (thing.Tag != null) { @@ -1249,6 +1306,8 @@ namespace EduNetworkBuilder private void pbNetworkView_DHCPRequest_Click(object sender, EventArgs e) { + ProcessChange(); + ItemClickedOn.DHCPRequestFromHere(); myNetwork.ProcessPackets(); UpdateMessages(); @@ -1260,6 +1319,7 @@ namespace EduNetworkBuilder private void pbNetworkView_Reset_Click(object sender, EventArgs e) { if (ItemClickedOn == null) return; + ProcessChange(); ItemClickedOn.ClearIPs(); //reset the device UpdateVisuals(); } @@ -1272,6 +1332,7 @@ namespace EduNetworkBuilder return; if (ItemClickedOn != null) { + ProcessChange(); DeviceConfig editwindow = new DeviceConfig(ItemClickedOn); editwindow.ShowDialog(); @@ -1302,6 +1363,8 @@ namespace EduNetworkBuilder { if(ShapeForEditing != null) { + ProcessChange(); + myNetwork.RemoveShape(ShapeForEditing); ShapeForEditing = null; UpdateForm(); @@ -1311,6 +1374,8 @@ namespace EduNetworkBuilder { if (ShapeForEditing != null) { + ProcessChange(); + ShapeEditor SE = new ShapeEditor(ShapeForEditing); SE.ShowDialog(); @@ -1322,11 +1387,14 @@ namespace EduNetworkBuilder private void pbNetworkView_Delete_Click(object sender, EventArgs e) { - if(ItemsSelected.Count ==0) + ProcessChange(); + if (ItemsSelected.Count == 0) + { TryDeleteOneItem(ItemClickedOn); + } else { - for(int i=ItemsSelected.Count -1; i>=0; i--) + for (int i = ItemsSelected.Count - 1; i >= 0; i--) { TryDeleteOneItem(ItemsSelected[i]); } @@ -1370,6 +1438,8 @@ namespace EduNetworkBuilder { if (ItemClickedOn == null) return; //we do not have something chosen to ping from ToolStripMenuItem Pressed = (ToolStripMenuItem)sender; + ProcessChange(); + string itemname = Pressed.Text; string dest = (string)Pressed.Tag; NB_IPAddress destination; @@ -1384,6 +1454,7 @@ namespace EduNetworkBuilder private void pbNetworkView_Traceroute_Name_Click(object sender, EventArgs e) { if (ItemClickedOn == null) return; //we do not have something chosen to ping from + ProcessChange(); ToolStripMenuItem Pressed = (ToolStripMenuItem)sender; string itemname = Pressed.Text; string dest = (string)Pressed.Tag; @@ -1428,6 +1499,7 @@ namespace EduNetworkBuilder private void pbNetworkView_ArpClear_Click(object sender, EventArgs e) { if (ItemClickedOn == null) return; //we do not have something chosen to arp request from + ProcessChange(); ItemClickedOn.ClearArps(); } @@ -1495,8 +1567,11 @@ namespace EduNetworkBuilder bool PoweredOff = false; //This mouse-up is part of a double-click operation. Do an edit. But only if we are powered on if (ItemClickedOn != null) PoweredOff = ItemClickedOn.PowerOff; - if(!PoweredOff) + if (!PoweredOff) + { + ProcessChange(); pbNetworkView_Edit_Click(sender, e); + } } else { @@ -1532,6 +1607,8 @@ namespace EduNetworkBuilder } else { + ProcessChange(); + //We just made a shape. try { @@ -1570,6 +1647,8 @@ namespace EduNetworkBuilder MouseIsDown = false; if (selectedButton == "btnLink") { + ProcessChange(); + //We are making a link LinkEditor myEditor = new LinkEditor(ItemClickedOn, ReleasedOn); myEditor.ShowDialog(); @@ -1578,6 +1657,8 @@ namespace EduNetworkBuilder } else if (ItemClickedOn == null) { + ProcessChange(); + NetworkComponent NC = null; switch (selectedButton) { @@ -1667,6 +1748,7 @@ namespace EduNetworkBuilder { if (!ItemClickedOn.IsLockedInLocation()) { + ProcessChange(); ItemClickedOn.ChangeLocation(CenteredLocation); ItemClickedOn.UnHide(); //If it was hidden, unhide it UpdateLinks(); @@ -1857,6 +1939,7 @@ namespace EduNetworkBuilder private void PrepForLoad() { processing = true; + ClearStoredNetworkStates(); myNetwork = new Network(""); myNetwork.RegisterDisplayArea(pbNetworkView); GC.Collect(); diff --git a/EduNetworkBuilder/SchoolworkClass.cs b/EduNetworkBuilder/SchoolworkClass.cs index ae446aa..a5f50e5 100644 --- a/EduNetworkBuilder/SchoolworkClass.cs +++ b/EduNetworkBuilder/SchoolworkClass.cs @@ -12,6 +12,7 @@ namespace EduNetworkBuilder /// /// A class holding a project that needs to be done as either a test, quiz, or homework /// + [Serializable] public class SchoolworkClass { public Network theProject = null; diff --git a/EduNetworkBuilder/TraversalClass.cs b/EduNetworkBuilder/TraversalClass.cs index b694412..fa8380f 100644 --- a/EduNetworkBuilder/TraversalClass.cs +++ b/EduNetworkBuilder/TraversalClass.cs @@ -6,6 +6,7 @@ using System.Threading.Tasks; namespace EduNetworkBuilder { + [Serializable] public class TraversalClass { protected struct TraversalRecord