1
0
mirror of synced 2024-11-14 09:29:17 +00:00

Added Autoscroll and Clear functionality, missing MessageBodyRenderer and fixed ReSharper code issues

This commit is contained in:
PsychoMark 2016-06-20 14:20:37 +02:00
parent e6fa2ee1d9
commit 7785b6a0ed
14 changed files with 77 additions and 32 deletions

View File

@ -7,7 +7,7 @@ using SimpleInjector;
namespace PettingZoo namespace PettingZoo
{ {
public partial class App : Application public partial class App
{ {
public void ApplicationStartup(object sender, StartupEventArgs e) public void ApplicationStartup(object sender, StartupEventArgs e)
{ {
@ -25,7 +25,7 @@ namespace PettingZoo
// Automatically register all Window and BaseViewModel descendants // Automatically register all Window and BaseViewModel descendants
foreach (var type in Assembly.GetExecutingAssembly().GetExportedTypes() 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)))) t.IsSubclassOf(typeof(Infrastructure.BaseViewModel))))
{ {
container.Register(type); container.Register(type);

View File

@ -58,7 +58,7 @@ namespace PettingZoo.Infrastructure
} }
foreach (UIElement element in InternalChildren) foreach (UIElement element in InternalChildren)
{ {
FrameworkElement fe = element as FrameworkElement; var fe = element as FrameworkElement;
if (null != fe) if (null != fe)
{ {
int row = GetRow(fe); int row = GetRow(fe);

View File

@ -20,11 +20,11 @@ namespace PettingZoo.Infrastructure
private static void OnBoundPasswordChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 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 // only handle this event when the property is attached to a PasswordBox
// and when the BindPassword attached property has been set to true // and when the BindPassword attached property has been set to true
if (d == null || !GetBindPassword(d)) if (box == null || !GetBindPassword(d))
{ {
return; return;
} }
@ -32,7 +32,7 @@ namespace PettingZoo.Infrastructure
// avoid recursive updating by ignoring the box's changed event // avoid recursive updating by ignoring the box's changed event
box.PasswordChanged -= HandlePasswordChanged; box.PasswordChanged -= HandlePasswordChanged;
string newPassword = (string) e.NewValue; var newPassword = (string) e.NewValue;
if (!GetUpdatingPassword(box)) if (!GetUpdatingPassword(box))
{ {
@ -47,15 +47,15 @@ namespace PettingZoo.Infrastructure
// when the BindPassword attached property is set on a PasswordBox, // when the BindPassword attached property is set on a PasswordBox,
// start listening to its PasswordChanged event // start listening to its PasswordChanged event
PasswordBox box = dp as PasswordBox; var box = dp as PasswordBox;
if (box == null) if (box == null)
{ {
return; return;
} }
bool wasBound = (bool) (e.OldValue); var wasBound = (bool) (e.OldValue);
bool needToBind = (bool) (e.NewValue); var needToBind = (bool) (e.NewValue);
if (wasBound) if (wasBound)
{ {
@ -70,7 +70,9 @@ namespace PettingZoo.Infrastructure
private static void HandlePasswordChanged(object sender, RoutedEventArgs e) 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 // set a flag to indicate that we're updating the password
SetUpdatingPassword(box, true); SetUpdatingPassword(box, true);

View File

@ -14,7 +14,7 @@
public static ConnectionInfo Default() public static ConnectionInfo Default()
{ {
return new ConnectionInfo() return new ConnectionInfo
{ {
Host = "localhost", Host = "localhost",
Port = 5672, Port = 5672,

View File

@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace PettingZoo.Model
{
public class MessageBodyRenderer
{
public static List<String> TextTypes = new List<string>
{
"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 "";
}
}
}

View File

@ -1,9 +1,11 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
namespace PettingZoo.Model namespace PettingZoo.Model
{ {
public class MessageInfo public class MessageInfo
{ {
public DateTime Timestamp { get; set; }
public string RoutingKey { get; set; } public string RoutingKey { get; set; }
public byte[] Body { get; set; } public byte[] Body { get; set; }
@ -18,5 +20,11 @@ namespace PettingZoo.Model
: ""; : "";
} }
} }
public MessageInfo()
{
Timestamp = DateTime.Now;
}
} }
} }

View File

@ -23,7 +23,7 @@ namespace PettingZoo.Model
break; break;
if (MessageReceived != null) if (MessageReceived != null)
MessageReceived(null, new MessageReceivedEventArgs(new MessageInfo() MessageReceived(null, new MessageReceivedEventArgs(new MessageInfo
{ {
RoutingKey = "test", RoutingKey = "test",
Body = Encoding.UTF8.GetBytes("{ \"hello\": \"world\" }"), Body = Encoding.UTF8.GetBytes("{ \"hello\": \"world\" }"),
@ -33,7 +33,7 @@ namespace PettingZoo.Model
{ "classType", "LEF.Messaging.Internal.ActieNewMessage" } { "classType", "LEF.Messaging.Internal.ActieNewMessage" }
} }
})); }));
Thread.Sleep(1000); Thread.Sleep(200);
} }
}, token); }, token);
} }

View File

