GUI/Node.cs
author paulwerelds
Sat, 02 Oct 2010 14:39:25 +0000
changeset 204 59278dadc5c0
parent 111 2b8a8cf92c3a
child 344 3145aadca3d2
permissions -rw-r--r--
Added a S.M.A.R.T dump for all drives, regardless of temperature presence so that we can start debugging SSD's and such.
moel@111
     1
/*
moel@111
     2
  
moel@111
     3
  Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@111
     4
moel@111
     5
  The contents of this file are subject to the Mozilla Public License Version
moel@111
     6
  1.1 (the "License"); you may not use this file except in compliance with
moel@111
     7
  the License. You may obtain a copy of the License at
moel@111
     8
 
moel@111
     9
  http://www.mozilla.org/MPL/
moel@111
    10
moel@111
    11
  Software distributed under the License is distributed on an "AS IS" basis,
moel@111
    12
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@111
    13
  for the specific language governing rights and limitations under the License.
moel@111
    14
moel@111
    15
  The Original Code is the Open Hardware Monitor code.
moel@111
    16
moel@111
    17
  The Initial Developer of the Original Code is 
moel@111
    18
  Michael Möller <m.moeller@gmx.ch>.
moel@111
    19
  Portions created by the Initial Developer are Copyright (C) 2009-2010
moel@111
    20
  the Initial Developer. All Rights Reserved.
moel@111
    21
moel@111
    22
  Contributor(s):
moel@111
    23
moel@111
    24
  Alternatively, the contents of this file may be used under the terms of
moel@111
    25
  either the GNU General Public License Version 2 or later (the "GPL"), or
moel@111
    26
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@111
    27
  in which case the provisions of the GPL or the LGPL are applicable instead
moel@111
    28
  of those above. If you wish to allow use of your version of this file only
moel@111
    29
  under the terms of either the GPL or the LGPL, and not to allow others to
moel@111
    30
  use your version of this file under the terms of the MPL, indicate your
moel@111
    31
  decision by deleting the provisions above and replace them with the notice
moel@111
    32
  and other provisions required by the GPL or the LGPL. If you do not delete
moel@111
    33
  the provisions above, a recipient may use your version of this file under
moel@111
    34
  the terms of any one of the MPL, the GPL or the LGPL.
moel@111
    35
 
moel@111
    36
*/
moel@111
    37
moel@111
    38
using System;
moel@111
    39
using System.Collections.Generic;
moel@111
    40
using System.Collections.ObjectModel;
moel@111
    41
using System.Drawing;
moel@111
    42
using Aga.Controls.Tree;
moel@111
    43
moel@111
    44
