From 68327299c0e2f1b38d728011b6ed0487cafc0db3 Mon Sep 17 00:00:00 2001 From: Tichau Date: Tue, 26 May 2015 21:55:52 +0200 Subject: [PATCH] Add error check. --- Application/FileConverter/ConversionPreset.cs | 107 ++++++++++++++++-- .../FileConverter/FileConverter.csproj | 2 +- .../{ => Resources}/ApplicationIcon.png | Bin Application/FileConverter/Settings.cs | 30 ++++- .../FileConverter/Windows/SettingsWindow.xaml | 36 +++++- .../Windows/SettingsWindow.xaml.cs | 17 ++- 6 files changed, 169 insertions(+), 23 deletions(-) rename Application/FileConverter/{ => Resources}/ApplicationIcon.png (100%) diff --git a/Application/FileConverter/ConversionPreset.cs b/Application/FileConverter/ConversionPreset.cs index cd57cbd..85899f3 100644 --- a/Application/FileConverter/ConversionPreset.cs +++ b/Application/FileConverter/ConversionPreset.cs @@ -4,12 +4,23 @@ using System.Collections.Generic; namespace FileConverter { + using System.ComponentModel; + using System.Linq; + using System.Runtime.CompilerServices; using System.Xml.Serialization; + using FileConverter.Annotations; + [XmlRoot] [XmlType] - public class ConversionPreset + public class ConversionPreset : INotifyPropertyChanged, IDataErrorInfo { + private string name; + + private OutputType outputType; + + private List inputTypes; + public ConversionPreset() { this.Name = "New Preset"; @@ -23,25 +34,51 @@ namespace FileConverter this.InputTypes.AddRange(inputTypes); } + public event PropertyChangedEventHandler PropertyChanged; + [XmlAttribute] public string Name { - get; - set; + get + { + return this.name; + } + + set + { + this.name = value; + this.OnPropertyChanged(); + } } [XmlAttribute] public OutputType OutputType { - get; - set; + get + { + return outputType; + } + + set + { + outputType = value; + this.OnPropertyChanged(); + } } [XmlElement] public List InputTypes { - get; - set; + get + { + return inputTypes; + } + + set + { + inputTypes = value; + this.OnPropertyChanged(); + } } [XmlIgnore] @@ -51,8 +88,64 @@ namespace FileConverter set; } + [NotifyPropertyChangedInvocator] + protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) + { + this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + } + public class Setting { } + + public string this[string columnName] + { + get + { + return this.Validate(columnName); + } + } + + public string Error + { + get + { + return this.Validate("Name"); + } + } + + private string Validate(string properyName) + { + // Return error message if there is an error, else return empty or null string. + switch (properyName) + { + case "Name": + { + if (string.IsNullOrEmpty(this.Name)) + { + return "The preset name can't be empty."; + } + + if (this.Name.Contains(";")) + { + return "The preset name can't contains the character ';'."; + } + + Application application = Application.Current as Application; + if (application?.Settings != null) + { + int count = application.Settings.ConversionPresets.Count(match => match.name == this.Name); + if (count > 1) + { + return "The preset name is already used."; + } + } + } + + break; + } + + return string.Empty; + } } } diff --git a/Application/FileConverter/FileConverter.csproj b/Application/FileConverter/FileConverter.csproj index e185a41..bc97a2c 100644 --- a/Application/FileConverter/FileConverter.csproj +++ b/Application/FileConverter/FileConverter.csproj @@ -140,7 +140,7 @@ - + - + @@ -64,7 +88,7 @@