Update Reference Sources to .NET Framework 4.6.1
[mono.git] / mcs / class / referencesource / System.Activities.Core.Presentation / System / Activities / Core / Presentation / StateMachineDesigner.xaml.cs
1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //----------------------------------------------------------------
4
5 namespace System.Activities.Core.Presentation
6 {
7     using System.Activities.Presentation;
8     using System.Activities.Presentation.Metadata;
9     using System.Activities.Presentation.Model;
10     using System.Activities.Presentation.View;
11     using System.Activities.Presentation.View.OutlineView;
12     using System.Activities.Statements;
13     using System.ComponentModel;
14     using System.Windows;
15     using System.Windows.Input;
16
17     [ActivityDesignerOptions(AlwaysCollapseChildren = true)]
18     partial class StateMachineDesigner
19     {
20         const string ExpandViewStateKey = "IsExpanded";
21         internal const string InitialStatePropertyName = "InitialState";
22         internal const string VariablesPropertyName = "Variables";
23         internal const string StatesPropertyName = "States";
24
25         StateContainerEditor stateContainerEditor = null;
26
27         public StateMachineDesigner()
28         {
29             InitializeComponent();
30         }
31
32         internal bool IsResizing { get; set; }
33
34         internal StateContainerEditor StateContainerEditor
35         {
36             get { return this.stateContainerEditor; }
37         }
38
39         void OnStateContainerLoaded(object sender, RoutedEventArgs e)
40         {
41             this.stateContainerEditor = sender as StateContainerEditor;
42         }
43
44         void OnStateContainerUnloaded(object sender, RoutedEventArgs e)
45         {
46             this.stateContainerEditor = null;
47         }
48
49         public static void RegisterMetadata(AttributeTableBuilder builder)
50         {
51             Type stateMachineType = typeof(StateMachine);
52             builder.AddCustomAttributes(stateMachineType, new DesignerAttribute(typeof(StateMachineDesigner)));
53             builder.AddCustomAttributes(stateMachineType, stateMachineType.GetProperty(StateMachineDesigner.StatesPropertyName), BrowsableAttribute.No);
54             builder.AddCustomAttributes(stateMachineType, stateMachineType.GetProperty(StateMachineDesigner.VariablesPropertyName), BrowsableAttribute.No);
55             builder.AddCustomAttributes(stateMachineType, stateMachineType.GetProperty(StateMachineDesigner.InitialStatePropertyName), BrowsableAttribute.No);
56             builder.AddCustomAttributes(stateMachineType, stateMachineType.GetProperty(StateMachineDesigner.InitialStatePropertyName), new ShowPropertyInOutlineViewAttribute() { DuplicatedChildNodesVisible = true });
57             builder.AddCustomAttributes(stateMachineType, stateMachineType.GetProperty(StateMachineDesigner.StatesPropertyName), new ShowPropertyInOutlineViewAttribute());
58
59
60             builder.AddCustomAttributes(stateMachineType, new FeatureAttribute(typeof(StateMachineValidationErrorSourceLocatorFeature)));
61         }
62
63         protected override void OnModelItemChanged(object newItem)
64         {
65             ViewStateService viewStateService = this.Context.Services.GetService<ViewStateService>();
66             if (viewStateService != null)
67             {
68                 // Make StateMachine designer always collapsed by default, but only if the user didn't explicitly specify collapsed or expanded.
69                 bool? isExpanded = (bool?)viewStateService.RetrieveViewState((ModelItem)newItem, ExpandViewStateKey);
70                 if (isExpanded == null)
71                 {
72                     viewStateService.StoreViewState((ModelItem)newItem, ExpandViewStateKey, false);
73                 }
74             }
75             base.OnModelItemChanged(newItem);
76         }
77
78         // do not proprogate up to StateMachineDesigner, because designer will set selection to itself on GotFocus event.
79         private void OnAdornerLayerGotFocus(object sender, RoutedEventArgs e)
80         {
81             e.Handled = true;
82         }
83
84         private void StateMachineDesignerKeyDown(object sender, KeyEventArgs e)
85         {
86             // Ignore KeyBoard input when in resizing mode.
87             e.Handled = IsResizing;
88         }
89
90         private void StateMachineDesignerPreviewKeyDown(object sender, KeyEventArgs e)
91         {
92             // Enter cannot be captured in KeyDown, so handle it in PreviewKeyDown event.
93             e.Handled = IsResizing && e.Key == Key.Enter;
94         }
95     }
96  }