~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/Libraries/SharpTreeView/ICSharpCode.TreeView/TreeFlattener.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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)
 
3
 
 
4
using System;
 
5
using System.Collections;
 
6
using System.Collections.Generic;
 
7
using System.Collections.Specialized;
 
8
using System.ComponentModel;
 
9
using System.Diagnostics;
 
10
using System.Linq;
 
11
using System.Text;
 
12
 
 
13
namespace ICSharpCode.TreeView
 
14
{
 
15
        sealed class TreeFlattener : IList, INotifyCollectionChanged
 
16
        {
 
17
                /// <summary>
 
18
                /// The root node of the flat list tree.
 
19
                /// Tjis is not necessarily the root of the model!
 
20
                /// </summary>
 
21
                internal SharpTreeNode root;
 
22
                readonly bool includeRoot;
 
23
                readonly object syncRoot = new object();
 
24
                
 
25
                public TreeFlattener(SharpTreeNode modelRoot, bool includeRoot)
 
26
                {
 
27
                        this.root = modelRoot;
 
28
                        while (root.listParent != null)
 
29
                                root = root.listParent;
 
30
                        root.treeFlattener = this;
 
31
                        this.includeRoot = includeRoot;
 
32
                }
 
33
 
 
34
                public event NotifyCollectionChangedEventHandler CollectionChanged;
 
35
                
 
36
                public void RaiseCollectionChanged(NotifyCollectionChangedEventArgs e)
 
37
                {
 
38
                        if (CollectionChanged != null)
 
39
                                CollectionChanged(this, e);
 
40
                }
 
41
                
 
42
                public void NodesInserted(int index, IEnumerable<SharpTreeNode> nodes)
 
43
                {
 
44
                        if (!includeRoot) index--;
 
45
                        foreach (SharpTreeNode node in nodes) {
 
46
                                RaiseCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, node, index++));
 
47
                        }
 
48
                }
 
49
                
 
50
                public void NodesRemoved(int index, IEnumerable<SharpTreeNode> nodes)
 
51
                {
 
52
                        if (!includeRoot) index--;
 
53
                        foreach (SharpTreeNode node in nodes) {
 
54
                                RaiseCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, node, index));
 
55
                        }
 
56
                }
 
57
                
 
58
                public void Stop()
 
59
                {
 
60
                        Debug.Assert(root.treeFlattener == this);
 
61
                        root.treeFlattener = null;
 
62
                }
 
63
                
 
64
                public object this[int index] {
 
65
                        get {
 
66
                                if (index < 0 || index >= this.Count)
 
67
                                        throw new ArgumentOutOfRangeException();
 
68
                                return SharpTreeNode.GetNodeByVisibleIndex(root, includeRoot ? index : index + 1);
 
69
                        }
 
70
                        set {
 
71
                                throw new NotSupportedException();
 
72
                        }
 
73
                }
 
74
                
 
75
                public int Count {
 
76
                        get {
 
77
                                return includeRoot ? root.GetTotalListLength() : root.GetTotalListLength() - 1;
 
78
                        }
 
79
                }
 
80
                
 
81
                public int IndexOf(object item)
 
82
                {
 
83
                        SharpTreeNode node = item as SharpTreeNode;
 
84
                        if (node != null && node.IsVisible && node.GetListRoot() == root) {
 
85
                                if (includeRoot)
 
86
                                        return SharpTreeNode.GetVisibleIndexForNode(node);
 
87
                                else
 
88
                                        return SharpTreeNode.GetVisibleIndexForNode(node) - 1;
 
89
                        } else {
 
90
                                return -1;
 
91
                        }
 
92
                }
 
93
                
 
94
                bool IList.IsReadOnly {
 
95
                        get { return true; }
 
96
                }
 
97
                
 
98
                bool IList.IsFixedSize {
 
99
                        get { return false; }
 
100
                }
 
101
                
 
102
                bool ICollection.IsSynchronized {
 
103
                        get { return false; }
 
104
                }
 
105
                
 
106
                object ICollection.SyncRoot {
 
107
                        get {
 
108
                                return syncRoot;
 
109
                        }
 
110
                }
 
111
                
 
112
                void IList.Insert(int index, object item)
 
113
                {
 
114
                        throw new NotSupportedException();
 
115
                }
 
116
                
 
117
                void IList.RemoveAt(int index)
 
118
                {
 
119
                        throw new NotSupportedException();
 
120
                }
 
121
                
 
122
                int IList.Add(object item)
 
123
                {
 
124
                        throw new NotSupportedException();
 
125
                }
 
126
                
 
127
                void IList.Clear()
 
128
                {
 
129
                        throw new NotSupportedException();
 
130
                }
 
131
                
 
132
                public bool Contains(object item)
 
133
                {
 
134
                        return IndexOf(item) >= 0;
 
135
                }
 
136
                
 
137
                public void CopyTo(Array array, int arrayIndex)
 
138
                {
 
139
                        foreach (object item in this)
 
140
                                array.SetValue(item, arrayIndex++);
 
141
                }
 
142
                
 
143
                void IList.Remove(object item)
 
144
                {
 
145
                        throw new NotSupportedException();
 
146
                }
 
147
                
 
148
                public IEnumerator GetEnumerator()
 
149
                {
 
150
                        for (int i = 0; i < this.Count; i++) {
 
151
                                yield return this[i];
 
152
                        }
 
153
                }
 
154
        }
 
155
}