moel@111: /* moel@111: moel@111: Version: MPL 1.1/GPL 2.0/LGPL 2.1 moel@111: moel@111: The contents of this file are subject to the Mozilla Public License Version moel@111: 1.1 (the "License"); you may not use this file except in compliance with moel@111: the License. You may obtain a copy of the License at moel@111: moel@111: http://www.mozilla.org/MPL/ moel@111: moel@111: Software distributed under the License is distributed on an "AS IS" basis, moel@111: WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License moel@111: for the specific language governing rights and limitations under the License. moel@111: moel@111: The Original Code is the Open Hardware Monitor code. moel@111: moel@111: The Initial Developer of the Original Code is moel@111: Michael Möller . moel@111: Portions created by the Initial Developer are Copyright (C) 2009-2010 moel@111: the Initial Developer. All Rights Reserved. moel@111: moel@111: Contributor(s): moel@111: moel@111: Alternatively, the contents of this file may be used under the terms of moel@111: either the GNU General Public License Version 2 or later (the "GPL"), or moel@111: the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), moel@111: in which case the provisions of the GPL or the LGPL are applicable instead moel@111: of those above. If you wish to allow use of your version of this file only moel@111: under the terms of either the GPL or the LGPL, and not to allow others to moel@111: use your version of this file under the terms of the MPL, indicate your moel@111: decision by deleting the provisions above and replace them with the notice moel@111: and other provisions required by the GPL or the LGPL. If you do not delete moel@111: the provisions above, a recipient may use your version of this file under moel@111: the terms of any one of the MPL, the GPL or the LGPL. moel@111: moel@111: */ moel@111: moel@111: using System; moel@111: using System.Collections.Generic; moel@111: using System.Collections.ObjectModel; moel@111: using System.Drawing; moel@111: using Aga.Controls.Tree; moel@111: moel@111: namespace OpenHardwareMonitor.GUI { moel@111: public class Node { moel@111: moel@111: private TreeModel treeModel; moel@111: private Node parent; moel@111: private NodeCollection nodes; moel@111: moel@111: private string text; moel@111: private Image image; moel@111: private bool visible; moel@111: moel@111: private TreeModel RootTreeModel() { moel@111: Node node = this; moel@111: while (node != null) { moel@111: if (node.Model != null) moel@111: return node.Model; moel@111: node = node.parent; moel@111: } moel@111: return null; moel@111: } moel@111: moel@111: public Node() : this(string.Empty) { } moel@111: moel@111: public Node(string text) { moel@111: this.text = text; moel@111: this.nodes = new NodeCollection(this); moel@111: this.visible = true; moel@111: } moel@111: moel@111: public TreeModel Model { moel@111: get { return treeModel; } moel@111: set { treeModel = value; } moel@111: } moel@111: moel@111: public Node Parent { moel@111: get { return parent; } moel@111: set { moel@111: if (value != parent) { moel@111: if (parent != null) moel@111: parent.nodes.Remove(this); moel@111: if (value != null) moel@111: value.nodes.Add(this); moel@111: } moel@111: } moel@111: } moel@111: moel@111: public Collection Nodes { moel@111: get { return nodes; } moel@111: } moel@111: moel@111: public virtual string Text { moel@111: get { return text; } moel@111: set { moel@111: if (text != value) { moel@111: text = value; moel@111: } moel@111: } moel@111: } moel@111: moel@111: public Image Image { moel@111: get { return image; } moel@111: set { moel@111: if (image != value) { moel@111: image = value; moel@111: } moel@111: } moel@111: } moel@111: moel@111: public virtual bool IsVisible { moel@111: get { return visible; } moel@111: set { moel@111: if (value != visible) { moel@111: visible = value; moel@111: TreeModel model = RootTreeModel(); moel@111: if (model != null && parent != null) { moel@111: int index = 0; moel@111: for (int i = 0; i < parent.nodes.Count; i++) { moel@111: Node node = parent.nodes[i]; moel@111: if (node == this) moel@111: break; moel@111: if (node.IsVisible || model.ForceVisible) moel@111: index++; moel@111: } moel@111: if (model.ForceVisible) { moel@111: model.OnNodeChanged(parent, index, this); moel@111: } else { moel@111: if (value) moel@111: model.OnNodeInserted(parent, index, this); moel@111: else moel@111: model.OnNodeRemoved(parent, index, this); moel@111: } moel@111: } moel@111: if (IsVisibleChanged != null) moel@111: IsVisibleChanged(this); moel@111: } moel@111: } moel@111: } moel@111: moel@111: public delegate void NodeEventHandler(Node node); moel@111: moel@111: public event NodeEventHandler IsVisibleChanged; moel@111: public event NodeEventHandler NodeAdded; moel@111: public event NodeEventHandler NodeRemoved; moel@111: moel@111: private class NodeCollection : Collection { moel@111: private Node owner; moel@111: moel@111: public NodeCollection(Node owner) { moel@111: this.owner = owner; moel@111: } moel@111: moel@111: protected override void ClearItems() { moel@111: while (this.Count != 0) moel@111: this.RemoveAt(this.Count - 1); moel@111: } moel@111: moel@111: protected override void InsertItem(int index, Node item) { moel@111: if (item == null) moel@111: throw new ArgumentNullException(); moel@111: moel@111: if (item.parent != owner) { moel@111: if (item.parent != null) moel@111: item.parent.nodes.Remove(item); moel@111: item.parent = owner; moel@111: base.InsertItem(index, item); moel@111: moel@111: TreeModel model = owner.RootTreeModel(); moel@111: if (model != null) moel@111: model.OnStructureChanged(owner); moel@111: if (owner.NodeAdded != null) moel@111: owner.NodeAdded(item); moel@111: } moel@111: } moel@111: moel@111: protected override void RemoveItem(int index) { moel@111: Node item = this[index]; moel@111: item.parent = null; moel@111: base.RemoveItem(index); moel@111: moel@111: TreeModel model = owner.RootTreeModel(); moel@111: if (model != null) moel@111: model.OnStructureChanged(owner); moel@111: if (owner.NodeRemoved != null) moel@111: owner.NodeRemoved(item); moel@111: } moel@111: moel@111: protected override void SetItem(int index, Node item) { moel@111: if (item == null) moel@111: throw new ArgumentNullException(); moel@111: moel@111: RemoveAt(index); moel@111: InsertItem(index, item); moel@111: } moel@111: } moel@111: } moel@111: }