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