From 7785b6a0ed30d0ea0115803748645afdee951d37 Mon Sep 17 00:00:00 2001 From: PsychoMark Date: Mon, 20 Jun 2016 14:20:37 +0200 Subject: [PATCH] Added Autoscroll and Clear functionality, missing MessageBodyRenderer and fixed ReSharper code issues --- App.xaml.cs | 4 ++-- Infrastructure/GridLayout.cs | 2 +- Infrastructure/PasswordBoxAssistant.cs | 16 ++++++++------- Model/ConnectionInfo.cs | 2 +- Model/MessageBodyRenderer.cs | 27 ++++++++++++++++++++++++++ Model/MessageInfo.cs | 10 +++++++++- Model/RabbitMQClientConnection.cs | 4 ++-- PettingZoo.csproj | 1 + Properties/AssemblyInfo.cs | 2 -- View/ConnectionWindow.xaml.cs | 2 +- View/MainWindow.xaml | 13 ++++++++++--- View/MainWindow.xaml.cs | 8 ++------ ViewModel/ConnectionViewModel.cs | 6 +++--- ViewModel/MainViewModel.cs | 12 +++++++++--- 14 files changed, 77 insertions(+), 32 deletions(-) create mode 100644 Model/MessageBodyRenderer.cs diff --git a/App.xaml.cs b/App.xaml.cs index 9427ccd..2fd8670 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -7,7 +7,7 @@ using SimpleInjector; namespace PettingZoo { - public partial class App : Application + public partial class App { public void ApplicationStartup(object sender, StartupEventArgs e) { @@ -25,7 +25,7 @@ namespace PettingZoo // Automatically register all Window and BaseViewModel descendants foreach (var type in Assembly.GetExecutingAssembly().GetExportedTypes() - .Where(t => t.IsSubclassOf(typeof(System.Windows.Window)) || + .Where(t => t.IsSubclassOf(typeof(Window)) || t.IsSubclassOf(typeof(Infrastructure.BaseViewModel)))) { container.Register(type); diff --git a/Infrastructure/GridLayout.cs b/Infrastructure/GridLayout.cs index 4555054..d4ed843 100644 --- a/Infrastructure/GridLayout.cs +++ b/Infrastructure/GridLayout.cs @@ -58,7 +58,7 @@ namespace PettingZoo.Infrastructure } foreach (UIElement element in InternalChildren) { - FrameworkElement fe = element as FrameworkElement; + var fe = element as FrameworkElement; if (null != fe) { int row = GetRow(fe); diff --git a/Infrastructure/PasswordBoxAssistant.cs b/Infrastructure/PasswordBoxAssistant.cs index 698c6f1..3ca477d 100644 --- a/Infrastructure/PasswordBoxAssistant.cs +++ b/Infrastructure/PasswordBoxAssistant.cs @@ -20,11 +20,11 @@ namespace PettingZoo.Infrastructure private static void OnBoundPasswordChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { - PasswordBox box = d as PasswordBox; + var box = d as PasswordBox; // only handle this event when the property is attached to a PasswordBox // and when the BindPassword attached property has been set to true - if (d == null || !GetBindPassword(d)) + if (box == null || !GetBindPassword(d)) { return; } @@ -32,7 +32,7 @@ namespace PettingZoo.Infrastructure // avoid recursive updating by ignoring the box's changed event box.PasswordChanged -= HandlePasswordChanged; - string newPassword = (string) e.NewValue; + var newPassword = (string) e.NewValue; if (!GetUpdatingPassword(box)) { @@ -47,15 +47,15 @@ namespace PettingZoo.Infrastructure // when the BindPassword attached property is set on a PasswordBox, // start listening to its PasswordChanged event - PasswordBox box = dp as PasswordBox; + var box = dp as PasswordBox; if (box == null) { return; } - bool wasBound = (bool) (e.OldValue); - bool needToBind = (bool) (e.NewValue); + var wasBound = (bool) (e.OldValue); + var needToBind = (bool) (e.NewValue); if (wasBound) { @@ -70,7 +70,9 @@ namespace PettingZoo.Infrastructure private static void HandlePasswordChanged(object sender, RoutedEventArgs e) { - PasswordBox box = sender as PasswordBox; + var box = sender as PasswordBox; + if (box == null) + return; // set a flag to indicate that we're updating the password SetUpdatingPassword(box, true); diff --git a/Model/ConnectionInfo.cs b/Model/ConnectionInfo.cs index 44955da..2e4ef77 100644 --- a/Model/ConnectionInfo.cs +++ b/Model/ConnectionInfo.cs @@ -14,7 +14,7 @@ public static ConnectionInfo Default() { - return new ConnectionInfo() + return new ConnectionInfo { Host = "localhost", Port = 5672, diff --git a/Model/MessageBodyRenderer.cs b/Model/MessageBodyRenderer.cs new file mode 100644 index 0000000..b486e39 --- /dev/null +++ b/Model/MessageBodyRenderer.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace PettingZoo.Model +{ + public class MessageBodyRenderer + { + public static List TextTypes = new List + { + "application/json", + "application/xml" + }; + + + public static string Render(byte[] body, string contentType = "") + { + if (TextTypes.Contains(contentType)) + { + return Encoding.UTF8.GetString(body); + } + + // ToDo hex output + return ""; + } + } +} diff --git a/Model/MessageInfo.cs b/Model/MessageInfo.cs index 2c8249d..f40b25d 100644 --- a/Model/MessageInfo.cs +++ b/Model/MessageInfo.cs @@ -1,9 +1,11 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; namespace PettingZoo.Model { public class MessageInfo { + public DateTime Timestamp { get; set; } public string RoutingKey { get; set; } public byte[] Body { get; set; } @@ -18,5 +20,11 @@ namespace PettingZoo.Model : ""; } } + + + public MessageInfo() + { + Timestamp = DateTime.Now; + } } } diff --git a/Model/RabbitMQClientConnection.cs b/Model/RabbitMQClientConnection.cs index 9570b07..72cf526 100644 --- a/Model/RabbitMQClientConnection.cs +++ b/Model/RabbitMQClientConnection.cs @@ -23,7 +23,7 @@ namespace PettingZoo.Model break; if (MessageReceived != null) - MessageReceived(null, new MessageReceivedEventArgs(new MessageInfo() + MessageReceived(null, new MessageReceivedEventArgs(new MessageInfo { RoutingKey = "test", Body = Encoding.UTF8.GetBytes("{ \"hello\": \"world\" }"), @@ -33,7 +33,7 @@ namespace PettingZoo.Model { "classType", "LEF.Messaging.Internal.ActieNewMessage" } } })); - Thread.Sleep(1000); + Thread.Sleep(200); } }, token); } diff --git a/PettingZoo.csproj b/PettingZoo.csproj index 52c51cc..9bdf486 100644 --- a/PettingZoo.csproj +++ b/PettingZoo.csproj @@ -100,6 +100,7 @@ + diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs index e1a67f9..0eebe89 100644 --- a/Properties/AssemblyInfo.cs +++ b/Properties/AssemblyInfo.cs @@ -1,6 +1,4 @@ using System.Reflection; -using System.Resources; -using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Windows; diff --git a/View/ConnectionWindow.xaml.cs b/View/ConnectionWindow.xaml.cs index 3fd4b19..2298a57 100644 --- a/View/ConnectionWindow.xaml.cs +++ b/View/ConnectionWindow.xaml.cs @@ -25,7 +25,7 @@ namespace PettingZoo.View } - public partial class ConnectionWindow : Window + public partial class ConnectionWindow { public ConnectionWindow(ConnectionViewModel viewModel) { diff --git a/View/MainWindow.xaml b/View/MainWindow.xaml index 472b25c..417e465 100644 --- a/View/MainWindow.xaml +++ b/View/MainWindow.xaml @@ -5,6 +5,7 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:viewModel="clr-namespace:PettingZoo.ViewModel" + xmlns:infrastructure="clr-namespace:PettingZoo.Infrastructure" mc:Ignorable="d" d:DataContext="{d:DesignInstance viewModel:MainViewModel}" Width="800" @@ -28,16 +29,22 @@ - + + SelectedItem="{Binding Path=SelectedMessage, Mode=TwoWay}" + infrastructure:ListBox.AutoScroll="True"> - + + + + + + diff --git a/View/MainWindow.xaml.cs b/View/MainWindow.xaml.cs index d751cc0..fbb366f 100644 --- a/View/MainWindow.xaml.cs +++ b/View/MainWindow.xaml.cs @@ -1,13 +1,9 @@ -using System; -using System.Windows; -using System.Windows.Controls; -using System.Windows.Controls.Primitives; -using System.Windows.Data; +using System.Windows; using PettingZoo.ViewModel; namespace PettingZoo.View { - public partial class MainWindow : Window + public partial class MainWindow { public MainWindow(MainViewModel viewModel) { diff --git a/ViewModel/ConnectionViewModel.cs b/ViewModel/ConnectionViewModel.cs index 0f975f8..eb04fef 100644 --- a/ViewModel/ConnectionViewModel.cs +++ b/ViewModel/ConnectionViewModel.cs @@ -8,7 +8,7 @@ namespace PettingZoo.ViewModel { public class ConnectionViewModel { - private static IMapper modelMapper = new MapperConfiguration(cfg => + private static readonly IMapper ModelMapper = new MapperConfiguration(cfg => { cfg.CreateMap(); cfg.CreateMap(); @@ -41,13 +41,13 @@ namespace PettingZoo.ViewModel public ConnectionViewModel(ConnectionInfo model) : this() { - modelMapper.Map(model, this); + ModelMapper.Map(model, this); } public ConnectionInfo ToModel() { - return modelMapper.Map(this); + return ModelMapper.Map(this); } diff --git a/ViewModel/MainViewModel.cs b/ViewModel/MainViewModel.cs index e30338f..241f983 100644 --- a/ViewModel/MainViewModel.cs +++ b/ViewModel/MainViewModel.cs @@ -124,24 +124,30 @@ namespace PettingZoo.ViewModel private void ClearExecute() { + messages.Clear(); + clearCommand.RaiseCanExecuteChanged(); } private bool ClearCanExecute() { - return false; + return messages.Count > 0; } private void ConnectionMessageReceived(object sender, MessageReceivedEventArgs e) { - RunFromUiScheduler(() => messages.Add(e.MessageInfo)); + RunFromUiScheduler(() => + { + messages.Add(e.MessageInfo); + clearCommand.RaiseCanExecuteChanged(); + }); } private void RunFromUiScheduler(Action action) { - Task.Factory.StartNew(action, CancellationToken.None, TaskCreationOptions.None, uiScheduler); + Task.Factory.StartNew(action, CancellationToken.None, TaskCreationOptions.None, uiScheduler); } } } \ No newline at end of file