~ubuntu-branches/ubuntu/karmic/mono-addins/karmic

« back to all changes in this revision

Viewing changes to Mono.Addins/Mono.Addins/TreeNode.cs

  • Committer: Bazaar Package Importer
  • Author(s): Mirco Bauer
  • Date: 2007-07-14 12:07:48 UTC
  • Revision ID: james.westby@ubuntu.com-20070714120748-2elczfsjlrdsrpms
Tags: upstream-0.2
ImportĀ upstreamĀ versionĀ 0.2

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
 
 
49
                public TreeNode (string id)
 
50
                {
 
51
                        this.id = id;
 
52
                                
 
53
                        // Root node
 
54
                        if (id.Length == 0)
 
55
                                childrenLoaded = true;
 
56
                }
 
57
                
 
58
                internal void AttachExtensionNode (ExtensionNode enode)
 
59
                {
 
60
                        this.extensionNode = enode;
 
61
                        if (extensionNode != null)
 
62
                                extensionNode.SetTreeNode (this);
 
63
                }
 
64
                
 
65
                public string Id {
 
66
                        get { return id; }
 
67
                }
 
68
                
 
69
                public ExtensionNode ExtensionNode {
 
70
                        get {
 
71
                                if (extensionNode == null && extensionPoint != null) {
 
72
                                        extensionNode = new ExtensionNode ();
 
73
                                        extensionNode.SetData (extensionPoint.RootAddin, null);
 
74
                                        AttachExtensionNode (extensionNode);
 
75
                                }
 
76
                                return extensionNode;
 
77
                        }
 
78
                }
 
79
                
 
80
                public ExtensionPoint ExtensionPoint {
 
81
                        get { return extensionPoint; }
 
82
                        set { extensionPoint = value; }
 
83
                }
 
84
                
 
85
                public ExtensionNodeSet ExtensionNodeSet {
 
86
                        get { return nodeTypes; }
 
87
                        set { nodeTypes = value; }
 
88
                }
 
89
                
 
90
                public TreeNode Parent {
 
91
                        get { return parent; }
 
92
                }
 
93
                
 
94
                public BaseCondition Condition {
 
95
                        get { return condition; }
 
96
                        set {
 
97
                                condition = value;
 
98
                        }
 
99
                }
 
100
                
 
101
                public virtual ExtensionContext Context {
 
102
                        get {
 
103
                                if (parent != null)
 
104
                                        return parent.Context;
 
105
                                else
 
106
                                        return null;
 
107
                        }
 
108
                }
 
109
                
 
110
                public bool IsEnabled {
 
111
                        get {
 
112
                                if (condition == null)
 
113
                                        return true;
 
114
                                ExtensionContext ctx = Context;
 
115
                                if (ctx == null)
 
116
                                        return true;
 
117
                                else
 
118
                                        return condition.Evaluate (ctx);
 
119
                        }
 
120
                }
 
121
                
 
122
                public bool ChildrenLoaded {
 
123
                        get { return childrenLoaded; }
 
124
                }
 
125
                
 
126
                public void AddChildNode (TreeNode node)
 
127
                {
 
128
                        node.parent = this;
 
129
                        if (childrenList == null)
 
130
                                childrenList = new ArrayList ();
 
131
                        childrenList.Add (node);
 
132
                }
 
133
                
 
134
                public void InsertChildNode (int n, TreeNode node)
 
135
                {
 
136
                        node.parent = this;
 
137
                        if (childrenList == null)
 
138
                                childrenList = new ArrayList ();
 
139
                        childrenList.Insert (n, node);
 
140
                        
 
141
                        // Dont call NotifyChildrenChanged here. It is called by ExtensionTree,
 
142
                        // after inserting all children of the node.
 
143
                }
 
144
                
 
145
                internal int ChildCount {
 
146
                        get { return childrenList == null ? 0 : childrenList.Count; }
 
147
                }
 
148
                
 
149
                public ExtensionNode GetExtensionNode (string path, string childId)
 
150
                {
 
151
                        TreeNode node = GetNode (path, childId);
 
152
                        return node != null ? node.ExtensionNode : null;
 
153
                }
 
154
                
 
155
                public ExtensionNode GetExtensionNode (string path)
 
156
                {
 
157
                        TreeNode node = GetNode (path);
 
158
                        return node != null ? node.ExtensionNode : null;
 
159
                }
 
160
                
 
161
                public TreeNode GetNode (string path, string childId)
 
162
                {
 
163
                        if (childId == null || childId.Length == 0)
 
164
                                return GetNode (path);
 
165
                        else
 
166
                                return GetNode (path + "/" + childId);
 
167
                }
 
168
                
 
169
                public TreeNode GetNode (string path)
 
170
                {
 
171
                        return GetNode (path, false);
 
172
                }
 
173
                
 
174
                public TreeNode GetNode (string path, bool buildPath)
 
175
                {
 
176
                        if (path.StartsWith ("/"))
 
177
                                path = path.Substring (1);
 
178
 
 
179
                        string[] parts = path.Split ('/');
 
180
                        TreeNode curNode = this;
 
181
 
 
182
                        foreach (string part in parts) {
 
183
                                int i = curNode.Children.IndexOfNode (part);
 
184
                                if (i != -1) {
 
185
                                        curNode = curNode.Children [i];
 
186
                                        continue;
 
187
                                }
 
188
                                
 
189
                                if (buildPath) {
 
190
                                        TreeNode newNode = new TreeNode (part);
 
191
                                        curNode.AddChildNode (newNode);
 
192
                                        curNode = newNode;
 
193
                                } else
 
194
                                        return null;
 
195
                        }
 
196
                        return curNode;
 
197
                }
 
