PageRenderTime 119ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/mcs/class/referencesource/System.Activities.Core.Presentation/System/ServiceModel/Activities/Presentation/ContentCorrelationTypeExpander.xaml.cs

https://github.com/pruiz/mono
C# | 315 lines | 268 code | 38 blank | 9 comment | 41 complexity | d837448e2fd169b9f4f9f30f7c25cd3d MD5 | raw file
Possible License(s): LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. //----------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //----------------------------------------------------------------
  4. namespace System.ServiceModel.Activities.Presentation
  5. {
  6. using System.Activities.Presentation;
  7. using System.Collections.Generic;
  8. using System.Reflection;
  9. using System.Runtime.Serialization;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Input;
  13. using System.Windows.Threading;
  14. using System.Xml;
  15. using System.Linq;
  16. using System.Collections;
  17. using System.Xml.Linq;
  18. partial class ContentCorrelationTypeExpander
  19. {
  20. static readonly DependencyPropertyKey IsSelectionValidPropertyKey = DependencyProperty.RegisterReadOnly(
  21. "IsSelectionValid",
  22. typeof(bool),
  23. typeof(ContentCorrelationTypeExpander),
  24. new UIPropertyMetadata(false, OnIsSelectionValidChanged));
  25. public static readonly DependencyProperty IsSelectionValidProperty = IsSelectionValidPropertyKey.DependencyProperty;
  26. public static readonly RoutedEvent IsSelectionValidChangedEvent = EventManager.RegisterRoutedEvent(
  27. "IsSelectionValidChanged",
  28. RoutingStrategy.Bubble,
  29. typeof(RoutedEventHandler),
  30. typeof(ContentCorrelationTypeExpander));
  31. public static readonly RoutedEvent SelectionChangedEvent = EventManager.RegisterRoutedEvent(
  32. "SelectionChanged",
  33. RoutingStrategy.Bubble,
  34. typeof(RoutedEventHandler),
  35. typeof(ContentCorrelationTypeExpander));
  36. public static readonly DependencyProperty TypesToExpandProperty = DependencyProperty.Register(
  37. "TypesToExpand",
  38. typeof(IList<ExpanderTypeEntry>),
  39. typeof(ContentCorrelationTypeExpander),
  40. new UIPropertyMetadata(null, OnTypesToExpandChanged));
  41. static readonly DependencyPropertyKey SelectedTypeEntryPropertyKey = DependencyProperty.RegisterReadOnly(
  42. "SelectedTypeEntry",
  43. typeof(ExpanderTypeEntry),
  44. typeof(ContentCorrelationTypeExpander),
  45. new UIPropertyMetadata(null));
  46. public static readonly DependencyProperty SelectedTypeEntryProperty = SelectedTypeEntryPropertyKey.DependencyProperty;
  47. static readonly Type[] PrimitiveTypesInXPath = new Type[]
  48. {
  49. typeof(DateTime),
  50. typeof(TimeSpan),
  51. typeof(XmlQualifiedName),
  52. typeof(Uri),
  53. typeof(Guid),
  54. typeof(XmlElement),
  55. typeof(string),
  56. typeof(object),
  57. typeof(Decimal),
  58. typeof(XElement),
  59. };
  60. MemberInfo[] path = null;
  61. Type selectedType = null;
  62. public ContentCorrelationTypeExpander()
  63. {
  64. InitializeComponent();
  65. }
  66. public event RoutedEventHandler IsSelectionValidChanged
  67. {
  68. add
  69. {
  70. AddHandler(IsSelectionValidChangedEvent, value);
  71. }
  72. remove
  73. {
  74. RemoveHandler(IsSelectionValidChangedEvent, value);
  75. }
  76. }
  77. public event RoutedEventHandler SelectionChanged
  78. {
  79. add
  80. {
  81. AddHandler(SelectionChangedEvent, value);
  82. }
  83. remove
  84. {
  85. RemoveHandler(SelectionChangedEvent, value);
  86. }
  87. }
  88. public bool IsSelectionValid
  89. {
  90. get { return (bool)GetValue(IsSelectionValidProperty); }
  91. private set { SetValue(IsSelectionValidPropertyKey, value); }
  92. }
  93. public IList<ExpanderTypeEntry> TypesToExpand
  94. {
  95. get { return (IList<ExpanderTypeEntry>)GetValue(TypesToExpandProperty); }
  96. set { SetValue(TypesToExpandProperty, value); }
  97. }
  98. public ExpanderTypeEntry SelectedTypeEntry
  99. {
  100. get { return (ExpanderTypeEntry)GetValue(SelectedTypeEntryProperty); }
  101. private set { SetValue(SelectedTypeEntryPropertyKey, value); }
  102. }
  103. public Type GetSelectedType()
  104. {
  105. return this.selectedType;
  106. }
  107. public MemberInfo[] GetMemberPath()
  108. {
  109. return this.path;
  110. }
  111. void RaiseSelectionValidChanged()
  112. {
  113. this.RaiseEvent(new RoutedEventArgs(IsSelectionValidChangedEvent, this));
  114. }
  115. void OnTypesToExpandChanged()
  116. {
  117. this.typeExpander.ItemsSource = this.TypesToExpand;
  118. this.emptyContent.Visibility = null == this.TypesToExpand || 0 == this.TypesToExpand.Count ? Visibility.Visible : Visibility.Collapsed;
  119. }
  120. void OnTypeExpanderLoaded(object sender, RoutedEventArgs e)
  121. {
  122. this.typeExpander.Focus();
  123. }
  124. void OnTreeViewItemLoaded(object sender, RoutedEventArgs e)
  125. {
  126. var item = (TreeViewItem)sender;
  127. if (null != item.Header)
  128. {
  129. if (item.Header is ExpanderTypeEntry)
  130. {
  131. item.IsExpanded = true;
  132. }
  133. else if (item.Header is Type)
  134. {
  135. var type = (Type)item.Header;
  136. item.IsExpanded = true;
  137. }
  138. }
  139. }
  140. void OnTreeViewItemMouseAccept(object sender, MouseButtonEventArgs e)
  141. {
  142. var item = sender as TreeViewItem;
  143. if (null != item && item.Header is ExpanderTypeEntry)
  144. {
  145. this.SelectedTypeEntry = (ExpanderTypeEntry)item.Header;
  146. }
  147. if (null != item && item.IsSelected && item.IsSelectionActive)
  148. {
  149. this.Accept(item);
  150. e.Handled = true;
  151. }
  152. }
  153. void OnTreeViewItemKeyboardAccept(object sender, KeyEventArgs e)
  154. {
  155. var item = sender as TreeViewItem;
  156. if (null != item && item.Header is ExpanderTypeEntry)
  157. {
  158. this.SelectedTypeEntry = (ExpanderTypeEntry)item.Header;
  159. }
  160. if (null != item && item.IsSelected && item.IsSelectionActive && Key.Enter == e.Key && Keyboard.Modifiers == ModifierKeys.None)
  161. {
  162. this.Accept(item);
  163. e.Handled = true;
  164. }
  165. }
  166. void Accept(TreeViewItem item)
  167. {
  168. bool isType = item.Header is ExpanderTypeEntry;
  169. bool isMember = item.Header is MemberInfo;
  170. if (isMember)
  171. {
  172. var members = new List<MemberInfo>(1);
  173. while (null != item && item.Header is MemberInfo)
  174. {
  175. var member = (MemberInfo)item.Header;
  176. members.Insert(0, member);
  177. if (item.Tag is TreeViewItem)
  178. {
  179. item = (TreeViewItem)item.Tag;
  180. }
  181. else
  182. {
  183. item = null;
  184. }
  185. }
  186. this.SelectedTypeEntry = (ExpanderTypeEntry)item.Header;
  187. this.selectedType = this.SelectedTypeEntry.TypeToExpand;
  188. this.path = members.ToArray();
  189. }
  190. else if (isType)
  191. {
  192. this.SelectedTypeEntry = (ExpanderTypeEntry)item.Header;
  193. this.selectedType = this.SelectedTypeEntry.TypeToExpand;
  194. this.path = new MemberInfo[0];
  195. }
  196. else
  197. {
  198. this.SelectedTypeEntry = null;
  199. this.selectedType = null;
  200. this.path = null;
  201. }
  202. this.IsSelectionValid = isType || isMember;
  203. this.RaiseEvent(new RoutedEventArgs(SelectionChangedEvent, this));
  204. }
  205. //The following types are considered as primitives as far as XPath generation is concerned and shouldn't be expanded any more
  206. // 1. CLR built-in types
  207. // 2. Byte array, DateTime, TimeSpan, GUID, Uri, XmlQualifiedName, XmlElement and XmlNode array [This includes XElement and XNode array from .NET 3.5]
  208. // 3. Enums
  209. // 4. Arrays and Collection classes including List<T>, Dictionary<K,V> and Hashtable (Anything that implements IEnumerable or IDictionary or is an array is treated as a collection).
  210. // 5. Type has [CollectionDataContract] attribute
  211. internal static bool IsPrimitiveTypeInXPath(Type type)
  212. {
  213. return ((type.IsPrimitive) || type.IsEnum || PrimitiveTypesInXPath.Any((item => item == type))
  214. || (typeof(IEnumerable).IsAssignableFrom(type)) || typeof(IDictionary).IsAssignableFrom(type) || type.IsArray
  215. || (type.GetCustomAttributes(typeof(CollectionDataContractAttribute), false).Length > 0));
  216. }
  217. static void OnIsSelectionValidChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
  218. {
  219. ((ContentCorrelationTypeExpander)sender).RaiseSelectionValidChanged();
  220. }
  221. static void OnTypesToExpandChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
  222. {
  223. var control = (ContentCorrelationTypeExpander)sender;
  224. control.Dispatcher.BeginInvoke(new Action(() => { control.OnTypesToExpandChanged(); }), DispatcherPriority.Render);
  225. }
  226. }
  227. internal sealed class TypeEntryContainer
  228. {
  229. public string DisplayText { get; set; }
  230. public IList<ExpanderTypeEntry> Items { get; set; }
  231. public override string ToString()
  232. {
  233. return this.DisplayText ?? base.ToString();
  234. }
  235. }
  236. internal sealed class ExpanderTypeEntry : DependencyObject
  237. {
  238. public static readonly DependencyProperty NameProperty = DependencyProperty.Register(
  239. "Name",
  240. typeof(string),
  241. typeof(ExpanderTypeEntry),
  242. new UIPropertyMetadata(string.Empty));
  243. public static readonly DependencyProperty TypeToExpandProperty = DependencyProperty.Register(
  244. "TypeToExpand",
  245. typeof(Type),
  246. typeof(ExpanderTypeEntry),
  247. new UIPropertyMetadata(null));
  248. public static readonly DependencyProperty TagProperty = DependencyProperty.Register(
  249. "Tag",
  250. typeof(object),
  251. typeof(ExpanderTypeEntry),
  252. new UIPropertyMetadata(null));
  253. public string Name
  254. {
  255. get { return (string)GetValue(NameProperty); }
  256. set { SetValue(NameProperty, value); }
  257. }
  258. public Type TypeToExpand
  259. {
  260. get { return (Type)GetValue(TypeToExpandProperty); }
  261. set { SetValue(TypeToExpandProperty, value); }
  262. }
  263. public Type[] TypeToExpandSource
  264. {
  265. get { return new Type[] { this.TypeToExpand }; }
  266. }
  267. public object Tag
  268. {
  269. get { return GetValue(TagProperty); }
  270. set { SetValue(TagProperty, value); }
  271. }
  272. public override string ToString()
  273. {
  274. return this.Name ?? "<null>";
  275. }
  276. }
  277. }