Skip to content

Commit

Permalink
Merge pull request #41 from ScottyMac52/featureAddFieldThatCanOverrid…
Browse files Browse the repository at this point in the history
…eModuleFolder

Added better error detection around loading the configuration. Modifi…
  • Loading branch information
ScottyMac52 authored Jan 26, 2019
2 parents cbd86ff + f6fda9e commit 9277493
Show file tree
Hide file tree
Showing 10 changed files with 705 additions and 499 deletions.
18 changes: 14 additions & 4 deletions MFDSettingsManager/Configuration/MFDConfigurationSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,20 @@ public static MFDConfigurationSection GetConfig(ILog logger, string fullPathtoCo
currentConfiguration = ConfigurationManager.OpenMappedExeConfiguration(exeFileMap, ConfigurationUserLevel.None);
}

var configSection = (MFDConfigurationSection)currentConfiguration.GetSection("MFDSettings");
configSection.Logger = logger;
configSection.IsDataDirty = false;
return configSection ?? new MFDConfigurationSection();
try
{
var configSection = (MFDConfigurationSection)currentConfiguration.GetSection("MFDSettings");
configSection.Logger = logger;
configSection.IsDataDirty = false;
return configSection ?? new MFDConfigurationSection();
}
catch (System.Exception ex)
{
logger.Error($"Unable to load the configuration due to errors.", ex);
}

return null;

}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public static class ConfigurationSectionExtensions
/// <returns></returns>
public static ModulesConfiguration ToModel(this MFDConfigurationSection section, ILog logger)
{
if(section == null)
{
return null;
}
var model = ConfigSectionModelMapper.MapFromConfigurationSection(section);
model.Logger = logger;
return model;
Expand Down
5 changes: 5 additions & 0 deletions MFDSettingsManager/Mappers/ConfigSectionModelMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public class ConfigSectionModelMapper
/// <returns></returns>
internal static ModulesConfiguration MapFromConfigurationSection(MFDConfigurationSection section)
{
if(section == null)
{
return null;
}

var logger = section.Logger;
var logSep = new string('-', 160);

Expand Down
32 changes: 21 additions & 11 deletions MFDisplay/ConfigurationWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using Application = System.Windows.Application;
using MenuItem = System.Windows.Controls.MenuItem;
using MessageBox = System.Windows.MessageBox;

Expand Down Expand Up @@ -69,38 +70,47 @@ private void TxtFilePath_TextChanged(object sender, TextChangedEventArgs e)
private void Window_Loaded(object sender, RoutedEventArgs e)
{
txtFilePath.TextChanged += TxtFilePath_TextChanged;
LoadConfig();
if(!LoadConfig())
{
((MFDisplayApp)Application.Current).ShowConfigurationError(this);
Close();
}
}

private void LoadConfigurationSectionAsModel()
{
var configSection = MFDConfigurationSection.GetConfig(Logger);
Config = configSection.ToModel(Logger);
Config = configSection?.ToModel(Logger);
}

private void LoadConfig()
private bool LoadConfig()
{
LoadConfigurationSectionAsModel();
cbModules.ItemsSource = Config.Modules;
if(Config == null)
{
return false;
}
cbModules.ItemsSource = Config?.Modules;
cbModules.DisplayMemberPath = "DisplayName";
cbModules.SelectedValuePath = "ModuleName";

cbDefaultModule.ItemsSource = Config.Modules;
cbDefaultModule.ItemsSource = Config?.Modules;
cbDefaultModule.DisplayMemberPath = "DisplayName";
cbDefaultModule.SelectedValuePath = "ModuleName";

txtFilePath.Text = Config.FilePath;
txtFilePath.Text = Config?.FilePath;
chkIsValidPath.IsChecked = IsValidConfig;
chkSaveClips.IsChecked = Config.SaveClips;
chkSaveClips.IsChecked = Config?.SaveClips;

if (!string.IsNullOrEmpty(Config.DefaultConfig))
if (!string.IsNullOrEmpty(Config?.DefaultConfig))
{
cbModules.SelectedValue = Config.DefaultConfig;
cbDefaultModule.SelectedValue = Config.DefaultConfig;
cbModules.SelectedValue = Config?.DefaultConfig;
cbDefaultModule.SelectedValue = Config?.DefaultConfig;
ModuleChanged();
}

IsDataDirty = false;
return true;
}


Expand Down Expand Up @@ -241,7 +251,7 @@ private void PreviewClick(object sender, RoutedEventArgs e)
SizeToContent = SizeToContent.WidthAndHeight,
WindowStyle = WindowStyle.None,
Owner = this,
FilePath = Path.Combine(Config.FilePath, config.ModuleName),
FilePath = Config.FilePath,
BorderThickness = new Thickness(5.0F),
AllowsTransparency = false
};
Expand Down
3 changes: 2 additions & 1 deletion MFDisplay/MFDisplay.csproj.user
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<EnableSecurityDebugging>false</EnableSecurityDebugging>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'">
<StartArguments>-mod C-101</StartArguments>
<StartArguments>
</StartArguments>
</PropertyGroup>
</Project>
35 changes: 29 additions & 6 deletions MFDisplay/MFDisplayApp.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows;
using System.Windows.Media;

namespace MFDisplay
{
Expand Down Expand Up @@ -82,6 +80,23 @@ static MFDisplayApp()
XmlConfigurator.Configure();
}

/// <summary>
/// Reports on Configuration errors
/// </summary>
/// <param name="parent">Parent Window</param>
internal void ShowConfigurationError(Window parent = null)
{
var logPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"Vyper Industries\MFD4CTS\status.log");
if(parent != null)
{
MessageBox.Show(parent, $"Unable to load the configuration. Please check the log at {logPath} for details.", "Error in Configuration", MessageBoxButton.OK, MessageBoxImage.Error);
}
else
{
MessageBox.Show($"Unable to load the configuration. Please check the log at {logPath} for details.", "Error in Configuration", MessageBoxButton.OK, MessageBoxImage.Error);
}
}


