SharpLibEar/Object.cs
author StephaneLenclud
Sun, 26 Feb 2017 18:00:10 +0100
changeset 280 f90d1e101932
parent 260 d44943088c67
permissions -rw-r--r--
Published v1.4.5
Disable static display layout in release build.
StephaneLenclud@238
     1
using System;
StephaneLenclud@238
     2
using System.Collections.Generic;
StephaneLenclud@238
     3
using System.Linq;
StephaneLenclud@238
     4
using System.Text;
StephaneLenclud@238
     5
using System.Threading.Tasks;
StephaneLenclud@238
     6
using System.Runtime.Serialization;
StephaneLenclud@244
     7
using System.ComponentModel;
StephaneLenclud@238
     8
StephaneLenclud@238
     9
namespace SharpLib.Ear
StephaneLenclud@238
    10
{
StephaneLenclud@238
    11
StephaneLenclud@238
    12
    /// <summary>
StephaneLenclud@238
    13
    /// EAR object provides serialization support.
StephaneLenclud@238
    14
    /// It assumes most derived class is decorated with AttributeObject.
StephaneLenclud@238
    15
    /// </summary>
StephaneLenclud@238
    16
    [DataContract]
StephaneLenclud@238
    17
    [KnownType("DerivedTypes")]
StephaneLenclud@244
    18
    public abstract class Object: IComparable, INotifyPropertyChanged
StephaneLenclud@238
    19
    {
Stephane@243
    20
        private bool iConstructed = false;
Stephane@243
    21
StephaneLenclud@265
    22
        [DataMember]
StephaneLenclud@265
    23
        public List<Object> Objects = new List<Object>();
StephaneLenclud@265
    24
StephaneLenclud@265
    25
        /// <summary>
StephaneLenclud@265
    26
        /// </summary>
StephaneLenclud@265
    27
        [DataMember]
StephaneLenclud@265
    28
        [AttributeObjectProperty
StephaneLenclud@265
    29
            (
StephaneLenclud@265
    30
                Id = "Object.Name",
StephaneLenclud@265
    31
                Name = "Name",
StephaneLenclud@265
    32
                Description = "Given object name."
StephaneLenclud@265
    33
            )
StephaneLenclud@265
    34
        ]
StephaneLenclud@265
    35
        public string Name { get; set; } = "";
StephaneLenclud@265
    36
StephaneLenclud@265
    37
StephaneLenclud@244
    38
        protected Object()
Stephane@243
    39
        {
Stephane@243
    40
            Construct();
Stephane@243
    41
        }
Stephane@243
    42
Stephane@243
    43
        /// <summary>
Stephane@243
    44
        /// Needed as our constructor is not called following internalization.
Stephane@243
    45
        /// </summary>
Stephane@243
    46
        public void Construct()
Stephane@243
    47
        {
StephaneLenclud@265
    48
            //Construct ourselves first
Stephane@243
    49
            if (!iConstructed)
Stephane@243
    50
            {
Stephane@243
    51
                DoConstruct();
Stephane@243
    52
                iConstructed = true;
Stephane@243
    53
            }
StephaneLenclud@265
    54
StephaneLenclud@265
    55
            //Then construct our children
StephaneLenclud@265
    56
            foreach (Object o in Objects)
StephaneLenclud@265
    57
            {
StephaneLenclud@265
    58
                o.Construct();
StephaneLenclud@265
    59
            }
Stephane@243
    60
        }
Stephane@243
    61
Stephane@243
    62
        /// <summary>
Stephane@243
    63
        /// 
Stephane@243
    64
        /// </summary>
Stephane@243
    65
        protected virtual void DoConstruct()
Stephane@243
    66
        {
StephaneLenclud@265
    67
            //Make sure our name is not null
StephaneLenclud@265
    68
            if (Name == null)
StephaneLenclud@265
    69
            {
StephaneLenclud@265
    70
                Name = "";
StephaneLenclud@265
    71
            }
Stephane@243
    72
StephaneLenclud@265
    73
            // Makes sure our objects are not null
StephaneLenclud@265
    74
            if (Objects == null)
StephaneLenclud@265
    75
            {
StephaneLenclud@265
    76
                Objects = new List<Object>();
StephaneLenclud@265
    77
            }
Stephane@243
    78
        }
Stephane@243
    79
StephaneLenclud@246
    80
        public enum State
StephaneLenclud@246
    81
        {
StephaneLenclud@246
    82
            Rest=0,
StephaneLenclud@246
    83
            Edit
StephaneLenclud@246
    84
        }
StephaneLenclud@246
    85
StephaneLenclud@246
    86
        State iCurrentState = State.Rest;
StephaneLenclud@246
    87
        public State CurrentState { get { return iCurrentState; } set { OnStateLeave(); iCurrentState = value; OnStateEnter(); } }
StephaneLenclud@246
    88
StephaneLenclud@246
    89
        /// <summary>
StephaneLenclud@246
    90
        /// 
StephaneLenclud@246
    91
        /// </summary>
StephaneLenclud@246
    92
        protected virtual void OnStateLeave()
StephaneLenclud@246
    93
        {
StephaneLenclud@246
    94
StephaneLenclud@246
    95
        }
StephaneLenclud@246
    96
StephaneLenclud@246
    97
        /// <summary>
StephaneLenclud@246
    98
        /// 
StephaneLenclud@246
    99
        /// </summary>
StephaneLenclud@246
   100
        protected virtual void OnStateEnter()
StephaneLenclud@246
   101
        {
StephaneLenclud@246
   102
StephaneLenclud@246
   103
        }
StephaneLenclud@246
   104
StephaneLenclud@239
   105
        /// <summary>
StephaneLenclud@239
   106
        /// Static object name.
StephaneLenclud@239
   107
        /// </summary>
StephaneLenclud@260
   108
        public string AttributeName
StephaneLenclud@238
   109
        {
StephaneLenclud@238
   110
            //Get the name of this object attribute
StephaneLenclud@238
   111
            get { return Utils.Reflection.GetAttribute<AttributeObject>(GetType()).Name; }
StephaneLenclud@238
   112
            private set { }
StephaneLenclud@238
   113
        }
StephaneLenclud@238
   114
StephaneLenclud@239
   115
        /// <summary>
StephaneLenclud@239
   116
        /// Static object description.
StephaneLenclud@239
   117
        /// </summary>
StephaneLenclud@260
   118
        public string AttributeDescription
StephaneLenclud@238
   119
        {
StephaneLenclud@238
   120
            //Get the description of this object attribute
StephaneLenclud@238
   121
            get { return Utils.Reflection.GetAttribute<AttributeObject>(GetType()).Description; }
StephaneLenclud@238
   122
            private set { }
StephaneLenclud@238
   123
        }
StephaneLenclud@238
   124
StephaneLenclud@239
   125
        /// <summary>
StephaneLenclud@239
   126
        /// Dynamic object description.
StephaneLenclud@239
   127
        /// </summary>
StephaneLenclud@239
   128
        /// <returns></returns>
StephaneLenclud@238
   129
        public virtual string Brief()
StephaneLenclud@238
   130
        {
StephaneLenclud@260
   131
            return AttributeName;
StephaneLenclud@238
   132
        }
StephaneLenclud@238
   133
StephaneLenclud@239
   134
        /// <summary>
StephaneLenclud@239
   135
        /// Needed to make sure our sorting makes sense
StephaneLenclud@239
   136
        /// </summary>
StephaneLenclud@239
   137
        /// <param name="obj"></param>
StephaneLenclud@239
   138
        /// <returns></returns>
StephaneLenclud@238
   139
        public int CompareTo(object obj)
StephaneLenclud@238
   140
        {
StephaneLenclud@238
   141
            //Sort by object name
StephaneLenclud@238
   142
            return Utils.Reflection.GetAttribute<AttributeObject>(GetType()).Name.CompareTo(obj.GetType());
StephaneLenclud@238
   143
        }
StephaneLenclud@238
   144
StephaneLenclud@238
   145
        /// <summary>
StephaneLenclud@239
   146
        /// Tells whether the current object configuration is valid.
StephaneLenclud@239
   147
        /// </summary>
StephaneLenclud@239
   148
        /// <returns></returns>
StephaneLenclud@239
   149
        public virtual bool IsValid()
StephaneLenclud@239
   150
        {
StephaneLenclud@239
   151
            return true;
StephaneLenclud@239
   152
        }
StephaneLenclud@239
   153
StephaneLenclud@239
   154
        /// <summary>
StephaneLenclud@238
   155
        /// So that data contract knows all our types.
StephaneLenclud@238
   156
        /// </summary>
StephaneLenclud@238
   157
        /// <returns></returns>
StephaneLenclud@238
   158
        private static IEnumerable<Type> DerivedTypes()
StephaneLenclud@238
   159
        {
StephaneLenclud@238
   160
            return SharpLib.Utils.Reflection.GetDerivedTypes<Object>();
StephaneLenclud@238
   161
        }
StephaneLenclud@238
   162
StephaneLenclud@244
   163
        /// <summary>
StephaneLenclud@244
   164
        /// 
StephaneLenclud@244
   165
        /// </summary>
StephaneLenclud@244
   166
        public event PropertyChangedEventHandler PropertyChanged;
StephaneLenclud@244
   167
StephaneLenclud@246
   168
StephaneLenclud@244
   169
        /// <summary>
StephaneLenclud@244
   170
        /// Invoke our event.
StephaneLenclud@244
   171
        /// </summary>
StephaneLenclud@244
   172
        /// <param name="name"></param>
StephaneLenclud@244
   173
        protected void OnPropertyChanged(string name)
StephaneLenclud@244
   174
        {
StephaneLenclud@244
   175
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
StephaneLenclud@244
   176
        }
StephaneLenclud@244
   177
StephaneLenclud@244
   178
StephaneLenclud@238
   179
    }
StephaneLenclud@238
   180
}