GUI/TreeModel.cs
author moel.mich
Mon, 11 Oct 2010 20:37:37 +0000
changeset 220 e51749b206ee
child 344 3145aadca3d2
permissions -rw-r--r--
Added a mainboard specific configuration for the ASRock AOD790GX-128M
moel@111
     1
/*
moel@111
     2
  
moel@111
     3
  Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@111
     4
moel@111
     5
  The contents of this file are subject to the Mozilla Public License Version
moel@111
     6
  1.1 (the "License"); you may not use this file except in compliance with
moel@111
     7
  the License. You may obtain a copy of the License at
moel@111
     8
 
moel@111
     9
  http://www.mozilla.org/MPL/
moel@111
    10
moel@111
    11
  Software distributed under the License is distributed on an "AS IS" basis,
moel@111
    12
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@111
    13
  for the specific language governing rights and limitations under the License.
moel@111
    14
moel@111
    15
  The Original Code is the Open Hardware Monitor code.
moel@111
    16
moel@111
    17
  The Initial Developer of the Original Code is 
moel@111
    18
  Michael Möller <m.moeller@gmx.ch>.
moel@111
    19
  Portions created by the Initial Developer are Copyright (C) 2009-2010
moel@111
    20
  the Initial Developer. All Rights Reserved.
moel@111
    21
moel@111
    22
  Contributor(s):
moel@111
    23
moel@111
    24
  Alternatively, the contents of this file may be used under the terms of
moel@111
    25
  either the GNU General Public License Version 2 or later (the "GPL"), or
moel@111
    26
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@111
    27
  in which case the provisions of the GPL or the LGPL are applicable instead
moel@111
    28
  of those above. If you wish to allow use of your version of this file only
moel@111
    29
  under the terms of either the GPL or the LGPL, and not to allow others to
moel@111
    30
  use your version of this file under the terms of the MPL, indicate your
moel@111
    31
  decision by deleting the provisions above and replace them with the notice
moel@111
    32
  and other provisions required by the GPL or the LGPL. If you do not delete
moel@111
    33
  the provisions above, a recipient may use your version of this file under
moel@111
    34
  the terms of any one of the MPL, the GPL or the LGPL.
moel@111
    35
 
moel@111
    36
*/
moel@111
    37
moel@111
    38
using System;
moel@111
    39
using System.Collections;
moel@111
    40
using System.Collections.Generic;
moel@111
    41
using System.Collections.ObjectModel;
moel@111
    42
using Aga.Controls.Tree;
moel@111
    43
moel@111
    44
namespace OpenHardwareMonitor.GUI {
moel@111
    45
  public class TreeModel : ITreeModel {
moel@111
    46
moel@111
    47
    private Node root;
moel@111
    48
    private bool forceVisible = false;
moel@111
    49
moel@111
    50
    public TreeModel() {
moel@111
    51
      root = new Node();
moel@111
    52
      root.Model = this;
moel@111
    53
    }
moel@111
    54
moel@111
    55
    public TreePath GetPath(Node node) {
moel@111
    56
      if (node == root)
moel@111
    57
        return TreePath.Empty;
moel@111
    58
      else {
moel@111
    59
        Stack<object> stack = new Stack<object>();
moel@111
    60
        while (node != root) {
moel@111
    61
          stack.Push(node);
moel@111
    62
          node = node.Parent;
moel@111
    63
        }
moel@111
    64
        return new TreePath(stack.ToArray());
moel@111
    65
      }
moel@111
    66
    }
moel@111
    67
moel@111
    68
    public Collection<Node> Nodes {
moel@111
    69
      get { return root.Nodes; }
moel@111
    70
    }
moel@111
    71
moel@111
    72
    private Node GetNode(TreePath treePath) {
moel@111
    73
      Node parent = root;
moel@111
    74
      foreach (object obj in treePath.FullPath) {
moel@111
    75
        Node node = obj as Node;
moel@111
    76
        if (node == null || node.Parent != parent)
moel@111
    77
          return null;
moel@111
    78
        parent = node;
moel@111
    79
      }
moel@111
    80
      return parent;
moel@111
    81
    }
moel@111
    82
moel@111
    83
    public IEnumerable GetChildren(TreePath treePath) {
moel@111
    84
      Node node = GetNode(treePath);
moel@111
    85
      if (node != null) {
moel@111
    86
        foreach (Node n in node.Nodes)
moel@111
    87
          if (forceVisible || n.IsVisible)
moel@111
    88
            yield return n;
moel@111
    89
      } else {
moel@111
    90
        yield break;
moel@111
    91
      }
moel@111
    92
    }
moel@111
    93
moel@111
    94
    public bool IsLeaf(TreePath treePath) {
moel@111
    95
      return false;
moel@111
    96
    }
moel@111
    97
moel@111
    98
    public bool ForceVisible {
moel@111
    99
      get {
moel@111
   100
        return forceVisible;
moel@111
   101
      }
moel@111
   102
      set {
moel@111
   103
        if (value != forceVisible) {
moel@111
   104
          forceVisible = value;
moel@111
   105
          OnStructureChanged(root);
moel@111
   106
        }
moel@111
   107
      }
moel@111
   108
    }
moel@111
   109
moel@111
   110
    #pragma warning disable 67
moel@111
   111
    public event EventHandler<TreeModelEventArgs> NodesChanged;
moel@111
   112
    public event EventHandler<TreePathEventArgs> StructureChanged;
moel@111
   113
    public event EventHandler<TreeModelEventArgs> NodesInserted;
moel@111
   114
    public event EventHandler<TreeModelEventArgs> NodesRemoved;
moel@111
   115
    #pragma warning restore 67
moel@111
   116
moel@111
   117
    public void OnNodeChanged(Node parent, int index, Node node) {
moel@111
   118
      if (NodesChanged != null && parent != null) {
moel@111
   119
        TreePath path = GetPath(parent);
moel@111
   120
        if (path != null) 
moel@111
   121
          NodesChanged(this, new TreeModelEventArgs(
moel@111
   122
            path, new int[] { index }, new object[] { node }));
moel@111
   123
      }
moel@111
   124
    }
moel@111
   125
moel@111
   126
    public void OnStructureChanged(Node node) {
moel@111
   127
      if (StructureChanged != null)
moel@111
   128
        StructureChanged(this,
moel@111
   129
          new TreeModelEventArgs(GetPath(node), new object[0]));
moel@111
   130
    }
moel@111
   131
moel@111
   132
    public void OnNodeInserted(Node parent, int index, Node node) {
moel@111
   133
      if (NodesInserted != null) {
moel@111
   134
        TreeModelEventArgs args = new TreeModelEventArgs(GetPath(parent),
moel@111
   135
          new int[] { index }, new object[] { node });
moel@111
   136
        NodesInserted(this, args);
moel@111
   137
      }
moel@111
   138
moel@111
   139
    }
moel@111
   140
moel@111
   141
    public void OnNodeRemoved(Node parent, int index, Node node) {
moel@111
   142
      if (NodesRemoved != null) {
moel@111
   143
        TreeModelEventArgs args = new TreeModelEventArgs(GetPath(parent), 
moel@111
   144
          new int[] { index }, new object[] { node });
moel@111
   145
        NodesRemoved(this, args);
moel@111
   146
      }
moel@111
   147
    }
moel@111
   148
moel@111
   149
  }
moel@111
   150
}