PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Main/src/DynamicDataDisplay/Charts/Navigation/LongOperationsIndicator.cs

#
C# | 144 lines | 122 code | 22 blank | 0 comment | 19 complexity | 06b7232a62e3d2138d22d0f83845d1eb MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Input;
  8. using System.Windows.Threading;
  9. namespace Microsoft.Research.DynamicDataDisplay.Charts.Navigation
  10. {
  11. public sealed class LongOperationsIndicator : IPlotterElement
  12. {
  13. public LongOperationsIndicator()
  14. {
  15. timer.Tick += new EventHandler(timer_Tick);
  16. }
  17. private void timer_Tick(object sender, EventArgs e)
  18. {
  19. UpdateWaitIndicator();
  20. timer.Stop();
  21. }
  22. #region IPlotterElement Members
  23. void IPlotterElement.OnPlotterAttached(Plotter plotter)
  24. {
  25. this.plotter = plotter;
  26. }
  27. void IPlotterElement.OnPlotterDetaching(Plotter plotter)
  28. {
  29. this.plotter = null;
  30. }
  31. private Plotter plotter;
  32. Plotter IPlotterElement.Plotter
  33. {
  34. get { return plotter; }
  35. }
  36. #endregion
  37. #region LongOperationRunning
  38. public static void BeginLongOperation(DependencyObject obj)
  39. {
  40. obj.SetValue(LongOperationRunningProperty, true);
  41. }
  42. public static void EndLongOperation(DependencyObject obj)
  43. {
  44. obj.SetValue(LongOperationRunningProperty, false);
  45. }
  46. public static bool GetLongOperationRunning(DependencyObject obj)
  47. {
  48. return (bool)obj.GetValue(LongOperationRunningProperty);
  49. }
  50. public static void SetLongOperationRunning(DependencyObject obj, bool value)
  51. {
  52. obj.SetValue(LongOperationRunningProperty, value);
  53. }
  54. public static readonly DependencyProperty LongOperationRunningProperty = DependencyProperty.RegisterAttached(
  55. "LongOperationRunning",
  56. typeof(bool),
  57. typeof(LongOperationsIndicator),
  58. new FrameworkPropertyMetadata(false, OnLongOperationRunningChanged));
  59. private static void OnLongOperationRunningChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  60. {
  61. Plotter plotter = null;
  62. IPlotterElement element = d as IPlotterElement;
  63. if (element == null)
  64. {
  65. plotter = Plotter.GetPlotter(d);
  66. }
  67. else
  68. {
  69. plotter = element.Plotter;
  70. }
  71. if (plotter != null)
  72. {
  73. var indicator = plotter.Children.OfType<LongOperationsIndicator>().FirstOrDefault();
  74. if (indicator != null)
  75. {
  76. indicator.OnLongOperationRunningChanged(element, (bool)e.NewValue);
  77. }
  78. }
  79. }
  80. UIElement indicator = LoadIndicator();
  81. private static UIElement LoadIndicator()
  82. {
  83. var resources = (ResourceDictionary)Application.LoadComponent(new Uri("/DynamicDataDisplay;component/Charts/Navigation/LongOperationsIndicatorResources.xaml", UriKind.Relative));
  84. UIElement indicator = (UIElement)resources["Indicator"];
  85. return indicator;
  86. }
  87. private readonly DispatcherTimer timer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(100) };
  88. private int operationsCounter = 0;
  89. private void OnLongOperationRunningChanged(IPlotterElement element, bool longOperationRunning)
  90. {
  91. int change = longOperationRunning ? +1 : -1;
  92. operationsCounter += change;
  93. if (plotter == null) return;
  94. if (operationsCounter == 1)
  95. {
  96. timer.Start();
  97. }
  98. else if (operationsCounter == 0)
  99. {
  100. timer.Stop();
  101. UpdateWaitIndicator();
  102. }
  103. }
  104. private void UpdateWaitIndicator()
  105. {
  106. if (operationsCounter == 1)
  107. {
  108. if (!plotter.MainCanvas.Children.Contains(indicator))
  109. {
  110. plotter.MainCanvas.Children.Add(indicator);
  111. }
  112. plotter.Cursor = Cursors.Wait;
  113. }
  114. else if (operationsCounter == 0)
  115. {
  116. plotter.MainCanvas.Children.Remove(indicator);
  117. plotter.ClearValue(Plotter.CursorProperty);
  118. }
  119. }
  120. #endregion // end of LongOperationRunning
  121. }
  122. }