Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TreeNode.cs « Mono.Addins « Mono.Addins - github.com/mono/mono-addins.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1c60176d378eee2c838611957a2f999d9ed52d43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
//
// TreeNode.cs
//
// Author:
//   Lluis Sanchez Gual
//
// Copyright (C) 2007 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//


using System;
using System.Text;
using System.Collections;
using Mono.Addins.Description;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Threading;

namespace Mono.Addins
{
	class TreeNode
	{
		ImmutableArray<TreeNode> children;

		ImmutableArray<TreeNode>.Builder childrenBuilder;
		ExtensionContextTransaction buildTransaction;

		ExtensionNode extensionNode;
		bool childrenFromExtensionsLoaded;
		string id;
		TreeNode parent;
		ExtensionNodeSet nodeTypes;
		ExtensionPoint extensionPoint;
		BaseCondition condition;
		protected AddinEngine addinEngine;

		public TreeNode (AddinEngine addinEngine, string id)
		{
			this.id = id;
			this.addinEngine = addinEngine;

			children = ImmutableArray<TreeNode>.Empty;

			// Root node
			if (id.Length == 0)
				childrenFromExtensionsLoaded = true;
		}
		
		public AddinEngine AddinEngine {
			get { return addinEngine; }
		}
		
		internal void AttachExtensionNode (ExtensionNode enode)
		{
			if (Interlocked.CompareExchange (ref extensionNode, enode, null) != null) {
				// Another thread already assigned the node
				return;
			}

			if (extensionNode != null)
				extensionNode.SetTreeNode (this);
		}
		
		public string Id {
			get { return id; }
		}
		
		public ExtensionNode ExtensionNode {
			get {
				if (extensionNode == null && extensionPoint != null) {
					var newNode = new ExtensionNode ();
					newNode.SetData (addinEngine, extensionPoint.RootAddin, null, null);
					AttachExtensionNode (newNode);
				}
				return extensionNode;
			}
		}

		public void NotifyAddinUnloaded ()
		{
			extensionNode?.OnAddinUnloaded ();
		}

		public void NotifyAddinLoaded ()
		{
			extensionNode?.OnAddinLoaded ();
		}

		public bool HasExtensionNode {
			get {
				return extensionNode != null || extensionPoint != null;
			}
		}

		public string AddinId {
			get {
				return extensionNode != null ? extensionNode.AddinId : extensionPoint?.RootAddin;
			}
		}

		public ExtensionPoint ExtensionPoint {
			get { return extensionPoint; }
			set { extensionPoint = value; }
		}
		
		public ExtensionNodeSet ExtensionNodeSet {
			get { return nodeTypes; }
			set { nodeTypes = value; }
		}
		
		public TreeNode Parent {
			get { return parent; }
		}
		
		public BaseCondition Condition {
			get { return condition; }
			set {
				condition = value;
			}
		}
		
		public virtual ExtensionContext Context {
			get {
				if (parent != null)
					return parent.Context;
				else
					return null;
			}
		}
		
		public bool IsEnabled {
			get {
				if (condition == null)
					return true;
				ExtensionContext ctx = Context;
				if (ctx == null)
					return true;
				else
					return condition.Evaluate (ctx);
			}
		}
		
		public bool ChildrenFromExtensionsLoaded {
			get { return childrenFromExtensionsLoaded; }
		}
		
		public void AddChildNode (ExtensionContextTransaction transaction, TreeNode node)
		{
			node.SetParent(transaction, this);

			if (childrenBuilder != null)
				childrenBuilder.Add (node);
			else
				children = children.Add (node);

			transaction.ReportChildrenChanged (this);
		}

		public void InsertChild (ExtensionContextTransaction transaction, int n, TreeNode node)
		{
			node.SetParent (transaction, this);

			if (childrenBuilder != null)
				childrenBuilder.Insert (n, node);
			else
				children = children.Insert (n, node);

			transaction.ReportChildrenChanged (this);
		}

		public void RemoveChild (ExtensionContextTransaction transaction, TreeNode node)
		{
			node.SetParent (transaction, null);

			if (childrenBuilder != null)
				childrenBuilder.Remove (node);
			else
				children = children.Remove (node);

			transaction.ReportChildrenChanged (this);
		}

