/SolutionFramework/HydraPackages/PackageMonitor/MainPage.xaml.cs
C# | 352 lines | 284 code | 68 blank | 0 comment | 34 complexity | 2cd57c577691578bb1ef3933ff3b8160 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-3.0
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Animation;
- using System.Windows.Shapes;
- using System.Windows.Messaging;
- using System.Linq;
- using System.Diagnostics;
- using System.Text.RegularExpressions;
- using Utils.Silverlight;
- using System.Windows.Media.Imaging;
- using System.Threading.Tasks;
- using System.Windows.Threading;
-
- namespace PackageMonitor
- {
- public partial class MainPage : UserControl
- {
- private Dictionary<Guid, Package> packages;
- private PackageInfo currentPackageInfo;
-
- public MainPage()
- {
- packages = new Dictionary<Guid, Package>();
-
- InitializeComponent();
- }
-
- private void DoDownload(Package package)
- {
- try
- {
- var storyboard = new Storyboard();
- var animation = new DoubleAnimation();
- var cloudPostion = CloudImage.GetRelativePosition(AnimationCanvas);
- var cloudCenter = cloudPostion.X + (CloudImage.ActualWidth / 2);
- var left = double.NaN;
-
- storyboard.Children.Add(animation);
-
- AnimationCanvas.Children.Add(package);
- AnimationCanvas.UpdateLayout();
-
- left = cloudCenter - (package.ActualWidth / 2);
-
- package.SetValue(Canvas.LeftProperty, left);
- package.SetValue(Canvas.TopProperty, (double) 0.0);
- package.SetValue(Canvas.ZIndexProperty, -1);
-
- animation.From = 0;
- animation.To = AnimationCanvas.ActualHeight;
- animation.Duration = new Duration(TimeSpan.FromMilliseconds(500));
-
- Storyboard.SetTarget(animation, package);
- Storyboard.SetTargetProperty(animation, new PropertyPath(Canvas.TopProperty));
-
- storyboard.Begin();
-
- storyboard.Completed += (sender, e) =>
- {
- AnimationCanvas.Children.Remove(package);
- PackagePanel.Children.Add(package);
- };
- }
- catch (Exception ex)
- {
- Debugger.Break();
- }
- }
-
- private void DoLoadFromCache(Package package)
- {
- try
- {
- var storyboard = new Storyboard();
- var animation = new DoubleAnimation();
- var animation2 = new DoubleAnimation();
- var cachePostion = Cache.GetRelativePosition(AnimationCanvas);
- var cacheCenter = cachePostion.X + (Cache.ActualWidth / 2);
- var panelCenterX = PackagePanel.ActualWidth / 2;
- var left = double.NaN;
- var top = double.NaN;
-
- storyboard.Children.Add(animation);
- storyboard.Children.Add(animation2);
-
- AnimationCanvas.Children.Add(package);
- AnimationCanvas.UpdateLayout();
-
- left = cacheCenter - (package.ActualWidth / 2);
- top = cachePostion.Y;
-
- package.SetValue(Canvas.LeftProperty, left);
- package.SetValue(Canvas.TopProperty, top);
- package.SetValue(Canvas.ZIndexProperty, -1);
-
- animation.From = left;
- animation.To = panelCenterX - (package.ActualWidth / 2);
- animation.Duration = new Duration(TimeSpan.FromMilliseconds(300));
-
- animation2.From = top;
- animation2.To = AnimationCanvas.ActualHeight;
- animation2.BeginTime = TimeSpan.FromMilliseconds(250);
- animation2.Duration = new Duration(TimeSpan.FromMilliseconds(200));
-
- Storyboard.SetTarget(animation, package);
- Storyboard.SetTargetProperty(animation, new PropertyPath(Canvas.LeftProperty));
-
- Storyboard.SetTarget(animation2, package);
- Storyboard.SetTargetProperty(animation2, new PropertyPath(Canvas.TopProperty));
-
- storyboard.Begin();
-
- storyboard.Completed += (sender, e) =>
- {
- AnimationCanvas.Children.Remove(package);
-
- if (!PackagePanel.Children.Contains(package))
- {
- PackagePanel.Children.Add(package);
- }
- };
- }
- catch (Exception ex)
- {
- Debugger.Break();
- }
- }
-
- private void UserControl_Loaded(object sender, RoutedEventArgs e)
- {
- var receiver = new LocalMessageReceiver("PackageMonitor", ReceiverNameScope.Global, LocalMessageReceiver.AnyDomain);
- var timer = new DispatcherTimer();
-
- timer.Interval = TimeSpan.FromMilliseconds(1);
-
- timer.Tick += (sender2, e2) =>
- {
- PositionImages();
-
- timer.Stop();
- };
-
- timer.Start();
-
- receiver.MessageReceived += (sender2, e2) =>
- {
- try
- {
- var message = e2.Message;
- var messageParts = message.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
- var command = messageParts.First();
-
- if (command == "Ping")
- {
- StatusBar.Text = "Receiving connection from " + e2.SenderDomain;
-
- packages.Clear();
- PackagePanel.Children.Clear();
- }
- else if (command == "PackageMessage")
- {
- var packageInfo = messageParts.ElementAt(1);
- var action = messageParts.ElementAt(2);
- var messageText = string.Empty;
- var textBlock = new TextBlock();
- var regex = new Regex(@"(?<name>.+?)(?<guid>\{.+?\})");
- var match = regex.Match(packageInfo);
- var packageName = match.Groups["name"].Value;
- var packageGuid = Guid.Parse(match.Groups["guid"].Value);
- Package package = null;
-
- if (packages.ContainsKey(packageGuid))
- {
- package = packages[packageGuid];
- }
- else
- {
- package = new Package(packageName, packageGuid.ToString());
- packages.Add(packageGuid, package);
-
- package.OnDoubleClick += new OnDoubleClickHandler(Package_OnDoubleClick);
- }
-
- if (messageParts.Length > 3)
- {
- messageText = messageParts.ElementAt(3);
- }
-
- package.Messages.Add(new PackageMessage(command, action, messageText));
-
- textBlock.Text = packageInfo + " " + action + " " + messageText;
-
- MessageList.Items.Add(textBlock);
-
- MessageList.UpdateLayout();
- MessageList.ScrollIntoView(textBlock);
-
- StatusBar.Text = action;
-
- if (action == "Downloading")
- {
- DoDownload(package);
- }
- else if (action == "LoadingFromCache")
- {
- DoLoadFromCache(package);
- }
- }
- else if (command == "PackageException")
- {
- var packageInfo = messageParts.ElementAt(1);
- var errorMessage = messageParts.ElementAt(2);
- var stackTrace = string.Empty;
- var textBlock = new TextBlock();
- var regex = new Regex(@"(?<name>.+?)(?<guid>\{.+?\})");
- var match = regex.Match(packageInfo);
- var packageName = match.Groups["name"].Value;
- var packageGuid = Guid.Parse(match.Groups["guid"].Value);
- var package = packages[packageGuid];
-
- if (messageParts.Length > 3)
- {
- stackTrace = messageParts.ElementAt(3);
- }
-
- textBlock.Text = packageName + " " + errorMessage;
- textBlock.Foreground = new SolidColorBrush(Colors.Red);
- textBlock.FontWeight = FontWeights.Bold;
-
- package.Messages.Add(new PackageMessage(errorMessage, stackTrace));
-
- MessageList.Items.Add(textBlock);
-
- MessageList.UpdateLayout();
- MessageList.ScrollIntoView(textBlock);
- }
- else if (command == "GeneralMessage")
- {
- var generalMessage = messageParts.ElementAt(1);
- var textBlock = new TextBlock();
-
- textBlock.Text = generalMessage;
-
- MessageList.Items.Add(textBlock);
-
- MessageList.UpdateLayout();
- MessageList.ScrollIntoView(textBlock);
- }
- else if (command == "GeneralException")
- {
- var errorMessage = messageParts.ElementAt(1);
- var stackTrace = messageParts.ElementAt(1);
- var textBlock = new TextBlock();
-
- textBlock.Text = errorMessage;
-
- MessageList.Items.Add(textBlock);
-
- MessageList.UpdateLayout();
- MessageList.ScrollIntoView(textBlock);
- }
- }
- catch (Exception ex)
- {
- Debugger.Break();
- }
- };
-
- try
- {
- receiver.Listen();
- }
- catch (Exception ex)
- {
- StatusBar.Text = ex.Message;
- }
- }
-
- private void Package_OnDoubleClick(Package package)
- {
- if (currentPackageInfo != null && AnimationCanvas.Children.Contains(currentPackageInfo))
- {
- AnimationCanvas.Children.Remove(currentPackageInfo);
- }
-
- currentPackageInfo = new PackageInfo(package);
-
- currentPackageInfo.OnClose += (sender, e) =>
- {
- AnimationCanvas.Children.Remove(e.Value);
- };
-
- AnimationCanvas.Children.Add(currentPackageInfo);
- AnimationCanvas.UpdateLayout();
-
- currentPackageInfo.SetValue(Canvas.LeftProperty, (AnimationCanvas.ActualWidth / 2) - (currentPackageInfo.ActualWidth / 2));
- currentPackageInfo.SetValue(Canvas.TopProperty, (AnimationCanvas.ActualHeight / 2) - (currentPackageInfo.ActualHeight / 2));
- }
-
- private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e)
- {
- PositionImages();
- }
-
- private void CloudImage_Loaded(object sender, RoutedEventArgs e)
- {
- PositionImages();
- }
-
- private void CacheImage_Loaded(object sender, RoutedEventArgs e)
- {
- PositionImages();
- }
-
- private void PositionImages()
- {
- if (CloudImage.ActualWidth != double.NaN && CloudImage.ActualWidth != 0)
- {
- var cloudLeft = (this.ActualWidth / 4) - (CloudImage.ActualWidth / 2);
-
- Cloud.SetValue(Canvas.LeftProperty, cloudLeft);
- }
-
- var bitmapImage = (BitmapImage)CacheImage.Source;
-
- if (bitmapImage.PixelHeight != double.NaN && bitmapImage.PixelHeight != 0)
- {
- var listPostion = MessageList.GetRelativePosition(this);
-
- Cache.SetValue(Canvas.LeftProperty, listPostion.X);
- Cache.SetValue(Canvas.TopProperty, listPostion.Y - bitmapImage.PixelHeight);
- }
- }
-
- private void Cloud_Loaded(object sender, RoutedEventArgs e)
- {
- PositionImages();
- }
-
- private void Cache_Loaded(object sender, RoutedEventArgs e)
- {
- PositionImages();
- }
- }
- }