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