PageRenderTime 25ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/Blocks/Configuration/Src/Design/Controls/Toolkit/Calendar/Microsoft/Windows/Automation/Peers/CalendarButtonAutomationPeer.cs

#
C# | 292 lines | 204 code | 40 blank | 48 comment | 18 complexity | 288b3870c53db11b939f9382efbb428c MD5 | raw file
  1. //===============================================================================
  2. // Microsoft patterns & practices Enterprise Library
  3. // Core
  4. //===============================================================================
  5. // Copyright Š Microsoft Corporation. All rights reserved.
  6. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
  7. // OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
  8. // LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  9. // FITNESS FOR A PARTICULAR PURPOSE.
  10. //===============================================================================
  11. //---------------------------------------------------------------------------
  12. //
  13. // Copyright (C) Microsoft Corporation. All rights reserved.
  14. //
  15. //---------------------------------------------------------------------------
  16. using System;
  17. using System.Windows.Automation;
  18. using System.Windows.Automation.Peers;
  19. using System.Windows.Automation.Provider;
  20. using System.Windows.Controls;
  21. using System.Windows.Input;
  22. using Microsoft.Practices.EnterpriseLibrary.Configuration.Design.Controls.Toolkit.Windows.Controls.Primitives;
  23. using Microsoft.Practices.EnterpriseLibrary.Configuration.Design.Controls.Toolkit.Windows.Controls;
  24. using Calendar = Microsoft.Practices.EnterpriseLibrary.Configuration.Design.Controls.Toolkit.Windows.Controls.Calendar;
  25. using CalendarMode = Microsoft.Practices.EnterpriseLibrary.Configuration.Design.Controls.Toolkit.Windows.Controls.CalendarMode;
  26. using CalendarSelectionMode = Microsoft.Practices.EnterpriseLibrary.Configuration.Design.Controls.Toolkit.Windows.Controls.CalendarSelectionMode;
  27. namespace Microsoft.Practices.EnterpriseLibrary.Configuration.Design.Controls.Toolkit.Windows.Automation.Peers
  28. {
  29. /// <summary>
  30. /// AutomationPeer for CalendarButton
  31. /// </summary>
  32. public sealed class CalendarButtonAutomationPeer : ButtonAutomationPeer, IGridItemProvider, ISelectionItemProvider
  33. {
  34. /// <summary>
  35. /// Initializes a new instance of the CalendarButtonAutomationPeer class.
  36. /// </summary>
  37. /// <param name="owner">Owning CalendarButton</param>
  38. public CalendarButtonAutomationPeer(CalendarButton owner)
  39. : base(owner)
  40. {
  41. }
  42. #region Private Properties
  43. private Calendar OwningCalendar
  44. {
  45. get
  46. {
  47. return this.OwningCalendarButton.Owner;
  48. }
  49. }
  50. private IRawElementProviderSimple OwningCalendarAutomationPeer
  51. {
  52. get
  53. {
  54. if (this.OwningCalendar != null)
  55. {
  56. AutomationPeer peer = CreatePeerForElement(this.OwningCalendar);
  57. if (peer != null)
  58. {
  59. return ProviderFromPeer(peer);
  60. }
  61. }
  62. return null;
  63. }
  64. }
  65. private CalendarButton OwningCalendarButton
  66. {
  67. get
  68. {
  69. return this.Owner as CalendarButton;
  70. }
  71. }
  72. private DateTime? Date
  73. {
  74. get
  75. {
  76. if (this.OwningCalendarButton != null && this.OwningCalendarButton.DataContext is DateTime)
  77. {
  78. return (DateTime?)this.OwningCalendarButton.DataContext;
  79. }
  80. else
  81. {
  82. return null;
  83. }
  84. }
  85. }
  86. #endregion Private Properties
  87. #region Public Methods
  88. /// <summary>
  89. /// Gets the control pattern that is associated with the specified System.Windows.Automation.Peers.PatternInterface.
  90. /// </summary>
  91. /// <param name="patternInterface">A value from the System.Windows.Automation.Peers.PatternInterface enumeration.</param>
  92. /// <returns>The object that supports the specified pattern, or null if unsupported.</returns>
  93. public override object GetPattern(PatternInterface patternInterface)
  94. {
  95. object result = null;
  96. switch (patternInterface)
  97. {
  98. case PatternInterface.SelectionItem:
  99. case PatternInterface.GridItem:
  100. {
  101. if (this.OwningCalendar != null && this.OwningCalendar.MonthControl != null && this.OwningCalendarButton != null)
  102. {
  103. result = this;
  104. }
  105. else
  106. {
  107. result = base.GetPattern(patternInterface);
  108. }
  109. break;
  110. }
  111. default:
  112. {
  113. result = base.GetPattern(patternInterface);
  114. break;
  115. }
  116. }
  117. return result;
  118. }
  119. #endregion Public methods
  120. #region Protected Methods
  121. /// <summary>
  122. /// Gets the control type for the element that is associated with the UI Automation peer.
  123. /// </summary>
  124. /// <returns>The control type.</returns>
  125. protected override AutomationControlType GetAutomationControlTypeCore()
  126. {
  127. return AutomationControlType.Button;
  128. }
  129. /// <summary>
  130. /// Called by GetClassName that gets a human readable name that, in addition to AutomationControlType,
  131. /// differentiates the control represented by this AutomationPeer.
  132. /// </summary>
  133. /// <returns>The string that contains the name.</returns>
  134. protected override string GetClassNameCore()
  135. {
  136. return Owner.GetType().Name;
  137. }
  138. /// <summary>
  139. /// Overrides the GetLocalizedControlTypeCore method for CalendarButtonAutomationPeer
  140. /// </summary>
  141. /// <returns></returns>
  142. protected override string GetLocalizedControlTypeCore()
  143. {
  144. return SR.Get(SRID.CalendarAutomationPeer_CalendarButtonLocalizedControlType);
  145. }
  146. /// <summary>
  147. /// Overrides the GetHelpTextCore method for CalendarButtonAutomationPeer
  148. /// </summary>
  149. /// <returns></returns>
  150. protected override string GetHelpTextCore()
  151. {
  152. DateTime? date = this.Date;
  153. return date.HasValue ? DateTimeHelper.ToLongDateString(date, DateTimeHelper.GetCulture(this.OwningCalendarButton)) : base.GetHelpTextCore();
  154. }
  155. /// <summary>
  156. /// Overrides the GetNameCore method for CalendarButtonAutomationPeer
  157. /// </summary>
  158. /// <returns></returns>
  159. protected override string GetNameCore()
  160. {
  161. DateTime? date = this.Date;
  162. if (date.HasValue)
  163. {
  164. if (this.OwningCalendar.DisplayMode == CalendarMode.Decade)
  165. {
  166. return DateTimeHelper.ToYearString(date, DateTimeHelper.GetCulture(this.OwningCalendarButton));
  167. }
  168. else
  169. {
  170. return DateTimeHelper.ToYearMonthPatternString(date, DateTimeHelper.GetCulture(this.OwningCalendarButton));
  171. }
  172. }
  173. else
  174. {
  175. return base.GetNameCore();
  176. }
  177. }
  178. #endregion Protected Methods
  179. #region IGridItemProvider
  180. int IGridItemProvider.Column
  181. {
  182. get
  183. {
  184. return (int)this.OwningCalendarButton.GetValue(Grid.ColumnProperty);
  185. }
  186. }
  187. int IGridItemProvider.ColumnSpan
  188. {
  189. get
  190. {
  191. return (int)this.OwningCalendarButton.GetValue(Grid.ColumnSpanProperty);
  192. }
  193. }
  194. IRawElementProviderSimple IGridItemProvider.ContainingGrid
  195. {
  196. get
  197. {
  198. return this.OwningCalendarAutomationPeer;
  199. }
  200. }
  201. int IGridItemProvider.Row
  202. {
  203. get
  204. {
  205. return (int)this.OwningCalendarButton.GetValue(Grid.RowSpanProperty);
  206. }
  207. }
  208. int IGridItemProvider.RowSpan
  209. {
  210. get
  211. {
  212. return 1;
  213. }
  214. }
  215. #endregion IGridItemProvider
  216. #region ISelectionItemProvider
  217. bool ISelectionItemProvider.IsSelected
  218. {
  219. get
  220. {
  221. return this.OwningCalendarButton.IsFocused;
  222. }
  223. }
  224. IRawElementProviderSimple ISelectionItemProvider.SelectionContainer
  225. {
  226. get
  227. {
  228. return this.OwningCalendarAutomationPeer;
  229. }
  230. }
  231. void ISelectionItemProvider.AddToSelection()
  232. {
  233. return;
  234. }
  235. void ISelectionItemProvider.RemoveFromSelection()
  236. {
  237. return;
  238. }
  239. void ISelectionItemProvider.Select()
  240. {
  241. if (this.OwningCalendarButton.IsEnabled)
  242. {
  243. this.OwningCalendarButton.MoveFocus(new TraversalRequest(FocusNavigationDirection.First));
  244. }
  245. else
  246. {
  247. throw new ElementNotEnabledException();
  248. }
  249. }
  250. #endregion ISelectionItemProvider
  251. }
  252. }