[reflection] Coop handles icalls in System.Reflection and System.RuntimeTypeHandle...
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / Model / ModelItemHelper.cs
1 //----------------------------------------------------------------
2 // <copyright company="Microsoft Corporation">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //----------------------------------------------------------------
6
7 namespace System.Activities.Presentation.Model
8 {
9     using System.Runtime;
10
11     internal static class ModelItemHelper
12     {
13         internal static ModelEditingScope ModelItemBeginEdit(ModelTreeManager modelTreeManager, string description, bool shouldApplyChangesImmediately)
14         {
15             if (shouldApplyChangesImmediately && modelTreeManager.Context.Services.GetService<UndoEngine>().IsBookmarkInPlace)
16             {
17                 throw FxTrace.Exception.AsError(new InvalidOperationException(SR.InvalidNestedModelItemBeginEditExceptionMessage));
18             }
19
20             EditingScope editingScope = modelTreeManager.CreateEditingScope(description, shouldApplyChangesImmediately);
21
22             if (shouldApplyChangesImmediately && editingScope == null)
23             {
24                 throw FxTrace.Exception.AsError(new InvalidOperationException(SR.InvalidNestedModelItemBeginEditExceptionMessage));
25             }
26
27             return editingScope;
28         }
29
30         internal static bool CanCreateImmediateEditingScope(ModelItem modelItem)
31         {
32             Fx.Assert(modelItem is IModelTreeItem, "modelItem must implement IModelTreeItem");
33
34             return ((IModelTreeItem)modelItem).ModelTreeManager.CanCreateImmediateEditingScope();
35         }
36
37         internal static void TryCreateImmediateEditingScopeAndExecute(EditingContext context, string editingScopeDescription, Action<EditingScope> modelEditingWork)
38         {
39             Fx.Assert(context != null, "context should not be null.");
40             Fx.Assert(modelEditingWork != null, "modelEditingWork should not be null.");
41
42             ModelTreeManager manager = context.Services.GetRequiredService<ModelTreeManager>();
43
44             if (manager.CanCreateImmediateEditingScope())
45             {
46                 using (EditingScope editingScope = manager.CreateEditingScope(editingScopeDescription, true))
47                 {
48                     modelEditingWork(editingScope);
49                 }
50             }
51             else
52             {
53                 modelEditingWork(null);
54             }
55         }
56
57         internal static EditingScope TryCreateImmediateEditingScope(ModelTreeManager manager, string editingScopeDescription)
58         {
59             if (manager.CanCreateImmediateEditingScope())
60             {
61                 return manager.CreateEditingScope(editingScopeDescription, true);
62             }
63
64             return null;
65         }
66     }
67 }