Update Reference Sources to .NET Framework 4.6.1
[mono.git] / mcs / class / referencesource / System.Activities.Core.Presentation / System / ServiceModel / Activities / Presentation / SendReplyDesigner.xaml.cs
1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //----------------------------------------------------------------
4
5 namespace System.ServiceModel.Activities.Presentation
6 {
7     using System;
8     using System.Activities.Core.Presentation.Themes;
9     using System.Activities.Presentation;
10     using System.Activities.Presentation.Model;
11     using System.Activities.Presentation.Metadata;
12     using System.Activities.Presentation.PropertyEditing;
13     using System.Collections.Generic;
14     using System.ComponentModel;
15     using System.Diagnostics.CodeAnalysis;
16     using System.Runtime;
17     using System.Windows;
18     using System.Windows.Input;
19     using System.Activities;
20
21     partial class SendReplyDesigner
22     {
23         const string CorrelationsCategoryLabelKey = "correlationsCategoryLabel";
24         const string EndpointCategoryLabelKey = "endpointCategoryLabel";
25         const string MiscellaneousCategoryLabelKey = "miscellaneousCategoryLabel";
26         const string AdvancedCategoryLabelKey = "advancedCategoryLabel";
27         static string Message;
28         static string Action;
29         static string DeclaredMessageType;
30
31         [SuppressMessage(FxCop.Category.Performance, FxCop.Rule.InitializeReferenceTypeStaticFieldsInline,
32             Justification = "PropertyValueEditors association needs to be done in the static constructor.")]
33         static SendReplyDesigner()
34         {
35             AttributeTableBuilder builder = new AttributeTableBuilder();
36             Type sendType = typeof(SendReply);
37
38             builder.AddCustomAttributes(sendType, sendType.GetProperty("CorrelationInitializers"), PropertyValueEditor.CreateEditorAttribute(typeof(CorrelationInitializerValueEditor)));
39
40             var categoryAttribute = new CategoryAttribute(EditorCategoryTemplateDictionary.Instance.GetCategoryTitle(CorrelationsCategoryLabelKey));
41
42             builder.AddCustomAttributes(sendType, sendType.GetProperty("CorrelationInitializers"), categoryAttribute, BrowsableAttribute.Yes,
43                 PropertyValueEditor.CreateEditorAttribute(typeof(CorrelationInitializerValueEditor)));
44
45             categoryAttribute = new CategoryAttribute(EditorCategoryTemplateDictionary.Instance.GetCategoryTitle(MiscellaneousCategoryLabelKey));
46
47             builder.AddCustomAttributes(sendType, sendType.GetProperty("DisplayName"), categoryAttribute);
48             var descriptionAttribute = new DescriptionAttribute(StringResourceDictionary.Instance.GetString("messagingValueHint", "<Value to bind>"));
49             builder.AddCustomAttributes(sendType, sendType.GetProperty("Content"), categoryAttribute, descriptionAttribute, PropertyValueEditor.CreateEditorAttribute(typeof(SendContentPropertyEditor)));
50             builder.AddCustomAttributes(sendType, sendType.GetProperty("Request"),
51                 categoryAttribute,
52                 PropertyValueEditor.CreateEditorAttribute(typeof(ActivityXRefPropertyEditor)));
53
54             var advancedAttribute = new EditorBrowsableAttribute(EditorBrowsableState.Advanced);
55             builder.AddCustomAttributes(sendType, sendType.GetProperty("Action"), categoryAttribute, advancedAttribute);
56
57             Action = sendType.GetProperty("Action").Name;
58
59             Type sendMessageContentType = typeof(SendMessageContent);
60             Message = sendMessageContentType.GetProperty("Message").Name;
61             DeclaredMessageType = sendMessageContentType.GetProperty("DeclaredMessageType").Name;
62
63             MetadataStore.AddAttributeTable(builder.CreateTable());
64
65             Func<Activity, IEnumerable<ArgumentAccessor>> argumentAccessorGenerator = (activity) => new ArgumentAccessor[]
66             {
67                 new ArgumentAccessor
68                 {
69                     Getter = (ownerActivity) =>
70                     {
71                         SendReply sendReply = (SendReply)ownerActivity;
72                         SendMessageContent content = sendReply.Content as SendMessageContent;
73                         return content != null ? content.Message : null;
74                     },
75                     Setter = (ownerActivity, arg) =>
76                     {
77                         SendReply sendReply = (SendReply)ownerActivity;
78                         SendMessageContent content = sendReply.Content as SendMessageContent;
79                         if (content != null)
80                         {
81                             content.Message = arg as InArgument;
82                         }
83                     },
84                 },
85             };
86             ActivityArgumentHelper.RegisterAccessorsGenerator(sendType, argumentAccessorGenerator);
87         }
88
89         public SendReplyDesigner()
90         {
91             InitializeComponent();
92         }
93
94         protected override void OnModelItemChanged(object newItem)
95         {
96             base.OnModelItemChanged(newItem);
97             if (null != this.ModelItem)
98             {
99                 this.ModelItem.PropertyChanged += OnModelItemPropertyChanged;
100             }
101         }
102
103         void OnModelItemPropertyChanged(object sender, PropertyChangedEventArgs e)
104         {
105             if (string.Equals(e.PropertyName, Message))
106             {
107                 SendMessageContent messageContent = ((SendReply)this.ModelItem.GetCurrentValue()).Content as SendMessageContent;
108                 this.ModelItem.Properties[DeclaredMessageType].SetValue(null == messageContent ? null : messageContent.Message.ArgumentType);
109             }
110         }
111
112         void OnDefineButtonClicked(object sender, RoutedEventArgs args)
113         {
114             using (EditingScope scope = this.Context.Services.GetRequiredService<ModelTreeManager>().CreateEditingScope(StringResourceDictionary.Instance.GetString("editSendContent"), true))
115             {
116                 if (SendContentDialog.ShowDialog(this.ModelItem, this.Context, this))
117                 {
118                     scope.Complete();
119                 }
120             }
121         }
122     }
123 }