comparison Source/Web/UI/WebObjectBinder.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.ComponentModel;
4 using System.Drawing;
5 using System.Drawing.Design;
6 using System.Web.Compilation;
7 using System.Web.UI;
8
9 using BLToolkit.ComponentModel;
10 using BLToolkit.ComponentModel.Design;
11 using BLToolkit.EditableObjects;
12
13 namespace BLToolkit.Web.UI
14 {
15 [DefaultProperty("TypeName")]
16 [ToolboxBitmap(typeof(WebObjectBinder))]
17 #if !FW4
18 [Designer(typeof(BLToolkit.Web.UI.Design.WebObjectBinderDesigner))]
19 #endif
20 [PersistChildren(false)]
21 [ParseChildren(true)]
22 [Description("BLToolkit Web Object Binder")]
23 [DisplayName("Object Binder")]
24 public class WebObjectBinder : DataSourceControl, IListSource
25 {
26 #region Constructors
27
28 public WebObjectBinder()
29 {
30 _objectBinder.ListChanged += _objectBinder_ListChanged;
31 }
32
33 #endregion
34
35 #region Public Members
36
37 [RefreshProperties(RefreshProperties.Repaint)]
38 [DefaultValue(null)]
39 [Category("Data")]
40 [Editor(typeof(TypeNameEditor), typeof(UITypeEditor))]
41 public string TypeName
42 {
43 get
44 {
45 Type type = _objectBinder.ItemType;
46 return type == null ? "(none)" : type.FullName;
47 }
48 set
49 {
50 _objectBinder.ItemType = string.IsNullOrEmpty(value) || value == "(none)"?
51 null: BuildManager.GetType(value, false, true);
52 }
53 }
54
55 [RefreshProperties(RefreshProperties.Repaint)]
56 [DefaultValue(null)]
57 [Category("Data")]
58 [Editor(typeof(ObjectViewTypeNameEditor), typeof(UITypeEditor))]
59 public string ObjectViewTypeName
60 {
61 get
62 {
63 Type type = _objectBinder.ObjectViewType;
64 return type == null ? "(none)" : type.FullName;
65 }
66 set
67 {
68 _objectBinder.ObjectViewType = string.IsNullOrEmpty(value) || value == "(none)"?
69 null: BuildManager.GetType(value, false, true);
70 }
71 }
72
73 [Browsable(false)]
74 [RefreshProperties(RefreshProperties.Repaint)]
75 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
76 public object Object
77 {
78 get { return _objectBinder.Object; }
79 set { _objectBinder.Object = value; }
80 }
81
82 [Browsable(false)]
83 [RefreshProperties(RefreshProperties.Repaint)]
84 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
85 public IList List
86 {
87 get { return _objectBinder.List; }
88 set { _objectBinder.List = value; }
89 }
90
91 #endregion
92
93 #region Protected members
94
95 internal ObjectBinder _objectBinder = new ObjectBinder();
96
97 private void _objectBinder_ListChanged(object sender, ListChangedEventArgs e)
98 {
99 switch (e.ListChangedType)
100 {
101 case ListChangedType.PropertyDescriptorAdded:
102 case ListChangedType.PropertyDescriptorChanged:
103 case ListChangedType.PropertyDescriptorDeleted:
104 RaiseDataSourceChangedEvent(e);
105 break;
106 }
107 }
108
109 public override void Dispose()
110 {
111 _objectBinder.Dispose();
112
113 base.Dispose();
114 }
115
116 #endregion
117
118 #region IListSource Members
119
120 bool IListSource.ContainsListCollection
121 {
122 get { return false; }
123 }
124
125 IList IListSource.GetList()
126 {
127 return _objectBinder.List;
128 }
129
130 #endregion
131
132 #region IDataSource Members
133
134 private ObjectDataSourceView _view;
135
136 protected override DataSourceView GetView(string viewName)
137 {
138 if (_view == null)
139 _view = new ObjectDataSourceView(this, "DefaultView");
140
141 return _view;
142 }
143
144 protected override ICollection GetViewNames()
145 {
146 return new string[] { "DefaultView" };
147 }
148
149 #endregion
150
151 #region ObjectDataSourceView
152
153 class ObjectDataSourceView : DataSourceView
154 {
155 public ObjectDataSourceView(WebObjectBinder owner, string viewName)
156 : base(owner, viewName)
157 {
158 _owner = owner;
159 }
160
161 readonly WebObjectBinder _owner;
162
163 protected override IEnumerable ExecuteSelect(DataSourceSelectArguments arguments)
164 {
165 return new ObjectEnumerator(_owner._objectBinder, arguments);
166 }
167
168 public override bool CanDelete { get { return _owner._objectBinder.AllowRemove; } }
169 public override bool CanInsert { get { return _owner._objectBinder.AllowNew; } }
170 public override bool CanUpdate { get { return _owner._objectBinder.AllowEdit; } }
171 public override bool CanPage { get { return true; } }
172 public override bool CanSort { get { return true; } }
173 public override bool CanRetrieveTotalRowCount { get { return true; } }
174 }
175
176 #endregion
177
178 #region ObjectEnumerator
179
180 class ObjectEnumerator : ICollection
181 {
182 public ObjectEnumerator(ObjectBinder objectBinder, DataSourceSelectArguments arguments)
183 {
184 _objectBinder = objectBinder;
185 _arguments = arguments;
186 }
187
188 private readonly ObjectBinder _objectBinder;
189 private readonly DataSourceSelectArguments _arguments;
190
191 #region ICollection Members
192
193 public void CopyTo(Array array, int index)
194 {
195 _objectBinder.List.CopyTo(array, index);
196 }
197
198 public int Count
199 {
200 get { return _objectBinder.List.Count; }
201 }
202
203 public bool IsSynchronized
204 {
205 get { return _objectBinder.List.IsSynchronized; }
206 }
207
208 public object SyncRoot
209 {
210 get { return _objectBinder.List.SyncRoot; }
211 }
212
213 #endregion
214
215 #region IEnumerable Members
216
217 public IEnumerator GetEnumerator()
218 {
219 _arguments.AddSupportedCapabilities(DataSourceCapabilities.Page);
220 _arguments.AddSupportedCapabilities(DataSourceCapabilities.Sort);
221 _arguments.AddSupportedCapabilities(DataSourceCapabilities.RetrieveTotalRowCount);
222
223 EditableArrayList list = (EditableArrayList)_objectBinder.List;
224
225 _arguments.TotalRowCount = list.Count;
226
227 if (!string.IsNullOrEmpty(_arguments.SortExpression))
228 {
229 list = new EditableArrayList(list.ItemType, list.Count);
230 list.AddRange(_objectBinder.List);
231 list.SortEx(_arguments.SortExpression);
232 }
233
234 int start = _arguments.StartRowIndex >= 0? _arguments.StartRowIndex: 0;
235 int count = _arguments.MaximumRows > 0?
236 Math.Min(_arguments.MaximumRows, list.Count): list.Count;
237
238 for (int i = 0; i < count; i++)
239 {
240 object o = list[i + start];
241
242 yield return o is ICustomTypeDescriptor? o: new ObjectHolder(o, _objectBinder);
243 }
244 }
245
246 #endregion
247 }
248
249 #endregion
250 }
251 }