~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/mono-addins/Mono.Addins/Mono.Addins/TreeNode.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// TreeNode.cs
 
3
//
 
4
// Author:
 
5
//   Lluis Sanchez Gual
 
6
//
 
7
// Copyright (C) 2007 Novell, Inc (http://www.novell.com)
 
8
//
 
9
// Permission is hereby granted, free of charge, to any person obtaining
 
10
// a copy of this software and associated documentation files (the
 
11
// "Software"), to deal in the Software without restriction, including
 
12
// without limitation the rights to use, copy, modify, merge, publish,
 
13
// distribute, sublicense, and/or sell copies of the Software, and to
 
14
// permit persons to whom the Software is furnished to do so, subject to
 
15
// the following conditions:
 
16
// 
 
17
// The above copyright notice and this permission notice shall be
 
18
// included in all copies or substantial portions of the Software.
 
19
// 
 
20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
21
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
22
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
23
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 
24
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 
25
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 
26
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
27
//
 
28
 
 
29
 
 
30
using System;
 
31
using System.Text;
 
32
using System.Collections;
 
33
using Mono.Addins.Description;
 
34
 
 
35
namespace Mono.Addins
 
36
{
 
37
        class TreeNode
 
38
        {
 
39
                ArrayList childrenList;
 
40
                TreeNodeCollection children;
 
41
                ExtensionNode extensionNode;
 
42
                bool childrenLoaded;
 
43
                string id;
 
44
                TreeNode parent;
 
45
                ExtensionNodeSet nodeTypes;
 
46
                ExtensionPoint extensionPoint;
 
47
                BaseCondition condition;
 
48
                protected AddinEngine addinEngine;
 
49
 
 
50
                public TreeNode (AddinEngine addinEngine, string id)
 
51
                {
 
52
                        this.id = id;
 
53
                        this.addinEngine = addinEngine;
 
54
                                
 
55
                        // Root node
 
56
                        if (id.Length == 0)
 
57
                                childrenLoaded = true;
 
58
                }
 
59
                
 
60
                public AddinEngine AddinEngine {
 
61
                        get { return addinEngine; }
 
62
                }
 
63
                
 
64
                internal void AttachExtensionNode (ExtensionNode enode)
 
65
                {
 
66
                        this.extensionNode = enode;
 
67
                        if (extensionNode != null)
 
68
                                extensionNode.SetTreeNode (this);
 
69
                }
 
70
                
 
71
                public string Id {
 
72
                        get { return id; }
 
73
                }
 
74
                
 
75
                public ExtensionNode ExtensionNode {
 
76
                        get {
 
77
                                if (extensionNode == null && extensionPoint != null) {
 
78
                                        extensionNode = new ExtensionNode ();
 
79
                                        extensionNode.SetData (addinEngine, extensionPoint.RootAddin, null, null);
 
80
                                        AttachExtensionNode (extensionNode);
 
81
                                }
 
82
                                return extensionNode;
 
83
                        }
 
84
                }
 
85
                
 
86
                public ExtensionPoint ExtensionPoint {
 
87
                        get { return extensionPoint; }
 
88
                        set { extensionPoint = value; }
 
89
                }
 
90
                
 
91
                public ExtensionNodeSet ExtensionNodeSet {
 
92
                        get { return nodeTypes; }
 
93
                        set { nodeTypes = value; }
 
94
                }
 
95
                
 
96
                public TreeNode Parent {
 
97
                        get { return parent; }
 
98
                }
 
99
                
 
100
                public BaseCondition Condition {
 
101
                        get { return condition; }
 
102
                        set {
 
103
                                condition = value;
 
104
                        }
 
105
                }
 
106
                
 
107
                public virtual ExtensionContext Context {
 
108
                        get {
 
109
                                if (parent != null)
 
110
                                        return parent.Context;
 
111
                                else
 
112
                                        return null;
 
113
                        }
 
114
                }
 
115
                
 
116
                public bool IsEnabled {
 
117
                        get {
 
118
                                if (condition == null)
 
119
                                        return true;
 
120
                                ExtensionContext ctx = Context;
 
121
                                if (ctx == null)
 
122
                                        return true;
 
123
                                else
 
124
                                        return condition.Evaluate (ctx);
 
125
                        }
 
126
                }
 
127
                
 
128
                public bool ChildrenLoaded {
 
129
                        get { return childrenLoaded; }
 
130
                }
 
131
                
 
132
                public void AddChildNode (TreeNode node)
 
133
                {
 
134
                        node.parent = this;
 
135
                        if (childrenList == null)
 
136
                                childrenList = new ArrayList ();
 
137
                        childrenList.Add (node);
 
138
                }
 
139
                
 
140
                public void InsertChildNode (int n, TreeNode node)
 
141
                {
 
142
                        node.parent = this;
 
143
                        if (childrenList == null)
 
144
                                childrenList = new ArrayList ();
 
145
                        childrenList.Insert (n, node);
 
146
                        
 
147
                        // Dont call NotifyChildrenChanged here. It is called by ExtensionTree,
 
148
                        // after inserting all children of the node.
 
149
                }
 
150
                
 
151
                internal int ChildCount {
 
152
                        get { return childrenList == null ? 0 : childrenList.Count; }
 
153
                }
 
154
                
 
155
                public ExtensionNode GetExtensionNode (string path, string childId)
 
156
                {
 
157
                        TreeNode node = GetNode (path, childId);
 
158
                        return node != null ? node.ExtensionNode : null;
 
159
                }
 
160
                
 
161
                public ExtensionNode GetExtensionNode (string path)
 
162
                {
 
163
                        TreeNode node = GetNode (path);
 
164
                        return node != null ? node.ExtensionNode : null;
 
165
                }
 
166
                
 
167
                public TreeNode GetNode (string path, string childId)
 
168
                {
 
169
                        if (childId == null || childId.Length == 0)
 
170
                                return GetNode (path);
 
171
                        else
 
172
                                return GetNode (path + "/" + childId);
 
173
                }
 
174
                
 
175
                public TreeNode GetNode (string path)
 
176
                {
 
177
                        return GetNode (path, false);
 
178
                }
 
179
                
 
180
                public TreeNode GetNode (string path, bool buildPath)
 
181
                {
 
182
                        if (path.StartsWith ("/"))
 
183
                                path = path.Substring (1);
 
184
 
 
185
                        string[] parts = path.Split ('/');
 
186
                        TreeNode curNode = this;
 
187
 
 
188
                        foreach (string part in parts) {
 
189
                                int i = curNode.Children.IndexOfNode (part);
 
190
                                if (i != -1) {
 
191
                                        curNode = curNode.Children [i];
 
192
                                        continue;
 
193
                                }
 
194
                                
 
195
                                if (buildPath) {
 
196
                                        TreeNode newNode = new TreeNode (addinEngine, part);
 
197
                                        curNode.AddChildNode (newNode);
 
198
                                        curNode = newNode;
 
199
                                } else
 
200
                                        return null;
 
201
                        }
 
202
                        return curNode;
 
203
                }
 
204
                
 
205
                public TreeNodeCollection Children {
 
206
                        get {
 
207
                                if (!childrenLoaded) {
 
208
                                        childrenLoaded = true;
 
209
                                        if (extensionPoint != null)
 
210
                                                Context.LoadExtensions (GetPath ());
 
211
                                        // We have to keep the relation info, since add-ins may be loaded/unloaded
 
212
                                }
 
213
                                if (childrenList == null)
 
214
                                        return TreeNodeCollection.Empty;
 
215
                                if (children == null)
 
216
                                        children = new TreeNodeCollection (childrenList);
 
217
                                return children;
 
218
                        }
 
219
                }
 
220
                
 
221
                public string GetPath ()
 
222
                {
 
223
                        int num=0;
 
224
                        TreeNode node = this;
 
225
                        while (node != null) {
 
226
                                num++;
 
227
                                node = node.parent;
 
228
                        }
 
229
                        
 
230
                        string[] ids = new string [num];
 
231
                        
 
232
                        node = this;
 
233
                        while (node != null) {
 
234
                                ids [--num] = node.id;
 
235
                                node = node.parent;
 
236
                        }
 
237
                        return string.Join ("/", ids);
 
238
                }
 
239
                
 
240
                public void NotifyAddinLoaded (RuntimeAddin ad, bool recursive)
 
241
                {
 
242
                        if (extensionNode != null && extensionNode.AddinId == ad.Addin.Id)
 
243
                                extensionNode.OnAddinLoaded ();
 
244
                        if (recursive && childrenLoaded) {
 
245
                                foreach (TreeNode node in Children.Clone ())
 
246
                                        node.NotifyAddinLoaded (ad, true);
 
247
                        }
 
248
                }
 
249
                
 
250
                public ExtensionPoint FindLoadedExtensionPoint (string path)
 
251
                {
 
252
                        if (path.StartsWith ("/"))
 
253
                                path = path.Substring (1);
 
254
 
 
255
                        string[] parts = path.Split ('/');
 
256
                        TreeNode curNode = this;
 
257
 
 
258
                        foreach (string part in parts) {
 
259
                                int i = curNode.Children.IndexOfNode (part);
 
260
                                if (i != -1) {
 
261
                                        curNode = curNode.Children [i];
 
262
                                        if (!curNode.ChildrenLoaded)
 
263
                                                return null;
 
264
                                        if (curNode.ExtensionPoint != null)
 
265
                                                return curNode.ExtensionPoint;
 
266
                                        continue;
 
267
                                }
 
268
                                return null;
 
269
                        }
 
270
                        return null;
 
271
                }
 
272
                
 
273
                public void FindAddinNodes (string id, ArrayList nodes)
 
274
                {
 
275
                        if (id != null && extensionPoint != null && extensionPoint.RootAddin == id) {
 
276
                                // It is an extension point created by the add-in. All nodes below this
 
277
                                // extension point will be added to the list, even if they come from other add-ins.
 
278
                                id = null;
 
279
                        }
 
280
 
 
281
                        if (childrenLoaded) {
 
282
                                // Deep-first search, to make sure children are removed before the parent.
 
283
                                foreach (TreeNode node in Children)
 
284
                                        node.FindAddinNodes (id, nodes);
 
285
                        }
 
286
                        
 
287
                        if (id == null || (ExtensionNode != null && ExtensionNode.AddinId == id))
 
288
                                nodes.Add (this);
 
289
                }
 
290
                
 
291
                public bool FindExtensionPathByType (IProgressStatus monitor, Type type, string nodeName, out string path, out string pathNodeName)
 
292
                {
 
293
                        if (extensionPoint != null) {
 
294
                                foreach (ExtensionNodeType nt in extensionPoint.NodeSet.NodeTypes) {
 
295
                                        if (nt.ObjectTypeName.Length > 0 && (nodeName.Length == 0 || nodeName == nt.Id)) {
 
296
                                                RuntimeAddin addin = addinEngine.GetAddin (extensionPoint.RootAddin);
 
297
                                                Type ot = addin.GetType (nt.ObjectTypeName);
 
298
                                                if (ot != null) {
 
299
                                                        if (ot.IsAssignableFrom (type)) {
 
300
                                                                path = extensionPoint.Path;
 
301
                                                                pathNodeName = nt.Id;
 
302
                                                                return true;
 
303
                                                        }
 
304
                                                }
 
305
                                                else
 
306
                                                        monitor.ReportError ("Type '" + nt.ObjectTypeName + "' not found in add-in '" + Id + "'", null);
 
307
                                        }
 
308
                                }
 
309
                        }
 
310
                        else {
 
311
                                foreach (TreeNode node in Children) {
 
312
                                        if (node.FindExtensionPathByType (monitor, type, nodeName, out path, out pathNodeName))
 
313
                                                return true;
 
314
                                }
 
315
                        }
 
316
                        path = null;
 
317
                        pathNodeName = null;
 
318
                        return false;
 
319
                }
 
320
                
 
321
                public void Remove ()
 
322
                {
 
323
                        if (parent != null) {
 
324
                                if (Condition != null)
 
325
                                        Context.UnregisterNodeCondition (this, Condition);
 
326
                                parent.childrenList.Remove (this);
 
327
                                parent.NotifyChildrenChanged ();
 
328
                        }
 
329
                }
 
330
                
 
331
                public bool NotifyChildrenChanged ()
 
332
                {
 
333
                        if (extensionNode != null)
 
334
                                return extensionNode.NotifyChildChanged ();
 
335
                        else
 
336
                                return false;
 
337
                }
 
338
 
 
339
                public void ResetCachedData ()
 
340
                {
 
341
                        if (extensionPoint != null) {
 
342
                                string aid = Addin.GetIdName (extensionPoint.ParentAddinDescription.AddinId);
 
343
                                RuntimeAddin ad = addinEngine.GetAddin (aid);
 
344
                                if (ad != null)
 
345
                                        extensionPoint = ad.Addin.Description.ExtensionPoints [GetPath ()];
 
346
                        }
 
347
                        if (childrenList != null) {
 
348
                                foreach (TreeNode cn in childrenList)
 
349
                                        cn.ResetCachedData ();
 
350
                        }
 
351
                }
 
352
        }
 
353
}