-
Notifications
You must be signed in to change notification settings - Fork 7
/
MhoraOptions.cs
429 lines (388 loc) · 12 KB
/
MhoraOptions.cs
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/******
Copyright (C) 2005 Ajit Krishnan (http://www.mudgala.com)
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
******/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace mhora
{
public delegate object ApplyOptions (Object sender);
/// <summary>
/// Display a PropertyGrid for any object, and deal with
/// event handlers to perform any requested updates
/// </summary>
public class MhoraOptions : System.Windows.Forms.Form
{
public System.Windows.Forms.PropertyGrid pGrid;
private System.Windows.Forms.Button bApply;
private System.Windows.Forms.Button bCancel;
private ApplyOptions applyEvent;
private System.Windows.Forms.Button bOK;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public MhoraOptions(Object a, ApplyOptions o, bool NoCancel)
{
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
pGrid.SelectedObject = new GlobalizedPropertiesWrapper(a);
pGrid.HelpVisible = true;
applyEvent = o;
this.bCancel.Enabled = false;
}
public MhoraOptions(Object a, ApplyOptions o)
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
pGrid.SelectedObject = new GlobalizedPropertiesWrapper(a);
applyEvent = o;
//this.applyEvent(pGrid.SelectedObject);
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.pGrid = new System.Windows.Forms.PropertyGrid();
this.bApply = new System.Windows.Forms.Button();
this.bCancel = new System.Windows.Forms.Button();
this.bOK = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// pGrid
//
this.pGrid.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.pGrid.CommandsVisibleIfAvailable = true;
this.pGrid.LargeButtons = false;
this.pGrid.LineColor = System.Drawing.SystemColors.ScrollBar;
this.pGrid.Location = new System.Drawing.Point(8, 8);
this.pGrid.Name = "pGrid";
this.pGrid.PropertySort = System.Windows.Forms.PropertySort.Categorized;
this.pGrid.Size = new System.Drawing.Size(284, 216);
this.pGrid.TabIndex = 1;
this.pGrid.Text = "propertyGrid1";
this.pGrid.ToolbarVisible = false;
this.pGrid.ViewBackColor = System.Drawing.SystemColors.Window;
this.pGrid.ViewForeColor = System.Drawing.SystemColors.WindowText;
this.pGrid.Click += new System.EventHandler(this.pGrid_Click);
//
// bApply
//
this.bApply.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.bApply.Location = new System.Drawing.Point(8, 232);
this.bApply.Name = "bApply";
this.bApply.TabIndex = 0;
this.bApply.Text = "Apply";
this.bApply.Click += new System.EventHandler(this.bApply_Click);
//
// bCancel
//
this.bCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.bCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.bCancel.Location = new System.Drawing.Point(204, 232);
this.bCancel.Name = "bCancel";
this.bCancel.TabIndex = 2;
this.bCancel.Text = "Cancel";
this.bCancel.Click += new System.EventHandler(this.bCancel_Click);
//
// bOK
//
this.bOK.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.bOK.Location = new System.Drawing.Point(104, 232);
this.bOK.Name = "bOK";
this.bOK.Size = new System.Drawing.Size(79, 23);
this.bOK.TabIndex = 3;
this.bOK.Text = "OK";
this.bOK.Click += new System.EventHandler(this.bOK_Click);
//
// MhoraOptions
//
this.AcceptButton = this.bApply;
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.CancelButton = this.bCancel;
this.ClientSize = new System.Drawing.Size(296, 273);
this.Controls.Add(this.bOK);
this.Controls.Add(this.bCancel);
this.Controls.Add(this.bApply);
this.Controls.Add(this.pGrid);
this.Name = "MhoraOptions";
this.Text = "Options";
this.Load += new System.EventHandler(this.MhoraOptions_Load);
this.ResumeLayout(false);
}
#endregion
private void MhoraOptions_Load(object sender, System.EventArgs e)
{
}
private void pGrid_Click(object sender, System.EventArgs e)
{
}
private void bCancel_Click(object sender, System.EventArgs e)
{
this.Close();
}
private void Apply (bool bKeepOpen)
{
GlobalizedPropertiesWrapper wrapper = (GlobalizedPropertiesWrapper)this.pGrid.SelectedObject;
object objApplied = applyEvent(wrapper.GetWrappedObject());
if (bKeepOpen)
pGrid.SelectedObject = new GlobalizedPropertiesWrapper(objApplied);
}
private void bApply_Click(object sender, System.EventArgs e)
{
this.Apply(true);
}
private void bOK_Click(object sender, System.EventArgs e)
{
this.Apply(false);
this.Close();
}
}
public class GlobalizedPropertiesWrapper : ICustomTypeDescriptor
{
private object obj = null;
public GlobalizedPropertiesWrapper (object _obj)
{
obj = _obj;
}
public object GetWrappedObject ()
{
return obj;
}
public String GetClassName()
{
return TypeDescriptor.GetClassName(obj,true);
}
public AttributeCollection GetAttributes()
{
return TypeDescriptor.GetAttributes(obj,true);
}
public String GetComponentName()
{
return TypeDescriptor.GetComponentName(obj, true);
}
public TypeConverter GetConverter()
{
return TypeDescriptor.GetConverter(obj, true);
}
public EventDescriptor GetDefaultEvent()
{
return TypeDescriptor.GetDefaultEvent(obj, true);
}
public PropertyDescriptor GetDefaultProperty()
{
return TypeDescriptor.GetDefaultProperty(obj, true);
}
public object GetEditor(Type editorBaseType)
{
return TypeDescriptor.GetEditor(obj, editorBaseType, true);
}
public EventDescriptorCollection GetEvents(Attribute[] attributes)
{
return TypeDescriptor.GetEvents(obj, attributes, true);
}
public EventDescriptorCollection GetEvents()
{
return TypeDescriptor.GetEvents(obj, true);
}
public object GetPropertyOwner(PropertyDescriptor pd)
{
return obj;
}
public bool IsPropertyVisible (PropertyDescriptor prop)
{
if (null != prop.Attributes[typeof(PGNotVisible)])
return false;
return true;
// Console.WriteLine ("Property {0} is invisible", prop.DisplayName);
//return true;
}
public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
ArrayList orderedProperties = new ArrayList();
PropertyDescriptorCollection retProps = new PropertyDescriptorCollection(null);
PropertyDescriptorCollection baseProps = TypeDescriptor.GetProperties(obj, attributes, true);
foreach (PropertyDescriptor oProp in baseProps)
{
Attribute attOrder = oProp.Attributes[typeof(PropertyOrderAttribute)];
if (false == IsPropertyVisible(oProp))
continue;
if (attOrder != null)
{
//
// If the attribute is found, then create an pair object to hold it
//
PropertyOrderAttribute poa = (PropertyOrderAttribute)attOrder;
orderedProperties.Add(new PropertyOrderPair(oProp, oProp.Name,poa.Order));
}
else
{
//
// If no order attribute is specifed then given it an order of 0
//
orderedProperties.Add(new PropertyOrderPair(oProp, oProp.Name,0));
}
//retProps.Add (new GlobalizedPropertyDescriptor(oProp));
//Console.WriteLine ("Enumerating property {0}", oProp.DisplayName);
//PGDisplayName invisible = (PGDisplayName)oProp.Attributes[typeof(PGNotVisible)];
//if (invisible == null)
//else
// Console.WriteLine ("Property {0} is invisible", oProp.DisplayName);
}
orderedProperties.Sort();
foreach (PropertyOrderPair pop in orderedProperties)
{
Console.WriteLine ("Adding sorted {0}", pop.Name);
retProps.Add (new GlobalizedPropertyDescriptor(pop.Property));
}
return retProps;
}
public PropertyDescriptorCollection GetProperties ()
{
PropertyDescriptorCollection baseProps = TypeDescriptor.GetProperties(obj, true);
return baseProps;
}
}
public class GlobalizedPropertyDescriptor : PropertyDescriptor
{
private PropertyDescriptor basePropertyDescriptor;
public GlobalizedPropertyDescriptor(PropertyDescriptor basePropertyDescriptor) : base(basePropertyDescriptor)
{
this.basePropertyDescriptor = basePropertyDescriptor;
}
public override bool CanResetValue(object component)
{
return basePropertyDescriptor.CanResetValue(component);
}
public override Type ComponentType
{
get { return basePropertyDescriptor.ComponentType; }
}
public override string DisplayName
{
get
{
PGDisplayName dn = (PGDisplayName)basePropertyDescriptor.Attributes[typeof(PGDisplayName)];
if (dn != null)
return dn.DisplayName;
return basePropertyDescriptor.DisplayName;
}
}
public override string Description
{
get { return basePropertyDescriptor.Description; }
}
public override object GetValue(object component)
{
return this.basePropertyDescriptor.GetValue(component);
}
public override bool IsReadOnly
{
get { return this.basePropertyDescriptor.IsReadOnly; }
}
public override string Name
{
get { return this.basePropertyDescriptor.Name; }
}
public override Type PropertyType
{
get { return this.basePropertyDescriptor.PropertyType; }
}
public override void ResetValue(object component)
{
this.basePropertyDescriptor.ResetValue(component);
}
public override bool ShouldSerializeValue(object component)
{
return this.basePropertyDescriptor.ShouldSerializeValue(component);
}
public override void SetValue(object component, object value)
{
this.basePropertyDescriptor.SetValue(component, value);
}
}
public class PropertyOrderPair : IComparable
{
private int _order;
private string _name;
private PropertyDescriptor _pdesc;
public string Name
{
get
{
return _name;
}
}
public PropertyDescriptor Property
{
get { return _pdesc; }
}
public PropertyOrderPair(PropertyDescriptor pdesc, string name, int order)
{
_pdesc = pdesc;
_order = order;
_name = name;
}
public int CompareTo(object obj)
{
//
// Sort the pair objects by ordering by order value
// Equal values get the same rank
//
int otherOrder = ((PropertyOrderPair)obj)._order;
if (otherOrder == _order)
{
//
// If order not specified, sort by name
//
string otherName = ((PropertyOrderPair)obj)._name;
return string.Compare(_name,otherName);
}
else if (otherOrder > _order)
{
return -1;
}
return 1;
}
}
}