198
                
 
199
                public TreeNodeCollection Children {
 
200
                        get {
 
201
                                if (!childrenLoaded) {
 
202
                                        childrenLoaded = true;
 
203
                                        if (extensionPoint != null)
 
204
                                                Context.LoadExtensions (GetPath ());
 
205
                                        // We have to keep the relation info, since add-ins may be loaded/unloaded
 
206
                                }
 
207
                                if (childrenList == null)
 
208
                                        return TreeNodeCollection.Empty;
 
209
                                if (children == null)
 
210
                                        children = new TreeNodeCollection (childrenList);
 
211
                                return children;
 
212
                        }
 
213
                }
 
214
                
 
215
                public string GetPath ()
 
216
                {
 
217
                        int num=0;
 
218
                        TreeNode node = this;
 
219
                        while (node != null) {
 
220
                                num++;
 
221
                                node = node.parent;
 
222
                        }
 
223
                        
 
224
                        string[] ids = new string [num];
 
225
                        
 
226
                        node = this;
 
227
                        while (node != null) {
 
228
                                ids [--num] = node.id;
 
229
                                node = node.parent;
 
230
                        }
 
231
                        return string.Join ("/", ids);
 
232
                }
 
233
                
 
234
                public void NotifyAddinLoaded (RuntimeAddin ad, bool recursive)
 
235
                {
 
236
                        if (extensionNode != null && extensionNode.AddinId == ad.Addin.Id)
 
237
                                extensionNode.OnAddinLoaded ();
 
238
                        if (recursive && childrenLoaded) {
 
239
                                foreach (TreeNode node in Children)
 
240
                                        node.NotifyAddinLoaded (ad, true);
 
241
                        }
 
242
                }
 
243
                
 
244
                public ExtensionPoint FindExtensionPoint (string path)
 
245
                {
 
246
                        if (path.StartsWith ("/"))
 
247
                                path = path.Substring (1);
 
248
 
 
249
                        string[] parts = path.Split ('/');
 
250
                        TreeNode curNode = this;
 
251
 
 
252
                        foreach (string part in parts) {
 
253
                                int i = curNode.Children.IndexOfNode (part);
 
254
                                if (i != -1) {
 
255
                                        curNode = curNode.Children [i];
 
256
                                        if (curNode.ExtensionPoint != null)
 
257
                                                return curNode.ExtensionPoint;
 
258
                                        continue;
 
259
                                }
 
260
                                return null;
 
261
                        }
 
262
                        return null;
 
263
                }
 
264
                
 
265
                public void FindAddinNodes (string id, ArrayList nodes)
 
266
                {
 
267
                        if (id != null && extensionPoint != null && extensionPoint.RootAddin == id) {
 
268
                                // It is an extension point created by the add-in. All nodes below this
 
269
                                // extension point will be added to the list, even if they come from other add-ins.
 
270
                                id = null;
 
271
                        }
 
272
 
 
273
                        if (childrenLoaded) {
 
274
                                // Deep-first search, to make sure children are removed before the parent.
 
275
                                foreach (TreeNode node in Children)
 
276
                                        node.FindAddinNodes (id, nodes);
 
277
                        }
 
278
                        
 
279
                        if (id == null || (ExtensionNode != null && ExtensionNode.AddinId == id))
 
280
                                nodes.Add (this);
 
281
                }
 
282
                
 
283
                public bool FindExtensionPathByType (IProgressStatus monitor, Type type, string nodeName, out string path, out string pathNodeName)
 
284
                {
 
285
                        if (extensionPoint != null) {
 
286
                                foreach (ExtensionNodeType nt in extensionPoint.NodeSet.NodeTypes) {
 
287
                                        if (nt.ObjectTypeName.Length > 0 && (nodeName.Length == 0 || nodeName == nt.Id)) {
 
288
                                                RuntimeAddin addin = AddinManager.SessionService.GetAddin (extensionPoint.RootAddin);
 
289
                                                Type ot = addin.GetType (nt.ObjectTypeName);
 
290
                                                if (ot != null) {
 
291
                                                        if (ot.IsAssignableFrom (type)) {
 
292
                                                                path = extensionPoint.Path;
 
293
                                                                pathNodeName = nt.Id;
 
294
                                                                return true;
 
295
                                                        }
 
296
                                                }
 
297
                                                else
 
298
                                                        monitor.ReportError ("Type '" + nt.ObjectTypeName + "' not found in add-in '" + Id + "'", null);
 
299
                                        }
 
300
                                }
 
301
                        }
 
302
                        else {
 
303
                                foreach (TreeNode node in Children) {
 
304
                                        if (node.FindExtensionPathByType (monitor, type, nodeName, out path, out pathNodeName))
 
305
                                                return true;
 
306
                                }
 
307
                        }
 
308
                        path = null;
 
309
                        pathNodeName = null;
 
310
                        return false;
 
311
                }
 
312
                
 
313
                public void Remove ()
 
314
                {
 
315
                        if (parent != null) {
 
316
                                if (Condition != null)
 
317
                                        Context.UnregisterNodeCondition (this, Condition);
 
318
                                parent.childrenList.Remove (this);
 
319
                                parent.NotifyChildrenChanged ();
 
320
                        }
 
321
                }
 
322
                
 
323
                public bool NotifyChildrenChanged ()
 
324
                {
 
325
                        if (extensionNode != null)
 
326
                                return extensionNode.NotifyChildChanged ();
 
327
                        else
 
328
                                return false;
 
329
                }
 
330
        }
 
331
}