NBTExplorer/Model/DataNode.cs

315 lines
7.3 KiB
C#
Raw Normal View History

2012-08-31 05:29:32 +00:00
using Substrate.Nbt;
2012-11-11 18:52:12 +00:00
using System.Collections.Generic;
2012-08-31 05:29:32 +00:00
namespace NBTExplorer.Model
{
2012-11-15 01:14:45 +00:00
public class RootDataNode : DataNode
{
private string _name = "Root";
private string _display = "";
public override string NodeName
{
get { return _name; }
set { _name = value; }
}
public override string NodeDisplay
{
get { return _display; }
set { _display = value; }
}
}
// FilterDataNode
// AndFilterDataNode
// OrFilterDataNode
2012-08-31 05:29:32 +00:00
public class DataNode
{
private DataNode _parent;
private DataNodeCollection _children;
private bool _expanded;
2012-11-11 18:52:12 +00:00
private bool _dataModified;
private bool _childModified;
2012-08-31 05:29:32 +00:00
public DataNode ()
{
_children = new DataNodeCollection(this);
}
public DataNode Parent
{
get { return _parent; }
internal set { _parent = value; }
}
public DataNodeCollection Nodes
{
get { return _children; }
}
public bool IsModified
{
2012-11-11 18:52:12 +00:00
get { return _dataModified || _childModified; }
}
protected bool IsDataModified
{
get { return _dataModified; }
set
{
_dataModified = value;
CalculateChildModifiedState();
}
}
protected bool IsChildModified
{
get { return _childModified; }
2012-08-31 05:29:32 +00:00
set
{
2012-11-11 18:52:12 +00:00
_childModified = value;
CalculateChildModifiedState();
2012-08-31 05:29:32 +00:00
}
}
2012-11-11 18:52:12 +00:00
protected bool IsParentModified
{
get { return Parent != null && Parent.IsModified; }
set
{
if (Parent != null)
Parent.IsDataModified = value;
}
}
private void CalculateChildModifiedState ()
{
_childModified = false;
foreach (DataNode child in Nodes)
if (child.IsModified)
_childModified = true;
if (Parent != null)
Parent.CalculateChildModifiedState();
}
2012-08-31 05:29:32 +00:00
public bool IsExpanded
{
get { return _expanded; }
private set { _expanded = value; }
}
public void Expand ()
{
if (!IsExpanded) {
ExpandCore();
IsExpanded = true;
}
}
protected virtual void ExpandCore () { }
public void Collapse ()
{
if (IsExpanded && !IsModified) {
Release();
IsExpanded = false;
}
}
public void Release ()
{
foreach (DataNode node in Nodes)
node.Release();
ReleaseCore();
IsExpanded = false;
2012-11-11 18:52:12 +00:00
IsDataModified = false;
2012-08-31 05:29:32 +00:00
}
protected virtual void ReleaseCore ()
{
Nodes.Clear();
}
2012-09-01 23:27:50 +00:00
public void Save ()
{
foreach (DataNode node in Nodes)
if (node.IsModified)
node.Save();
SaveCore();
2012-11-11 18:52:12 +00:00
IsDataModified = false;
2012-09-01 23:27:50 +00:00
}
protected virtual void SaveCore ()
{
}
2012-08-31 05:29:32 +00:00
public virtual string NodeName
{
get { return ""; }
}
public virtual string NodeDisplay
{
get { return ""; }
}
public virtual bool HasUnexpandedChildren
{
get { return false; }
}
2012-11-11 18:52:12 +00:00
protected Dictionary<string, object> BuildExpandSet (DataNode node)
{
if (node == null || !node.IsExpanded)
return null;
Dictionary<string, object> dict = new Dictionary<string, object>();
foreach (DataNode child in node.Nodes) {
Dictionary<string, object> childDict = BuildExpandSet(child);
if (childDict != null)
dict[child.NodeDisplay] = childDict;
}
return dict;
}
protected void RestoreExpandSet (DataNode node, Dictionary<string, object> expandSet)
{
node.Expand();
foreach (DataNode child in node.Nodes) {
if (expandSet.ContainsKey(child.NodeDisplay)) {
Dictionary<string, object> childDict = (Dictionary<string, object>)expandSet[child.NodeDisplay];
if (childDict != null)
RestoreExpandSet(child, childDict);
}
}
}
2012-08-31 05:29:32 +00:00
#region Capabilities
protected virtual NodeCapabilities Capabilities
{
get { return NodeCapabilities.None; }
}
public virtual bool CanRenameNode
{
get { return (Capabilities & NodeCapabilities.Rename) != NodeCapabilities.None; }
}
public virtual bool CanEditNode
{
get { return (Capabilities & NodeCapabilities.Edit) != NodeCapabilities.None; }
}
public virtual bool CanDeleteNode
{
get { return (Capabilities & NodeCapabilities.Delete) != NodeCapabilities.None; }
}
public virtual bool CanCopyNode
{
get { return (Capabilities & NodeCapabilities.Copy) != NodeCapabilities.None; }
}
public virtual bool CanCutNode
{
get { return (Capabilities & NodeCapabilities.Cut) != NodeCapabilities.None; }
}
public virtual bool CanPasteIntoNode
{
get { return (Capabilities & NodeCapabilities.PasteInto) != NodeCapabilities.None; }
}
public virtual bool CanSearchNode
{
get { return (Capabilities & NodeCapabilities.Search) != NodeCapabilities.None; }
}
2012-09-02 03:59:53 +00:00
public virtual bool CanReoderNode
{
get { return (Capabilities & NodeCapabilities.Reorder) != NodeCapabilities.None; }
}
2012-11-11 18:52:12 +00:00
public virtual bool CanRefreshNode
{
get { return (Capabilities & NodeCapabilities.Refresh) != NodeCapabilities.None; }
}
2012-09-02 03:59:53 +00:00
public virtual bool CanMoveNodeUp
{
get { return false; }
}
public virtual bool CanMoveNodeDown
{
get { return false; }
}
2012-08-31 05:29:32 +00:00
public virtual bool CanCreateTag (TagType type)
{
return false;
}
#endregion
#region Operations
public virtual bool CreateNode (TagType type)
{
return false;
}
public virtual bool RenameNode ()
{
return false;
}
public virtual bool EditNode ()
{
return false;
}
public virtual bool DeleteNode ()
{
return false;
}
public virtual bool CopyNode ()
{
return false;
}
public virtual bool CutNode ()
{
return false;
}
public virtual bool PasteNode ()
{
return false;
}
2012-09-02 03:59:53 +00:00
public virtual bool ChangeRelativePosition (int offset)
{
return false;
}
2012-11-11 18:52:12 +00:00
public virtual bool RefreshNode ()
{
return false;
}
2012-08-31 05:29:32 +00:00
#endregion
}
}