1 //-----------------------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 //-----------------------------------------------------------------------------
5 namespace System.Activities.Presentation.Model
8 using System.Collections.Generic;
9 using System.ComponentModel;
12 // This class provides useful shared utility functions that are
13 // needed by our ModelItemImpl class implementations.
14 internal static class ModelUtilities
16 internal static bool IsSwitchCase(ModelItem modelItem)
18 if (IsModelItemKeyValuePair(modelItem.ItemType))
20 if (modelItem.Parent != null && //modelItem.Parent - ItemsCollection
21 modelItem.Parent.Parent != null && //modelItem.Parent.Parent - Cases
22 modelItem.Parent.Parent.Parent != null && //modelItem.Parent.Parent.Parent - Switch
23 modelItem.Parent.Parent.Parent.ItemType.IsGenericType &&
24 modelItem.Parent.Parent.Parent.ItemType.GetGenericTypeDefinition() == typeof(System.Activities.Statements.Switch<>))
32 internal static bool IsModelItemKeyValuePair(Type type)
34 Fx.Assert(type != null, "Parameter type is null!");
35 return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(ModelItemKeyValuePair<,>);
38 // Returns a wrapped type converter for the given item.
39 internal static TypeConverter GetConverter(IModelTreeItem item)
41 return GetConverter(item.ModelTreeManager, item.ModelItem);
44 // Returns a wrapped converter for the given item.
45 internal static TypeConverter GetConverter(ModelTreeManager modelTreeManager, ModelItem item)
47 return new ModelTypeConverter(modelTreeManager, XamlUtilities.GetConverter(item.ItemType));
50 // Returns the default property on the item, or null if the item has
51 internal static PropertyDescriptor GetDefaultProperty(ModelItem item)
53 DefaultPropertyAttribute propAttr = TypeDescriptor.GetAttributes(item.ItemType)[typeof(DefaultPropertyAttribute)] as DefaultPropertyAttribute;
54 if (propAttr != null && !string.IsNullOrEmpty(propAttr.Name))
56 ModelProperty prop = item.Properties.Find(propAttr.Name);
59 return new ModelPropertyDescriptor(prop);
65 // Wraps an item's properties in PropertyDescriptors and returns a
66 // collection of them.
67 internal static PropertyDescriptorCollection WrapProperties(ModelItem item)
69 List<PropertyDescriptor> descriptors = new List<PropertyDescriptor>();
70 foreach (ModelProperty prop in item.Properties)
72 descriptors.Add(new ModelPropertyDescriptor(prop));
74 return new PropertyDescriptorCollection(descriptors.ToArray(), true);
78 internal static ModelItem ReverseFindFirst(ModelItem start, Predicate<ModelItem> matcher)
80 Fx.Assert(start != null, "start should not be null");
81 Fx.Assert(matcher != null, "matcher should not be null");
83 ModelItem result = null;
84 ModelUtilities.ReverseTraverse(start, (ModelItem current) =>
97 // Traverse model graph via ModelItem's parent. Stop traversing if shouldContinue returns false
98 internal static void ReverseTraverse(ModelItem start, Predicate<ModelItem> shouldContinue)
100 Fx.Assert(start != null, "start should not be null");
101 Fx.Assert(shouldContinue != null, "shouldContinue should not be null");
103 HashSet<ModelItem> visited = new HashSet<ModelItem>();
104 ModelItem current = start;
106 while (current != null)
108 if (!shouldContinue(current))
113 visited.Add(current);
114 current = current.Parent;
115 if (visited.Contains(current))