CSLA.NET 6.0.0
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
BindingSourceNode.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="BindingSourceNode.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>Maintains a reference to a BindingSource object</summary>
7//-----------------------------------------------------------------------
8using System;
9using System.Collections.Generic;
10using System.ComponentModel;
11using System.Windows.Forms;
12using Csla.Core;
13
14namespace Csla.Windows
15{
20 public class BindingSourceNode
21 {
28 public BindingSourceNode(BindingSource source)
29 {
30 _source = source;
31 _source.CurrentChanged += BindingSource_CurrentChanged;
32 }
33
34 private BindingSource _source;
35 private List<BindingSourceNode> _children;
36 private BindingSourceNode _parent;
37
38 private void BindingSource_CurrentChanged(object sender, EventArgs e)
39 {
40 if (_children.Count > 0)
41 foreach (BindingSourceNode child in _children)
42 child.Source.EndEdit();
43 }
44
45 internal BindingSource Source
46 {
47 get { return _source; }
48 }
49
50 internal List<BindingSourceNode> Children
51 {
52 get
53 {
54 if (_children == null)
55 _children = new List<BindingSourceNode>();
56
57 return _children;
58 }
59 }
60
61 internal BindingSourceNode Parent
62 {
63 get { return _parent; }
64 set { _parent = value; }
65 }
66
67 internal void Unbind(bool cancel)
68 {
69 if (_source == null)
70 return;
71
72 if (_children.Count > 0)
73 foreach (BindingSourceNode child in _children)
74 child.Unbind(cancel);
75
76 IEditableObject current = _source.Current as IEditableObject;
77
78 if (!(_source.DataSource is BindingSource))
79 _source.DataSource = null;
80
81 if (current != null)
82 {
83 if (cancel)
84 current.CancelEdit();
85 else
86 current.EndEdit();
87 }
88
89 if (_source.DataSource is BindingSource)
90 _source.DataSource = _parent.Source;
91 }
92
93 internal void EndEdit()
94 {
95 if (Source == null)
96 return;
97
98 if (_children.Count > 0)
99 foreach (BindingSourceNode child in _children)
100 child.EndEdit();
101
102 _source.EndEdit();
103 }
104
105 internal void SetEvents(bool value)
106 {
107 if (_source == null)
108 return;
109
110 _source.RaiseListChangedEvents = value;
111
112 if (_children.Count > 0)
113 foreach (BindingSourceNode child in _children)
114 child.SetEvents(value);
115 }
116
117 internal void ResetBindings(bool refreshMetadata)
118 {
119 if (_source == null)
120 return;
121
122 if (_children.Count > 0)
123 foreach (BindingSourceNode child in _children)
124 child.ResetBindings(refreshMetadata);
125
126 _source.ResetBindings(refreshMetadata);
127 }
128
135 public void Bind(object objectToBind)
136 {
137 ISupportUndo root = objectToBind as ISupportUndo;
138
139 if (root != null)
140 root.BeginEdit();
141
142 _source.DataSource = objectToBind;
143 SetEvents(true);
144 ResetBindings(false);
145 }
146
150 public void Apply()
151 {
152 SetEvents(false);
153
154 ISupportUndo root = _source.DataSource as ISupportUndo;
155
156 Unbind(false);
157 EndEdit();
158
159 if (root != null)
160 root.ApplyEdit();
161 }
162
167 public void Cancel(object businessObject)
168 {
169 SetEvents(false);
170
171 ISupportUndo root = _source.DataSource as ISupportUndo;
172
173 Unbind(true);
174
175 if (root != null)
176 root.CancelEdit();
177
178 Bind(businessObject);
179 }
180
184 public void Close()
185 {
186 SetEvents(false);
187 Unbind(true);
188 }
189
190 }
191}
Maintains a reference to a BindingSource object on the form.
void Close()
Disconnects from the BindingSource object.
void Apply()
Applies changes to the business object.
void Cancel(object businessObject)
Cancels changes to the business object.
BindingSourceNode(BindingSource source)
Creates an instance of the object.
void Bind(object objectToBind)
Binds a business object to the BindingSource.
Define the common methods used by the UI to interact with n-level undo.
Definition: ISupportUndo.cs:25
void CancelEdit()
Cancels the current edit process, restoring the object's state to its previous values.
void ApplyEdit()
Commits the current edit process.
void BeginEdit()
Starts a nested edit on the object.