|
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
namespace Bulasoft.Common.Elements
{
public class DefaultElement : IElement
{
public const string ElementName = "Name";
public const string IDName = "ID";
public DefaultElement()
{
_id = Guid.NewGuid();
SerializationAttribute attr = (SerializationAttribute)Attribute.GetCustomAttribute(GetType(), typeof(SerializationAttribute));
if (attr != null)
{
_fixedName = attr.FixedName;
_name = _fixedName;
}
}
private string _fixedName;
[Browsable(false)]
public string FixedName
{
get { return _fixedName; }
}
#region IElement Members
private Dictionary<string, object> _changes;
[Browsable(false)]
public Dictionary<string, object> Changes
{
get { return _changes; }
}
public virtual void AcceptChanges()
{
_changes = null;
}
private IElement _container;
[Browsable(false)]
public IElement Container
{
get { return _container; }
set { _container = value; }
}
[Browsable(false)]
public virtual bool ElementChanged
{
get
{
return ((State & ElementState.MaskChanges) > 0) ||
((Changes != null) && (Changes.Count > 0));
}
}
private Guid? _id;
[Serialization(IDName)]
[Browsable(false)]
public virtual Guid? ID
{
get { return _id; }
set { _id = value; }
}
private bool _isDeleted;
public virtual bool IsDeleted
{
get { return _isDeleted; }
set { _isDeleted = value; }
}
private string _name;
[Serialization(ElementName, SerializationSettings.SerializeAsAttribute)]
[Category("Design"), Description("Specifies the name of the object used in expressions"), RefreshProperties(RefreshProperties.Repaint)]
public virtual string Name
{
get { return _name; }
set
{
if (!String.Equals(_name, value))
{
if ((_fixedName != null) && (value != _fixedName))
throw new Exception("Cannot change name for permanent named object");
LogChange(ElementName, _name, value);
_name = value;
}
}
}
private ElementState _state = ElementState.New;
[Browsable(false)]
public ElementState State
{
get { return _state; }
set { _state = value; }
}
protected List<IElement> containerElements = new List<IElement>();
public virtual void AddElement(IElement element)
{
containerElements.Add(element);
}
public virtual bool RemoveElement(IElement element)
{
return containerElements.Remove(element);
}
#endregion
#region IEnumerable Members
public virtual IEnumerator GetEnumerator()
{
return containerElements.GetEnumerator();
}
#endregion
protected virtual void LogChange(string propertyName, object orgValue, object newValue)
{
if ((State & ElementState.New) != 0)
MarkModified(ElementState.ThisModified);
else
{
if (TrackChanges())
{
object TempValue;
if ((Changes != null) && Changes.TryGetValue(propertyName, out TempValue) && Equals(TempValue, newValue))
{
Changes.Remove(propertyName);
State &= ~ElementState.ThisModified;
IElement container = ((IElement)this).Container;
while (container != null)
{
foreach (Element element in container)
{
if (element.ElementChanged)
break;
}
State &= ~ElementState.ChildrenModified;
container = container.Container;
}
}
else
{
if (Changes == null)
_changes = new Dictionary<string, object>();
Changes[propertyName] = orgValue;
MarkModified(ElementState.ThisModified);
}
}
}
}
protected virtual void MarkModified(ElementState addedFlags)
{
if ((_container != null) && ((_container.State & ElementState.ChildrenModified) == 0))
{
if (_container is DefaultElement)
((DefaultElement)_container).MarkModified(ElementState.ChildrenModified);
}
State |= addedFlags;
}
protected virtual bool TrackChanges()
{
return true;
}
public override string ToString()
{
return Name;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.