Refactored Clipboard model to separate UX layer.

This commit is contained in:
Justin Aquadro 2012-11-09 00:14:23 -05:00
parent 20b3640783
commit e0bc93eb59
8 changed files with 158 additions and 48 deletions

View file

@ -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;

View file

@ -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);

View file

@ -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);

View file

@ -81,6 +81,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="FormRegistry.cs" />
<Compile Include="NbtClipboardController.cs" />
<Compile Include="Windows\CancelSearchForm.cs">
<SubType>Form</SubType>
</Compile>
@ -192,6 +193,7 @@
<Compile Include="Vendor\Be.Windows.Forms.HexBox\NativeMethods.cs" />
<Compile Include="Vendor\Be.Windows.Forms.HexBox\Util.cs" />
<Compile Include="Windows\FormHandlers.cs" />
<Compile Include="Windows\NbtClipboardControllerWin.cs" />
<EmbeddedResource Include="Windows\CancelSearchForm.resx">
<DependentUpon>CancelSearchForm.cs</DependentUpon>
</EmbeddedResource>

51
NbtClipboardController.cs Normal file
View file

@ -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 ();
}
}

View file

@ -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"];
}
}
}

View file

@ -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;

View file

@ -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); }
}
}
}