		void SetParent (ExtensionContextTransaction transaction, TreeNode newParent)
		{
			if (parent == newParent)
				return;

			if (this.parent != null && newParent != null)
				throw new InvalidOperationException ("Node already has a parent");

			var currentCtx = Context;
			if (currentCtx != null && currentCtx != transaction.Context)
				throw new InvalidOperationException ("Invalid context");

			this.parent = newParent;

			if (newParent != null) {
				if (newParent.Context != null)
					OnAttachedToContext (transaction);
			} else {
				if (currentCtx != null)
					OnDetachedFromContext (transaction);
			}
		}

		void OnAttachedToContext (ExtensionContextTransaction transaction)
		{
			// Once the node is part of a context, let's register the condition
			if (Condition != null)
				transaction.RegisterNodeCondition (this, Condition);

			// Propagate event to children
			foreach (var child in GetLoadedChildren ())
				child.OnAttachedToContext (transaction);
		}

		void OnDetachedFromContext (ExtensionContextTransaction transaction)
		{
			// Node being removed, unregister from context
			if (Condition != null)
				transaction.UnregisterNodeCondition (this, Condition);

			// Propagate event to children
			foreach (var child in GetLoadedChildren ())
				child.OnDetachedFromContext (transaction);
		}

		public ExtensionNode GetExtensionNode (string path, string childId)
		{
			TreeNode node = GetNode (path, childId);
			return node != null ? node.ExtensionNode : null;
		}
		
		public ExtensionNode GetExtensionNode (string path)
		{
			TreeNode node = GetNode (path);
			return node != null ? node.ExtensionNode : null;
		}
		
		public TreeNode GetNode (string path, string childId)
		{
			if (childId == null || childId.Length == 0)
				return GetNode (path);
			else
				return GetNode (path + "/" + childId);
		}
		
		public TreeNode GetNode (string path)
		{
			return GetNode (path, false);
		}
		
		public TreeNode GetNode (string path, bool buildPath)
		{
			if (path.StartsWith ("/"))
				path = path.Substring (1);

			string[] parts = path.Split ('/');
			TreeNode curNode = this;

			foreach (string part in parts) {
				var node = curNode.GetChildNode (part);
				if (node != null) {
					curNode = node;
					continue;
				}
				
				if (buildPath) {
					var transaction = BeginContextTransaction (out var dispose);
					try {
						// Check again inside the lock, just in case
						node = curNode.GetChildNode (part);
						if (node != null) {
							curNode = node;
							continue;
						}

						TreeNode newNode = new TreeNode (addinEngine, part);
						curNode.AddChildNode (transaction, newNode);
						curNode = newNode;
					} finally {
						if (dispose)
							transaction.Dispose ();
					}
				} else
					return null;
			}
			return curNode;
		}

		public TreeNode GetChildNode (string id)
		{
			var childrenList = Children;
			foreach (var node in childrenList) {
				if (node.Id == id)
					return node;
			}
			return null;
		}

		public int IndexOfChild (string id)
		{
			var childrenList = Children;
			for (int n = 0; n < childrenList.Count; n++) {
				if (childrenList [n].Id == id)
					return n;
			}
			return -1;
		}

		public IReadOnlyList<TreeNode> Children {
			get {
				if (IsInChildrenUpdateTransaction) {
					return childrenBuilder;
				}

				if (!childrenFromExtensionsLoaded) {
					var transaction = BeginContextTransaction (out var disposeTransaction);
					try {
						if (!childrenFromExtensionsLoaded) {
							if (extensionPoint != null) {
								BeginChildrenUpdateTransaction (transaction);
								Context.LoadExtensions (transaction, GetPath (), this);
								// We have to keep the reference to the extension point, since add-ins may be loaded/unloaded
							}
						}
					} finally {
						if (disposeTransaction)
							transaction.Dispose ();
					}
					childrenFromExtensionsLoaded = true;
				}
				return (IReadOnlyList<TreeNode>)childrenBuilder ?? (IReadOnlyList<TreeNode>)children;
			}
		}

		IReadOnlyList<TreeNode> GetLoadedChildren ()
		{
			if (IsInChildrenUpdateTransaction)
				return childrenBuilder;
			else
				return children;
		}

		/// <summary>
		/// Returns true if the tree node has a child update transaction in progress,
		/// and the current thread is the one that created the transaction.
		/// </summary>
		bool IsInChildrenUpdateTransaction => childrenBuilder != null && Context.IsCurrentThreadInTransaction;

		ExtensionContextTransaction BeginContextTransaction (out bool dispose)
		{
			if (IsInChildrenUpdateTransaction) {
				dispose = false;
				return buildTransaction;
			} else {
				dispose = true;
				return Context.BeginTransaction ();
			}
		}

