SharpLibEar/Object.cs
author StephaneLenclud
Thu, 25 Aug 2016 13:34:05 +0200
changeset 255 c57b8ac80fc6
parent 246 30a221eecc06
child 260 d44943088c67
permissions -rw-r--r--
Published v1.0.2.0.
Fixed Harmony async issue prevent the config to be fetched.
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 using System.Runtime.Serialization;
     7 using System.ComponentModel;
     8 
     9 namespace SharpLib.Ear
    10 {
    11 
    12     /// <summary>
    13     /// EAR object provides serialization support.
    14     /// It assumes most derived class is decorated with AttributeObject.
    15     /// </summary>
    16     [DataContract]
    17     [KnownType("DerivedTypes")]
    18     public abstract class Object: IComparable, INotifyPropertyChanged
    19     {
    20         private bool iConstructed = false;
    21 
    22         protected Object()
    23         {
    24             Construct();
    25         }
    26 
    27         /// <summary>
    28         /// Needed as our constructor is not called following internalization.
    29         /// </summary>
    30         public void Construct()
    31         {
    32             if (!iConstructed)
    33             {
    34                 DoConstruct();
    35                 iConstructed = true;
    36             }
    37         }
    38 
    39         /// <summary>
    40         /// 
    41         /// </summary>
    42         protected virtual void DoConstruct()
    43         {
    44 
    45         }
    46 
    47 
    48         public enum State
    49         {
    50             Rest=0,
    51             Edit
    52         }
    53 
    54         State iCurrentState = State.Rest;
    55         public State CurrentState { get { return iCurrentState; } set { OnStateLeave(); iCurrentState = value; OnStateEnter(); } }
    56 
    57         /// <summary>
    58         /// 
    59         /// </summary>
    60         protected virtual void OnStateLeave()
    61         {
    62 
    63         }
    64 
    65         /// <summary>
    66         /// 
    67         /// </summary>
    68         protected virtual void OnStateEnter()
    69         {
    70 
    71         }
    72 
    73         /// <summary>
    74         /// Static object name.
    75         /// </summary>
    76         public string Name
    77         {
    78             //Get the name of this object attribute
    79             get { return Utils.Reflection.GetAttribute<AttributeObject>(GetType()).Name; }
    80             private set { }
    81         }
    82 
    83         /// <summary>
    84         /// Static object description.
    85         /// </summary>
    86         public string Description
    87         {
    88             //Get the description of this object attribute
    89             get { return Utils.Reflection.GetAttribute<AttributeObject>(GetType()).Description; }
    90             private set { }
    91         }
    92 
    93         /// <summary>
    94         /// Dynamic object description.
    95         /// </summary>
    96         /// <returns></returns>
    97         public virtual string Brief()
    98         {
    99             return Name;
   100         }
   101 
   102         /// <summary>
   103         /// Needed to make sure our sorting makes sense
   104         /// </summary>
   105         /// <param name="obj"></param>
   106         /// <returns></returns>
   107         public int CompareTo(object obj)
   108         {
   109             //Sort by object name
   110             return Utils.Reflection.GetAttribute<AttributeObject>(GetType()).Name.CompareTo(obj.GetType());
   111         }
   112 
   113         /// <summary>
   114         /// Tells whether the current object configuration is valid.
   115         /// </summary>
   116         /// <returns></returns>
   117         public virtual bool IsValid()
   118         {
   119             return true;
   120         }
   121 
   122         /// <summary>
   123         /// So that data contract knows all our types.
   124         /// </summary>
   125         /// <returns></returns>
   126         private static IEnumerable<Type> DerivedTypes()
   127         {
   128             return SharpLib.Utils.Reflection.GetDerivedTypes<Object>();
   129         }
   130 
   131         /// <summary>
   132         /// 
   133         /// </summary>
   134         public event PropertyChangedEventHandler PropertyChanged;
   135 
   136 
   137         /// <summary>
   138         /// Invoke our event.
   139         /// </summary>
   140         /// <param name="name"></param>
   141         protected void OnPropertyChanged(string name)
   142         {
   143             PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
   144         }
   145 
   146 
   147     }
   148 }