affa0ab553ac98f3b78d6644fd72039c496d6e27
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / Model / GenericArgumentsUpdater.cs
1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //----------------------------------------------------------------
4
5 namespace System.Activities.Presentation.Model
6 {
7
8     using System;
9     using System.Activities;
10     using System.Diagnostics;
11     using System.Linq;
12     using System.Runtime;
13     using System.Activities.Presentation.View;
14     using System.Windows.Threading;
15
16     class GenericArgumentUpdater
17     {
18         EditingContext context;
19         const string TypeArgumentPropertyName = "TypeArgument";
20
21         public GenericArgumentUpdater(EditingContext context)
22         {
23             this.context = context;
24         }
25
26         public void AddSupportForUpdatingTypeArgument(Type modelItemType)
27         {
28             AttachedProperty<Type> typeArgumentProperty = new AttachedProperty<Type>
29             {
30                 Name = TypeArgumentPropertyName,
31                 OwnerType = modelItemType,
32                 Getter = (modelItem) => modelItem.Parent == null ? null : GetTypeArgument(modelItem),
33                 Setter = (modelItem, value) => UpdateTypeArgument(modelItem, value),
34                 IsBrowsable = true
35             };
36             this.context.Services.GetService<AttachedPropertiesService>().AddProperty(typeArgumentProperty);
37         }
38
39         private static void UpdateTypeArgument(ModelItem modelItem, Type value)
40         {
41             if (value != null)
42             {
43                 Type oldModelItemType = modelItem.ItemType;
44                 Fx.Assert(oldModelItemType.GetGenericArguments().Count() == 1, "we only support changing a single type parameter ?");
45                 Type newModelItemType = oldModelItemType.GetGenericTypeDefinition().MakeGenericType(value);
46                 Fx.Assert(newModelItemType != null, "New model item type needs to be non null or we cannot proceed further");
47                 ModelItem newModelItem = ModelFactory.CreateItem(modelItem.GetEditingContext(), Activator.CreateInstance(newModelItemType));
48                 MorphHelper.MorphObject(modelItem, newModelItem);
49                 MorphHelper.MorphProperties(modelItem, newModelItem);
50
51                 if (oldModelItemType.IsSubclassOf(typeof(Activity)) && newModelItemType.IsSubclassOf(typeof(Activity)))
52                 {
53                     if (string.Equals((string)modelItem.Properties["DisplayName"].ComputedValue, GetActivityDefaultName(oldModelItemType), StringComparison.Ordinal))
54                     {
55                         newModelItem.Properties["DisplayName"].SetValue(GetActivityDefaultName(newModelItemType));
56                     }
57                 }
58
59                 DesignerView designerView = modelItem.GetEditingContext().Services.GetService<DesignerView>();
60                 if (designerView != null)
61                 {
62                     Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Render, (Action)(() =>
63                     {
64                         if (designerView.RootDesigner != null && ((WorkflowViewElement)designerView.RootDesigner).ModelItem == modelItem)
65                         {
66                             designerView.MakeRootDesigner(newModelItem, true);
67                         }
68                         Selection.SelectOnly(modelItem.GetEditingContext(), newModelItem);
69                     }));
70                 }
71             }
72         }
73
74         private static Type GetTypeArgument(ModelItem modelItem)
75         {
76             Fx.Assert(modelItem.ItemType.GetGenericArguments().Count() == 1, "we only support changing a single type parameter ?");
77             return modelItem.ItemType.GetGenericArguments()[0];
78         }
79
80         private static string GetActivityDefaultName(Type activityType)
81         {
82             Fx.Assert(activityType.IsSubclassOf(typeof(Activity)), "activityType is not a subclass of System.Activities.Activity");
83             Activity activity = (Activity)Activator.CreateInstance(activityType);
84             return activity.DisplayName;
85         }
86     }
87
88
89
90 }