moel@345
|
1 |
using System;
|
moel@345
|
2 |
using System.Collections.Generic;
|
moel@345
|
3 |
using System.Text;
|
moel@345
|
4 |
using System.Collections.ObjectModel;
|
moel@345
|
5 |
using System.Windows.Forms;
|
moel@345
|
6 |
using System.Drawing;
|
moel@345
|
7 |
|
moel@345
|
8 |
namespace Aga.Controls.Tree
|
moel@345
|
9 |
{
|
moel@345
|
10 |
public class Node
|
moel@345
|
11 |
{
|
moel@345
|
12 |
#region NodeCollection
|
moel@345
|
13 |
|
moel@345
|
14 |
private class NodeCollection : Collection<Node>
|
moel@345
|
15 |
{
|
moel@345
|
16 |
private Node _owner;
|
moel@345
|
17 |
|
moel@345
|
18 |
public NodeCollection(Node owner)
|
moel@345
|
19 |
{
|
moel@345
|
20 |
_owner = owner;
|
moel@345
|
21 |
}
|
moel@345
|
22 |
|
moel@345
|
23 |
protected override void ClearItems()
|
moel@345
|
24 |
{
|
moel@345
|
25 |
while (this.Count != 0)
|
moel@345
|
26 |
this.RemoveAt(this.Count - 1);
|
moel@345
|
27 |
}
|
moel@345
|
28 |
|
moel@345
|
29 |
protected override void InsertItem(int index, Node item)
|
moel@345
|
30 |
{
|
moel@345
|
31 |
if (item == null)
|
moel@345
|
32 |
throw new ArgumentNullException("item");
|
moel@345
|
33 |
|
moel@345
|
34 |
if (item.Parent != _owner)
|
moel@345
|
35 |
{
|
moel@345
|
36 |
if (item.Parent != null)
|
moel@345
|
37 |
item.Parent.Nodes.Remove(item);
|
moel@345
|
38 |
item._parent = _owner;
|
moel@345
|
39 |
item._index = index;
|
moel@345
|
40 |
for (int i = index; i < Count; i++)
|
moel@345
|
41 |
this[i]._index++;
|
moel@345
|
42 |
base.InsertItem(index, item);
|
moel@345
|
43 |
|
moel@345
|
44 |
TreeModel model = _owner.FindModel();
|
moel@345
|
45 |
if (model != null)
|
moel@345
|
46 |
model.OnNodeInserted(_owner, index, item);
|
moel@345
|
47 |
}
|
moel@345
|
48 |
}
|
moel@345
|
49 |
|
moel@345
|
50 |
protected override void RemoveItem(int index)
|
moel@345
|
51 |
{
|
moel@345
|
52 |
Node item = this[index];
|
moel@345
|
53 |
item._parent = null;
|
moel@345
|
54 |
item._index = -1;
|
moel@345
|
55 |
for (int i = index + 1; i < Count; i++)
|
moel@345
|
56 |
this[i]._index--;
|
moel@345
|
57 |
base.RemoveItem(index);
|
moel@345
|
58 |
|
moel@345
|
59 |
TreeModel model = _owner.FindModel();
|
moel@345
|
60 |
if (model != null)
|
moel@345
|
61 |
model.OnNodeRemoved(_owner, index, item);
|
moel@345
|
62 |
}
|
moel@345
|
63 |
|
moel@345
|
64 |
protected override void SetItem(int index, Node item)
|
moel@345
|
65 |
{
|
moel@345
|
66 |
if (item == null)
|
moel@345
|
67 |
throw new ArgumentNullException("item");
|
moel@345
|
68 |
|
moel@345
|
69 |
RemoveAt(index);
|
moel@345
|
70 |
InsertItem(index, item);
|
moel@345
|
71 |
}
|
moel@345
|
72 |
}
|
moel@345
|
73 |
|
moel@345
|
74 |
#endregion
|
moel@345
|
75 |
|
moel@345
|
76 |
#region Properties
|
moel@345
|
77 |
|
moel@345
|
78 |
private TreeModel _model;
|
moel@345
|
79 |
internal TreeModel Model
|
moel@345
|
80 |
{
|
moel@345
|
81 |
get { return _model; }
|
moel@345
|
82 |
set { _model = value; }
|
moel@345
|
83 |
}
|
moel@345
|
84 |
|
moel@345
|
85 |
private NodeCollection _nodes;
|
moel@345
|
86 |
public Collection<Node> Nodes
|
moel@345
|
87 |
{
|
moel@345
|
88 |
get { return _nodes; }
|
moel@345
|
89 |
}
|
moel@345
|
90 |
|
moel@345
|
91 |
private Node _parent;
|
moel@345
|
92 |
public Node Parent
|
moel@345
|
93 |
{
|
moel@345
|
94 |
get { return _parent; }
|
moel@345
|
95 |
set
|
moel@345
|
96 |
{
|
moel@345
|
97 |
if (value != _parent)
|
moel@345
|
98 |
{
|
moel@345
|
99 |
if (_parent != null)
|
moel@345
|
100 |
_parent.Nodes.Remove(this);
|
moel@345
|
101 |
|
moel@345
|
102 |
if (value != null)
|
moel@345
|
103 |
value.Nodes.Add(this);
|
moel@345
|
104 |
}
|
moel@345
|
105 |
}
|
moel@345
|
106 |
}
|
moel@345
|
107 |
|
moel@345
|
108 |
private int _index = -1;
|
moel@345
|
109 |
public int Index
|
moel@345
|
110 |
{
|
moel@345
|
111 |
get
|
moel@345
|
112 |
{
|
moel@345
|
113 |
return _index;
|
moel@345
|
114 |
}
|
moel@345
|
115 |
}
|
moel@345
|
116 |
|
moel@345
|
117 |
public Node PreviousNode
|
moel@345
|
118 |
{
|
moel@345
|
119 |
get
|
moel@345
|
120 |
{
|
moel@345
|
121 |
int index = Index;
|
moel@345
|
122 |
if (index > 0)
|
moel@345
|
123 |
return _parent.Nodes[index - 1];
|
moel@345
|
124 |
else
|
moel@345
|
125 |
return null;
|
moel@345
|
126 |
}
|
moel@345
|
127 |
}
|
moel@345
|
128 |
|
moel@345
|
129 |
public Node NextNode
|
moel@345
|
130 |
{
|
moel@345
|
131 |
get
|
moel@345
|
132 |
{
|
moel@345
|
133 |
int index = Index;
|
moel@345
|
134 |
if (index >= 0 && index < _parent.Nodes.Count - 1)
|
moel@345
|
135 |
return _parent.Nodes[index + 1];
|
moel@345
|
136 |
else
|
moel@345
|
137 |
return null;
|
moel@345
|
138 |
}
|
moel@345
|
139 |
}
|
moel@345
|
140 |
|
moel@345
|
141 |
private string _text;
|
moel@345
|
142 |
public virtual string Text
|
moel@345
|
143 |
{
|
moel@345
|
144 |
get { return _text; }
|
moel@345
|
145 |
set
|
moel@345
|
146 |
{
|
moel@345
|
147 |
if (_text != value)
|
moel@345
|
148 |
{
|
moel@345
|
149 |
_text = value;
|
moel@345
|
150 |
NotifyModel();
|
moel@345
|
151 |
}
|
moel@345
|
152 |
}
|
moel@345
|
153 |
}
|
moel@345
|
154 |
|
moel@345
|
155 |
private CheckState _checkState;
|
moel@345
|
156 |
public virtual CheckState CheckState
|
moel@345
|
157 |
{
|
moel@345
|
158 |
get { return _checkState; }
|
moel@345
|
159 |
set
|
moel@345
|
160 |
{
|
moel@345
|
161 |
if (_checkState != value)
|
moel@345
|
162 |
{
|
moel@345
|
163 |
_checkState = value;
|
moel@345
|
164 |
NotifyModel();
|
moel@345
|
165 |
}
|
moel@345
|
166 |
}
|
moel@345
|
167 |
}
|
moel@345
|
168 |
|
moel@345
|
169 |
private Image _image;
|
moel@345
|
170 |
public Image Image
|
moel@345
|
171 |
{
|
moel@345
|
172 |
get { return _image; }
|
moel@345
|
173 |
set
|
moel@345
|
174 |
{
|
moel@345
|
175 |
if (_image != value)
|
moel@345
|
176 |
{
|
moel@345
|
177 |
_image = value;
|
moel@345
|
178 |
NotifyModel();
|
moel@345
|
179 |
}
|
moel@345
|
180 |
}
|
moel@345
|
181 |
}
|
moel@345
|
182 |
|
moel@345
|
183 |
private object _tag;
|
moel@345
|
184 |
public object Tag
|
moel@345
|
185 |
{
|
moel@345
|
186 |
get { return _tag; }
|
moel@345
|
187 |
set { _tag = value; }
|
moel@345
|
188 |
}
|
moel@345
|
189 |
|
moel@345
|
190 |
public bool IsChecked
|
moel@345
|
191 |
{
|
moel@345
|
192 |
get
|
moel@345
|
193 |
{
|
moel@345
|
194 |
return CheckState != CheckState.Unchecked;
|
moel@345
|
195 |
}
|
moel@345
|
196 |
set
|
moel@345
|
197 |
{
|
moel@345
|
198 |
if (value)
|
moel@345
|
199 |
CheckState = CheckState.Checked;
|
moel@345
|
200 |
else
|
moel@345
|
201 |
CheckState = CheckState.Unchecked;
|
moel@345
|
202 |
}
|
moel@345
|
203 |
}
|
moel@345
|
204 |
|
moel@345
|
205 |
public virtual bool IsLeaf
|
moel@345
|
206 |
{
|
moel@345
|
207 |
get
|
moel@345
|
208 |
{
|
moel@345
|
209 |
return false;
|
moel@345
|
210 |
}
|
moel@345
|
211 |
}
|
moel@345
|
212 |
|
moel@345
|
213 |
#endregion
|
moel@345
|
214 |
|
moel@345
|
215 |
public Node()
|
moel@345
|
216 |
: this(string.Empty)
|
moel@345
|
217 |
{
|
moel@345
|
218 |
}
|
moel@345
|
219 |
|
moel@345
|
220 |
public Node(string text)
|
moel@345
|
221 |
{
|
moel@345
|
222 |
_text = text;
|
moel@345
|
223 |
_nodes = new NodeCollection(this);
|
moel@345
|
224 |
}
|
moel@345
|
225 |
|
moel@345
|
226 |
public override string ToString()
|
moel@345
|
227 |
{
|
moel@345
|
228 |
return Text;
|
moel@345
|
229 |
}
|
moel@345
|
230 |
|
moel@345
|
231 |
private TreeModel FindModel()
|
moel@345
|
232 |
{
|
moel@345
|
233 |
Node node = this;
|
moel@345
|
234 |
while (node != null)
|
moel@345
|
235 |
{
|
moel@345
|
236 |
if (node.Model != null)
|
moel@345
|
237 |
return node.Model;
|
moel@345
|
238 |
node = node.Parent;
|
moel@345
|
239 |
}
|
moel@345
|
240 |
return null;
|
moel@345
|
241 |
}
|
moel@345
|
242 |
|
moel@345
|
243 |
protected void NotifyModel()
|
moel@345
|
244 |
{
|
moel@345
|
245 |
TreeModel model = FindModel();
|
moel@345
|
246 |
if (model != null && Parent != null)
|
moel@345
|
247 |
{
|
moel@345
|
248 |
TreePath path = model.GetPath(Parent);
|
moel@345
|
249 |
if (path != null)
|
moel@345
|
250 |
{
|
moel@345
|
251 |
TreeModelEventArgs args = new TreeModelEventArgs(path, new int[] { Index }, new object[] { this });
|
moel@345
|
252 |
model.OnNodesChanged(args);
|
moel@345
|
253 |
}
|
moel@345
|
254 |
}
|
moel@345
|
255 |
}
|
moel@345
|
256 |
}
|
moel@345
|
257 |
}
|