comparison Source/EditableObjects/EditableObjectHolder.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f990fcb411a9
1 using System;
2 using System.Collections;
3 using System.Reflection;
4 using System.ComponentModel;
5
6 using BLToolkit.TypeBuilder;
7
8 namespace BLToolkit.EditableObjects
9 {
10 [Serializable]
11 public class /*struct*/ EditableObjectHolder : IEditable, IMemberwiseEditable, ISetParent, IPrintDebugState
12 {
13 public EditableObjectHolder(EditableObject obj)
14 {
15 _original = obj;
16 _current = obj;
17
18 if (_current != null)
19 _current.PropertyChanged += _current_PropertyChanged;
20 }
21
22 void _current_PropertyChanged(object sender, PropertyChangedEventArgs e)
23 {
24 EditableObject obj = _parent as EditableObject;
25
26 if (obj != null)
27 obj.OnPropertyChanged(_propertyInfo.Name + "." + e.PropertyName);
28 }
29
30 private EditableObject _original;
31 private EditableObject _current;
32 private object _parent;
33 private PropertyInfo _propertyInfo;
34
35 [GetValue, SetValue]
36 public EditableObject Value
37 {
38 get { return _current; }
39 set
40 {
41 if (_current != null)
42 _current.PropertyChanged -= _current_PropertyChanged;
43
44 _current = value;
45
46 if (_current != null)
47 _current.PropertyChanged += _current_PropertyChanged;
48 }
49 }
50
51 #region IEditable Members
52
53 public void AcceptChanges()
54 {
55 _original = _current;
56
57 if (_current != null)
58 _current.AcceptChanges();
59 }
60
61 public void RejectChanges()
62 {
63 _current = _original;
64
65 if (_current != null)
66 _current.RejectChanges();
67 }
68
69 public bool IsDirty
70 {
71 get
72 {
73 if (_current == null)
74 return _original != null;
75
76 return _current != _original || _current.IsDirty;
77 }
78 }
79
80 #endregion
81
82 #region IMemberwiseEditable Members
83
84 public bool AcceptMemberChanges(PropertyInfo propertyInfo, string memberName)
85 {
86 if (memberName != propertyInfo.Name)
87 return false;
88
89 AcceptChanges();
90
91 return true;
92 }
93
94 public bool RejectMemberChanges(PropertyInfo propertyInfo, string memberName)
95 {
96 if (memberName != propertyInfo.Name)
97 return false;
98
99 RejectChanges();
100
101 return true;
102 }
103
104 public bool IsDirtyMember(PropertyInfo propertyInfo, string memberName, ref bool isDirty)
105 {
106 if (memberName != propertyInfo.Name)
107 return false;
108
109 isDirty = IsDirty;
110
111 return true;
112 }
113
114 public void GetDirtyMembers(PropertyInfo propertyInfo, ArrayList list)
115 {
116 if (IsDirty)
117 list.Add(propertyInfo);
118 }
119
120 #endregion
121
122 #region IPrintDebugState Members
123
124 public void PrintDebugState(PropertyInfo propertyInfo, ref string str)
125 {
126 str += string.Format("{0,-20} {1} {2,-40} {3,-40} \r\n",
127 propertyInfo.Name, IsDirty? "*": " ", _original, _current);
128 }
129
130 #endregion
131
132 #region ISetParent Members
133
134 public void SetParent(object parent, PropertyInfo propertyInfo)
135 {
136 _parent = parent;
137 _propertyInfo = propertyInfo;
138 }
139
140 #endregion
141 }
142 }