From e0bc93eb59673def9477f386ad625652c1073c6f Mon Sep 17 00:00:00 2001 From: Justin Aquadro Date: Fri, 9 Nov 2012 00:14:23 -0500 Subject: [PATCH] Refactored Clipboard model to separate UX layer. --- Model/TagCompoundDataNode.cs | 6 +-- Model/TagDataNode.cs | 6 +-- Model/TagListDataNode.cs | 13 ++++--- NBTExplorer.csproj | 2 + NbtClipboardController.cs | 51 ++++++++++++++++++++++++ NbtClipboardData.cs | 57 +++++++++++++-------------- Windows/MainForm.cs | 13 ++++--- Windows/NbtClipboardControllerWin.cs | 58 ++++++++++++++++++++++++++++ 8 files changed, 158 insertions(+), 48 deletions(-) create mode 100644 NbtClipboardController.cs create mode 100644 Windows/NbtClipboardControllerWin.cs diff --git a/Model/TagCompoundDataNode.cs b/Model/TagCompoundDataNode.cs index ad09b87..8063c21 100644 --- a/Model/TagCompoundDataNode.cs +++ b/Model/TagCompoundDataNode.cs @@ -40,7 +40,7 @@ namespace NBTExplorer.Model public override bool CanPasteIntoNode { - get { return NbtClipboardData.ContainsData; } + get { return NbtClipboardController.ContainsData; } } public override bool CreateNode (TagType type) @@ -68,8 +68,8 @@ namespace NBTExplorer.Model if (!CanPasteIntoNode) return false; - NbtClipboardData clipboard = NbtClipboardData.CopyFromClipboard(); - if (clipboard.Node == null) + NbtClipboardData clipboard = NbtClipboardController.CopyFromClipboard(); + if (clipboard == null || clipboard.Node == null) return false; string name = clipboard.Name; diff --git a/Model/TagDataNode.cs b/Model/TagDataNode.cs index 48235f9..0d44ceb 100644 --- a/Model/TagDataNode.cs +++ b/Model/TagDataNode.cs @@ -238,8 +238,7 @@ namespace NBTExplorer.Model public override bool CopyNode () { if (CanCopyNode) { - NbtClipboardData clip = new NbtClipboardData(NodeName, Tag); - clip.CopyToClipboard(); + NbtClipboardController.CopyToClipboard(new NbtClipboardData(NodeName, Tag)); return true; } @@ -249,8 +248,7 @@ namespace NBTExplorer.Model public override bool CutNode () { if (CanCutNode) { - NbtClipboardData clip = new NbtClipboardData(NodeName, Tag); - clip.CopyToClipboard(); + NbtClipboardController.CopyToClipboard(new NbtClipboardData(NodeName, Tag)); TagParent.DeleteTag(Tag); Parent.Nodes.Remove(this); diff --git a/Model/TagListDataNode.cs b/Model/TagListDataNode.cs index ecc630b..3a9c26b 100644 --- a/Model/TagListDataNode.cs +++ b/Model/TagListDataNode.cs @@ -40,9 +40,12 @@ namespace NBTExplorer.Model { get { - if (NbtClipboardData.ContainsData) { - TagNode node = NbtClipboardData.CopyFromClipboard().Node; - if (node != null && node.GetTagType() == Tag.ValueType) + if (NbtClipboardController.ContainsData) { + NbtClipboardData data = NbtClipboardController.CopyFromClipboard(); + if (data == null) + return false; + + if (data.Node != null && data.Node.GetTagType() == Tag.ValueType) return true; } @@ -68,8 +71,8 @@ namespace NBTExplorer.Model if (!CanPasteIntoNode) return false; - NbtClipboardData clipboard = NbtClipboardData.CopyFromClipboard(); - if (clipboard.Node == null) + NbtClipboardData clipboard = NbtClipboardController.CopyFromClipboard(); + if (clipboard == null || clipboard.Node == null) return false; AppendTag(clipboard.Node); diff --git a/NBTExplorer.csproj b/NBTExplorer.csproj index a68738f..5c1d29d 100644 --- a/NBTExplorer.csproj +++ b/NBTExplorer.csproj @@ -81,6 +81,7 @@ + Form @@ -192,6 +193,7 @@ + CancelSearchForm.cs diff --git a/NbtClipboardController.cs b/NbtClipboardController.cs new file mode 100644 index 0000000..6a4c5ce --- /dev/null +++ b/NbtClipboardController.cs @@ -0,0 +1,51 @@ +using System; + +namespace NBTExplorer +{ + public static class NbtClipboardController + { + private static INbtClipboardController _instance; + + public static void Initialize (INbtClipboardController controller) + { + _instance = controller; + } + + public static bool IsInitialized + { + get { return _instance != null; } + } + + public static bool ContainsData + { + get + { + if (_instance == null) + return false; + return _instance.ContainsData; + } + } + + public static NbtClipboardData CopyFromClipboard () + { + if (_instance == null) + return null; + return _instance.CopyFromClipboard(); + } + + public static void CopyToClipboard (NbtClipboardData data) + { + if (_instance == null) + return; + _instance.CopyToClipboard(data); + } + } + + public interface INbtClipboardController + { + bool ContainsData { get; } + + void CopyToClipboard (NbtClipboardData data); + NbtClipboardData CopyFromClipboard (); + } +} diff --git a/NbtClipboardData.cs b/NbtClipboardData.cs index 02ea8bf..271cfac 100644 --- a/NbtClipboardData.cs +++ b/NbtClipboardData.cs @@ -1,53 +1,51 @@ using System; using System.IO; -using System.Windows.Forms; using Substrate.Nbt; namespace NBTExplorer { - [Serializable] public class NbtClipboardData { - public string Name; + private string _name; + private TagNode _node; - private byte[] _data; - - [NonSerialized] - public TagNode Node; - - public NbtClipboardData (String name, TagNode node) + public string Name { - Name = name; + get { return _name; } + set { _name = value; } + } + public TagNode Node + { + get { return _node; } + set { _node = value; } + } + + public NbtClipboardData (string name, TagNode node) + { + _name = name; + _node = node; + } + + public static byte[] SerializeNode (TagNode node) + { TagNodeCompound root = new TagNodeCompound(); root.Add("root", node); NbtTree tree = new NbtTree(root); using (MemoryStream ms = new MemoryStream()) { tree.WriteTo(ms); - _data = new byte[ms.Length]; - Array.Copy(ms.GetBuffer(), _data, ms.Length); + byte[] data = new byte[ms.Length]; + Array.Copy(ms.GetBuffer(), data, ms.Length); + + return data; } } - public static bool ContainsData + public static TagNode DeserializeNode (byte[] data) { - get { return Clipboard.ContainsData(typeof(NbtClipboardData).FullName); } - } - - public void CopyToClipboard () - { - Clipboard.SetData(typeof(NbtClipboardData).FullName, this); - } - - public static NbtClipboardData CopyFromClipboard () - { - NbtClipboardData clip = Clipboard.GetData(typeof(NbtClipboardData).FullName) as NbtClipboardData; - if (clip == null) - return null; - NbtTree tree = new NbtTree(); - using (MemoryStream ms = new MemoryStream(clip._data)) { + using (MemoryStream ms = new MemoryStream(data)) { tree.ReadFrom(ms); } @@ -55,8 +53,7 @@ namespace NBTExplorer if (root == null || !root.ContainsKey("root")) return null; - clip.Node = root["root"]; - return clip; + return root["root"]; } } } diff --git a/Windows/MainForm.cs b/Windows/MainForm.cs index bf6bbdf..5580722 100644 --- a/Windows/MainForm.cs +++ b/Windows/MainForm.cs @@ -45,6 +45,7 @@ namespace NBTExplorer.Windows InitializeComponent(); InitializeIconRegistry(); FormHandlers.Register(); + NbtClipboardController.Initialize(new NbtClipboardControllerWin()); FormClosing += MainForm_Closing; @@ -662,21 +663,21 @@ namespace NBTExplorer.Windows _buttonAddTagString.Enabled = node.CanCreateTag(TagType.TAG_STRING); _buttonSave.Enabled = CheckModifications(); - _buttonCopy.Enabled = node.CanCopyNode; - _buttonCut.Enabled = node.CanCutNode; + _buttonCopy.Enabled = node.CanCopyNode && NbtClipboardController.IsInitialized; + _buttonCut.Enabled = node.CanCutNode && NbtClipboardController.IsInitialized; _buttonDelete.Enabled = node.CanDeleteNode; _buttonEdit.Enabled = node.CanEditNode; _buttonFindNext.Enabled = node.CanSearchNode || _searchState != null; - _buttonPaste.Enabled = node.CanPasteIntoNode; + _buttonPaste.Enabled = node.CanPasteIntoNode && NbtClipboardController.IsInitialized; _buttonRename.Enabled = node.CanRenameNode; _menuItemSave.Enabled = _buttonSave.Enabled; - _menuItemCopy.Enabled = node.CanCopyNode; - _menuItemCut.Enabled = node.CanCutNode; + _menuItemCopy.Enabled = node.CanCopyNode && NbtClipboardController.IsInitialized; + _menuItemCut.Enabled = node.CanCutNode && NbtClipboardController.IsInitialized; _menuItemDelete.Enabled = node.CanDeleteNode; _menuItemEditValue.Enabled = node.CanEditNode; _menuItemFind.Enabled = node.CanSearchNode; - _menuItemPaste.Enabled = node.CanPasteIntoNode; + _menuItemPaste.Enabled = node.CanPasteIntoNode && NbtClipboardController.IsInitialized; _menuItemRename.Enabled = node.CanRenameNode; _menuItemFind.Enabled = node.CanSearchNode; _menuItemFindNext.Enabled = _searchState != null; diff --git a/Windows/NbtClipboardControllerWin.cs b/Windows/NbtClipboardControllerWin.cs new file mode 100644 index 0000000..36425d6 --- /dev/null +++ b/Windows/NbtClipboardControllerWin.cs @@ -0,0 +1,58 @@ +using System; +using System.Windows.Forms; +using Substrate.Nbt; + +namespace NBTExplorer.Windows +{ + public class NbtClipboardControllerWin : INbtClipboardController + { + public bool ContainsData + { + get { return Clipboard.ContainsData(typeof(NbtClipboardDataWin).FullName); } + } + + public void CopyToClipboard (NbtClipboardData data) + { + NbtClipboardDataWin dataWin = new NbtClipboardDataWin(data); + Clipboard.SetData(typeof(NbtClipboardDataWin).FullName, dataWin); + } + + public NbtClipboardData CopyFromClipboard () + { + NbtClipboardDataWin clip = Clipboard.GetData(typeof(NbtClipboardDataWin).FullName) as NbtClipboardDataWin; + if (clip == null) + return null; + + TagNode node = clip.Node; + if (node == null) + return null; + + return new NbtClipboardData(clip.Name, node); + } + } + + [Serializable] + public class NbtClipboardDataWin + { + private string _name; + private byte[] _data; + + public NbtClipboardDataWin (NbtClipboardData data) + { + Name = data.Name; + Node = data.Node; + } + + public string Name + { + get { return _name; } + set { _name = value; } + } + + public TagNode Node + { + get { return NbtClipboardData.DeserializeNode(_data); } + set { _data = NbtClipboardData.SerializeNode(value); } + } + } +}