~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/AddIns/DisplayBindings/WpfDesign/WpfDesign.XamlDom/Project/CollectionSupport.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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)
 
3
 
 
4
using System;
 
5
using System.Diagnostics;
 
6
using System.Collections;
 
7
using System.ComponentModel;
 
8
using System.Globalization;
 
9
using System.Reflection;
 
10
using System.Windows;
 
11
using System.Windows.Markup;
 
12
 
 
13
namespace ICSharpCode.WpfDesign.XamlDom
 
14
{
 
15
        /// <summary>
 
16
        /// Static class containing helper methods to work with collections (like the XamlParser does)
 
17
        /// </summary>
 
18
        public static class CollectionSupport
 
19
        {
 
20
                /// <summary>
 
21
                /// Gets if the type is considered a collection in XAML.
 
22
                /// </summary>
 
23
                public static bool IsCollectionType(Type type)
 
24
                {
 
25
                        return typeof(IList).IsAssignableFrom(type)
 
26
                                || type.IsArray
 
27
                                || typeof(IAddChild).IsAssignableFrom(type)
 
28
                                || typeof(ResourceDictionary).IsAssignableFrom(type);
 
29
                }               
 
30
 
 
31
                /// <summary>
 
32
                /// Gets if the collection type <paramref name="col"/> can accepts items of type
 
33
                /// <paramref name="item"/>.
 
34
                /// </summary>
 
35
                public static bool CanCollectionAdd(Type col, Type item)
 
36
                {
 
37
                        var e = col.GetInterface("IEnumerable`1");
 
38
                        if (e != null && e.IsGenericType) {
 
39
                                var a = e.GetGenericArguments()[0];
 
40
                                return a.IsAssignableFrom(item);
 
41
                        }
 
42
                        return true;
 
43
                }
 
44
 
 
45
                /// <summary>
 
46
                /// Gets if the collection type <paramref name="col"/> can accept the specified items.
 
47
                /// </summary>
 
48
                public static bool CanCollectionAdd(Type col, IEnumerable items)
 
49
                {
 
50
                        foreach (var item in items) {
 
51
                                if (!CanCollectionAdd(col, item.GetType())) return false;
 
52
                        }
 
53
                        return true;
 
54
                }
 
55
                
 
56
                /// <summary>
 
57
                /// Adds a value to the end of a collection.
 
58
                /// </summary>
 
59
                public static void AddToCollection(Type collectionType, object collectionInstance, XamlPropertyValue newElement)
 
60
                {
 
61
                        IAddChild addChild = collectionInstance as IAddChild;
 
62
                        if (addChild != null) {
 
63
                                if (newElement is XamlTextValue) {
 
64
                                        addChild.AddText((string)newElement.GetValueFor(null));
 
65
                                } else {
 
66
                                        addChild.AddChild(newElement.GetValueFor(null));
 
67
                                }
 
68
                        } else if (collectionInstance is ResourceDictionary) {
 
69
                                object val = newElement.GetValueFor(null);
 
70
                                object key = newElement is XamlObject ? ((XamlObject)newElement).GetXamlAttribute("Key") : null;
 
71
                                if (key == null) {
 
72
                                        if (val is Style)
 
73
                                                key = ((Style)val).TargetType;
 
74
                                }
 
75
                                ((ResourceDictionary)collectionInstance).Add(key, val);
 
76
                        } else {
 
77
                                collectionType.InvokeMember(
 
78
                                        "Add", BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Instance,
 
79
                                        null, collectionInstance,
 
80
                                        new object[] { newElement.GetValueFor(null) },
 
81
                                        CultureInfo.InvariantCulture);
 
82
                        }
 
83
                }
 
84
                
 
85
                        /// <summary>
 
86
                /// Adds a value at the specified index in the collection.
 
87
                /// </summary>
 
88
                public static void Insert(Type collectionType, object collectionInstance, XamlPropertyValue newElement, int index)
 
89
                {
 
90
                        collectionType.InvokeMember(
 
91
                                "Insert", BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Instance,
 
92
                                null, collectionInstance,
 
93
                                new object[] { index, newElement.GetValueFor(null) },
 
94
                                CultureInfo.InvariantCulture);
 
95
                }
 
96
                
 
97
                static readonly Type[] RemoveAtParameters = { typeof(int) };
 
98
                
 
99
                /// <summary>
 
100
                /// Removes the item at the specified index of the collection.
 
101
                /// </summary>
 
102
                /// <returns>True if the removal succeeded, false if the collection type does not support RemoveAt.</returns>
 
103
                public static bool RemoveItemAt(Type collectionType, object collectionInstance, int index)
 
104
                {
 
105
                        MethodInfo m = collectionType.GetMethod("RemoveAt", RemoveAtParameters);
 
106
                        if (m != null) {
 
107
                                m.Invoke(collectionInstance, new object[] { index });
 
108
                                return true;
 
109
                        } else {
 
110
                                return false;
 
111
                        }
 
112
                }
 
113
                
 
114
                /// <summary>
 
115
                /// Removes an item instance from the specified collection.
 
116
                /// </summary>
 
117
                public static void RemoveItem(Type collectionType, object collectionInstance, object item)
 
118
                {
 
119
                        collectionType.InvokeMember(
 
120
                                "Remove", BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Instance,
 
121
                                null, collectionInstance,
 
122
                                new object[] { item },
 
123
                                CultureInfo.InvariantCulture);
 
124
                }
 
125
        }
 
126
}