1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
5
using System.Collections;
6
using System.Collections.Generic;
7
using System.Collections.Specialized;
8
using System.ComponentModel;
9
using System.Diagnostics;
13
namespace ICSharpCode.TreeView
15
sealed class TreeFlattener : IList, INotifyCollectionChanged
18
/// The root node of the flat list tree.
19
/// Tjis is not necessarily the root of the model!
21
internal SharpTreeNode root;
22
readonly bool includeRoot;
23
readonly object syncRoot = new object();
25
public TreeFlattener(SharpTreeNode modelRoot, bool includeRoot)
27
this.root = modelRoot;
28
while (root.listParent != null)
29
root = root.listParent;
30
root.treeFlattener = this;
31
this.includeRoot = includeRoot;
34
public event NotifyCollectionChangedEventHandler CollectionChanged;
36
public void RaiseCollectionChanged(NotifyCollectionChangedEventArgs e)
38
if (CollectionChanged != null)
39
CollectionChanged(this, e);
42
public void NodesInserted(int index, IEnumerable<SharpTreeNode> nodes)
44
if (!includeRoot) index--;
45
foreach (SharpTreeNode node in nodes) {
46
RaiseCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, node, index++));
50
public void NodesRemoved(int index, IEnumerable<SharpTreeNode> nodes)
52
if (!includeRoot) index--;
53
foreach (SharpTreeNode node in nodes) {
54
RaiseCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, node, index));
60
Debug.Assert(root.treeFlattener == this);
61
root.treeFlattener = null;
64
public object this[int index] {
66
if (index < 0 || index >= this.Count)
67
throw new ArgumentOutOfRangeException();
68
return SharpTreeNode.GetNodeByVisibleIndex(root, includeRoot ? index : index + 1);
71
throw new NotSupportedException();
77
return includeRoot ? root.GetTotalListLength() : root.GetTotalListLength() - 1;
81
public int IndexOf(object item)
83
SharpTreeNode node = item as SharpTreeNode;
84
if (node != null && node.IsVisible && node.GetListRoot() == root) {
86
return SharpTreeNode.GetVisibleIndexForNode(node);
88
return SharpTreeNode.GetVisibleIndexForNode(node) - 1;
94
bool IList.IsReadOnly {
98
bool IList.IsFixedSize {
102
bool ICollection.IsSynchronized {
103
get { return false; }
106
object ICollection.SyncRoot {
112
void IList.Insert(int index, object item)
114
throw new NotSupportedException();
117
void IList.RemoveAt(int index)
119
throw new NotSupportedException();
122
int IList.Add(object item)
124
throw new NotSupportedException();
129
throw new NotSupportedException();
132
public bool Contains(object item)
134
return IndexOf(item) >= 0;
137
public void CopyTo(Array array, int arrayIndex)
139
foreach (object item in this)
140
array.SetValue(item, arrayIndex++);
143
void IList.Remove(object item)
145
throw new NotSupportedException();
148
public IEnumerator GetEnumerator()
150
for (int i = 0; i < this.Count; i++) {
151
yield return this[i];