		public void BeginChildrenUpdateTransaction (ExtensionContextTransaction transaction)
		{
			// If a transaction already started, just reuse it
			if (buildTransaction != null)
				return;

			childrenBuilder = children.ToBuilder ();
			this.buildTransaction = transaction;

			transaction.RegisterChildrenUpdateTransaction (this);
		}

		internal void CommitChildrenUpdateTransaction ()
		{
			if (buildTransaction == null)
				throw new InvalidOperationException ("No transaction started");

			children = childrenBuilder.ToImmutable ();

			var transaction = buildTransaction;
			
			childrenBuilder = null;
			buildTransaction = null;

			transaction.ReportChildrenChanged (this);
		}

		public string GetPath ()
		{
			int num=0;
			TreeNode node = this;
			while (node != null) {
				num++;
				node = node.parent;
			}
			
			string[] ids = new string [num];
			
			node = this;
			while (node != null) {
				ids [--num] = node.id;
				node = node.parent;
			}
			return string.Join ("/", ids);
		}
		
		public void NotifyAddinLoaded (RuntimeAddin ad, bool recursive)
		{
			if (extensionNode != null && extensionNode.AddinId == ad.Addin.Id)
				extensionNode.OnAddinLoaded ();
			if (recursive && childrenFromExtensionsLoaded) {
				foreach (TreeNode node in Children)
					node.NotifyAddinLoaded (ad, true);
			}
		}
		
		public ExtensionPoint FindLoadedExtensionPoint (string path)
		{
			if (path.StartsWith ("/"))
				path = path.Substring (1);

			string[] parts = path.Split ('/');
			TreeNode curNode = this;

			foreach (string part in parts) {
				var node = curNode.GetChildNode (part);
				if (node != null) {
					curNode = node;
					if (!curNode.ChildrenFromExtensionsLoaded)
						return null;
					if (curNode.ExtensionPoint != null)
						return curNode.ExtensionPoint;
					continue;
				}
				return null;
			}
			return null;
		}
		
		public void FindAddinNodes (string id, List<TreeNode> nodes)
		{
			if (id != null && extensionPoint != null && extensionPoint.RootAddin == id) {
				// It is an extension point created by the add-in. All nodes below this
				// extension point will be added to the list, even if they come from other add-ins.
				id = null;
			}

			if (childrenFromExtensionsLoaded) {
				// Deep-first search, to make sure children are removed before the parent.
				foreach (TreeNode node in Children)
					node.FindAddinNodes (id, nodes);
			}
			
			if (id == null || AddinId == id)
				nodes.Add (this);
		}
		
		public bool FindExtensionPathByType (IProgressStatus monitor, Type type, string nodeName, out string path, out string pathNodeName)
		{
			if (extensionPoint != null) {
				foreach (ExtensionNodeType nt in extensionPoint.NodeSet.NodeTypes) {
					if (nt.ObjectTypeName.Length > 0 && (nodeName.Length == 0 || nodeName == nt.Id)) {
						RuntimeAddin addin = addinEngine.GetAddin (extensionPoint.RootAddin);
						Type ot = addin.GetType (nt.ObjectTypeName, true);
						if (ot != null) {
							if (ot.IsAssignableFrom (type)) {
								path = extensionPoint.Path;
								pathNodeName = nt.Id;
								return true;
							}
						}
						else
							monitor.ReportError ("Type '" + nt.ObjectTypeName + "' not found in add-in '" + Id + "'", null);
					}
				}
			}
			else {
				foreach (TreeNode node in Children) {
					if (node.FindExtensionPathByType (monitor, type, nodeName, out path, out pathNodeName))
						return true;
				}
			}
			path = null;
			pathNodeName = null;
			return false;
		}
		
		public bool NotifyChildrenChanged ()
		{
			if (extensionNode != null)
				return extensionNode.NotifyChildChanged ();
			else
				return false;
		}

		public void ResetCachedData (ExtensionContextTransaction transaction)
		{
			if (extensionPoint != null) {
				string aid = Addin.GetIdName (extensionPoint.ParentAddinDescription.AddinId);
				RuntimeAddin ad = addinEngine.GetAddin (aid);
				if (ad != null)
					extensionPoint = ad.Addin.Description.ExtensionPoints [GetPath ()];
			}
			if (childrenBuilder != null) {
				foreach (TreeNode cn in childrenBuilder)
					cn.ResetCachedData (transaction);
			}

			foreach (TreeNode cn in children)
				cn.ResetCachedData (transaction);
		}
	}
}