moel@111: /*
moel@344:  
moel@344:   This Source Code Form is subject to the terms of the Mozilla Public
moel@344:   License, v. 2.0. If a copy of the MPL was not distributed with this
moel@344:   file, You can obtain one at http://mozilla.org/MPL/2.0/.
moel@344:  
moel@344:   Copyright (C) 2009-2010 Michael Möller <mmoeller@openhardwaremonitor.org>
moel@344: 	
moel@344: */
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<Node> 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<Node> {
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@167:           throw new ArgumentNullException("item");
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@167:           throw new ArgumentNullException("item");
moel@111: 
moel@111:         RemoveAt(index);
moel@111:         InsertItem(index, item);
moel@111:       }
moel@111:     }
moel@111:   }
moel@111: }