forked from fdorg/flashdevelop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilteredGrid.cs
More file actions
364 lines (324 loc) · 11.7 KB
/
Copy pathFilteredGrid.cs
File metadata and controls
364 lines (324 loc) · 11.7 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
using System;
using System.Data;
using System.Text;
using System.Drawing;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
namespace FlashDevelop.Controls
{
/// <summary>
/// This class overrides the standard PropertyGrid provided by Microsoft.
/// It also allows to hide (or filter) the properties of the SelectedObject displayed by the PropertyGrid.
/// </summary>
public class FilteredGrid : PropertyGrid
{
/// <summary>
/// Contain a reference to the collection of properties to show in the parent PropertyGrid.
/// </summary>
List<PropertyDescriptor> m_PropertyDescriptors = new List<PropertyDescriptor>();
/// <summary>
/// Contain a reference to the array of properties to display in the PropertyGrid.
/// </summary>
private AttributeCollection m_HiddenAttributes = null, m_BrowsableAttributes = null;
/// <summary>
/// Contain references to the arrays of properties or categories to hide.
/// </summary>
private string[] m_BrowsableProperties = null, m_HiddenProperties = null;
/// <summary>
/// Contain a reference to the wrapper that contains the object to be displayed into the PropertyGrid.
/// </summary>
private ObjectWrapper m_Wrapper = null;
/// <summary>
/// Public constructor.
/// </summary>
public FilteredGrid()
{
base.SelectedObject = m_Wrapper;
}
/// <summary>
///
/// </summary>
public new AttributeCollection BrowsableAttributes
{
get { return m_BrowsableAttributes; }
set
{
if (m_BrowsableAttributes != value)
{
m_HiddenAttributes = null;
m_BrowsableAttributes = value;
RefreshProperties();
}
}
}
/// <summary>
/// Get or set the categories to hide.
/// </summary>
public AttributeCollection HiddenAttributes
{
get { return m_HiddenAttributes; }
set
{
if (value != m_HiddenAttributes)
{
m_HiddenAttributes = value;
m_BrowsableAttributes = null;
RefreshProperties();
}
}
}
/// <summary>
/// Get or set the properties to show.
/// </summary>
public string[] BrowsableProperties
{
get { return m_BrowsableProperties; }
set
{
if (value != m_BrowsableProperties)
{
m_BrowsableProperties = value;
RefreshProperties();
}
}
}
/// <summary>
/// Get or set the properties to hide.
/// </summary>
public string[] HiddenProperties
{
get { return m_HiddenProperties; }
set
{
if (value != m_HiddenProperties)
{
m_HiddenProperties = value;
RefreshProperties();
}
}
}
/// <summary>
/// Overwrite the PropertyGrid.SelectedObject property.
/// </summary>
public new object SelectedObject
{
get { return m_Wrapper != null ? ((ObjectWrapper)base.SelectedObject).SelectedObject : null; }
set
{
if (value != null)
{
if (m_Wrapper == null)
{
m_Wrapper = new ObjectWrapper(value);
RefreshProperties();
}
else if (m_Wrapper.SelectedObject != value)
{
bool needrefresh;
needrefresh = (value.GetType() != m_Wrapper.SelectedObject.GetType());
m_Wrapper.SelectedObject = value;
if (needrefresh) RefreshProperties();
}
// Set the list of properties to the wrapper.
m_Wrapper.PropertyDescriptors = m_PropertyDescriptors;
// Link the wrapper to the parent PropertyGrid.
base.SelectedObject = m_Wrapper;
}
else
{
m_Wrapper = null;
base.SelectedObject = null;
}
}
}
/// <summary>
/// Called when the browsable properties have changed.
/// </summary>
private void OnBrowsablePropertiesChanged()
{
if(m_Wrapper == null) return;
}
/// <summary>
/// Build the list of the properties to be displayed in the PropertyGrid, following the filters defined the Browsable and Hidden properties.
/// </summary>
private void RefreshProperties()
{
if (m_Wrapper == null) return;
// Clear the list of properties to be displayed.
m_PropertyDescriptors.Clear();
// Check whether the list is filtered
if (m_BrowsableAttributes != null && m_BrowsableAttributes.Count > 0)
{
// Add to the list the attributes that need to be displayed.
foreach(Attribute attribute in m_BrowsableAttributes) ShowAttribute(attribute);
}
else
{
// Fill the collection with all the properties.
PropertyDescriptorCollection originalpropertydescriptors = TypeDescriptor.GetProperties(m_Wrapper.SelectedObject);
foreach(PropertyDescriptor propertydescriptor in originalpropertydescriptors) m_PropertyDescriptors.Add(propertydescriptor);
// Remove from the list the attributes that mustn't be displayed.
if(m_HiddenAttributes != null) foreach(Attribute attribute in m_HiddenAttributes) HideAttribute(attribute);
}
// Get all the properties of the SelectedObject
PropertyDescriptorCollection allproperties = TypeDescriptor.GetProperties(m_Wrapper.SelectedObject);
// Hide if necessary, some properties
if (m_HiddenProperties != null && m_HiddenProperties.Length > 0)
{
// Remove from the list the properties that mustn't be displayed.
foreach(string propertyname in m_HiddenProperties)
{
try
{
PropertyDescriptor property = allproperties[propertyname];
// Remove from the list the property
HideProperty(property);
}
catch(Exception ex)
{
throw new ArgumentException(ex.Message);
}
}
}
if (m_BrowsableProperties != null && m_BrowsableProperties.Length > 0)
{
// Clear properties to filter the list from scratch BY IAP
m_PropertyDescriptors.Clear();
foreach(string propertyname in m_BrowsableProperties)
{
try
{
ShowProperty(allproperties[propertyname]);
}
catch (Exception)
{
throw new ArgumentException("Property not found.", propertyname);
}
}
}
}
/// <summary>
/// Allows to hide a set of properties to the parent PropertyGrid.
/// </summary>
private void HideAttribute(Attribute attribute)
{
PropertyDescriptorCollection filteredoriginalpropertydescriptors = TypeDescriptor.GetProperties(m_Wrapper.SelectedObject, new Attribute[] { attribute });
if(filteredoriginalpropertydescriptors == null || filteredoriginalpropertydescriptors.Count == 0) throw new ArgumentException("Attribute not found", attribute.ToString());
foreach(PropertyDescriptor propertydescriptor in filteredoriginalpropertydescriptors) HideProperty(propertydescriptor);
}
/// <summary>
/// Add all the properties that match an attribute to the list of properties to be displayed in the PropertyGrid.
/// </summary>
private void ShowAttribute(Attribute attribute)
{
PropertyDescriptorCollection filteredoriginalpropertydescriptors = TypeDescriptor.GetProperties(m_Wrapper.SelectedObject,new Attribute[] { attribute });
if (filteredoriginalpropertydescriptors == null || filteredoriginalpropertydescriptors.Count == 0) throw new ArgumentException("Attribute not found", attribute.ToString());
foreach(PropertyDescriptor propertydescriptor in filteredoriginalpropertydescriptors) ShowProperty(propertydescriptor);
}
/// <summary>
/// Add a property to the list of properties to be displayed in the PropertyGrid.
/// </summary>
private void ShowProperty(PropertyDescriptor property)
{
if (!m_PropertyDescriptors.Contains(property)) m_PropertyDescriptors.Add(property);
}
/// <summary>
/// Allows to hide a property to the parent PropertyGrid.
/// </summary>
private void HideProperty(PropertyDescriptor property)
{
if (m_PropertyDescriptors.Contains(property)) m_PropertyDescriptors.Remove(property);
}
}
#region Internal Classes
/// <summary>
/// This class is a wrapper. It contains the object the propertyGrid has to display.
/// </summary>
internal class ObjectWrapper : ICustomTypeDescriptor
{
/// <summary>
/// Contain a reference to the selected objet that will linked to the parent PropertyGrid.
/// </summary>
private object m_SelectedObject = null;
/// <summary>
/// Contain a reference to the collection of properties to show in the parent PropertyGrid.
/// </summary>
List<PropertyDescriptor> m_PropertyDescriptors = new List<PropertyDescriptor>();
/// <summary>
/// Simple constructor.
/// </summary>
/// <param name="obj">A reference to the selected object that will linked to the parent PropertyGrid.</param>
internal ObjectWrapper(object obj)
{
m_SelectedObject = obj;
}
/// <summary>
/// Get or set a reference to the selected objet that will linked to the parent PropertyGrid.
/// </summary>
public object SelectedObject
{
get { return m_SelectedObject; }
set { if (m_SelectedObject != value) m_SelectedObject = value; }
}
/// <summary>
/// Get or set a reference to the collection of properties to show in the parent PropertyGrid.
/// </summary>
public List<PropertyDescriptor> PropertyDescriptors
{
get { return m_PropertyDescriptors; }
set { m_PropertyDescriptors = value; }
}
#region ICustomTypeDescriptor Members
public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
return GetProperties();
}
public PropertyDescriptorCollection GetProperties()
{
return new PropertyDescriptorCollection(m_PropertyDescriptors.ToArray(), true);
}
public AttributeCollection GetAttributes()
{
return TypeDescriptor.GetAttributes(m_SelectedObject, true);
}
public String GetClassName()
{
return TypeDescriptor.GetClassName(m_SelectedObject, true);
}
public String GetComponentName()
{
return TypeDescriptor.GetComponentName(m_SelectedObject, true);
}
public TypeConverter GetConverter()
{
return TypeDescriptor.GetConverter(m_SelectedObject, true);
}
public EventDescriptor GetDefaultEvent()
{
return TypeDescriptor.GetDefaultEvent(m_SelectedObject, true);
}
public PropertyDescriptor GetDefaultProperty()
{
return TypeDescriptor.GetDefaultProperty(m_SelectedObject, true);
}
public object GetEditor(Type editorBaseType)
{
return TypeDescriptor.GetEditor(this, editorBaseType, true);
}
public EventDescriptorCollection GetEvents(Attribute[] attributes)
{
return TypeDescriptor.GetEvents(m_SelectedObject, attributes, true);
}
public EventDescriptorCollection GetEvents()
{
return TypeDescriptor.GetEvents(m_SelectedObject, true);
}
public object GetPropertyOwner(PropertyDescriptor pd)
{
return m_SelectedObject;
}
#endregion
}
#endregion
}