moel@1: /* moel@1: moel@344: This Source Code Form is subject to the terms of the Mozilla Public moel@344: License, v. 2.0. If a copy of the MPL was not distributed with this moel@344: file, You can obtain one at http://mozilla.org/MPL/2.0/. moel@1: moel@344: Copyright (C) 2009-2012 Michael Möller moel@344: moel@1: */ moel@1: moel@1: using System; moel@1: using System.Collections.Generic; moel@1: using System.Drawing; moel@1: using OpenHardwareMonitor.Hardware; moel@1: moel@1: namespace OpenHardwareMonitor.GUI { moel@1: public class HardwareNode : Node { moel@1: moel@165: private PersistentSettings settings; moel@165: private UnitManager unitManager; moel@1: private IHardware hardware; moel@1: moel@1: private List typeNodes = new List(); moel@1: moel@165: public HardwareNode(IHardware hardware, PersistentSettings settings, moel@275: UnitManager unitManager) : base() moel@165: { moel@165: this.settings = settings; moel@165: this.unitManager = unitManager; moel@1: this.hardware = hardware; moel@176: this.Image = HardwareTypeImage.Instance.GetImage(hardware.HardwareType); moel@1: moel@340: foreach (SensorType sensorType in Enum.GetValues(typeof(SensorType))) moel@340: typeNodes.Add(new TypeNode(sensorType)); moel@317: moel@1: foreach (ISensor sensor in hardware.Sensors) moel@1: SensorAdded(sensor); moel@1: moel@1: hardware.SensorAdded +=new SensorEventHandler(SensorAdded); moel@1: hardware.SensorRemoved += new SensorEventHandler(SensorRemoved); moel@1: } moel@1: moel@275: public override string Text { moel@275: get { return hardware.Name; } moel@275: set { hardware.Name = value; } moel@275: } moel@275: moel@1: public IHardware Hardware { moel@1: get { return hardware; } moel@1: } moel@1: moel@111: private void UpdateNode(TypeNode node) { moel@111: if (node.Nodes.Count > 0) { moel@1: if (!Nodes.Contains(node)) { moel@1: int i = 0; moel@1: while (i < Nodes.Count && moel@1: ((TypeNode)Nodes[i]).SensorType < node.SensorType) moel@1: i++; moel@1: Nodes.Insert(i, node); moel@1: } moel@1: } else { moel@1: if (Nodes.Contains(node)) moel@1: Nodes.Remove(node); moel@1: } moel@1: } moel@1: moel@1: private void SensorRemoved(ISensor sensor) { moel@111: foreach (TypeNode typeNode in typeNodes) moel@111: if (typeNode.SensorType == sensor.SensorType) { moel@111: SensorNode sensorNode = null; moel@111: foreach (Node node in typeNode.Nodes) { moel@111: SensorNode n = node as SensorNode; moel@111: if (n != null && n.Sensor == sensor) moel@111: sensorNode = n; moel@111: } moel@327: if (sensorNode != null) { moel@327: sensorNode.PlotSelectionChanged -= SensorPlotSelectionChanged; moel@327: typeNode.Nodes.Remove(sensorNode); moel@327: UpdateNode(typeNode); moel@327: } moel@1: } moel@327: if (PlotSelectionChanged != null) moel@327: PlotSelectionChanged(this, null); moel@1: } moel@1: moel@1: private void InsertSorted(Node node, ISensor sensor) { moel@1: int i = 0; moel@1: while (i < node.Nodes.Count && moel@1: ((SensorNode)node.Nodes[i]).Sensor.Index < sensor.Index) moel@1: i++; moel@165: SensorNode sensorNode = new SensorNode(sensor, settings, unitManager); moel@327: sensorNode.PlotSelectionChanged += SensorPlotSelectionChanged; moel@111: node.Nodes.Insert(i, sensorNode); moel@1: } moel@1: moel@327: private void SensorPlotSelectionChanged(object sender, EventArgs e) { moel@327: if (PlotSelectionChanged != null) moel@327: PlotSelectionChanged(this, null); moel@327: } moel@327: moel@1: private void SensorAdded(ISensor sensor) { moel@111: foreach (TypeNode typeNode in typeNodes) moel@111: if (typeNode.SensorType == sensor.SensorType) { moel@111: InsertSorted(typeNode, sensor); moel@111: UpdateNode(typeNode); moel@1: } moel@327: if (PlotSelectionChanged != null) moel@327: PlotSelectionChanged(this, null); moel@327: } moel@327: moel@327: public event EventHandler PlotSelectionChanged; moel@1: } moel@1: }