[reflection] Coop handles icalls in System.Reflection and System.RuntimeTypeHandle...
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / Base / Core / Internal / PropertyEditing / Model / ModelPropertyEntryCollection.cs
1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //----------------------------------------------------------------
4 namespace System.Activities.Presentation.Internal.PropertyEditing.Model 
5 {
6     using System;
7     using System.Collections.Generic;
8     using System.Collections.ObjectModel;
9     using System.ComponentModel;
10     using System.Diagnostics;
11     using System.Text;
12     using System.Activities.Presentation.Model;
13     using System.Activities.Presentation.PropertyEditing;
14     using System.Runtime;
15
16     // <summary>
17     // Collection of ModelPropertyEntries used to model sub-properties of PropertyValues
18     // </summary>
19     internal class ModelPropertyEntryCollection : PropertyEntryCollection 
20     {
21
22         List<ModelPropertyEntry> _properties;
23
24         // <summary>
25         // Basic ctor
26         // </summary>
27         // <param name="parentProperty">Parent property</param>
28         public ModelPropertyEntryCollection(ModelPropertyEntry parentProperty)
29             : base(parentProperty.PropertyValue) 
30         {
31
32             CreateCollection(parentProperty);
33         }
34
35         // <summary>
36         // Basic ctor
37         // </summary>
38         // <param name="parentProperty">Parent indexer</param>
39         public ModelPropertyEntryCollection(ModelPropertyIndexer parentProperty)
40             : base(parentProperty.PropertyValue) 
41         {
42
43             CreateCollection(parentProperty);
44         }
45
46         // <summary>
47         // Gets the number of PropertyEntries in this collection
48         // </summary>
49         public override int Count 
50         {
51             get {
52                 return _properties == null ? 0 : _properties.Count;
53             }
54         }
55
56         // <summary>
57         // Gets the property from this collection of the given name (case sensitive)
58         // </summary>
59         // <param name="propertyName">Name to look up</param>
60         // <returns>Corresponding PropertyEntry if one exists.</returns>
61         public override PropertyEntry this[string propertyName] {
62             get {
63                 if (_properties == null)
64                 {
65                     return null;
66                 }
67
68                 foreach (ModelPropertyEntry entry in _properties)
69                 {
70                     if (string.Equals(entry.PropertyName, propertyName))
71                     {
72                         return entry;
73                     }
74                 }
75
76                 return null;
77             }
78         }
79
80         // <summary>
81         // Gets the enumerator for this collection
82         // </summary>
83         // <returns></returns>
84         public override IEnumerator<PropertyEntry> GetEnumerator() 
85         {
86             if (_properties == null)
87             {
88                 yield break;
89             }
90
91             for (int i = 0; i < _properties.Count; i++) 
92             {
93                 yield return _properties[i];
94             }
95         }
96
97         // Parses the sub-properties of the given parent collection item and populates a corresponding
98         // private list of ModelPropertyEntries that represents it.
99         private void CreateCollection(ModelPropertyIndexer parentCollectionItem) 
100         {
101
102             // Assert some assumptions that should be true at this point
103             Fx.Assert(parentCollectionItem.ModelItem != null, "parentCollectionItem.ModelItem should not be null");
104
105             List<ModelProperty> subProperties = ExtensibilityAccessor.GetSubProperties(parentCollectionItem.ModelItem);
106
107             if (subProperties == null || subProperties.Count < 1)
108             {
109                 return;
110             }
111
112             // At this point we have at least one ModelProperty that acts as a subProperty of the 
113             // given ModelItem.  Wrap the list in ModelPropertyEntries and exit.
114
115             _properties = new List<ModelPropertyEntry>(subProperties.Count);
116
117             for (int i = 0; i < subProperties.Count; i++) 
118             {
119                 _properties.Add(new ModelPropertyEntry(subProperties[i], (ModelPropertyValue)parentCollectionItem.PropertyValue));
120             }
121
122             // Sort the sub-properties by their OrderToken as well as their name
123             if (_properties != null)
124             {
125                 _properties.Sort();
126             }
127         }
128
129         private void CreateCollection(ModelPropertyEntry parentProperty) 
130         {
131
132             // Assert some assumptions that should be true at this point
133             Fx.Assert(parentProperty != null, "parentProperty should not be null");
134             Fx.Assert(parentProperty.ModelPropertySet != null, "parentProperty.ModelPropertySet should not be null");
135             Fx.Assert(parentProperty.ModelPropertySet.Count > 0, "parentProperty.ModelPropertySet.Count should be > 0");
136
137             // Ignore sub-properties of MarkupExtensions for v1
138             if (parentProperty.IsMarkupExtension)
139             {
140                 return;
141             }
142
143             IEnumerable<IList<ModelProperty>> mergedSubProperties = ModelPropertyMerger.GetMergedSubProperties(parentProperty.ModelPropertySet);
144
145             int index = 0;
146             bool multiSelect = parentProperty.ModelPropertySet.Count > 1;
147             foreach (IList<ModelProperty> subPropertySet in mergedSubProperties) 
148             {
149
150                 if (index == 0)
151                 {
152                     _properties = new List<ModelPropertyEntry>();
153                 }
154
155                 ModelPropertyEntry entry;
156
157                 if (multiSelect)
158                 {
159                     entry = new ModelPropertyEntry(subPropertySet, (ModelPropertyValue)parentProperty.PropertyValue);
160                 }
161                 else
162                 {
163                     entry = new ModelPropertyEntry(subPropertySet[0], (ModelPropertyValue)parentProperty.PropertyValue);
164                 }
165
166                 _properties.Add(entry);
167                 index++;
168             }
169
170             // Sort the sub-properties by their OrderToken as well as their name
171             if (_properties != null)
172             {
173                 _properties.Sort();
174             }
175         }
176     }
177 }