GUI/Node.cs
author moel.mich
Wed, 04 Aug 2010 20:27:05 +0000
changeset 162 2129ccee0bd1
child 167 b7cc9d09aefe
permissions -rw-r--r--
Added an ISA bus mutex.
     1 /*
     2   
     3   Version: MPL 1.1/GPL 2.0/LGPL 2.1
     4 
     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
     8  
     9   http://www.mozilla.org/MPL/
    10 
    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.
    14 
    15   The Original Code is the Open Hardware Monitor code.
    16 
    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.
    21 
    22   Contributor(s):
    23 
    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.
    35  
    36 */
    37 
    38 using System;
    39 using System.Collections.Generic;
    40 using System.Collections.ObjectModel;
    41 using System.Drawing;
    42 using Aga.Controls.Tree;
    43 
    44 namespace OpenHardwareMonitor.GUI {
    45   public class Node {
    46 
    47     private TreeModel treeModel;
    48     private Node parent;
    49     private NodeCollection nodes;
    50 
    51     private string text;
    52     private Image image;
    53     private bool visible;
    54 
    55     private TreeModel RootTreeModel() {
    56       Node node = this;
    57       while (node != null) {
    58         if (node.Model != null)
    59           return node.Model;
    60         node = node.parent;
    61       }
    62       return null;
    63     }
    64 
    65     public Node() : this(string.Empty) { }
    66 
    67     public Node(string text) {
    68       this.text = text;
    69       this.nodes = new NodeCollection(this);
    70       this.visible = true;
    71     }
    72 
    73     public TreeModel Model {
    74       get { return treeModel; }
    75       set { treeModel = value; }
    76     }
    77 
    78     public Node Parent {
    79       get { return parent; }
    80       set {
    81         if (value != parent) {
    82           if (parent != null)
    83             parent.nodes.Remove(this);
    84           if (value != null)
    85             value.nodes.Add(this);
    86         }
    87       }
    88     }
    89 
    90     public Collection<Node> Nodes {
    91       get { return nodes; }
    92     }
    93 
    94     public virtual string Text {
    95       get { return text; }
    96       set {
    97         if (text != value) {
    98           text = value;
    99         }
   100       }
   101     }
   102 
   103     public Image Image {
   104       get { return image; }
   105       set {
   106         if (image != value) {
   107           image = value;
   108         }
   109       }
   110     }
   111 
   112     public virtual bool IsVisible {
   113       get { return visible; }
   114       set {
   115         if (value != visible) {
   116           visible = value;          
   117           TreeModel model = RootTreeModel();
   118           if (model != null && parent != null) {
   119             int index = 0;
   120             for (int i = 0; i < parent.nodes.Count; i++) {
   121               Node node = parent.nodes[i];
   122               if (node == this)
   123                 break;
   124               if (node.IsVisible || model.ForceVisible)
   125                 index++;
   126             }
   127             if (model.ForceVisible) {
   128                 model.OnNodeChanged(parent, index, this);
   129             } else {              
   130               if (value)
   131                 model.OnNodeInserted(parent, index, this);
   132               else
   133                 model.OnNodeRemoved(parent, index, this);
   134             }
   135           }
   136           if (IsVisibleChanged != null)
   137             IsVisibleChanged(this);
   138         }
   139       }
   140     }
   141 
   142     public delegate void NodeEventHandler(Node node);
   143 
   144     public event NodeEventHandler IsVisibleChanged;
   145     public event NodeEventHandler NodeAdded;
   146     public event NodeEventHandler NodeRemoved;
   147 
   148     private class NodeCollection : Collection<Node> {
   149       private Node owner;
   150 
   151       public NodeCollection(Node owner) {
   152         this.owner = owner;
   153       }
   154 
   155       protected override void ClearItems() {
   156         while (this.Count != 0)
   157           this.RemoveAt(this.Count - 1);
   158       }
   159 
   160       protected override void InsertItem(int index, Node item) {
   161         if (item == null)
   162           throw new ArgumentNullException();
   163 
   164         if (item.parent != owner) {
   165           if (item.parent != null)
   166             item.parent.nodes.Remove(item);
   167           item.parent = owner;
   168           base.InsertItem(index, item);
   169 
   170           TreeModel model = owner.RootTreeModel();
   171           if (model != null)
   172             model.OnStructureChanged(owner);
   173           if (owner.NodeAdded != null)
   174             owner.NodeAdded(item);
   175         }
   176       }
   177 
   178       protected override void RemoveItem(int index) {
   179         Node item = this[index];
   180         item.parent = null;
   181         base.RemoveItem(index);
   182 
   183         TreeModel model = owner.RootTreeModel();
   184         if (model != null) 
   185           model.OnStructureChanged(owner);
   186         if (owner.NodeRemoved != null)
   187           owner.NodeRemoved(item);
   188       }
   189 
   190       protected override void SetItem(int index, Node item) {
   191         if (item == null)
   192           throw new ArgumentNullException();
   193 
   194         RemoveAt(index);
   195         InsertItem(index, item);
   196       }
   197     }
   198   }
   199 }