moel@111
|
1 |
/*
|
moel@344
|
2 |
|
moel@344
|
3 |
This Source Code Form is subject to the terms of the Mozilla Public
|
moel@344
|
4 |
License, v. 2.0. If a copy of the MPL was not distributed with this
|
moel@344
|
5 |
file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
moel@344
|
6 |
|
moel@344
|
7 |
Copyright (C) 2009-2010 Michael Möller <mmoeller@openhardwaremonitor.org>
|
moel@344
|
8 |
|
moel@344
|
9 |
*/
|
moel@111
|
10 |
|
moel@111
|
11 |
|
moel@111
|
12 |
using System;
|
moel@111
|
13 |
using System.Collections.Generic;
|
moel@111
|
14 |
using System.Collections.ObjectModel;
|
moel@111
|
15 |
using System.Drawing;
|
moel@111
|
16 |
using Aga.Controls.Tree;
|
moel@111
|
17 |
|
moel@111
|
18 |
namespace OpenHardwareMonitor.GUI {
|
moel@111
|
19 |
public class Node {
|
moel@111
|
20 |
|
moel@111
|
21 |
private TreeModel treeModel;
|
moel@111
|
22 |
private Node parent;
|
moel@111
|
23 |
private NodeCollection nodes;
|
moel@111
|
24 |
|
moel@111
|
25 |
private string text;
|
moel@111
|
26 |
private Image image;
|
moel@111
|
27 |
private bool visible;
|
moel@111
|
28 |
|
moel@111
|
29 |
private TreeModel RootTreeModel() {
|
moel@111
|
30 |
Node node = this;
|
moel@111
|
31 |
while (node != null) {
|
moel@111
|
32 |
if (node.Model != null)
|
moel@111
|
33 |
return node.Model;
|
moel@111
|
34 |
node = node.parent;
|
moel@111
|
35 |
}
|
moel@111
|
36 |
return null;
|
moel@111
|
37 |
}
|
moel@111
|
38 |
|
moel@111
|
39 |
public Node() : this(string.Empty) { }
|
moel@111
|
40 |
|
moel@111
|
41 |
public Node(string text) {
|
moel@111
|
42 |
this.text = text;
|
moel@111
|
43 |
this.nodes = new NodeCollection(this);
|
moel@111
|
44 |
this.visible = true;
|
moel@111
|
45 |
}
|
moel@111
|
46 |
|
moel@111
|
47 |
public TreeModel Model {
|
moel@111
|
48 |
get { return treeModel; }
|
moel@111
|
49 |
set { treeModel = value; }
|
moel@111
|
50 |
}
|
moel@111
|
51 |
|
moel@111
|
52 |
public Node Parent {
|
moel@111
|
53 |
get { return parent; }
|
moel@111
|
54 |
set {
|
moel@111
|
55 |
if (value != parent) {
|
moel@111
|
56 |
if (parent != null)
|
moel@111
|
57 |
parent.nodes.Remove(this);
|
moel@111
|
58 |
if (value != null)
|
moel@111
|
59 |
value.nodes.Add(this);
|
moel@111
|
60 |
}
|
moel@111
|
61 |
}
|
moel@111
|
62 |
}
|
moel@111
|
63 |
|
moel@111
|
64 |
public Collection<Node> Nodes {
|
moel@111
|
65 |
get { return nodes; }
|
moel@111
|
66 |
}
|
moel@111
|
67 |
|
moel@111
|
68 |
public virtual string Text {
|
moel@111
|
69 |
get { return text; }
|
moel@111
|
70 |
set {
|
moel@111
|
71 |
if (text != value) {
|
moel@111
|
72 |
text = value;
|
moel@111
|
73 |
}
|
moel@111
|
74 |
}
|
moel@111
|
75 |
}
|
moel@111
|
76 |
|
moel@111
|
77 |
public Image Image {
|
moel@111
|
78 |
get { return image; }
|
moel@111
|
79 |
set {
|
moel@111
|
80 |
if (image != value) {
|
moel@111
|
81 |
image = value;
|
moel@111
|
82 |
}
|
moel@111
|
83 |
}
|
moel@111
|
84 |
}
|
moel@111
|
85 |
|
moel@111
|
86 |
public virtual bool IsVisible {
|
moel@111
|
87 |
get { return visible; }
|
moel@111
|
88 |
set {
|
moel@111
|
89 |
if (value != visible) {
|
moel@111
|
90 |
visible = value;
|
moel@111
|
91 |
TreeModel model = RootTreeModel();
|
moel@111
|
92 |
if (model != null && parent != null) {
|
moel@111
|
93 |
int index = 0;
|
moel@111
|
94 |
for (int i = 0; i < parent.nodes.Count; i++) {
|
moel@111
|
95 |
Node node = parent.nodes[i];
|
moel@111
|
96 |
if (node == this)
|
moel@111
|
97 |
break;
|
moel@111
|
98 |
if (node.IsVisible || model.ForceVisible)
|
moel@111
|
99 |
index++;
|
moel@111
|
100 |
}
|
moel@111
|
101 |
if (model.ForceVisible) {
|
moel@111
|
102 |
model.OnNodeChanged(parent, index, this);
|
moel@111
|
103 |
} else {
|
moel@111
|
104 |
if (value)
|
moel@111
|
105 |
model.OnNodeInserted(parent, index, this);
|
moel@111
|
106 |
else
|
moel@111
|
107 |
model.OnNodeRemoved(parent, index, this);
|
moel@111
|
108 |
}
|
moel@111
|
109 |
}
|
moel@111
|
110 |
if (IsVisibleChanged != null)
|
moel@111
|
111 |
IsVisibleChanged(this);
|
moel@111
|
112 |
}
|
moel@111
|
113 |
}
|
moel@111
|
114 |
}
|
moel@111
|
115 |
|
moel@111
|
116 |
public delegate void NodeEventHandler(Node node);
|
moel@111
|
117 |
|
moel@111
|
118 |
public event NodeEventHandler IsVisibleChanged;
|
moel@111
|
119 |
public event NodeEventHandler NodeAdded;
|
moel@111
|
120 |
public event NodeEventHandler NodeRemoved;
|
moel@111
|
121 |
|
moel@111
|
122 |
private class NodeCollection : Collection<Node> {
|
moel@111
|
123 |
private Node owner;
|
moel@111
|
124 |
|
moel@111
|
125 |
public NodeCollection(Node owner) {
|
moel@111
|
126 |
this.owner = owner;
|
moel@111
|
127 |
}
|
moel@111
|
128 |
|
moel@111
|
129 |
protected override void ClearItems() {
|
moel@111
|
130 |
while (this.Count != 0)
|
moel@111
|
131 |
this.RemoveAt(this.Count - 1);
|
moel@111
|
132 |
}
|
moel@111
|
133 |
|
moel@111
|
134 |
protected override void InsertItem(int index, Node item) {
|
moel@111
|
135 |
if (item == null)
|
moel@167
|
136 |
throw new ArgumentNullException("item");
|
moel@111
|
137 |
|
moel@111
|
138 |
if (item.parent != owner) {
|
moel@111
|
139 |
if (item.parent != null)
|
moel@111
|
140 |
item.parent.nodes.Remove(item);
|
moel@111
|
141 |
item.parent = owner;
|
moel@111
|
142 |
base.InsertItem(index, item);
|
moel@111
|
143 |
|
moel@111
|
144 |
TreeModel model = owner.RootTreeModel();
|
moel@111
|
145 |
if (model != null)
|
moel@111
|
146 |
model.OnStructureChanged(owner);
|
moel@111
|
147 |
if (owner.NodeAdded != null)
|
moel@111
|
148 |
owner.NodeAdded(item);
|
moel@111
|
149 |
}
|
moel@111
|
150 |
}
|
moel@111
|
151 |
|
moel@111
|
152 |
protected override void RemoveItem(int index) {
|
moel@111
|
153 |
Node item = this[index];
|
moel@111
|
154 |
item.parent = null;
|
moel@111
|
155 |
base.RemoveItem(index);
|
moel@111
|
156 |
|
moel@111
|
157 |
TreeModel model = owner.RootTreeModel();
|
moel@111
|
158 |
if (model != null)
|
moel@111
|
159 |
model.OnStructureChanged(owner);
|
moel@111
|
160 |
if (owner.NodeRemoved != null)
|
moel@111
|
161 |
owner.NodeRemoved(item);
|
moel@111
|
162 |
}
|
moel@111
|
163 |
|
moel@111
|
164 |
protected override void SetItem(int index, Node item) {
|
moel@111
|
165 |
if (item == null)
|
moel@167
|
166 |
throw new ArgumentNullException("item");
|
moel@111
|
167 |
|
moel@111
|
168 |
RemoveAt(index);
|
moel@111
|
169 |
InsertItem(index, item);
|
moel@111
|
170 |
}
|
moel@111
|
171 |
}
|
moel@111
|
172 |
}
|
moel@111
|
173 |
}
|