namespace OpenHardwareMonitor.GUI {
moel@111
    45
  public class Node {
moel@111
    46
moel@111
    47
    private TreeModel treeModel;
moel@111
    48
    private Node parent;
moel@111
    49
    private NodeCollection nodes;
moel@111
    50
moel@111
    51
    private string text;
moel@111
    52
    private Image image;
moel@111
    53
    private bool visible;
moel@111
    54
moel@111
    55
    private TreeModel RootTreeModel() {
moel@111
    56
      Node node = this;
moel@111
    57
      while (node != null) {
moel@111
    58
        if (node.Model != null)
moel@111
    59
          return node.Model;
moel@111
    60
        node = node.parent;
moel@111
    61
      }
moel@111
    62
      return null;
moel@111
    63
    }
moel@111
    64
moel@111
    65
    public Node() : this(string.Empty) { }
moel@111
    66
moel@111
    67
    public Node(string text) {
moel@111
    68
      this.text = text;
moel@111
    69
      this.nodes = new NodeCollection(this);
moel@111
    70
      this.visible = true;
moel@111
    71
    }
moel@111
    72
moel@111
    73
    public TreeModel Model {
moel@111
    74
      get { return treeModel; }
moel@111
    75
      set { treeModel = value; }
moel@111
    76
    }
moel@111
    77
moel@111
    78
    public Node Parent {
moel@111
    79
      get { return parent; }
moel@111
    80
      set {
moel@111
    81
        if (value != parent) {
moel@111
    82
          if (parent != null)
moel@111
    83
            parent.nodes.Remove(this);
moel@111
    84
          if (value != null)
moel@111
    85
            value.nodes.Add(this);
moel@111
    86
        }
moel@111
    87
      }
moel@111
    88
    }
moel@111
    89
moel@111
    90
    public Collection<Node> Nodes {
moel@111
    91
      get { return nodes; }
moel@111
    92
    }
moel@111
    93
moel@111
    94
    public virtual string Text {
moel@111
    95
      get { return text; }
moel@111
    96
      set {
moel@111
    97
        if (text != value) {
moel@111
    98
          text = value;
moel@111
    99
        }
moel@111
   100
      }
moel@111
   101
    }
moel@111
   102
moel@111
   103
    public Image Image {
moel@111
   104
      get { return image; }
moel@111
   105
      set {
moel@111
   106
        if (image != value) {
moel@111
   107
          image = value;
moel@111
   108
        }
moel@111
   109
      }
moel@111
   110
    }
moel@111
   111
moel@111
   112
    public virtual bool IsVisible {
moel@111
   113
      get { return visible; }
moel@111
   114
      set {
moel@111
   115
        if (value != visible) {
moel@111
   116
          visible = value;          
moel@111
   117
          TreeModel model = RootTreeModel();
moel@111
   118
          if (model != null && parent != null) {
moel@111
   119
            int index = 0;
moel@111
   120
            for (int i = 0; i < parent.nodes.Count; i++) {
moel@111
   121
              Node node = parent.nodes[i];
moel@111
   122
              if (node == this)
moel@111
   123
                break;
moel@111
   124
              if (node.IsVisible || model.ForceVisible)
moel@111
   125
                index++;
moel@111
   126
            }
moel@111
   127
            if (model.ForceVisible) {
moel@111
   128
                model.OnNodeChanged(parent, index, this);
moel@111
   129
            } else {              
moel@111
   130
              if (value)
moel@111
   131
                model.OnNodeInserted(parent, index, this);
moel@111
   132
              else
moel@111
   133
                model.OnNodeRemoved(parent, index, this);
moel@111
   134
            }
moel@111
   135
          }
moel@111
   136
          if (IsVisibleChanged != null)
moel@111
   137
            IsVisibleChanged(this);
moel@111
   138
        }
moel@111
   139
      }
moel@111
   140
    }
moel@111
   141
moel@111
   142
    public delegate void NodeEventHandler(Node node);
moel@111
   143
moel@111
   144
    public event NodeEventHandler IsVisibleChanged;
moel@111
   145
    public event NodeEventHandler NodeAdded;
moel@111
   146
    public event NodeEventHandler NodeRemoved;
moel@111
   147
moel@111
   148
    private class NodeCollection : Collection<Node> {
moel@111
   149
      private Node owner;
moel@111
   150
moel@111
   151
      public NodeCollection(Node owner) {
moel@111
   152
        this.owner = owner;
moel@111
   153
      }
moel@111
   154
moel@111
   155
      protected override void ClearItems() {
moel@111
   156
        while (this.Count != 0)
moel@111
   157
          this.RemoveAt(this.Count - 1);
moel@111
   158
      }
moel@111
   159
moel@111
   160
      protected override void InsertItem(int index, Node item) {
moel@111
   161
        if (item == null)
moel@167
   162
          throw new ArgumentNullException("item");
moel@111
   163
moel@111
   164
        if (item.parent != owner) {
moel@111
   165
          if (item.parent != null)
moel@111
   166
            item.parent.nodes.Remove(item);
moel@111
   167
          item.parent = owner;
moel@111
   168
          base.InsertItem(index, item);
moel@111
   169
moel@111
   170
          TreeModel model = owner.RootTreeModel();
moel@111
   171
          if (model != null)
moel@111
   172
            model.OnStructureChanged(owner);
moel@111
   173
          if (owner.NodeAdded != null)
moel@111
   174
            owner.NodeAdded(item);
moel@111
   175
        }
moel@111
   176
      }
moel@111
   177
moel@111
   178
      protected override void RemoveItem(int index) {
moel@111
   179
        Node item = this[index];
moel@111
   180
        item.parent = null;
moel@111
   181
        base.RemoveItem(index);
moel@111
   182
moel@111
   183
        TreeModel model = owner.RootTreeModel();
moel@111
   184
        if (model != null) 
moel@111
   185
          model.OnStructureChanged(owner);
moel@111
   186
        if (owner.NodeRemoved != null)
moel@111
   187
          owner.NodeRemoved(item);
moel@111
   188
      }
moel@111
   189
moel@111
   190
      protected override void SetItem(int index, Node item) {
moel@111
   191
        if (item == null)
moel@167
   192
          throw new ArgumentNullException("item");
moel@111
   193
moel@111
   194
        RemoveAt(index);
moel@111
   195
        InsertItem(index, item);
moel@111
   196
      }
moel@111
   197
    }
moel@111
   198
  }
moel@111
   199
}