GUI/HardwareNode.cs
author moel.mich
Sat, 17 Jul 2010 13:25:42 +0000
changeset 155 8ffea928daad
parent 111 2b8a8cf92c3a
child 165 813d8bc3192f
permissions -rw-r--r--
Fixed Issue 91.
     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.Drawing;
    41 using OpenHardwareMonitor.Hardware;
    42 
    43 namespace OpenHardwareMonitor.GUI {
    44   public class HardwareNode : Node {
    45 
    46     private IHardware hardware;
    47 
    48     private List<TypeNode> typeNodes = new List<TypeNode>();
    49 
    50     public HardwareNode(IHardware hardware) : base(hardware.Name) {
    51       
    52       this.hardware = hardware;
    53       this.Image = hardware.Icon;
    54 
    55       typeNodes.Add(new TypeNode(SensorType.Voltage));
    56       typeNodes.Add(new TypeNode(SensorType.Clock));      
    57       typeNodes.Add(new TypeNode(SensorType.Temperature));
    58       typeNodes.Add(new TypeNode(SensorType.Load));
    59       typeNodes.Add(new TypeNode(SensorType.Fan));
    60       typeNodes.Add(new TypeNode(SensorType.Flow));
    61       typeNodes.Add(new TypeNode(SensorType.Control));
    62       
    63       foreach (ISensor sensor in hardware.Sensors)
    64         SensorAdded(sensor);
    65 
    66       hardware.SensorAdded +=new SensorEventHandler(SensorAdded);
    67       hardware.SensorRemoved += new SensorEventHandler(SensorRemoved);
    68     }
    69 
    70     public IHardware Hardware {
    71       get { return hardware; }
    72     }
    73 
    74     private void UpdateNode(TypeNode node) {  
    75       if (node.Nodes.Count > 0) {
    76         if (!Nodes.Contains(node)) {
    77           int i = 0;
    78           while (i < Nodes.Count &&
    79             ((TypeNode)Nodes[i]).SensorType < node.SensorType)
    80             i++;
    81           Nodes.Insert(i, node);  
    82         }
    83       } else {
    84         if (Nodes.Contains(node))
    85           Nodes.Remove(node);
    86       }
    87     }
    88 
    89     private void SensorRemoved(ISensor sensor) {
    90       foreach (TypeNode typeNode in typeNodes)
    91         if (typeNode.SensorType == sensor.SensorType) { 
    92           SensorNode sensorNode = null;
    93           foreach (Node node in typeNode.Nodes) {
    94             SensorNode n = node as SensorNode;
    95             if (n != null && n.Sensor == sensor)
    96               sensorNode = n;
    97           }
    98           typeNode.Nodes.Remove(sensorNode);
    99           UpdateNode(typeNode);
   100         }
   101     }
   102 
   103     private void InsertSorted(Node node, ISensor sensor) {
   104       int i = 0;
   105       while (i < node.Nodes.Count &&
   106         ((SensorNode)node.Nodes[i]).Sensor.Index < sensor.Index)
   107         i++;
   108       SensorNode sensorNode = new SensorNode(sensor);
   109       node.Nodes.Insert(i, sensorNode);
   110     }
   111 
   112     private void SensorAdded(ISensor sensor) {
   113       foreach (TypeNode typeNode in typeNodes)
   114         if (typeNode.SensorType == sensor.SensorType) {
   115           InsertSorted(typeNode, sensor);
   116           UpdateNode(typeNode);          
   117         }
   118     }    
   119   }
   120 }