GUI/HardwareNode.cs
author moel.mich
Fri, 29 Jan 2010 21:58:31 +0000
changeset 12 a105b5eee02f
child 24 09ab31bee6bd
permissions -rw-r--r--
Release version 0.1.4. Fixed EntryPointNotFoundException for ADL_Adapter_ID_Get by adding two fallback implementations.
     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 Aga.Controls.Tree;
    42 using OpenHardwareMonitor.Hardware;
    43 
    44 namespace OpenHardwareMonitor.GUI {
    45   public class HardwareNode : Node {
    46 
    47     private IHardware hardware;
    48 
    49     private List<TypeNode> typeNodes = new List<TypeNode>();
    50 
    51     public HardwareNode(IHardware hardware) : base(hardware.Name) {
    52       
    53       this.hardware = hardware;
    54       this.Image = hardware.Icon;
    55 
    56       typeNodes.Add(new TypeNode(SensorType.Voltage));
    57       typeNodes.Add(new TypeNode(SensorType.Clock));
    58       typeNodes.Add(new TypeNode(SensorType.Temperature));
    59       typeNodes.Add(new TypeNode(SensorType.Fan));
    60       
    61       foreach (ISensor sensor in hardware.Sensors)
    62         SensorAdded(sensor);
    63 
    64       hardware.SensorAdded +=new SensorEventHandler(SensorAdded);
    65       hardware.SensorRemoved += new SensorEventHandler(SensorRemoved);
    66     }
    67 
    68     public IHardware Hardware {
    69       get { return hardware; }
    70     }
    71 
    72     public void SetVisible(SensorType sensorType, bool visible) {
    73       foreach (TypeNode node in typeNodes)
    74         if (node.SensorType == sensorType) {
    75           node.IsVisible = visible;
    76           UpdateNode(node);
    77         }
    78     }
    79 
    80     private void UpdateNode(TypeNode node) {      
    81       if (node.IsVisible && node.Nodes.Count > 0) {
    82         if (!Nodes.Contains(node)) {
    83           int i = 0;
    84           while (i < Nodes.Count &&
    85             ((TypeNode)Nodes[i]).SensorType < node.SensorType)
    86             i++;
    87           Nodes.Insert(i, node);  
    88         }
    89       } else {
    90         if (Nodes.Contains(node))
    91           Nodes.Remove(node);
    92       }
    93     }
    94 
    95     private void SensorRemoved(ISensor sensor) {
    96       foreach (TypeNode node in typeNodes)
    97         if (node.SensorType == sensor.SensorType) {
    98           node.Nodes.Remove(new SensorNode(sensor));
    99           UpdateNode(node);
   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       node.Nodes.Insert(i, new SensorNode(sensor));        
   109     }
   110 
   111     private void SensorAdded(ISensor sensor) {
   112       foreach (TypeNode node in typeNodes)
   113         if (node.SensorType == sensor.SensorType) {
   114           InsertSorted(node, sensor);
   115           UpdateNode(node);
   116         }
   117     }
   118   }
   119 }