1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 //----------------------------------------------------------------
5 namespace System.Activities.Presentation.Model
8 using System.Activities.Presentation.Internal.PropertyEditing;
9 using System.Collections.Generic;
10 using System.ComponentModel;
12 using System.Activities.Presentation;
13 using System.Diagnostics.CodeAnalysis;
16 public delegate object PropertyValueMorphHelper(ModelItem originalValue, ModelProperty newModelProperty);
18 public static class MorphHelper
20 [SuppressMessage(FxCop.Category.Naming, FxCop.Rule.IdentifiersShouldBeSpelledCorrectly,
21 Justification = "Morph is the right word here")]
23 static Dictionary<Type, PropertyValueMorphHelper> morphExtensions = new Dictionary<Type, PropertyValueMorphHelper>();
25 [SuppressMessage(FxCop.Category.Naming, FxCop.Rule.IdentifiersShouldBeSpelledCorrectly,
26 Justification = "Morph is the right word here")]
27 public static void AddPropertyValueMorphHelper(Type propertyType, PropertyValueMorphHelper extension)
29 if (propertyType == null)
31 throw FxTrace.Exception.AsError(new ArgumentNullException("propertyType"));
33 if (extension == null)
35 throw FxTrace.Exception.AsError(new ArgumentNullException("extension"));
37 morphExtensions[propertyType] = extension;
40 [SuppressMessage(FxCop.Category.Naming, FxCop.Rule.IdentifiersShouldBeSpelledCorrectly,
41 Justification = "Morph is the right word here")]
42 public static PropertyValueMorphHelper GetPropertyValueMorphHelper(Type propertyType)
44 if (propertyType == null)
46 throw FxTrace.Exception.AsError(new ArgumentNullException("propertyType"));
48 PropertyValueMorphHelper extension = null;
49 morphExtensions.TryGetValue(propertyType, out extension);
50 if (extension == null && propertyType.IsGenericType)
52 morphExtensions.TryGetValue(propertyType.GetGenericTypeDefinition(), out extension);
57 [SuppressMessage(FxCop.Category.Naming, FxCop.Rule.IdentifiersShouldBeSpelledCorrectly,
58 Justification = "Morph is the right word here")]
59 // This updates back links
60 public static void MorphObject(ModelItem oldModelItem, ModelItem newModelitem)
62 if (oldModelItem == null)
64 throw FxTrace.Exception.AsError(new ArgumentNullException("oldModelItem"));
66 if (newModelitem == null)
68 throw FxTrace.Exception.AsError(new ArgumentNullException("newModelitem"));
71 var collectionParents = from parent in oldModelItem.Parents
72 where parent is ModelItemCollection
73 select (ModelItemCollection)parent;
74 foreach (ModelItemCollection collectionParent in collectionParents.ToList())
76 int index = collectionParent.IndexOf(oldModelItem);
77 collectionParent.Remove(oldModelItem);
78 collectionParent.Insert(index, newModelitem);
80 foreach (ModelProperty modelProperty in oldModelItem.Sources.ToList())
82 modelProperty.SetValue(newModelitem);
88 [SuppressMessage(FxCop.Category.Naming, FxCop.Rule.IdentifiersShouldBeSpelledCorrectly,
89 Justification = "Morph is the right word here")]
90 // this updates forward links
91 public static void MorphProperties(ModelItem oldModelItem, ModelItem newModelitem)
93 if (oldModelItem == null)
95 throw FxTrace.Exception.AsError(new ArgumentNullException("oldModelItem"));
97 if (newModelitem == null)
99 throw FxTrace.Exception.AsError(new ArgumentNullException("newModelitem"));
102 foreach (ModelProperty modelProperty in oldModelItem.Properties)
104 ModelProperty propertyInNewModelItem = newModelitem.Properties[modelProperty.Name];
105 if (propertyInNewModelItem != null)
107 Console.WriteLine(propertyInNewModelItem.Name);
108 if (CanCopyProperty(modelProperty, propertyInNewModelItem))
110 if (propertyInNewModelItem.PropertyType.Equals(modelProperty.PropertyType))
112 propertyInNewModelItem.SetValue(modelProperty.Value);
113 modelProperty.SetValue(null);
115 else // See if there is morph helper for this type.
117 PropertyValueMorphHelper extension = GetPropertyValueMorphHelper(modelProperty.PropertyType);
118 if (extension != null)
120 propertyInNewModelItem.SetValue(extension(modelProperty.Value, propertyInNewModelItem));
121 modelProperty.SetValue(null);
131 static bool CanCopyProperty(ModelProperty modelProperty, ModelProperty propertyInNewModelItem)
133 bool canCopyProperty = false;
134 DesignerSerializationVisibilityAttribute designerSerializationVisibility = ExtensibilityAccessor.GetAttribute<DesignerSerializationVisibilityAttribute>(modelProperty.Attributes);
135 if (modelProperty.Value == null)
137 canCopyProperty = false;
139 else if (designerSerializationVisibility != null && designerSerializationVisibility.Visibility != DesignerSerializationVisibility.Visible)
141 canCopyProperty = false;
143 else if (propertyInNewModelItem != null && !propertyInNewModelItem.IsAttached && !propertyInNewModelItem.IsReadOnly)
145 canCopyProperty = true;
147 return canCopyProperty;