This repository has been archived by the owner on Nov 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathSharpTreeNodeProxy.cs
166 lines (134 loc) · 5.1 KB
/
SharpTreeNodeProxy.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
/*
Copyright (c) 2015 Ki
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
namespace ICSharpCode.TreeView {
struct ObjectChangedEventArgs {
public SharpTreeNode OldNode { get; }
public SharpTreeNode NewNode { get; }
public ObjectChangedEventArgs(SharpTreeNode oldNode, SharpTreeNode newNode) {
OldNode = oldNode;
NewNode = newNode;
}
}
internal class SharpTreeNodeProxy : CustomTypeDescriptor {
static SharpTreeNodeProxy() {
descMap = new Dictionary<string, IPropDesc>();
AddPropertyDesc("Foreground", node => node.Foreground);
AddPropertyDesc("IsExpanded", node => node.IsExpanded, (node, value) => node.IsExpanded = value);
AddPropertyDesc("IsChecked", node => node.IsChecked, (node, value) => node.IsChecked = value);
AddPropertyDesc("ToolTip", node => node.ToolTip);
AddPropertyDesc("Icon", node => node.Icon);
AddPropertyDesc("Text", node => node.Text);
AddPropertyDesc("IsEditing", node => node.IsEditing, (node, value) => node.IsEditing = value);
AddPropertyDesc("ShowIcon", node => node.ShowIcon);
AddPropertyDesc("ShowExpander", node => node.ShowExpander);
AddPropertyDesc("ExpandedIcon", node => node.ExpandedIcon);
AddPropertyDesc("IsCheckable", node => node.IsCheckable);
AddPropertyDesc("IsCut", node => node.IsCut);
descs = new PropertyDescriptorCollection(descMap.Values.Cast<PropertyDescriptor>().ToArray());
}
static readonly PropertyDescriptorCollection descs;
static readonly Dictionary<string, IPropDesc> descMap;
static void AddPropertyDesc<T>(string name, Func<SharpTreeNode, T> getter, Action<SharpTreeNode, T> setter = null) {
var desc = new PropDesc<T>(name, getter, setter);
descMap.Add(name, desc);
}
public SharpTreeNodeProxy(SharpTreeNode obj) {
UpdateObject(obj);
}
public void UpdateObject(SharpTreeNode obj) {
if (Object == obj)
return;
if (Object != null)
Object.PropertyChanged -= OnPropertyChanged;
var oldNode = Object;
Object = obj;
if (obj == null)
IsNull = true;
else {
IsNull = false;
obj.PropertyChanged += OnPropertyChanged;
foreach (var desc in descMap) {
desc.Value.OnValueChanged(this);
}
}
if (ObjectChanged != null)
ObjectChanged(this, new ObjectChangedEventArgs(oldNode, obj));
}
void OnPropertyChanged(object sender, PropertyChangedEventArgs e) {
IPropDesc desc;
if (descMap.TryGetValue(e.PropertyName, out desc))
desc.OnValueChanged(this);
}
public event EventHandler<ObjectChangedEventArgs> ObjectChanged;
public bool IsNull { get; private set; }
public SharpTreeNode Object { get; private set; }
public override PropertyDescriptorCollection GetProperties() {
return descs;
}
public override PropertyDescriptorCollection GetProperties(Attribute[] attributes) {
return GetProperties();
}
interface IPropDesc {
void OnValueChanged(object component);
}
class PropDesc<T> : PropertyDescriptor, IPropDesc {
readonly Func<SharpTreeNode, T> getter;
readonly Action<SharpTreeNode, T> setter;
public PropDesc(string name, Func<SharpTreeNode, T> getter, Action<SharpTreeNode, T> setter)
: base(name, null) {
this.getter = getter;
this.setter = setter;
}
public override object GetValue(object component) {
return getter(((SharpTreeNodeProxy)component).Object);
}
public override bool IsReadOnly {
get {
return setter == null;
;
}
}
public override Type PropertyType {
get { return typeof(T); }
}
public override void SetValue(object component, object value) {
setter(((SharpTreeNodeProxy)component).Object, (T)value);
}
public void OnValueChanged(object component) {
OnValueChanged(component, new PropertyChangedEventArgs(Name));
}
public override bool CanResetValue(object component) {
return false;
}
public override bool ShouldSerializeValue(object component) {
return false;
}
public override void ResetValue(object component) {
throw new NotSupportedException();
}
public override Type ComponentType {
get { return null; }
}
}
}
}