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/.
7 Copyright (C) 2009-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
12 using System.Collections.Generic;
14 using OpenHardwareMonitor.Hardware;
16 namespace OpenHardwareMonitor.GUI {
17 public class HardwareNode : Node {
19 private PersistentSettings settings;
20 private UnitManager unitManager;
21 private IHardware hardware;
23 private List<TypeNode> typeNodes = new List<TypeNode>();
25 public HardwareNode(IHardware hardware, PersistentSettings settings,
26 UnitManager unitManager) : base()
28 this.settings = settings;
29 this.unitManager = unitManager;
30 this.hardware = hardware;
31 this.Image = HardwareTypeImage.Instance.GetImage(hardware.HardwareType);
33 foreach (SensorType sensorType in Enum.GetValues(typeof(SensorType)))
34 typeNodes.Add(new TypeNode(sensorType));
36 foreach (ISensor sensor in hardware.Sensors)
39 hardware.SensorAdded +=new SensorEventHandler(SensorAdded);
40 hardware.SensorRemoved += new SensorEventHandler(SensorRemoved);
43 public override string Text {
44 get { return hardware.Name; }
45 set { hardware.Name = value; }
48 public IHardware Hardware {
49 get { return hardware; }
52 private void UpdateNode(TypeNode node) {
53 if (node.Nodes.Count > 0) {
54 if (!Nodes.Contains(node)) {
56 while (i < Nodes.Count &&
57 ((TypeNode)Nodes[i]).SensorType < node.SensorType)
59 Nodes.Insert(i, node);
62 if (Nodes.Contains(node))
67 private void SensorRemoved(ISensor sensor) {
68 foreach (TypeNode typeNode in typeNodes)
69 if (typeNode.SensorType == sensor.SensorType) {
70 SensorNode sensorNode = null;
71 foreach (Node node in typeNode.Nodes) {
72 SensorNode n = node as SensorNode;
73 if (n != null && n.Sensor == sensor)
76 if (sensorNode != null) {
77 sensorNode.PlotSelectionChanged -= SensorPlotSelectionChanged;
78 typeNode.Nodes.Remove(sensorNode);
82 if (PlotSelectionChanged != null)
83 PlotSelectionChanged(this, null);
86 private void InsertSorted(Node node, ISensor sensor) {
88 while (i < node.Nodes.Count &&
89 ((SensorNode)node.Nodes[i]).Sensor.Index < sensor.Index)
91 SensorNode sensorNode = new SensorNode(sensor, settings, unitManager);
92 sensorNode.PlotSelectionChanged += SensorPlotSelectionChanged;
93 node.Nodes.Insert(i, sensorNode);
96 private void SensorPlotSelectionChanged(object sender, EventArgs e) {
97 if (PlotSelectionChanged != null)
98 PlotSelectionChanged(this, null);
101 private void SensorAdded(ISensor sensor) {
102 foreach (TypeNode typeNode in typeNodes)
103 if (typeNode.SensorType == sensor.SensorType) {
104 InsertSorted(typeNode, sensor);
105 UpdateNode(typeNode);
107 if (PlotSelectionChanged != null)
108 PlotSelectionChanged(this, null);
111 public event EventHandler PlotSelectionChanged;