71f0968492f917b550279f86165acf94e240386b
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Core.Presentation / System / Activities / Core / Presentation / InteropDesigner.xaml.cs
1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //----------------------------------------------------------------
4 namespace System.Activities.Core.Presentation
5 {
6     using System;
7     using System.Activities.Presentation;
8     using System.Activities.Presentation.Metadata;
9     using System.Activities.Presentation.PropertyEditing;
10     using System.Activities.Presentation.View;
11     using System.Activities.Statements;
12     using System.ComponentModel;
13     using System.IO;
14     using System.Runtime;
15     using System.Windows.Threading;
16     
17     partial class InteropDesigner
18     {
19         private static Func<Type, bool> filter; 
20         private static string interopTypeAssemblyQualifiedName = null;
21
22         public InteropDesigner()
23         {
24             this.InitializeComponent();
25         }
26
27         public static Func<Type, bool> Filter
28         {
29             get 
30             {
31                 if (InteropDesigner.filter == null)
32                 {
33                     // We will build type name for System.Workflow.ComponentModel.Activity
34                     string typeName = typeof(Activity).AssemblyQualifiedName;
35                     typeName = typeName.Replace("System.Activities", "System.Workflow.ComponentModel");
36
37                     Type activityType = GetTypeByQualifiedName(typeName);
38                     if (activityType != null)
39                     {
40                         //Interop.Body has to be a 3.5 Activity
41                         InteropDesigner.filter = (type) => activityType.IsAssignableFrom(type);
42                     }
43                 }
44
45                 return InteropDesigner.filter; 
46             }
47         }
48
49         public static string InteropTypeAssemblyQualifiedName
50         {
51             get
52             {
53                 if (interopTypeAssemblyQualifiedName == null)
54                 {
55                     // Construct the type name dynamically to avoid hardcoding the version number and public key token.
56                     // The constructed type name should look like: "System.Activities.Statements.Interop, System.Workflow.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
57                     interopTypeAssemblyQualifiedName = typeof(Parallel).AssemblyQualifiedName.Replace("Parallel", "Interop").Replace("System.Activities,", "System.Workflow.Runtime,");
58                 }
59
60                 return interopTypeAssemblyQualifiedName;
61             }
62         }
63
64         protected override void OnModelItemChanged(object newItem)
65         {
66             base.OnModelItemChanged(newItem);
67             this.ModelItem.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(OnModelItemPropertyChanged);
68         }
69
70         private void OnModelItemPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
71         {
72             if (e.PropertyName == "ActivityType")
73             {
74                 //Whenever ActivityType property changes, the activity will generate a new set of 
75                 // dynamic properties. the property browser will not pick up the changes till
76                 // we select some other modelitem and then select this back.
77                 // modelItem.root is theone that will be always available.
78
79                 this.Dispatcher.BeginInvoke(DispatcherPriority.SystemIdle, (Action)(() =>
80                 {
81                     Selection.SelectOnly(this.Context, this.ModelItem.Root);
82                 }));
83                 this.Dispatcher.BeginInvoke(DispatcherPriority.SystemIdle, (Action)(() =>
84                 {
85                     Selection.SelectOnly(this.Context, this.ModelItem);
86                 }));
87                
88             }
89         }
90
91         public static void RegisterMetadata(AttributeTableBuilder builder)
92         {
93             Type activityType = GetTypeByQualifiedName(InteropDesigner.InteropTypeAssemblyQualifiedName);
94
95             //activityType will be null in ClientSKU since System.Workflow.Runtime.dll is not a part of clientSKU.
96             if (activityType != null)
97             {
98                 builder.AddCustomAttributes(activityType, new DesignerAttribute(typeof(InteropDesigner)));
99                 builder.AddCustomAttributes(
100                             activityType,
101                             "ActivityType",
102                             new EditorOptionAttribute { Name = TypePropertyEditor.BrowseTypeDirectly, Value = true });
103                 builder.AddCustomAttributes(
104                             activityType,
105                             "ActivityType",
106                             new EditorOptionAttribute { Name = TypePropertyEditor.Filter, Value = Filter });
107                 builder.AddCustomAttributes(
108                             activityType,
109                             "ActivityType",
110                             new RefreshPropertiesAttribute(RefreshProperties.All));
111                 builder.AddCustomAttributes(activityType, new ActivityDesignerOptionsAttribute { AllowDrillIn = false });
112             }
113         }
114
115         // Gets a System.Type object using the type's assembly qualified name.
116         // Non-fatal exceptions are ----ed.
117         private static Type GetTypeByQualifiedName(string assemblyQualifiedName)
118         {
119             Fx.Assert(assemblyQualifiedName != null, "assemblyQualifiedName cannot be null.");
120
121             try
122             {
123                 Type type = Type.GetType(assemblyQualifiedName);
124                 return type;
125             }
126             catch (Exception e)
127             {
128                 if (Fx.IsFatal(e))
129                 {
130                     throw;
131                 }
132             }
133
134             return null;
135         }
136     }
137 }