Repository navigation
Expand file tree
/
Copy pathScreenBinder.cs
More file actions
195 lines (164 loc) · 5.96 KB
/
Copy pathScreenBinder.cs
File metadata and controls
195 lines (164 loc) · 5.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using OSPSuite.Utility.Extensions;
namespace OSPSuite.DataBinding
{
public enum SavingMode
{
/// <summary>
/// Saves the value into the source even if the element has an error
/// </summary>
Always,
/// <summary>
/// Saves the value into the source only if the value in the control is valid
/// </summary>
OnlyWhenValid
}
public class ScreenBinder<TObjectType> : IBinder
{
//List of all screen elements maanaged by the binder
private readonly IList<IElementBinder<TObjectType>> _allElements;
public bool IsLatched { get; set; }
public event Action Changed = delegate { };
/// <summary>
/// Event is fired whenever the value in one item is changing but has not be saved necessarily in the source
/// </summary>
public event Action Changing = delegate { };
/// <summary>
/// Event is fired whenever an error was found during the validation process
/// </summary>
public event Action<Control, string> OnValidationError = delegate { };
/// <summary>
/// Event is fired whenever the validation of an item was successful
/// </summary>
public event Action<Control> OnValidated = delegate { };
public BindingMode BindingMode { get; set; }
/// <summary>
/// The SavingMode value determines when the data be saved in the source
/// Default value is OnlyWhenValid
/// </summary>
public SavingMode SavingMode { get; set; }
public ScreenBinder() : this(new List<IElementBinder<TObjectType>>())
{
}
internal ScreenBinder(IList<IElementBinder<TObjectType>> allElements)
{
_allElements = allElements;
BindingMode = BindingMode.TwoWay;
SavingMode = SavingMode.OnlyWhenValid;
}
/// <summary>
/// Adds a screen element to the the screen binder
/// </summary>
/// <param name="elementBinder">screen element to add</param>
public void AddElement(IElementBinder<TObjectType> elementBinder)
{
_allElements.Add(elementBinder);
elementBinder.Changed += () => Changed();
elementBinder.Changing += () => Changing();
elementBinder.ParentBinder = this;
}
/// <summary>
/// Binds the screen to the given source
/// </summary>
/// <param name="source">Source object to bind to</param>
public void BindToSource(TObjectType source)
{
//Fisrt bind to the source
this.DoWithinLatch(() => _allElements.Each(element => element.Bind(source)));
//then validate all elements at once
Validate();
}
public bool HasError
{
get { return _allElements.Any(element => element.HasError); }
}
public IEnumerable<string> ErrorMessages => from e in _allElements
let error = e.ErrorMessage
where !string.IsNullOrEmpty(error)
select error;
public string ErrorMessage => ErrorMessages.ToString("\n");
/// <summary>
/// Clears error for the screen element given as parameter
/// </summary>
/// <param name="elementBinder">element for which the error provider should be cleared</param>
public void ClearError(IElementBinder<TObjectType> elementBinder)
{
this.DoWithinLatch(() => OnValidated(elementBinder.Control));
}
/// <summary>
/// Sets error for the screen element given as parameter
/// </summary>
/// <param name="elementBinder">element for which the error provider should be set</param>
/// <param name="errorMessage">Error message to be displayed by the error provider</param>
public void SetError(IElementBinder<TObjectType> elementBinder, string errorMessage)
{
this.DoWithinLatch(() => OnValidationError(elementBinder.Control, errorMessage));
}
/// <summary>
/// Removes the element binder from the screen binder. This should only be used in extrem scenarios
/// This should be called before the bind methods
/// </summary>
/// <param name="elementBinder">Screen element to remove</param>
public void Remove(IElementBinder<TObjectType> elementBinder)
{
_allElements.Remove(elementBinder);
}
/// <summary>
/// Refreshes the content of all list items(e.g update the combo box list)
/// </summary>
public void RefreshListElements()
{
var query = from element in _allElements
let listElement = element as IListElementBinder<TObjectType>
where listElement != null
select listElement;
this.DoWithinLatch(() => query.Each(listElement => listElement.Refresh()));
}
/// <summary>
/// Validates all avaialble elements
/// </summary>
public void Validate()
{
_allElements.Each(element => element.Validate());
}
/// <summary>
/// Resets all screen element to the original value of the source when BindToSource was called
/// </summary>
public void Reset()
{
_allElements.Each(element => element.Reset());
}
/// <summary>
/// Updates all screen element with the current value of the source
/// </summary>
public void Update()
{
this.DoWithinLatch(() => _allElements.Each(element => element.Update()));
}
private bool _disposed;
public void Dispose()
{
if (_disposed) return;
cleanup();
GC.SuppressFinalize(this);
_disposed = true;
}
private void cleanup()
{
DeleteBinding();
_allElements.Each(element => element.Dispose());
_allElements.Clear();
}
public void DeleteBinding()
{
_allElements.Each(element => element.DeleteBinding());
}
~ScreenBinder()
{
cleanup();
}
}
}