Fixed Issue 91.
3 Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 The contents of this file are subject to the Mozilla Public License Version
6 1.1 (the "License"); you may not use this file except in compliance with
7 the License. You may obtain a copy of the License at
9 http://www.mozilla.org/MPL/
11 Software distributed under the License is distributed on an "AS IS" basis,
12 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 for the specific language governing rights and limitations under the License.
15 The Original Code is the Open Hardware Monitor code.
17 The Initial Developer of the Original Code is
18 Michael Möller <m.moeller@gmx.ch>.
19 Portions created by the Initial Developer are Copyright (C) 2009-2010
20 the Initial Developer. All Rights Reserved.
24 Alternatively, the contents of this file may be used under the terms of
25 either the GNU General Public License Version 2 or later (the "GPL"), or
26 the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 in which case the provisions of the GPL or the LGPL are applicable instead
28 of those above. If you wish to allow use of your version of this file only
29 under the terms of either the GPL or the LGPL, and not to allow others to
30 use your version of this file under the terms of the MPL, indicate your
31 decision by deleting the provisions above and replace them with the notice
32 and other provisions required by the GPL or the LGPL. If you do not delete
33 the provisions above, a recipient may use your version of this file under
34 the terms of any one of the MPL, the GPL or the LGPL.
39 using System.Collections.Generic;
40 using System.Collections.ObjectModel;
42 using Aga.Controls.Tree;
44 namespace OpenHardwareMonitor.GUI {
47 private TreeModel treeModel;
49 private NodeCollection nodes;
55 private TreeModel RootTreeModel() {
57 while (node != null) {
58 if (node.Model != null)
65 public Node() : this(string.Empty) { }
67 public Node(string text) {
69 this.nodes = new NodeCollection(this);
73 public TreeModel Model {
74 get { return treeModel; }
75 set { treeModel = value; }
79 get { return parent; }
81 if (value != parent) {
83 parent.nodes.Remove(this);
85 value.nodes.Add(this);
90 public Collection<Node> Nodes {
94 public virtual string Text {
104 get { return image; }
106 if (image != value) {
112 public virtual bool IsVisible {
113 get { return visible; }
115 if (value != visible) {
117 TreeModel model = RootTreeModel();
118 if (model != null && parent != null) {
120 for (int i = 0; i < parent.nodes.Count; i++) {
121 Node node = parent.nodes[i];
124 if (node.IsVisible || model.ForceVisible)
127 if (model.ForceVisible) {
128 model.OnNodeChanged(parent, index, this);
131 model.OnNodeInserted(parent, index, this);
133 model.OnNodeRemoved(parent, index, this);
136 if (IsVisibleChanged != null)
137 IsVisibleChanged(this);
142 public delegate void NodeEventHandler(Node node);
144 public event NodeEventHandler IsVisibleChanged;
145 public event NodeEventHandler NodeAdded;
146 public event NodeEventHandler NodeRemoved;
148 private class NodeCollection : Collection<Node> {
151 public NodeCollection(Node owner) {
155 protected override void ClearItems() {
156 while (this.Count != 0)
157 this.RemoveAt(this.Count - 1);
160 protected override void InsertItem(int index, Node item) {
162 throw new ArgumentNullException();
164 if (item.parent != owner) {
165 if (item.parent != null)
166 item.parent.nodes.Remove(item);
168 base.InsertItem(index, item);
170 TreeModel model = owner.RootTreeModel();
172 model.OnStructureChanged(owner);
173 if (owner.NodeAdded != null)
174 owner.NodeAdded(item);
178 protected override void RemoveItem(int index) {
179 Node item = this[index];
181 base.RemoveItem(index);
183 TreeModel model = owner.RootTreeModel();
185 model.OnStructureChanged(owner);
186 if (owner.NodeRemoved != null)
187 owner.NodeRemoved(item);
190 protected override void SetItem(int index, Node item) {
192 throw new ArgumentNullException();
195 InsertItem(index, item);