TimePicker.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Controls.Primitives;
  7. using System.Windows.Data;
  8. using System.Windows.Input;
  9. using System.Windows.Media;
  10. using System.Windows.Threading;
  11. using HandyControl.Data;
  12. using HandyControl.Interactivity;
  13. namespace HandyControl.Controls;
  14. /// <summary>
  15. /// 时间选择器
  16. /// </summary>
  17. [TemplatePart(Name = ElementRoot, Type = typeof(Grid))]
  18. [TemplatePart(Name = ElementTextBox, Type = typeof(WatermarkTextBox))]
  19. [TemplatePart(Name = ElementButton, Type = typeof(Button))]
  20. [TemplatePart(Name = ElementPopup, Type = typeof(Popup))]
  21. public class TimePicker : Control
  22. {
  23. #region Constants
  24. private const string ElementRoot = "PART_Root";
  25. private const string ElementTextBox = "PART_TextBox";
  26. private const string ElementButton = "PART_Button";
  27. private const string ElementPopup = "PART_Popup";
  28. #endregion Constants
  29. #region Data
  30. private string _defaultText;
  31. private ButtonBase _dropDownButton;
  32. private Popup _popup;
  33. private bool _disablePopupReopen;
  34. private WatermarkTextBox _textBox;
  35. private IDictionary<DependencyProperty, bool> _isHandlerSuspended;
  36. private DateTime? _originalSelectedTime;
  37. #endregion Data
  38. #region Public Events
  39. public static readonly RoutedEvent SelectedTimeChangedEvent =
  40. EventManager.RegisterRoutedEvent("SelectedTimeChanged", RoutingStrategy.Direct,
  41. typeof(EventHandler<FunctionEventArgs<DateTime?>>), typeof(TimePicker));
  42. public event EventHandler<FunctionEventArgs<DateTime?>> SelectedTimeChanged
  43. {
  44. add => AddHandler(SelectedTimeChangedEvent, value);
  45. remove => RemoveHandler(SelectedTimeChangedEvent, value);
  46. }
  47. public event RoutedEventHandler ClockClosed;
  48. public event RoutedEventHandler ClockOpened;
  49. #endregion Public Events
  50. static TimePicker()
  51. {
  52. EventManager.RegisterClassHandler(typeof(TimePicker), GotFocusEvent, new RoutedEventHandler(OnGotFocus));
  53. KeyboardNavigation.TabNavigationProperty.OverrideMetadata(typeof(TimePicker), new FrameworkPropertyMetadata(KeyboardNavigationMode.Once));
  54. KeyboardNavigation.IsTabStopProperty.OverrideMetadata(typeof(TimePicker), new FrameworkPropertyMetadata(ValueBoxes.FalseBox));
  55. }
  56. public TimePicker()
  57. {
  58. CommandBindings.Add(new CommandBinding(ControlCommands.Clear, (s, e) =>
  59. {
  60. SetCurrentValue(SelectedTimeProperty, null);
  61. SetCurrentValue(TextProperty, "");
  62. _textBox.Text = string.Empty;
  63. }));
  64. }
  65. #region Public Properties
  66. #region TimeFormat
  67. public static readonly DependencyProperty TimeFormatProperty = DependencyProperty.Register(
  68. nameof(TimeFormat), typeof(string), typeof(TimePicker), new PropertyMetadata("HH:mm:ss"));
  69. public string TimeFormat
  70. {
  71. get => (string) GetValue(TimeFormatProperty);
  72. set => SetValue(TimeFormatProperty, value);
  73. }
  74. #endregion TimeFormat
  75. #region DisplayTime
  76. public DateTime DisplayTime
  77. {
  78. get => (DateTime) GetValue(DisplayTimeProperty);
  79. set => SetValue(DisplayTimeProperty, value);
  80. }
  81. public static readonly DependencyProperty DisplayTimeProperty =
  82. DependencyProperty.Register(
  83. nameof(DisplayTime),
  84. typeof(DateTime),
  85. typeof(TimePicker),
  86. new FrameworkPropertyMetadata(DateTime.Now, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, null, CoerceDisplayTime));
  87. private static object CoerceDisplayTime(DependencyObject d, object value)
  88. {
  89. var dp = (TimePicker) d;
  90. dp.Clock.DisplayTime = (DateTime) value;
  91. return dp.Clock.DisplayTime;
  92. }
  93. #endregion DisplayTime
  94. #region IsDropDownOpen
  95. public bool IsDropDownOpen
  96. {
  97. get => (bool) GetValue(IsDropDownOpenProperty);
  98. set => SetValue(IsDropDownOpenProperty, ValueBoxes.BooleanBox(value));
  99. }
  100. public static readonly DependencyProperty IsDropDownOpenProperty =
  101. DependencyProperty.Register(
  102. nameof(IsDropDownOpen),
  103. typeof(bool),
  104. typeof(TimePicker),
  105. new FrameworkPropertyMetadata(ValueBoxes.FalseBox, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsDropDownOpenChanged, OnCoerceIsDropDownOpen));
  106. private static object OnCoerceIsDropDownOpen(DependencyObject d, object baseValue)
  107. {
  108. return d is TimePicker { IsEnabled: false }
  109. ? false
  110. : baseValue;
  111. }
  112. private static void OnIsDropDownOpenChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  113. {
  114. var dp = d as TimePicker;
  115. var newValue = (bool) e.NewValue;
  116. if (dp?._popup != null && dp._popup.IsOpen != newValue)
  117. {
  118. dp._popup.IsOpen = newValue;
  119. if (newValue)
  120. {
  121. dp._originalSelectedTime = dp.SelectedTime;
  122. dp.Dispatcher.BeginInvoke(DispatcherPriority.Input, (Action) delegate
  123. {
  124. dp.Clock.Focus();
  125. });
  126. }
  127. }
  128. }
  129. #endregion IsDropDownOpen
  130. #region SelectedTime
  131. public DateTime? SelectedTime
  132. {
  133. get => (DateTime?) GetValue(SelectedTimeProperty);
  134. set => SetValue(SelectedTimeProperty, value);
  135. }
  136. public static readonly DependencyProperty SelectedTimeProperty =
  137. DependencyProperty.Register(
  138. nameof(SelectedTime),
  139. typeof(DateTime?),
  140. typeof(TimePicker),
  141. new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnSelectedTimeChanged, CoerceSelectedTime));
  142. private static void OnSelectedTimeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  143. {
  144. if (d is not TimePicker dp) return;
  145. if (dp.SelectedTime.HasValue)
  146. {
  147. var time = dp.SelectedTime.Value;
  148. dp.SetTextInternal(dp.DateTimeToString(time));
  149. }
  150. dp.RaiseEvent(new FunctionEventArgs<DateTime?>(SelectedTimeChangedEvent, dp)
  151. {
  152. Info = dp.SelectedTime
  153. });
  154. }
  155. private static object CoerceSelectedTime(DependencyObject d, object value)
  156. {
  157. var dp = (TimePicker) d;
  158. if (dp.Clock is null)
  159. {
  160. return (DateTime?) value;
  161. }
  162. dp.Clock.SelectedTime = (DateTime?) value;
  163. return dp.Clock.SelectedTime;
  164. }
  165. #endregion SelectedDate
  166. #region Text
  167. public string Text
  168. {
  169. get => (string) GetValue(TextProperty);
  170. set => SetValue(TextProperty, value);
  171. }
  172. public static readonly DependencyProperty TextProperty =
  173. DependencyProperty.Register(
  174. nameof(Text),
  175. typeof(string),
  176. typeof(TimePicker),
  177. new FrameworkPropertyMetadata(string.Empty, OnTextChanged));
  178. private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  179. {
  180. if (d is TimePicker dp && !dp.IsHandlerSuspended(TextProperty))
  181. {
  182. if (e.NewValue is string newValue)
  183. {
  184. if (dp._textBox != null)
  185. {
  186. dp._textBox.Text = newValue;
  187. }
  188. else
  189. {
  190. dp._defaultText = newValue;
  191. }
  192. dp.SetSelectedTime();
  193. }
  194. else
  195. {
  196. dp.SetValueNoCallback(SelectedTimeProperty, null);
  197. }
  198. }
  199. }
  200. /// <summary>
  201. /// Sets the local Text property without breaking bindings
  202. /// </summary>
  203. /// <param name="value"></param>
  204. private void SetTextInternal(string value)
  205. {
  206. SetCurrentValue(TextProperty, value);
  207. }
  208. #endregion Text
  209. public static readonly DependencyProperty SelectionBrushProperty =
  210. TextBoxBase.SelectionBrushProperty.AddOwner(typeof(TimePicker));
  211. public Brush SelectionBrush
  212. {
  213. get => (Brush) GetValue(SelectionBrushProperty);
  214. set => SetValue(SelectionBrushProperty, value);
  215. }
  216. #if !(NET40 || NET45 || NET451 || NET452 || NET46 || NET461 || NET462 || NET47 || NET471 || NET472)
  217. public static readonly DependencyProperty SelectionTextBrushProperty =
  218. TextBoxBase.SelectionTextBrushProperty.AddOwner(typeof(TimePicker));
  219. public Brush SelectionTextBrush
  220. {
  221. get => (Brush) GetValue(SelectionTextBrushProperty);
  222. set => SetValue(SelectionTextBrushProperty, value);
  223. }
  224. #endif
  225. public static readonly DependencyProperty SelectionOpacityProperty =
  226. TextBoxBase.SelectionOpacityProperty.AddOwner(typeof(TimePicker));
  227. public double SelectionOpacity
  228. {
  229. get => (double) GetValue(SelectionOpacityProperty);
  230. set => SetValue(SelectionOpacityProperty, value);
  231. }
  232. public static readonly DependencyProperty CaretBrushProperty =
  233. TextBoxBase.CaretBrushProperty.AddOwner(typeof(TimePicker));
  234. public Brush CaretBrush
  235. {
  236. get => (Brush) GetValue(CaretBrushProperty);
  237. set => SetValue(CaretBrushProperty, value);
  238. }
  239. public static readonly DependencyProperty ClockProperty = DependencyProperty.Register(
  240. nameof(Clock), typeof(ClockBase), typeof(TimePicker), new FrameworkPropertyMetadata(default(Clock), FrameworkPropertyMetadataOptions.NotDataBindable, OnClockChanged));
  241. private static void OnClockChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  242. {
  243. var ctl = (TimePicker) d;
  244. if (e.OldValue is ClockBase oldClock)
  245. {
  246. oldClock.SelectedTimeChanged -= ctl.Clock_SelectedTimeChanged;
  247. oldClock.Confirmed -= ctl.Clock_Confirmed;
  248. ctl.SetPopupChild(null);
  249. }
  250. if (e.NewValue is ClockBase newClock)
  251. {
  252. newClock.ShowConfirmButton = true;
  253. newClock.SelectedTimeChanged += ctl.Clock_SelectedTimeChanged;
  254. newClock.Confirmed += ctl.Clock_Confirmed;
  255. ctl.SetPopupChild(newClock);
  256. }
  257. }
  258. public ClockBase Clock
  259. {
  260. get => (ClockBase) GetValue(ClockProperty);
  261. set => SetValue(ClockProperty, value);
  262. }
  263. #endregion Public Properties
  264. #region Public Methods
  265. public override void OnApplyTemplate()
  266. {
  267. if (DesignerProperties.GetIsInDesignMode(this)) return;
  268. if (_popup != null)
  269. {
  270. _popup.PreviewMouseLeftButtonDown -= PopupPreviewMouseLeftButtonDown;
  271. _popup.Opened -= PopupOpened;
  272. _popup.Closed -= PopupClosed;
  273. _popup.Child = null;
  274. }
  275. if (_dropDownButton != null)
  276. {
  277. _dropDownButton.Click -= DropDownButton_Click;
  278. _dropDownButton.MouseLeave -= DropDownButton_MouseLeave;
  279. }
  280. if (_textBox != null)
  281. {
  282. _textBox.KeyDown -= TextBox_KeyDown;
  283. _textBox.TextChanged -= TextBox_TextChanged;
  284. _textBox.LostFocus -= TextBox_LostFocus;
  285. }
  286. base.OnApplyTemplate();
  287. _popup = GetTemplateChild(ElementPopup) as Popup;
  288. _dropDownButton = GetTemplateChild(ElementButton) as Button;
  289. _textBox = GetTemplateChild(ElementTextBox) as WatermarkTextBox;
  290. CheckNull();
  291. _popup.PreviewMouseLeftButtonDown += PopupPreviewMouseLeftButtonDown;
  292. _popup.Opened += PopupOpened;
  293. _popup.Closed += PopupClosed;
  294. _popup.Child = Clock;
  295. _dropDownButton.Click += DropDownButton_Click;
  296. _dropDownButton.MouseLeave += DropDownButton_MouseLeave;
  297. var selectedTime = SelectedTime;
  298. if (_textBox != null)
  299. {
  300. _textBox.SetBinding(SelectionBrushProperty, new Binding(SelectionBrushProperty.Name) { Source = this });
  301. #if !(NET40 || NET45 || NET451 || NET452 || NET46 || NET461 || NET462 || NET47 || NET471 || NET472)
  302. _textBox.SetBinding(SelectionTextBrushProperty, new Binding(SelectionTextBrushProperty.Name) { Source = this });
  303. #endif
  304. _textBox.SetBinding(SelectionOpacityProperty, new Binding(SelectionOpacityProperty.Name) { Source = this });
  305. _textBox.SetBinding(CaretBrushProperty, new Binding(CaretBrushProperty.Name) { Source = this });
  306. _textBox.KeyDown += TextBox_KeyDown;
  307. _textBox.TextChanged += TextBox_TextChanged;
  308. _textBox.LostFocus += TextBox_LostFocus;
  309. if (selectedTime == null)
  310. {
  311. if (!string.IsNullOrEmpty(_defaultText))
  312. {
  313. _textBox.Text = _defaultText;
  314. SetSelectedTime();
  315. }
  316. }
  317. else
  318. {
  319. _textBox.Text = DateTimeToString(selectedTime.Value);
  320. }
  321. }
  322. EnsureClock();
  323. if (selectedTime is null)
  324. {
  325. _originalSelectedTime ??= DateTime.Now;
  326. SetCurrentValue(DisplayTimeProperty, _originalSelectedTime);
  327. }
  328. else
  329. {
  330. SetCurrentValue(DisplayTimeProperty, selectedTime);
  331. }
  332. }
  333. public override string ToString() => SelectedTime?.ToString(TimeFormat) ?? string.Empty;
  334. #endregion
  335. #region Protected Methods
  336. protected virtual void OnClockClosed(RoutedEventArgs e)
  337. {
  338. var handler = ClockClosed;
  339. handler?.Invoke(this, e);
  340. Clock?.OnClockClosed();
  341. }
  342. protected virtual void OnClockOpened(RoutedEventArgs e)
  343. {
  344. var handler = ClockOpened;
  345. handler?.Invoke(this, e);
  346. Clock?.OnClockOpened();
  347. }
  348. #endregion Protected Methods
  349. #region Private Methods
  350. private void CheckNull()
  351. {
  352. if (_dropDownButton == null || _popup == null || _textBox == null)
  353. throw new Exception();
  354. }
  355. private void TextBox_LostFocus(object sender, RoutedEventArgs e) => SetSelectedTime();
  356. private void SetIsHandlerSuspended(DependencyProperty property, bool value)
  357. {
  358. if (value)
  359. {
  360. _isHandlerSuspended ??= new Dictionary<DependencyProperty, bool>(2);
  361. _isHandlerSuspended[property] = true;
  362. }
  363. else
  364. {
  365. _isHandlerSuspended?.Remove(property);
  366. }
  367. }
  368. private void SetValueNoCallback(DependencyProperty property, object value)
  369. {
  370. SetIsHandlerSuspended(property, true);
  371. try
  372. {
  373. SetCurrentValue(property, value);
  374. }
  375. finally
  376. {
  377. SetIsHandlerSuspended(property, false);
  378. }
  379. }
  380. private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
  381. {
  382. SetValueNoCallback(TextProperty, _textBox.Text);
  383. }
  384. private bool ProcessTimePickerKey(KeyEventArgs e)
  385. {
  386. switch (e.Key)
  387. {
  388. case Key.System:
  389. {
  390. switch (e.SystemKey)
  391. {
  392. case Key.Down:
  393. {
  394. if ((Keyboard.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt)
  395. {
  396. TogglePopup();
  397. return true;
  398. }
  399. break;
  400. }
  401. }
  402. break;
  403. }
  404. case Key.Enter:
  405. {
  406. SetSelectedTime();
  407. return true;
  408. }
  409. }
  410. return false;
  411. }
  412. private void TextBox_KeyDown(object sender, KeyEventArgs e)
  413. {
  414. e.Handled = ProcessTimePickerKey(e) || e.Handled;
  415. }
  416. private void DropDownButton_MouseLeave(object sender, MouseEventArgs e)
  417. {
  418. _disablePopupReopen = false;
  419. }
  420. private bool IsHandlerSuspended(DependencyProperty property)
  421. {
  422. return _isHandlerSuspended != null && _isHandlerSuspended.ContainsKey(property);
  423. }
  424. private void PopupPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  425. {
  426. if (sender is Popup { StaysOpen: false })
  427. {
  428. if (_dropDownButton?.InputHitTest(e.GetPosition(_dropDownButton)) != null)
  429. {
  430. _disablePopupReopen = true;
  431. }
  432. }
  433. }
  434. private void Clock_SelectedTimeChanged(object sender, FunctionEventArgs<DateTime?> e) => SelectedTime = e.Info;
  435. private void Clock_Confirmed() => TogglePopup();
  436. private void PopupOpened(object sender, EventArgs e)
  437. {
  438. if (!IsDropDownOpen)
  439. {
  440. SetCurrentValue(IsDropDownOpenProperty, ValueBoxes.TrueBox);
  441. }
  442. Clock?.MoveFocus(new TraversalRequest(FocusNavigationDirection.First));
  443. OnClockOpened(new RoutedEventArgs());
  444. }
  445. private void PopupClosed(object sender, EventArgs e)
  446. {
  447. if (IsDropDownOpen)
  448. {
  449. SetCurrentValue(IsDropDownOpenProperty, ValueBoxes.FalseBox);
  450. }
  451. if (Clock.IsKeyboardFocusWithin)
  452. {
  453. MoveFocus(new TraversalRequest(FocusNavigationDirection.First));
  454. }
  455. OnClockClosed(new RoutedEventArgs());
  456. }
  457. private void DropDownButton_Click(object sender, RoutedEventArgs e) => TogglePopup();
  458. private void TogglePopup()
  459. {
  460. if (IsDropDownOpen)
  461. {
  462. SetCurrentValue(IsDropDownOpenProperty, ValueBoxes.FalseBox);
  463. }
  464. else
  465. {
  466. if (_disablePopupReopen)
  467. {
  468. _disablePopupReopen = false;
  469. }
  470. else
  471. {
  472. SetSelectedTime();
  473. SetCurrentValue(IsDropDownOpenProperty, ValueBoxes.TrueBox);
  474. }
  475. }
  476. }
  477. private void SafeSetText(string s)
  478. {
  479. if (string.Compare(Text, s, StringComparison.Ordinal) != 0)
  480. {
  481. SetCurrentValue(TextProperty, s);
  482. }
  483. }
  484. private DateTime? ParseText(string text)
  485. {
  486. try
  487. {
  488. return DateTime.Parse(text);
  489. }
  490. catch
  491. {
  492. // ignored
  493. }
  494. return null;
  495. }
  496. private DateTime? SetTextBoxValue(string s)
  497. {
  498. if (string.IsNullOrEmpty(s))
  499. {
  500. SafeSetText(s);
  501. return SelectedTime;
  502. }
  503. var d = ParseText(s);
  504. if (d != null)
  505. {
  506. SafeSetText(DateTimeToString((DateTime) d));
  507. return d;
  508. }
  509. if (SelectedTime != null)
  510. {
  511. var newtext = DateTimeToString((DateTime) SelectedTime);
  512. SafeSetText(newtext);
  513. return SelectedTime;
  514. }
  515. SafeSetText(DateTimeToString(DisplayTime));
  516. return DisplayTime;
  517. }
  518. private void SetSelectedTime()
  519. {
  520. if (_textBox != null)
  521. {
  522. if (!string.IsNullOrEmpty(_textBox.Text))
  523. {
  524. var s = _textBox.Text;
  525. if (SelectedTime != null)
  526. {
  527. var selectedTime = DateTimeToString(SelectedTime.Value);
  528. if (string.Compare(selectedTime, s, StringComparison.Ordinal) == 0)
  529. {
  530. return;
  531. }
  532. }
  533. var d = SetTextBoxValue(s);
  534. if (!SelectedTime.Equals(d))
  535. {
  536. SetCurrentValue(SelectedTimeProperty, d);
  537. SetCurrentValue(DisplayTimeProperty, d);
  538. }
  539. }
  540. else
  541. {
  542. if (SelectedTime.HasValue)
  543. {
  544. SetCurrentValue(SelectedTimeProperty, null);
  545. }
  546. }
  547. }
  548. else
  549. {
  550. var d = SetTextBoxValue(_defaultText);
  551. if (!SelectedTime.Equals(d))
  552. {
  553. SetCurrentValue(SelectedTimeProperty, d);
  554. }
  555. }
  556. }
  557. private string DateTimeToString(DateTime d) => d.ToString(TimeFormat);
  558. private static void OnGotFocus(object sender, RoutedEventArgs e)
  559. {
  560. var picker = (TimePicker) sender;
  561. if (!e.Handled && picker._textBox != null)
  562. {
  563. if (Equals(e.OriginalSource, picker))
  564. {
  565. picker._textBox.Focus();
  566. e.Handled = true;
  567. }
  568. else if (Equals(e.OriginalSource, picker._textBox))
  569. {
  570. picker._textBox.SelectAll();
  571. e.Handled = true;
  572. }
  573. }
  574. }
  575. private void EnsureClock()
  576. {
  577. if (Clock is not null)
  578. {
  579. return;
  580. }
  581. SetCurrentValue(ClockProperty, new Clock());
  582. SetPopupChild(Clock);
  583. }
  584. private void SetPopupChild(UIElement element)
  585. {
  586. if (_popup is not null)
  587. {
  588. _popup.Child = Clock;
  589. }
  590. }
  591. #endregion Private Methods
  592. }