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.ObjectModel;
10
namespace ICSharpCode.Core
13
/// Represents an extension path in the <see cref="AddInTree"/>.
15
public sealed class AddInTreeNode
17
readonly object lockObj = new object();
18
Dictionary<string, AddInTreeNode> childNodes = new Dictionary<string, AddInTreeNode>();
19
ReadOnlyCollection<Codon> codons;
20
List<IEnumerable<Codon>> codonInput;
23
/// A dictionary containing the child paths.
25
public Dictionary<string, AddInTreeNode> ChildNodes {
31
public void AddCodons(IEnumerable<Codon> newCodons)
33
if (newCodons == null)
34
throw new ArgumentNullException("newCodons");
36
if (codonInput == null) {
37
codonInput = new List<IEnumerable<Codon>>();
39
codonInput.Add(codons);
41
codonInput.Add(newCodons);
46
/// A list of child <see cref="Codon"/>s.
48
public ReadOnlyCollection<Codon> Codons {
52
if (codonInput == null) {
53
codons = new ReadOnlyCollection<Codon>(new Codon[0]);
55
codons = TopologicalSort.Sort(codonInput).AsReadOnly();
65
/// Builds the child items in this path. Ensures that all items have the type T.
67
/// <param name="caller">The owner used to create the objects.</param>
68
/// <param name="additionalConditions">Additional conditions applied to the node.</param>
69
public List<T> BuildChildItems<T>(object caller, IEnumerable<ICondition> additionalConditions = null)
71
var codons = this.Codons;
72
List<T> items = new List<T>(codons.Count);
73
foreach (Codon codon in codons) {
74
object result = BuildChildItem(codon, caller, additionalConditions);
77
IBuildItemsModifier mod = result as IBuildItemsModifier;
80
} else if (result is T) {
83
throw new InvalidCastException("The AddInTreeNode <" + codon.Name + " id='" + codon.Id
84
+ "'> returned an instance of " + result.GetType().FullName
85
+ " but the type " + typeof(T).FullName + " is expected.");
91
public object BuildChildItem(Codon codon, object caller, IEnumerable<ICondition> additionalConditions = null)
94
throw new ArgumentNullException("codon");
96
AddInTreeNode subItemNode;
97
childNodes.TryGetValue(codon.Id, out subItemNode);
99
IEnumerable<ICondition> conditions;
100
if (additionalConditions == null)
101
conditions = codon.Conditions;
102
else if (codon.Conditions.Length == 0)
103
conditions = additionalConditions;
105
conditions = additionalConditions.Concat(codon.Conditions);
107
return codon.BuildItem(new BuildItemArgs(caller, codon, conditions, subItemNode));
111
/// Builds the child items in this path.
113
/// <param name="caller">The owner used to create the objects.</param>
114
[Obsolete("Use the generic BuildChildItems version instead")]
115
public ArrayList BuildChildItems(object caller)
117
return new ArrayList(this.BuildChildItems<object>(caller));
121
/// Builds a specific child items in this path.
123
/// <param name="childItemID">
124
/// The ID of the child item to build.
126
/// <param name="caller">The owner used to create the objects.</param>
127
/// <param name="additionalConditions">Additional conditions applied to the created object</param>
128
/// <exception cref="TreePathNotFoundException">
129
/// Occurs when <paramref name="childItemID"/> does not exist in this path.
131
public object BuildChildItem(string childItemID, object caller, IEnumerable<ICondition> additionalConditions = null)
133
foreach (Codon codon in this.Codons) {
134
if (codon.Id == childItemID) {
135
return BuildChildItem(codon, caller, additionalConditions);
138
throw new TreePathNotFoundException(childItemID);