eafcd5037c6c5da6dd53fe2b9871ae785f29a50e
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / View / ExpressionValueEditor.cs
1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //----------------------------------------------------------------
4
5 namespace System.Activities.Presentation.View
6 {
7     using System.Activities.Presentation.Internal.PropertyEditing;
8     using System.Activities.Presentation.Model;
9     using System.Activities.Presentation.PropertyEditing;
10     using System.Activities.Presentation.Converters;
11     using System.Globalization;
12     using System.Windows;
13     using System.Windows.Controls;
14     using System.Windows.Data;
15     using System.Runtime;
16
17     class ExpressionValueEditor : DialogPropertyValueEditor
18     {
19         public ExpressionValueEditor()
20         {
21             //default template for inline editor
22             this.InlineEditorTemplate = EditorResources.GetResources()["inlineExpressionEditorTemplate"] as DataTemplate;
23         }
24
25         public override void ShowDialog(PropertyValue propertyValue, IInputElement commandSource)
26         {
27             //get the property entry to model item converter
28             IValueConverter converter = (ModelPropertyEntryToOwnerActivityConverter)EditorResources.GetResources()["ModelPropertyEntryToOwnerActivityConverter"];
29             ModelItem item = (ModelItem)converter.Convert(propertyValue.ParentProperty, typeof(ModelItem), false, null);
30             //we need editing context 
31             EditingContext ctx = ((IModelTreeItem)item).ModelTreeManager.Context;
32             //get the default dialog owner
33             DependencyObject owner = ctx.Services.GetService<DesignerView>();
34
35             //create and show dialog with owner, edited expression and context
36             (new EditorDialog(owner, propertyValue, ctx, this.DialogTemplate, this.DialogTitle)).ShowOkCancel();
37         }
38
39         protected virtual DataTemplate DialogTemplate
40         {
41             get { return (DataTemplate)EditorResources.GetResources()["dialogExpressionEditorTemplate"]; }
42         }
43
44         protected virtual string DialogTitle
45         {
46             get { return (string)EditorResources.GetResources()["dialogExpressionEditorTitle"]; }
47         }
48
49         private sealed class EditorDialog : WorkflowElementDialog
50         {
51             public EditorDialog(DependencyObject owner, PropertyValue propertyValue, EditingContext context, DataTemplate dialogTemplate, string title)
52             {
53                 //setup properties
54                 this.MinWidth = 350;
55                 this.MinHeight = 185;
56                 this.WindowResizeMode = ResizeMode.CanResize;
57                 this.WindowSizeToContent = SizeToContent.Manual;
58
59                 this.Owner = owner;
60                 this.Context = context;
61                 this.Title = title;
62                 ContentPresenter contentPresenter = new ContentPresenter()
63                 {
64                     Content = propertyValue,
65                     //get default editor template for content presenter 
66                     ContentTemplate = dialogTemplate
67                 };
68
69                 this.Content = contentPresenter;
70                 this.Loaded += OnWindowLoaded;
71             }
72
73             void OnWindowLoaded(object sender, RoutedEventArgs args)
74             {
75                 ContentPresenter presenter = (ContentPresenter)this.Content;
76                 PropertyValue propertyValue = (PropertyValue)presenter.Content;
77                 Button okButton = (Button)this.FindName("okButton");
78                 ExpressionTextBox etb = VisualTreeUtils.GetNamedChild<ExpressionTextBox>(presenter, "PART_expressionTextBox");
79                 TextBlock hint = VisualTreeUtils.GetNamedChild<TextBlock>(presenter, "PART_hintText");
80                 Fx.Assert(etb != null, "ExpressionTextBox with name 'PART_expressionTextBox' should be in the template!");
81                 Fx.Assert(hint != null, "Hint TextBlock with name 'PART_hintText' should be in the template!");
82                 //bind button with ETB's commit command
83                 okButton.Command = DesignerView.CommitCommand;
84                 okButton.CommandTarget = etb;
85                 etb.Loaded += new RoutedEventHandler(OnExpressionTextBoxLoaded);
86
87                 if (null != etb && null != hint)
88                 {
89                     IValueConverter typeToStringConverter = (IValueConverter)EditorResources.GetResources()["TypeParameterConverter"];
90                     string hintFormatString = (string)EditorResources.GetResources()["dialogExpressionEditorHintFormatString"];
91
92                     //convert expression's container type to friendly name (i.e. replace generic '1 with <T>)                
93                     string friendlyTypeName = (string)
94                         typeToStringConverter.Convert(etb.ExpressionType ?? propertyValue.ParentProperty.PropertyType, typeof(string), null, CultureInfo.CurrentCulture);
95
96                     //format editor title to include friendly type name and property name
97                     hint.Text = string.Format(CultureInfo.CurrentCulture, hintFormatString, propertyValue.ParentProperty.PropertyName, friendlyTypeName);
98                 }
99             }
100
101             void OnExpressionTextBoxLoaded(object sender, RoutedEventArgs e)
102             {
103                 (sender as ExpressionTextBox).BeginEdit();
104             }
105         }
106     }
107 }