[corlib] Avoid unnecessary ephemeron array resizes
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / View / ActivityTypeDesigner.xaml.cs
1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //----------------------------------------------------------------
4
5 namespace System.Activities.Presentation.View
6 {
7     using System.Activities.Presentation.Model;
8     using System.Activities.Presentation.Services;
9     using System.Activities.Presentation.Toolbox;
10     using System.Activities.Presentation.Xaml;
11     using System.Linq;
12     using System.Windows;
13     using System.Windows.Controls;
14     using System.Windows.Input;
15     using System.Windows.Threading;
16
17     partial class ActivityTypeDesigner : IExpandChild
18     {
19         public ActivityTypeDesigner()
20         {
21             this.InitializeComponent();
22             this.DragHandle = null;
23         }
24
25         protected override void OnModelItemChanged(object newItem)
26         {
27             base.OnModelItemChanged(newItem);
28             if (this.Context.Services.GetService<DisplayNameUpdater>() == null)
29             {
30                 this.Context.Services.Publish<DisplayNameUpdater>(new DisplayNameUpdater(this.Context));
31             }
32         }
33
34         protected override void OnContextMenuLoaded(ContextMenu menu)
35         {
36             base.OnContextMenuLoaded(menu);
37             if (null == this.ModelItem.Properties["Implementation"].Value)
38             {
39                 var toHide = menu.Items.OfType<MenuItem>().Where(p =>
40                     p.Command == DesignerView.GoToParentCommand ||
41                     p.Command == DesignerView.ExpandCommand);
42
43                 foreach (var item in toHide)
44                 {
45                     item.Visibility = Visibility.Collapsed;
46                 }
47             }
48         }
49
50         void OnAddAnnotationCommandCanExecute(object sender, CanExecuteRoutedEventArgs e)
51         {
52             if (EditingContextUtilities.GetSingleSelectedModelItem(this.Context) == this.ModelItem)
53             {
54                 e.CanExecute = false;
55                 e.Handled = true;
56             }
57         }
58
59         void OnEditAnnotationCommandCanExecute(object sender, CanExecuteRoutedEventArgs e)
60         {
61             if (EditingContextUtilities.GetSingleSelectedModelItem(this.Context) == this.ModelItem)
62             {
63                 e.CanExecute = false;
64                 e.Handled = true;
65             }
66         }
67
68         void OnDeleteAnnotationCommandCanExecute(object sender, CanExecuteRoutedEventArgs e)
69         {
70             if (EditingContextUtilities.GetSingleSelectedModelItem(this.Context) == this.ModelItem)
71             {
72                 e.CanExecute = false;
73                 e.Handled = true;
74             }
75         }
76
77         // the job of this service is to add an attached property on ActivityBuilder called displayName.
78         // this name will be shown in breadcrumb bar.
79         // also whenever the Name property changes
80         // we want to refresh the DisplayName property too ( by calling displayNameProperty.NotifyPropertyChanged())
81         class DisplayNameUpdater
82         {
83             AttachedProperty<string> activityBuilderDisplayNameProperty;
84             AttachedProperty<string> activityTemplateFactoryBuilderDisplayNameProperty;
85
86             public DisplayNameUpdater(EditingContext context)
87             {
88                 activityBuilderDisplayNameProperty = new AttachedProperty<string>
89                 {
90                     Name = "DisplayName",
91                     OwnerType = typeof(ActivityBuilder),
92                     Getter = (modelItem) => ViewUtilities.GetActivityBuilderDisplayName(modelItem)
93                 };
94                 activityTemplateFactoryBuilderDisplayNameProperty = new AttachedProperty<string>
95                 {
96                     Name = "DisplayName",
97                     OwnerType = typeof(ActivityTemplateFactoryBuilder),
98                     Getter = (modelItem) => ViewUtilities.GetActivityBuilderDisplayName(modelItem)
99                 };
100                 AttachedPropertiesService attachedPropertiesService = context.Services.GetService<AttachedPropertiesService>();
101                 attachedPropertiesService.AddProperty(activityBuilderDisplayNameProperty);
102                 attachedPropertiesService.AddProperty(activityTemplateFactoryBuilderDisplayNameProperty);
103                 context.Services.GetService<ModelService>().ModelChanged += new EventHandler<ModelChangedEventArgs>(ModelChanged);
104             }
105
106             void ModelChanged(object sender, ModelChangedEventArgs e)
107             {
108                 ModelChangeInfo changeInfo = e.ModelChangeInfo;
109
110                 if (changeInfo != null && changeInfo.ModelChangeType == ModelChangeType.PropertyChanged)
111                 {
112                     Type propertyType = changeInfo.Subject.ItemType;
113                     if (changeInfo.PropertyName == "Name")
114                     {
115                         if (propertyType.Equals(typeof(ActivityBuilder)))
116                         {
117                             activityBuilderDisplayNameProperty.NotifyPropertyChanged(changeInfo.Subject);
118                         }
119                         else if (propertyType.Equals(typeof(ActivityTemplateFactoryBuilder)))
120                         {
121                             activityTemplateFactoryBuilderDisplayNameProperty.NotifyPropertyChanged(changeInfo.Subject);
122                         }
123                     }
124                 }
125             }
126         }
127
128
129         public ModelItem ExpandedChild
130         {
131             get
132             {
133                 ModelItem modelItemToSelect = null;
134                 if (this.ModelItem != null)
135                 {
136                     modelItemToSelect = this.ModelItem.Properties["Implementation"].Value;
137                 }
138                 return modelItemToSelect;
139             }
140         }
141
142     }
143 }