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
[DataMember]
23
public List<Object> Objects = new List<Object>();
24
25
26
27
28
[AttributeObjectProperty
29
(
30
Id = "Object.Name",
31
Name = "Name",
32
Description = "Given object name."
33
)
34
]
35
public string Name { get; set; } = "";
36
37
38
protected Object()
39
40
Construct();
41
}
42
43
44
/// Needed as our constructor is not called following internalization.
45
46
public void Construct()
47
48
//Construct ourselves first
49
if (!iConstructed)
50
51
DoConstruct();
52
iConstructed = true;
53
54
55
//Then construct our children
56
foreach (Object o in Objects)
57
58
o.Construct();
59
60
61
62
63
///
64
65
protected virtual void DoConstruct()
66
67
//Make sure our name is not null
68
if (Name == null)
69
70
Name = "";
71
72
73
// Makes sure our objects are not null
74
if (Objects == null)
75
76
Objects = new List<Object>();
77
78
79
80
public enum State
81
82
Rest=0,
83
Edit
84
85
86
State iCurrentState = State.Rest;
87
public State CurrentState { get { return iCurrentState; } set { OnStateLeave(); iCurrentState = value; OnStateEnter(); } }
88
89
90
91
92
protected virtual void OnStateLeave()
93
94
95
96
97
98
99
100
protected virtual void OnStateEnter()
101
102
103
104
105
106
/// Static object name.
107
108
public string AttributeName
109
110
//Get the name of this object attribute
111
get { return Utils.Reflection.GetAttribute<AttributeObject>(GetType()).Name; }
112
private set { }
113
114
115
116
/// Static object description.
117
118
public string AttributeDescription
119
120
//Get the description of this object attribute
121
get { return Utils.Reflection.GetAttribute<AttributeObject>(GetType()).Description; }
122
123
124
125
126
/// Dynamic object description.
127
128
/// <returns></returns>
129
public virtual string Brief()
130
131
return AttributeName;
132
133
134
135
/// Needed to make sure our sorting makes sense
136
137
/// <param name="obj"></param>
138
139
public int CompareTo(object obj)
140
141
//Sort by object name
142
return Utils.Reflection.GetAttribute<AttributeObject>(GetType()).Name.CompareTo(obj.GetType());
143
144
145
146
/// Tells whether the current object configuration is valid.
147
148
149
public virtual bool IsValid()
150
151
return true;
152
153
154
155
/// So that data contract knows all our types.
156
157
158
private static IEnumerable<Type> DerivedTypes()
159
160
return SharpLib.Utils.Reflection.GetDerivedTypes<Object>();
161
162
163
164
165
166
public event PropertyChangedEventHandler PropertyChanged;
167
168
169
170
/// Invoke our event.
171
172
/// <param name="name"></param>
173
protected void OnPropertyChanged(string name)
174
175
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
176
177
178
179
180