@ -100,6 +100,7 @@
<Compile Include="Infrastructure\DelegateCommand.cs" /> <Compile Include="Infrastructure\DelegateCommand.cs" />
<Compile Include="Infrastructure\BaseViewModel.cs" /> <Compile Include="Infrastructure\BaseViewModel.cs" />
<Compile Include="Infrastructure\GridLayout.cs" /> <Compile Include="Infrastructure\GridLayout.cs" />
<Compile Include="Infrastructure\ListBoxAutoScroll.cs" />
<Compile Include="Infrastructure\PasswordBoxAssistant.cs" /> <Compile Include="Infrastructure\PasswordBoxAssistant.cs" />
<Compile Include="Model\IConnection.cs" /> <Compile Include="Model\IConnection.cs" />
<Compile Include="Model\IConnectionFactory.cs" /> <Compile Include="Model\IConnectionFactory.cs" />

View File

@ -1,6 +1,4 @@
using System.Reflection; using System.Reflection;
using System.Resources;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Windows; using System.Windows;

View File

@ -25,7 +25,7 @@ namespace PettingZoo.View
} }
public partial class ConnectionWindow : Window public partial class ConnectionWindow
{ {
public ConnectionWindow(ConnectionViewModel viewModel) public ConnectionWindow(ConnectionViewModel viewModel)
{ {

View File

@ -5,6 +5,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:viewModel="clr-namespace:PettingZoo.ViewModel" xmlns:viewModel="clr-namespace:PettingZoo.ViewModel"
xmlns:infrastructure="clr-namespace:PettingZoo.Infrastructure"
mc:Ignorable="d" mc:Ignorable="d"
d:DataContext="{d:DesignInstance viewModel:MainViewModel}" d:DataContext="{d:DesignInstance viewModel:MainViewModel}"
Width="800" Width="800"
@ -28,16 +29,22 @@
<Grid.ColumnDefinitions> <Grid.ColumnDefinitions>
<ColumnDefinition Width="*" /> <ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" /> <ColumnDefinition Width="Auto" />
<ColumnDefinition Width="300" /> <ColumnDefinition Width="*" />
</Grid.ColumnDefinitions> </Grid.ColumnDefinitions>
<ListBox Grid.Column="0" Grid.Row="0" <ListBox Grid.Column="0" Grid.Row="0"
HorizontalAlignment="Stretch" HorizontalAlignment="Stretch"
ItemsSource="{Binding Messages}" ItemsSource="{Binding Messages}"
SelectedItem="{Binding Path=SelectedMessage, Mode=TwoWay}"> SelectedItem="{Binding Path=SelectedMessage, Mode=TwoWay}"
infrastructure:ListBox.AutoScroll="True">
<ListBox.ItemTemplate> <ListBox.ItemTemplate>
<DataTemplate> <DataTemplate>
<Grid> <Grid>
<TextBlock Text="{Binding RoutingKey}"></TextBlock> <Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" MinWidth="150"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Grid.Row="0" Text="{Binding Timestamp}"></TextBlock>
<TextBlock Grid.Column="1" Grid.Row="0" Text="{Binding RoutingKey}"></TextBlock>
</Grid> </Grid>
</DataTemplate> </DataTemplate>
</ListBox.ItemTemplate> </ListBox.ItemTemplate>

View File

@ -1,13 +1,9 @@
using System; using System.Windows;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using PettingZoo.ViewModel; using PettingZoo.ViewModel;
namespace PettingZoo.View namespace PettingZoo.View
{ {
public partial class MainWindow : Window public partial class MainWindow
{ {
public MainWindow(MainViewModel viewModel) public MainWindow(MainViewModel viewModel)
{ {

View File

@ -8,7 +8,7 @@ namespace PettingZoo.ViewModel
{ {
public class ConnectionViewModel public class ConnectionViewModel
{ {
private static IMapper modelMapper = new MapperConfiguration(cfg => private static readonly IMapper ModelMapper = new MapperConfiguration(cfg =>
{ {
cfg.CreateMap<ConnectionInfo, ConnectionViewModel>(); cfg.CreateMap<ConnectionInfo, ConnectionViewModel>();
cfg.CreateMap<ConnectionViewModel, ConnectionInfo>(); cfg.CreateMap<ConnectionViewModel, ConnectionInfo>();
@ -41,13 +41,13 @@ namespace PettingZoo.ViewModel
public ConnectionViewModel(ConnectionInfo model) : this() public ConnectionViewModel(ConnectionInfo model) : this()
{ {
modelMapper.Map(model, this); ModelMapper.Map(model, this);
} }
public ConnectionInfo ToModel() public ConnectionInfo ToModel()
{ {
return modelMapper.Map<ConnectionInfo>(this); return ModelMapper.Map<ConnectionInfo>(this);
} }

View File

@ -124,24 +124,30 @@ namespace PettingZoo.ViewModel
private void ClearExecute() private void ClearExecute()
{ {
messages.Clear();
clearCommand.RaiseCanExecuteChanged();
} }
private bool ClearCanExecute() private bool ClearCanExecute()
{ {
return false; return messages.Count > 0;
} }
private void ConnectionMessageReceived(object sender, MessageReceivedEventArgs e) private void ConnectionMessageReceived(object sender, MessageReceivedEventArgs e)
{ {
RunFromUiScheduler(() => messages.Add(e.MessageInfo)); RunFromUiScheduler(() =>
{
messages.Add(e.MessageInfo);
clearCommand.RaiseCanExecuteChanged();
});
} }
private void RunFromUiScheduler(Action action) private void RunFromUiScheduler(Action action)
{ {
Task.Factory.StartNew(action, CancellationToken.None, TaskCreationOptions.None, uiScheduler); Task.Factory.StartNew(action, CancellationToken.None, TaskCreationOptions.None, uiScheduler);
} }
} }
} }