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