[reflection] Coop handles icalls in System.Reflection and System.RuntimeTypeHandle...
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / Base / Core / Internal / PropertyEditing / PropertyValueEditorCommandHandler.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.Collections.Generic;
8     using System.Diagnostics;
9     using System.Windows;
10     using System.Windows.Input;
11
12     using System.Activities.Presentation;
13     using System.Runtime;
14     using System.Activities.Presentation.Model;
15     using System.Activities.Presentation.PropertyEditing;
16
17     using System.Activities.Presentation.Internal.PropertyEditing.Model;
18
19     // <summary>
20     // Container for standard PropertyValueEditorCommand handlers.  These handlers need to do their
21     // work both for the PropertyInspector control as well as for any standard WPF dialog editor
22     // launched from the PropertyInspector itself.  As such, the functionality is extracted here
23     // to be shared in both cases.
24     // </summary>
25     internal class PropertyValueEditorCommandHandler : IDisposable 
26     {
27
28         private UIElement _commandHost;
29         private List<CommandBinding> _activeCommandBindings = new List<CommandBinding>();
30         private List<ModelEditingScope> _pendingTransactions = new List<ModelEditingScope>();
31
32         public PropertyValueEditorCommandHandler(UIElement commandHost) 
33         {
34             Fx.Assert(commandHost != null, "commandHost parameter is null");
35             _commandHost = commandHost;
36
37             AddEditModeCommandBindings();
38             AddTransactionCommandBindings();
39
40             foreach (CommandBinding binding in _activeCommandBindings)
41             {
42                 _commandHost.CommandBindings.Add(binding);
43             }
44         }
45
46         // <summary>
47         // Cleans up by removing all CommandBindings added by this class from the commandHost
48         // </summary>
49         public void Dispose() 
50         {
51             if (_commandHost != null && _activeCommandBindings.Count > 0) 
52             {
53                 foreach (CommandBinding binding in _activeCommandBindings) 
54                 {
55                     _commandHost.CommandBindings.Remove(binding);
56                 }
57                 _activeCommandBindings.Clear();
58             }
59         }
60
61         // <summary>
62         // Commits any pending and open transactions in the correct order
63         // </summary>
64         public void CommitOpenTransactions() 
65         {
66             for (int i = _pendingTransactions.Count - 1; i >= 0; i--) 
67             {
68                 _pendingTransactions[i].Complete();
69                 _pendingTransactions.RemoveAt(i);
70             }
71         }
72
73         // <summary>
74         // Aborts any pending and open transactions in the correct order
75         // </summary>
76         public void AbortOpenTransactions() 
77         {
78             for (int i = _pendingTransactions.Count - 1; i >= 0; i--) 
79             {
80                 _pendingTransactions[i].Revert();
81                 _pendingTransactions.RemoveAt(i);
82             }
83         }
84
85         // ActiveEditMode Switch Handlers
86
87         // Adds handlers for ShowInlineEditor, ShowExtendedPopupEditor, ShowExtendedPinnedEditor,
88         // and ShowDialogEditor commands.  These handlers the ActiveEditMode of the closest PropertyContainer
89         // that contains the UIElement that invoked it
90         private void AddEditModeCommandBindings() 
91         {
92             _activeCommandBindings.Add(new CommandBinding(PropertyValueEditorCommands.ShowInlineEditor, OnShowInlineEditor));
93             _activeCommandBindings.Add(new CommandBinding(PropertyValueEditorCommands.ShowExtendedPopupEditor, OnShowExtendedPopupEditor));
94             _activeCommandBindings.Add(new CommandBinding(PropertyValueEditorCommands.ShowExtendedPinnedEditor, OnShowExtendedPinnedEditor));
95             _activeCommandBindings.Add(new CommandBinding(PropertyValueEditorCommands.ShowDialogEditor, OnShowDialogEditor));
96         }
97
98         private void OnShowInlineEditor(object sender, ExecutedRoutedEventArgs e) 
99         {
100             SwitchActiveEditMode(e, PropertyContainerEditMode.Inline);
101         }
102
103         private void OnShowExtendedPopupEditor(object sender, ExecutedRoutedEventArgs e) 
104         {
105             SwitchActiveEditMode(e, PropertyContainerEditMode.ExtendedPopup);
106         }
107
108         private void OnShowExtendedPinnedEditor(object sender, ExecutedRoutedEventArgs e) 
109         {
110             SwitchActiveEditMode(e, PropertyContainerEditMode.ExtendedPinned);
111         }
112
113         private void OnShowDialogEditor(object sender, ExecutedRoutedEventArgs e) 
114         {
115             SwitchActiveEditMode(e, PropertyContainerEditMode.Dialog);
116         }
117
118         private static void SwitchActiveEditMode(ExecutedRoutedEventArgs e, PropertyContainerEditMode newMode) 
119         {
120             PropertyContainer container = GetContainerFromEventArgs(e);
121             if (container == null)
122             {
123                 return;
124             }
125
126             container.ActiveEditMode = newMode;
127         }
128
129
130
131         // Transaction Handlers
132
133         // Adds command handlers for BeginTransaction, CommitTransaction, and AbortTransaction
134         // commands.  These handlers open, commit, or abort a transaction
135         private void AddTransactionCommandBindings() 
136         {
137             _activeCommandBindings.Add(new CommandBinding(PropertyValueEditorCommands.BeginTransaction, OnBeginTransaction));
138             _activeCommandBindings.Add(new CommandBinding(PropertyValueEditorCommands.CommitTransaction, OnCommitTransaction));
139             _activeCommandBindings.Add(new CommandBinding(PropertyValueEditorCommands.AbortTransaction, OnAbortTransaction));
140         }
141
142         private void OnBeginTransaction(object sender, ExecutedRoutedEventArgs e) 
143         {
144             ModelPropertyEntryBase property = GetContainedPropertyFromEventArgs(e);
145             if (property == null)
146             {
147                 return;
148             }
149
150             _pendingTransactions.Add(property.BeginEdit(e.Parameter as string));
151         }
152
153         private void OnCommitTransaction(object sender, ExecutedRoutedEventArgs e) 
154         {
155             if (_pendingTransactions.Count == 0)
156             {
157                 throw FxTrace.Exception.AsError(new InvalidOperationException(System.Activities.Presentation.Internal.Properties.Resources.PropertyEditing_ErrorCommit_NoTransactionsOpened));
158             }
159
160             try 
161             {
162                 _pendingTransactions[_pendingTransactions.Count - 1].Complete();
163             }
164             finally 
165             {
166                 _pendingTransactions.RemoveAt(_pendingTransactions.Count - 1);
167             }
168         }
169
170         private void OnAbortTransaction(object sender, ExecutedRoutedEventArgs e) 
171         {
172             if (_pendingTransactions.Count == 0)
173             {
174                 throw FxTrace.Exception.AsError(new InvalidOperationException(System.Activities.Presentation.Internal.Properties.Resources.PropertyEditing_ErrorAbort_NoTransactionsOpened));
175             }
176
177             try 
178             {
179                 _pendingTransactions[_pendingTransactions.Count - 1].Revert();
180             }
181             finally 
182             {
183                 _pendingTransactions.RemoveAt(_pendingTransactions.Count - 1);
184             }
185         }
186
187
188
189         // Static Helpers
190
191         private static ModelPropertyEntryBase GetContainedPropertyFromEventArgs(ExecutedRoutedEventArgs e) 
192         {
193             PropertyContainer container = GetContainerFromEventArgs(e);
194             if (container == null)
195             {
196                 return null;
197             }
198
199             return container.PropertyEntry as ModelPropertyEntryBase;
200         }
201
202         private static PropertyContainer GetContainerFromEventArgs(ExecutedRoutedEventArgs e) 
203         {
204             DependencyObject source = e.OriginalSource as DependencyObject;
205             if (source == null)
206             {
207                 return null;
208             }
209
210             return PropertyContainer.GetOwningPropertyContainer(source);
211         }
212
213
214     }
215 }