1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 //----------------------------------------------------------------
5 namespace System.Activities.Presentation
7 using System.Activities.Debugger;
8 using System.Activities.Expressions;
9 using System.Activities.Presentation.Model;
10 using System.Collections.Generic;
11 using System.Collections.ObjectModel;
12 using System.ComponentModel;
15 using System.Runtime.Versioning;
16 using System.ServiceModel.Activities;
17 using Microsoft.Activities.Presentation;
18 using Microsoft.VisualBasic.Activities;
20 //the class does several things:
21 //1. make sure a special property "Imports" (implemented using VisualBasicSettings attached properties) is added to the root object before it's loaded into ModelTree
22 //2. make sure the "root workflow" of the root object always have the same VisualBasicSettings
23 static class NamespaceSettingsHandler
25 static Type WorkflowServiceType = typeof(WorkflowService);
27 static public void PreviewLoadRoot(object sender, WorkflowDesigner.PreviewLoadEventArgs args)
29 object root = args.Instance;
31 DesignerConfigurationService configService = args.Context.Services.GetService<DesignerConfigurationService>();
33 if (configService != null && configService.NamespaceConversionEnabled)
35 ConvertNamespaces(root, args.Context);
38 if (root.GetType() == WorkflowServiceType)
40 args.Context.Services.Subscribe<ModelTreeManager>(manager => manager.Root.PropertyChanged += new PropertyChangedEventHandler(OnRootPropertyChanged));
43 TypeDescriptor.AddProvider(new RootModelTypeDescriptionProvider(root), root);
46 private static void ConvertNamespaces(object root, EditingContext context)
48 VisualBasicSettings settings = VisualBasic.GetSettings(root);
49 IList<AssemblyReference> references;
50 IList<string> importedNamespaces = NamespaceHelper.GetTextExpressionNamespaces(root, out references);
51 FrameworkName targetFramework = WorkflowDesigner.GetTargetFramework(context);
52 if (targetFramework.IsLessThan45())
56 if ((importedNamespaces != null) && (importedNamespaces.Count > 0))
58 NamespaceHelper.ConvertToVBSettings(
66 settings = new VisualBasicSettings();
69 NamespaceHelper.SetVisualBasicSettings(root, settings);
70 NamespaceHelper.SetTextExpressionNamespaces(root, null, null);
73 IDebuggableWorkflowTree debuggableWorkflowTree = root as IDebuggableWorkflowTree;
74 if (debuggableWorkflowTree != null)
76 Activity rootActivity = debuggableWorkflowTree.GetWorkflowRoot();
77 if (rootActivity != null)
79 NamespaceHelper.SetVisualBasicSettings(rootActivity, settings);
80 NamespaceHelper.SetTextExpressionNamespaces(rootActivity, null, null);
86 if ((importedNamespaces == null) || (importedNamespaces.Count == 0))
90 NamespaceHelper.ConvertToTextExpressionImports(settings, out importedNamespaces, out references);
91 NamespaceHelper.SetTextExpressionNamespaces(root, importedNamespaces, references);
92 NamespaceHelper.SetVisualBasicSettings(root, null);
96 NamespaceHelper.SetTextExpressionNamespaces(root, new Collection<string>(), new Collection<AssemblyReference>());
102 static void OnRootPropertyChanged(object sender, PropertyChangedEventArgs e)
104 ModelItem rootModel = sender as ModelItem;
105 Fx.Assert(rootModel != null, "sender item could not be null");
106 ModelProperty changedProperty = rootModel.Properties[e.PropertyName];
107 if (changedProperty == null)
112 object changedPropertyValue = changedProperty.ComputedValue;
113 if (changedPropertyValue == null)
118 Fx.Assert(rootModel.GetCurrentValue().GetType() == WorkflowServiceType, "This handler should only be attached when the root is WorkflowService");
119 IDebuggableWorkflowTree root = rootModel.GetCurrentValue() as IDebuggableWorkflowTree;
120 Activity rootActivity = root.GetWorkflowRoot();
121 if (rootActivity == changedPropertyValue)
123 if (WorkflowDesigner.GetTargetFramework(rootModel.GetEditingContext()).IsLessThan45())
125 VisualBasicSettings settings = VisualBasic.GetSettings(root);
126 NamespaceHelper.SetVisualBasicSettings(changedPropertyValue, settings);
130 IList<AssemblyReference> referencedAssemblies;
131 IList<string> namespaces = NamespaceHelper.GetTextExpressionNamespaces(root, out referencedAssemblies);
132 NamespaceHelper.SetTextExpressionNamespaces(rootActivity, namespaces, referencedAssemblies);
138 class RootModelTypeDescriptionProvider : TypeDescriptionProvider
140 public RootModelTypeDescriptionProvider(object instance)
141 : base(TypeDescriptor.GetProvider(instance))
145 public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object instance)
147 ICustomTypeDescriptor defaultDescriptor = base.GetTypeDescriptor(objectType, instance);
148 return new RootModelTypeDescriptor(defaultDescriptor, instance);
152 class RootModelTypeDescriptor : CustomTypeDescriptor
155 NamespaceListPropertyDescriptor importDescriptor;
157 public RootModelTypeDescriptor(ICustomTypeDescriptor parent, object root)
163 PropertyDescriptor ImportDescriptor
167 if (this.importDescriptor == null)
169 this.importDescriptor = new NamespaceListPropertyDescriptor(this.root);
172 return this.importDescriptor;
176 public override PropertyDescriptorCollection GetProperties()
178 return GetProperties(null);
181 public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)
183 return new PropertyDescriptorCollection(base.GetProperties(attributes).Cast<PropertyDescriptor>()
184 .Union(new PropertyDescriptor[] { this.ImportDescriptor }).ToArray());