Added root node renaming and container manipulation capability

This commit is contained in:
Justin Aquadro 2013-02-05 02:14:52 -05:00
parent 24c349ce50
commit d27234e2cd
6 changed files with 143 additions and 7 deletions

View file

@ -39,6 +39,7 @@ namespace NBTExplorer
}
public String Value { get; set; }
public bool AllowEmpty { get; set; }
}
public class RestrictedStringFormData : StringFormData

View file

@ -3,6 +3,7 @@ using System.Text.RegularExpressions;
using Substrate.Core;
using Substrate.Nbt;
using System.Collections.Generic;
using System;
namespace NBTExplorer.Model
{
@ -59,7 +60,8 @@ namespace NBTExplorer.Model
return NodeCapabilities.CreateTag
| NodeCapabilities.PasteInto
| NodeCapabilities.Search
| NodeCapabilities.Refresh;
| NodeCapabilities.Refresh
| NodeCapabilities.Rename;
}
}
@ -70,7 +72,17 @@ namespace NBTExplorer.Model
public override string NodeDisplay
{
get { return NodeName; }
get
{
if (_tree != null && _tree.Root != null) {
if (!string.IsNullOrEmpty(_tree.Name))
return NodeName + " [" + _tree.Name + ": " + _tree.Root.Count + " entries]";
else
return NodeName + " [" + _tree.Root.Count + " entries]";
}
else
return NodeName;
}
}
public override bool HasUnexpandedChildren
@ -78,6 +90,8 @@ namespace NBTExplorer.Model
get { return !IsExpanded; }
}
//private TagNodeCompound _metaRoot;
protected override void ExpandCore ()
{
if (_tree == null) {
@ -85,10 +99,20 @@ namespace NBTExplorer.Model
_tree = new NbtTree();
_tree.ReadFrom(file.GetDataInputStream(_compressionType));
if (_tree.Root != null)
//_metaRoot = new TagNodeCompound();
if (_tree.Root != null) {
//_metaRoot.Add(_tree.Name, _tree.Root);
_container = new CompoundTagContainer(_tree.Root);
}
}
/*foreach (TagNode tag in _metaRoot.Values) {
TagDataNode node = TagDataNode.CreateFromTag(tag);
if (node != null)
Nodes.Add(node);
}*/
foreach (TagNode tag in _tree.Root.Values) {
TagDataNode node = TagDataNode.CreateFromTag(tag);
if (node != null)
@ -119,6 +143,78 @@ namespace NBTExplorer.Model
return true;
}
public override bool CanRenameNode
{
get { return _tree != null; }
}
public override bool RenameNode ()
{
if (CanRenameNode && FormRegistry.EditString != null) {
RestrictedStringFormData data = new RestrictedStringFormData(_tree.Name ?? "") {
AllowEmpty = true,
};
if (FormRegistry.RenameTag(data)) {
if (_tree.Name != data.Value) {
_tree.Name = data.Value;
IsDataModified = true;
return true;
}
}
}
return false;
}
public override bool CanCreateTag (TagType type)
{
return _tree != null && _tree.Root != null && Enum.IsDefined(typeof(TagType), type) && type != TagType.TAG_END;
}
public override bool CanPasteIntoNode
{
get { return _tree != null && _tree.Root != null && NbtClipboardController.ContainsData; }
}
public override bool CreateNode (TagType type)
{
if (!CanCreateTag(type))
return false;
if (FormRegistry.CreateNode != null) {
CreateTagFormData data = new CreateTagFormData() {
TagType = type,
HasName = true,
};
data.RestrictedNames.AddRange(_container.TagNamesInUse);
if (FormRegistry.CreateNode(data)) {
AddTag(data.TagNode, data.TagName);
return true;
}
}
return false;
}
public override bool PasteNode ()
{
if (!CanPasteIntoNode)
return false;
NbtClipboardData clipboard = NbtClipboardController.CopyFromClipboard();
if (clipboard == null || clipboard.Node == null)
return false;
string name = clipboard.Name;
if (String.IsNullOrEmpty(name))
name = "UNNAMED";
AddTag(clipboard.Node, MakeUniqueName(name));
return true;
}
public bool IsNamedContainer
{
get { return true; }
@ -148,5 +244,35 @@ namespace NBTExplorer.Model
{
return _container.DeleteTag(tag);
}
private void AddTag (TagNode tag, string name)
{
_container.AddTag(tag, name);
IsDataModified = true;
if (IsExpanded) {
TagDataNode node = TagDataNode.CreateFromTag(tag);
if (node != null)
Nodes.Add(node);
}
}
private string MakeUniqueName (string name)
{
List<string> names = new List<string>(_container.TagNamesInUse);
if (!names.Contains(name))
return name;
int index = 1;
while (names.Contains(MakeCandidateName(name, index)))
index++;
return MakeCandidateName(name, index);
}
private string MakeCandidateName (string name, int index)
{
return name + " (Copy " + index + ")";
}
}
}

View file

@ -91,7 +91,7 @@ namespace NBTExplorer.Vendor.MultiSelectTreeView
TreeNode node = this.GetNodeAt( e.Location );
if( node != null )
{
int leftBound = node.Bounds.X; // - 20; // Allow user to click on image
int leftBound = node.Bounds.X - 20; // Allow user to click on image
int rightBound = node.Bounds.Right + 10; // Give a little extra room
if( e.Location.X > leftBound && e.Location.X < rightBound )
{

View file

@ -31,6 +31,8 @@ namespace NBTExplorer.Windows
get { return _invalidNames; }
}
public bool AllowEmpty { get; set; }
public bool IsModified
{
get { return _name != _originalName; }
@ -52,7 +54,7 @@ namespace NBTExplorer.Windows
private bool ValidateNameInput ()
{
string text = _nameField.Text.Trim();
if (String.IsNullOrEmpty(text)) {
if (String.IsNullOrEmpty(text) && !AllowEmpty) {
MessageBox.Show("You must provide a nonempty name.");
return false;
}

View file

@ -34,7 +34,9 @@ namespace NBTExplorer.Windows
public static bool RenameTagHandler (RestrictedStringFormData data)
{
EditName form = new EditName(data.Value);
EditName form = new EditName(data.Value) {
AllowEmpty = data.AllowEmpty,
};
form.InvalidNames.AddRange(data.RestrictedValues);
if (form.ShowDialog() == DialogResult.OK && form.IsModified) {

View file

@ -323,8 +323,11 @@ namespace NBTExplorer.Windows
node.Nodes.Clear();
DataNode backNode = node.Tag as DataNode;
if (!backNode.IsExpanded)
if (!backNode.IsExpanded) {
backNode.Expand();
node.Text = backNode.NodeDisplay;
UpdateUI(backNode);
}
foreach (DataNode child in backNode.Nodes)
node.Nodes.Add(CreateUnexpandedNode(child));
@ -340,6 +343,8 @@ namespace NBTExplorer.Windows
return;
backNode.Collapse();
node.Text = backNode.NodeDisplay;
UpdateUI(backNode);
node.Nodes.Clear();
if (backNode.HasUnexpandedChildren)