moel@111: /* moel@111: 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@111: moel@344: Copyright (C) 2009-2010 Michael Möller moel@344: moel@111: */ moel@111: moel@111: using System; moel@111: using System.Collections; moel@111: using System.Collections.Generic; moel@111: using System.Collections.ObjectModel; moel@111: using Aga.Controls.Tree; moel@111: moel@111: namespace OpenHardwareMonitor.GUI { moel@111: public class TreeModel : ITreeModel { moel@111: moel@111: private Node root; moel@111: private bool forceVisible = false; moel@111: moel@111: public TreeModel() { moel@111: root = new Node(); moel@111: root.Model = this; moel@111: } moel@111: moel@111: public TreePath GetPath(Node node) { moel@111: if (node == root) moel@111: return TreePath.Empty; moel@111: else { moel@111: Stack stack = new Stack(); moel@111: while (node != root) { moel@111: stack.Push(node); moel@111: node = node.Parent; moel@111: } moel@111: return new TreePath(stack.ToArray()); moel@111: } moel@111: } moel@111: moel@111: public Collection Nodes { moel@111: get { return root.Nodes; } moel@111: } moel@111: moel@111: private Node GetNode(TreePath treePath) { moel@111: Node parent = root; moel@111: foreach (object obj in treePath.FullPath) { moel@111: Node node = obj as Node; moel@111: if (node == null || node.Parent != parent) moel@111: return null; moel@111: parent = node; moel@111: } moel@111: return parent; moel@111: } moel@111: moel@111: public IEnumerable GetChildren(TreePath treePath) { moel@111: Node node = GetNode(treePath); moel@111: if (node != null) { moel@111: foreach (Node n in node.Nodes) moel@111: if (forceVisible || n.IsVisible) moel@111: yield return n; moel@111: } else { moel@111: yield break; moel@111: } moel@111: } moel@111: moel@111: public bool IsLeaf(TreePath treePath) { moel@111: return false; moel@111: } moel@111: moel@111: public bool ForceVisible { moel@111: get { moel@111: return forceVisible; moel@111: } moel@111: set { moel@111: if (value != forceVisible) { moel@111: forceVisible = value; moel@111: OnStructureChanged(root); moel@111: } moel@111: } moel@111: } moel@111: moel@111: #pragma warning disable 67 moel@111: public event EventHandler NodesChanged; moel@111: public event EventHandler StructureChanged; moel@111: public event EventHandler NodesInserted; moel@111: public event EventHandler NodesRemoved; moel@111: #pragma warning restore 67 moel@111: moel@111: public void OnNodeChanged(Node parent, int index, Node node) { moel@111: if (NodesChanged != null && parent != null) { moel@111: TreePath path = GetPath(parent); moel@111: if (path != null) moel@111: NodesChanged(this, new TreeModelEventArgs( moel@111: path, new int[] { index }, new object[] { node })); moel@111: } moel@111: } moel@111: moel@111: public void OnStructureChanged(Node node) { moel@111: if (StructureChanged != null) moel@111: StructureChanged(this, moel@111: new TreeModelEventArgs(GetPath(node), new object[0])); moel@111: } moel@111: moel@111: public void OnNodeInserted(Node parent, int index, Node node) { moel@111: if (NodesInserted != null) { moel@111: TreeModelEventArgs args = new TreeModelEventArgs(GetPath(parent), moel@111: new int[] { index }, new object[] { node }); moel@111: NodesInserted(this, args); moel@111: } moel@111: moel@111: } moel@111: moel@111: public void OnNodeRemoved(Node parent, int index, Node node) { moel@111: if (NodesRemoved != null) { moel@111: TreeModelEventArgs args = new TreeModelEventArgs(GetPath(parent), moel@111: new int[] { index }, new object[] { node }); moel@111: NodesRemoved(this, args); moel@111: } moel@111: } moel@111: moel@111: } moel@111: }