Add error check.

This commit is contained in:
Tichau committed 2015-05-26 21:55:52 +02:00
1 parent 4c7ff20193
commit 68327299c0
6 files changed
+169 -23

No files matched your search

+100 -7
View File
@@ -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<string> 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<string> 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;
}
}
}
@@ -140,7 +140,7 @@
<Resource Include="ApplicationIcon.ico" />
</ItemGroup>
<ItemGroup>
<Resource Include="ApplicationIcon.png" />
<Resource Include="Resources\ApplicationIcon.png" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
+27 -3
View File
@@ -18,7 +18,7 @@ namespace FileConverter
using FileConverter.Annotations;
public class Settings : INotifyPropertyChanged
public class Settings : INotifyPropertyChanged, IDataErrorInfo
{
private const char PresetSeparator = ';';
@@ -43,8 +43,7 @@ namespace FileConverter
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
handler?.Invoke(this, new PropertyChangedEventArgs(propertyName));
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public ConversionPreset GetPresetFromName(string presetName)
@@ -359,5 +358,30 @@ namespace FileConverter
return;
}
}
public string this[string columnName]
{
get
{
return this.Error;
}
}
public string Error
{
get
{
for (int index = 0; index < this.ConversionPresets.Count; index++)
{
string error = this.ConversionPresets[index].Error;
if (!string.IsNullOrEmpty(error))
{
return error;
}
}
return string.Empty;
}
}
}
}
@@ -1,17 +1,39 @@
<Window x:Name="window" x:Class="FileConverter.SettingsWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:FileConverter"
Height="340" Width="520" MinHeight="340" MinWidth="440"
Height="350" Width="520" MinHeight="350" MinWidth="440"
WindowStartupLocation="CenterScreen">
<Window.Resources>
<local:InputTypesToBool x:Key="InputTypesToBool"/>
<local:ApplicationVersionToApplicationName x:Key="ApplicationVersionToApplicationName"/>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<DockPanel LastChildFill="true">
<Border Background="#F44336" DockPanel.Dock="right" Margin="5,0,0,0" Width="20" Height="20" CornerRadius="10"
ToolTip="{Binding ElementName=customAdorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">
<TextBlock Text="!" VerticalAlignment="center" HorizontalAlignment="center" FontWeight="Bold" Foreground="white"/>
</Border>
<AdornedElementPlaceholder Name="customAdorner" VerticalAlignment="Center" >
<Border BorderBrush="#F44336" BorderThickness="1" />
</AdornedElementPlaceholder>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Window.Title>
<Binding Converter="{StaticResource ApplicationVersionToApplicationName}" Mode="OneWay" Path="(local:Application.ApplicationVersion)"/>
</Window.Title>
<Grid Margin="10">
<Grid.CommandBindings>
<CommandBinding Command="New" CanExecute="CanSaveSettings" />
</Grid.CommandBindings>
<Grid.RowDefinitions>
<RowDefinition Height="48"/>
<RowDefinition Height="*"/>
@@ -25,7 +47,7 @@
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Image HorizontalAlignment="Left" Height="48" Width="48" VerticalAlignment="Top" Source="ApplicationIcon.png" />
<Image HorizontalAlignment="Left" Height="48" Width="48" VerticalAlignment="Top" Source="..\Resources\ApplicationIcon.png" />
<Label Content="Settings" FontSize="20" Margin="4" Grid.Column="1" VerticalAlignment="Center" />
<Label HorizontalAlignment="Right" FontStyle="Italic" FontSize="14" Grid.Column="2" VerticalAlignment="Center">
<Label.Content>
@@ -41,7 +63,8 @@
</Grid.ColumnDefinitions>
<Label Content="Presets" Margin="0,5,0,0" VerticalAlignment="Top" HorizontalContentAlignment="Center" HorizontalAlignment="Left" Width="100" Grid.ColumnSpan="2"/>
<ListBox Name="PresetList" HorizontalAlignment="Left" Margin="0,36,0,0" Width="100" Grid.ColumnSpan="2" IsSynchronizedWithCurrentItem="True" SelectedItem="{Binding SelectedPreset, ElementName=window}">
<ListBox Name="PresetList" HorizontalAlignment="Left" Margin="0,36,0,0" Width="100" Grid.ColumnSpan="2"
Grid.Column="0" IsSynchronizedWithCurrentItem="True" SelectedItem="{Binding SelectedPreset, ElementName=window}">
<ItemsControl.DataContext>
<local:ConversionPreset/>
</ItemsControl.DataContext>
@@ -53,7 +76,8 @@
</ListBox>
<!-- Preset settings -->
<GroupBox Header="Preset settings" Margin="5,5,0,0" Grid.Column="1">
<GroupBox Header="Preset
" Margin="5,5,0,0" Grid.Column="1">
<Grid Margin="5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
@@ -64,7 +88,7 @@
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Label Content="Preset name" Grid.Row="0" HorizontalContentAlignment="Right" Margin="0,0,5,0"/>
<TextBox Name="PresetNameTextBox" Grid.Row="0" Grid.Column="1" Margin="5,2,5,2" Text="{Binding SelectedPreset.Name, Mode=TwoWay, ElementName=window}" TextWrapping="Wrap" />
<TextBox Name="PresetNameTextBox" Grid.Row="0" Grid.Column="1" Margin="5,2,25,2" Text="{Binding SelectedPreset.Name, Mode=TwoWay, ElementName=window, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" TextWrapping="Wrap" Width="Auto" />
<GroupBox Grid.Row="1" Grid.Column="0" Header="Input formats" Margin="5">
<StackPanel>
@@ -88,7 +112,7 @@
<Button Name="RemovePresetButton" Grid.Row="2" Content="-" HorizontalAlignment="Left" Width="28" Margin="54,4,0,0" Click="RemovePresetButton_Click"/>
<Button Name="CloseButton" Grid.Row="2" Content="Close" HorizontalAlignment="Right" Width="100" Margin="0,4,105,0" Click="CloseButton_Click" />
<Button Name="SaveButton" Grid.Row="2" Content="Save" HorizontalAlignment="Right" Width="100" Margin="0,4,0,0" Click="SaveButton_Click"/>
<Button Name="SaveButton" Grid.Row="2" Content="Save" HorizontalAlignment="Right" Width="100" Margin="0,4,0,0" Click="SaveButton_Click" Command="New"/>
</Grid>
</Window>
@@ -6,6 +6,7 @@ namespace FileConverter
using System.Windows;
using System.ComponentModel;
using System.Windows.Controls;
using System.Windows.Input;
using FileConverter.Annotations;
@@ -16,12 +17,15 @@ namespace FileConverter
{
private ConversionPreset selectedPreset;
private Settings settings;
public SettingsWindow()
{
this.InitializeComponent();
Application application = Application.Current as Application;
this.PresetList.ItemsSource = application.Settings.ConversionPresets;
settings = application.Settings;
this.PresetList.ItemsSource = settings.ConversionPresets;
OutputType[] outputTypes = new[]
{
@@ -53,11 +57,7 @@ namespace FileConverter
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private void ToggleButton_OnChecked(object sender, RoutedEventArgs e)
@@ -122,5 +122,10 @@ namespace FileConverter
Application application = Application.Current as Application;
application.Settings.ConversionPresets.Remove(this.selectedPreset);
}
private void CanSaveSettings(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = this.settings != null && string.IsNullOrEmpty(this.settings.Error);
}
}
}