StephaneLenclud@238: using System;
StephaneLenclud@238: using System.Collections.Generic;
StephaneLenclud@238: using System.Linq;
StephaneLenclud@238: using System.Text;
StephaneLenclud@238: using System.Threading.Tasks;
StephaneLenclud@238: using System.Runtime.Serialization;
StephaneLenclud@244: using System.ComponentModel;
StephaneLenclud@238:
StephaneLenclud@238: namespace SharpLib.Ear
StephaneLenclud@238: {
StephaneLenclud@238:
StephaneLenclud@238: ///
StephaneLenclud@238: /// EAR object provides serialization support.
StephaneLenclud@238: /// It assumes most derived class is decorated with AttributeObject.
StephaneLenclud@238: ///
StephaneLenclud@238: [DataContract]
StephaneLenclud@238: [KnownType("DerivedTypes")]
StephaneLenclud@244: public abstract class Object: IComparable, INotifyPropertyChanged
StephaneLenclud@238: {
Stephane@243: private bool iConstructed = false;
Stephane@243:
StephaneLenclud@265: [DataMember]
StephaneLenclud@265: public List Objects = new List();
StephaneLenclud@265:
StephaneLenclud@265: ///
StephaneLenclud@265: ///
StephaneLenclud@265: [DataMember]
StephaneLenclud@265: [AttributeObjectProperty
StephaneLenclud@265: (
StephaneLenclud@265: Id = "Object.Name",
StephaneLenclud@265: Name = "Name",
StephaneLenclud@265: Description = "Given object name."
StephaneLenclud@265: )
StephaneLenclud@265: ]
StephaneLenclud@265: public string Name { get; set; } = "";
StephaneLenclud@265:
StephaneLenclud@265:
StephaneLenclud@244: protected Object()
Stephane@243: {
Stephane@243: Construct();
Stephane@243: }
Stephane@243:
Stephane@243: ///
Stephane@243: /// Needed as our constructor is not called following internalization.
Stephane@243: ///
Stephane@243: public void Construct()
Stephane@243: {
StephaneLenclud@265: //Construct ourselves first
Stephane@243: if (!iConstructed)
Stephane@243: {
Stephane@243: DoConstruct();
Stephane@243: iConstructed = true;
Stephane@243: }
StephaneLenclud@265:
StephaneLenclud@265: //Then construct our children
StephaneLenclud@265: foreach (Object o in Objects)
StephaneLenclud@265: {
StephaneLenclud@265: o.Construct();
StephaneLenclud@265: }
Stephane@243: }
Stephane@243:
Stephane@243: ///
Stephane@243: ///
Stephane@243: ///
Stephane@243: protected virtual void DoConstruct()
Stephane@243: {
StephaneLenclud@265: //Make sure our name is not null
StephaneLenclud@265: if (Name == null)
StephaneLenclud@265: {
StephaneLenclud@265: Name = "";
StephaneLenclud@265: }
Stephane@243:
StephaneLenclud@265: // Makes sure our objects are not null
StephaneLenclud@265: if (Objects == null)
StephaneLenclud@265: {
StephaneLenclud@265: Objects = new List();
StephaneLenclud@265: }
Stephane@243: }
Stephane@243:
StephaneLenclud@246: public enum State
StephaneLenclud@246: {
StephaneLenclud@246: Rest=0,
StephaneLenclud@246: Edit
StephaneLenclud@246: }
StephaneLenclud@246:
StephaneLenclud@246: State iCurrentState = State.Rest;
StephaneLenclud@246: public State CurrentState { get { return iCurrentState; } set { OnStateLeave(); iCurrentState = value; OnStateEnter(); } }
StephaneLenclud@246:
StephaneLenclud@246: ///
StephaneLenclud@246: ///
StephaneLenclud@246: ///
StephaneLenclud@246: protected virtual void OnStateLeave()
StephaneLenclud@246: {
StephaneLenclud@246:
StephaneLenclud@246: }
StephaneLenclud@246:
StephaneLenclud@246: ///
StephaneLenclud@246: ///
StephaneLenclud@246: ///
StephaneLenclud@246: protected virtual void OnStateEnter()
StephaneLenclud@246: {
StephaneLenclud@246:
StephaneLenclud@246: }
StephaneLenclud@246:
StephaneLenclud@239: ///
StephaneLenclud@239: /// Static object name.
StephaneLenclud@239: ///
StephaneLenclud@260: public string AttributeName
StephaneLenclud@238: {
StephaneLenclud@238: //Get the name of this object attribute
StephaneLenclud@238: get { return Utils.Reflection.GetAttribute(GetType()).Name; }
StephaneLenclud@238: private set { }
StephaneLenclud@238: }
StephaneLenclud@238:
StephaneLenclud@239: ///
StephaneLenclud@239: /// Static object description.
StephaneLenclud@239: ///
StephaneLenclud@260: public string AttributeDescription
StephaneLenclud@238: {
StephaneLenclud@238: //Get the description of this object attribute
StephaneLenclud@238: get { return Utils.Reflection.GetAttribute(GetType()).Description; }
StephaneLenclud@238: private set { }
StephaneLenclud@238: }
StephaneLenclud@238:
StephaneLenclud@239: ///
StephaneLenclud@239: /// Dynamic object description.
StephaneLenclud@239: ///
StephaneLenclud@239: ///
StephaneLenclud@238: public virtual string Brief()
StephaneLenclud@238: {
StephaneLenclud@260: return AttributeName;
StephaneLenclud@238: }
StephaneLenclud@238:
StephaneLenclud@239: ///
StephaneLenclud@239: /// Needed to make sure our sorting makes sense
StephaneLenclud@239: ///
StephaneLenclud@239: ///
StephaneLenclud@239: ///
StephaneLenclud@238: public int CompareTo(object obj)
StephaneLenclud@238: {
StephaneLenclud@238: //Sort by object name
StephaneLenclud@238: return Utils.Reflection.GetAttribute(GetType()).Name.CompareTo(obj.GetType());
StephaneLenclud@238: }
StephaneLenclud@238:
StephaneLenclud@238: ///
StephaneLenclud@239: /// Tells whether the current object configuration is valid.
StephaneLenclud@239: ///
StephaneLenclud@239: ///
StephaneLenclud@239: public virtual bool IsValid()
StephaneLenclud@239: {
StephaneLenclud@239: return true;
StephaneLenclud@239: }
StephaneLenclud@239:
StephaneLenclud@239: ///
StephaneLenclud@238: /// So that data contract knows all our types.
StephaneLenclud@238: ///
StephaneLenclud@238: ///
StephaneLenclud@238: private static IEnumerable DerivedTypes()
StephaneLenclud@238: {
StephaneLenclud@238: return SharpLib.Utils.Reflection.GetDerivedTypes();
StephaneLenclud@238: }
StephaneLenclud@238:
StephaneLenclud@244: ///
StephaneLenclud@244: ///
StephaneLenclud@244: ///
StephaneLenclud@244: public event PropertyChangedEventHandler PropertyChanged;
StephaneLenclud@244:
StephaneLenclud@246:
StephaneLenclud@244: ///
StephaneLenclud@244: /// Invoke our event.
StephaneLenclud@244: ///
StephaneLenclud@244: ///
StephaneLenclud@244: protected void OnPropertyChanged(string name)
StephaneLenclud@244: {
StephaneLenclud@244: PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
StephaneLenclud@244: }
StephaneLenclud@244:
StephaneLenclud@244:
StephaneLenclud@238: }
StephaneLenclud@238: }