/// <summary>
/// Executed when the application starts
Expand All @@ -100,8 +115,16 @@ protected override void OnStartup(StartupEventArgs e)
Logger = LogManager.GetLogger("MFD4CTS");
var assmLocation = Assembly.GetExecutingAssembly().Location;
var sectionConfig = MFDConfigurationSection.GetConfig(Logger);

if(sectionConfig == null)
{
ShowConfigurationError();
Shutdown(5);
return;
}

Configuration = sectionConfig.ToModel(Logger);
while (!Directory.Exists(Configuration.FilePath))
while (!Directory.Exists(Configuration?.FilePath))
{
var configWindow = new ConfigurationWindow()
{
Expand All @@ -111,17 +134,17 @@ protected override void OnStartup(StartupEventArgs e)
configWindow.ShowDialog();
sectionConfig = MFDConfigurationSection.GetConfig(Logger);
Configuration = sectionConfig.ToModel(Logger);
if (!Directory.Exists(Configuration.FilePath))
if (!Directory.Exists(Configuration?.FilePath))
{
var msgResult = MessageBox.Show($"{Configuration.FilePath} is not a valid path. Do you wish to try another path?", "Invalid Path Configuration", MessageBoxButton.YesNo, MessageBoxImage.Hand);
var msgResult = MessageBox.Show($"{Configuration?.FilePath} is not a valid path. Do you wish to try another path?", "Invalid Path Configuration", MessageBoxButton.YesNo, MessageBoxImage.Hand);
if(msgResult != MessageBoxResult.Yes)
{
break;
}
}
}

if (!Directory.Exists(Configuration.FilePath))
if (!Directory.Exists(Configuration?.FilePath))
{
Shutdown(2);
}
Expand Down
5 changes: 3 additions & 2 deletions MFDisplay/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public void CreateMFDs()
{
Logger = Logger,
Configuration = config,
FilePath = Path.Combine(Config.FilePath, SelectedModule.ModuleName)
FilePath = Config.FilePath
};
newmfdWindow.Show();
if (newmfdWindow.IsMFDLoaded)
Expand Down Expand Up @@ -214,7 +214,8 @@ private void CbModules_Loaded(object sender, RoutedEventArgs e)
{
Logger.Info($"Loading the requested configuration {PassedModule}...");
}
cbModules.SelectedValue = PassedModule ?? Config.DefaultConfig;
var selectedModule = PassedModule ?? Config.DefaultConfig;
cbModules.SelectedValue = selectedModule;
}
}

Expand Down
4 changes: 2 additions & 2 deletions MFDisplay/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.0.0.0")]
[assembly: AssemblyFileVersion("2.0.0.0")]
[assembly: AssemblyVersion("2.1.0.0")]
[assembly: AssemblyFileVersion("2.1.0.0")]
Loading

0 comments on commit 9277493

Please sign in to comment.