moel@111: /* moel@111: moel@111: Version: MPL 1.1/GPL 2.0/LGPL 2.1 moel@111: moel@111: The contents of this file are subject to the Mozilla Public License Version moel@111: 1.1 (the "License"); you may not use this file except in compliance with moel@111: the License. You may obtain a copy of the License at moel@111: moel@111: http://www.mozilla.org/MPL/ moel@111: moel@111: Software distributed under the License is distributed on an "AS IS" basis, moel@111: WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License moel@111: for the specific language governing rights and limitations under the License. moel@111: moel@111: The Original Code is the Open Hardware Monitor code. moel@111: moel@111: The Initial Developer of the Original Code is moel@111: Michael Möller . moel@111: Portions created by the Initial Developer are Copyright (C) 2009-2010 moel@111: the Initial Developer. All Rights Reserved. moel@111: moel@111: Contributor(s): moel@111: moel@111: Alternatively, the contents of this file may be used under the terms of moel@111: either the GNU General Public License Version 2 or later (the "GPL"), or moel@111: the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), moel@111: in which case the provisions of the GPL or the LGPL are applicable instead moel@111: of those above. If you wish to allow use of your version of this file only moel@111: under the terms of either the GPL or the LGPL, and not to allow others to moel@111: use your version of this file under the terms of the MPL, indicate your moel@111: decision by deleting the provisions above and replace them with the notice moel@111: and other provisions required by the GPL or the LGPL. If you do not delete moel@111: the provisions above, a recipient may use your version of this file under moel@111: the terms of any one of the MPL, the GPL or the LGPL. moel@111: 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: }