[amd64/tramp] hide interpreter specific trampoline behind ifdef
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / Base / Core / Internal / PropertyEditing / PropertyValueDialogControl.xaml.cs
1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //----------------------------------------------------------------
4 namespace System.Activities.Presentation.Internal.PropertyEditing 
5 {
6     using System;
7     using System.Diagnostics;
8     using System.Diagnostics.CodeAnalysis;
9     using System.Windows;
10     using System.Windows.Controls;
11     using System.Windows.Input;
12
13     using System.Runtime;
14     using System.Activities.Presentation.Model;
15     using System.Activities.Presentation.PropertyEditing;
16
17     using System.Activities.Presentation.Internal.PropertyEditing.Editors;
18     using System.Activities.Presentation.Internal.PropertyEditing.Model;
19     using System.Activities.Presentation.Internal.PropertyEditing.Resources;
20     using System.Activities.Presentation.Internal.PropertyEditing.FromExpression.Framework.ValueEditors;
21
22     // <summary>
23     // Root WPF control used for DialogPropertyValueEditors.  It contains a place for the
24     // editor and adds standard OK / Cancel buttons.  It's also responsible for handling
25     // PropertyValueEditorCommands and opening and closing the root transaction that
26     // responds to the OK / Cancel buttons.
27     // </summary>
28     internal partial class PropertyValueDialogControl 
29     {
30
31         private ModelEditingScope _rootTransaction;
32         private PropertyValueEditorCommandHandler _defaultCommandHandler;
33
34         // <summary>
35         // Basic ctor
36         // </summary>
37         // <param name="property">Property to display</param>
38         // <param name="valueDialogTemplate">Template to use</param>
39         public PropertyValueDialogControl(PropertyEntry property, DataTemplate valueDialogTemplate) 
40         {
41             Fx.Assert(property != null, "property parameter is null");
42             Fx.Assert(valueDialogTemplate != null, "valueDialogTemplate parameter is null");
43
44             ModelPropertyEntry modelPropertyValue = property as ModelPropertyEntry;
45             if (modelPropertyValue != null) 
46             {
47                 _rootTransaction = modelPropertyValue.FirstModelProperty.Value.BeginEdit();
48             }
49
50             InitializeComponent();
51
52             // Make sure we use PI-specific resources within this control
53             this.Resources.MergedDictionaries.Add(PropertyInspectorResources.GetResources());
54
55             // Hook into an opening of nested dialogs.  PropertyInspector class takes care of this for us.
56             // However, we are using Blend's collection editor which doesn't do the same thing, so
57             // we need to reproduce that behavior manually.
58             PropertyValueDialogHost.AttachOpenDialogHandlers(this);
59
60             // Hook into the standard set of Commands
61             _defaultCommandHandler = new PropertyValueEditorCommandHandler(this);
62
63
64             _OKButton.Click += new RoutedEventHandler(OnOkButtonClicked);
65             _cancelButton.Click += new RoutedEventHandler(OnCancelButtonClicked);
66             _contentControl.Content = property.PropertyValue;
67             _contentControl.ContentTemplate = valueDialogTemplate;
68
69             //Handle the commit and cancel keys within the property inspector, that is hosted in the collection editor
70             ValueEditorUtils.SetHandlesCommitKeys(this, true);
71
72         }
73
74         // Internal event we use for this control to signal that the uber-dialog (which is actually
75         // a Form) should be closed.  This event is raised whenever the OK / Cancel buttons are
76         // pressed
77         internal event EventHandler CloseParentDialog;
78
79         [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Propagating the error might cause VS to crash")]
80         [SuppressMessage("Reliability", "Reliability108", Justification = "Propagating the error might cause VS to crash")]
81         private void OnOkButtonClicked(object sender, RoutedEventArgs e) 
82         {
83             try 
84             {
85                 CommitDanglingTransactions();
86                 CommitRootTransaction();
87                 SignalCloseParentDialog();
88             }
89             catch (Exception ex) 
90             {
91                 ErrorReporting.ShowErrorMessage(ex.Message);
92             }
93         }
94
95         private void OnCancelButtonClicked(object sender, RoutedEventArgs e) 
96         {
97             AbortDanglingTransactions();
98             AbortRootTransaction();
99             SignalCloseParentDialog();
100         }
101
102
103         private void CommitDanglingTransactions() 
104         {
105             _defaultCommandHandler.CommitOpenTransactions();
106         }
107
108         private void AbortDanglingTransactions() 
109         {
110             _defaultCommandHandler.AbortOpenTransactions();
111         }
112
113         private void SignalCloseParentDialog() 
114         {
115             if (CloseParentDialog != null)
116             {
117                 CloseParentDialog(this, EventArgs.Empty);
118             }
119         }
120
121         // Called when the parent dialog gets closed.  If this happened without us clicking on
122         // the OK or Cancel button, the root transaction will still exist and we need to manually
123         // abort it.
124         public void OnParentDialogClosing() 
125         {
126             AbortRootTransaction();
127         }
128
129         private void CommitRootTransaction() 
130         {
131             if (_rootTransaction == null)
132             {
133                 return;
134             }
135
136             _rootTransaction.Complete();
137             _rootTransaction = null;
138         }
139
140         private void AbortRootTransaction() 
141         {
142             if (_rootTransaction == null)
143             {
144                 return;
145             }
146
147             _rootTransaction.Revert();
148             _rootTransaction = null;
149         }
150     }
151 }