1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 //----------------------------------------------------------------
5 namespace System.Activities.Core.Presentation
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;
15 using System.Windows.Input;
17 [ActivityDesignerOptions(AlwaysCollapseChildren = true)]
18 partial class StateMachineDesigner
20 const string ExpandViewStateKey = "IsExpanded";
21 internal const string InitialStatePropertyName = "InitialState";
22 internal const string VariablesPropertyName = "Variables";
23 internal const string StatesPropertyName = "States";
25 StateContainerEditor stateContainerEditor = null;
27 public StateMachineDesigner()
29 InitializeComponent();
32 internal bool IsResizing { get; set; }
34 internal StateContainerEditor StateContainerEditor
36 get { return this.stateContainerEditor; }
39 void OnStateContainerLoaded(object sender, RoutedEventArgs e)
41 this.stateContainerEditor = sender as StateContainerEditor;
44 void OnStateContainerUnloaded(object sender, RoutedEventArgs e)
46 this.stateContainerEditor = null;
49 public static void RegisterMetadata(AttributeTableBuilder builder)
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());
60 builder.AddCustomAttributes(stateMachineType, new FeatureAttribute(typeof(StateMachineValidationErrorSourceLocatorFeature)));
63 protected override void OnModelItemChanged(object newItem)
65 ViewStateService viewStateService = this.Context.Services.GetService<ViewStateService>();
66 if (viewStateService != null)
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)
72 viewStateService.StoreViewState((ModelItem)newItem, ExpandViewStateKey, false);
75 base.OnModelItemChanged(newItem);
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)
84 private void StateMachineDesignerKeyDown(object sender, KeyEventArgs e)
86 // Ignore KeyBoard input when in resizing mode.
87 e.Handled = IsResizing;
90 private void StateMachineDesignerPreviewKeyDown(object sender, KeyEventArgs e)
92 // Enter cannot be captured in KeyDown, so handle it in PreviewKeyDown event.
93 e.Handled = IsResizing && e.Key == Key.Enter;