OSDN Git Service

Validate the values in the options
authorKazuhiro Fujieda <fujieda@users.osdn.me>
Fri, 23 Sep 2016 13:41:39 +0000 (22:41 +0900)
committerKazuhiro Fujieda <fujieda@users.osdn.me>
Sat, 24 Sep 2016 10:51:04 +0000 (19:51 +0900)
BurageSnap/OptionView.xaml
BurageSnap/OptionViewModel.cs
BurageSnap/Properties/Resources.Designer.cs
BurageSnap/Properties/Resources.ja.resx
BurageSnap/Properties/Resources.resx

index 7acb89d..6e09e68 100644 (file)
                         <RadioButton x:Name="radioButtonJpg" Content="JPG" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0 0 5 0" IsChecked="{Binding Path=Options.Format, Converter={StaticResource EnumBooleanConverter}, ConverterParameter=Jpg}"/>
                         <RadioButton x:Name="radioButtonPng" Content="PNG" HorizontalAlignment="Left" VerticalAlignment="Top"  IsChecked="{Binding Path=Options.Format, Converter={StaticResource EnumBooleanConverter}, ConverterParameter=Png}"/>
                     </StackPanel>
-                    <CheckBox x:Name="checkBoxAnimationGif" Content="{x:Static proprties:Resources.OptionView_Animation_GIF}" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Row="1" Grid.Column="1" Margin="0 5 0 0" IsChecked="{Binding Options.AnimationGif}"/>
+                    <CheckBox x:Name="checkBoxAnimationGif" Content="{x:Static proprties:Resources.OptionView_Animation_GIF}" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Row="1" Grid.Column="1" Margin="0 5 0 0" IsChecked="{Binding AnimationGif}"/>
                 </Grid>
                 <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" Margin="0 0 0 5">
                     <Label x:Name="labelInterval" Content="{x:Static proprties:Resources.OptionView_Interval}" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="0 0 5 0" />
-                    <TextBox x:Name="textBoxInterval" HorizontalAlignment="Left" Text="{Binding Options.Interval}" VerticalAlignment="Center" Width="41" TextAlignment="Right" Margin="0 0 5 0"/>
+                    <TextBox x:Name="textBoxInterval" HorizontalAlignment="Left" Text="{Binding Interval, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Center" Width="41" TextAlignment="Right" Margin="0 0 5 0"/>
                     <Label x:Name="labelMs" Content="ms" HorizontalAlignment="Left" VerticalAlignment="Center"/>
                 </StackPanel>
                 <StackPanel HorizontalAlignment="Left" VerticalAlignment="Top" Orientation="Horizontal" Margin="0 0 0 15">
                     <Label x:Name="labelRingBuffer" Content="{x:Static proprties:Resources.OptionView_Ring_buffer}" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="0 0 5 0"/>
-                    <TextBox x:Name="textBoxRingBuffer" HorizontalAlignment="Left" Text="{Binding Options.RingBuffer}" VerticalAlignment="Center" Width="41" TextAlignment="Right"  Margin="0 0 5 0"/>
+                    <TextBox x:Name="textBoxRingBuffer" HorizontalAlignment="Left" Text="{Binding RingBuffer, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Center" Width="41" TextAlignment="Right"  Margin="0 0 5 0"/>
                     <Label x:Name="labelFrames" Content="{x:Static proprties:Resources.OptionView_Frames}" HorizontalAlignment="Left" VerticalAlignment="Center"/>
                 </StackPanel>
                 <Grid Margin="0 0 0 15">
index 64ef931..6f3b422 100644 (file)
 // limitations under the License.
 
 using System;
+using System.Collections;
 using System.Collections.Generic;
+using System.ComponentModel;
 using System.Linq;
+using System.Runtime.CompilerServices;
 using System.Windows.Input;
+using BurageSnap.Properties;
 using Prism.Commands;
 using Prism.Interactivity.InteractionRequest;
 using Prism.Mvvm;
 
 namespace BurageSnap
 {
-    public class OptionViewModel : BindableBase, IInteractionRequestAware
+    public class OptionViewModel : BindableBase, IInteractionRequestAware, INotifyDataErrorInfo
     {
         private INotification _notification;
 
@@ -32,6 +36,9 @@ namespace BurageSnap
             set
             {
                 Options = (OptionContent)value.Content;
+                Interval = Options.Interval.ToString();
+                RingBuffer = Options.RingBuffer.ToString();
+                AnimationGif = Options.AnimationGif;
                 Modifier = new KeyModifier {Value = Options.HotKeyModifier};
                 HotKey = Options.HotKey;
                 SetProperty(ref _notification, value);
@@ -48,6 +55,74 @@ namespace BurageSnap
             set { SetProperty(ref _options, value); }
         }
 
+        private string _interval;
+
+        public string Interval
+        {
+            get { return _interval; }
+            set
+            {
+                SetProperty(ref _interval, value);
+                int result;
+                if (!int.TryParse(_interval, out result) || result < 10 || result > 1000 * 1000)
+                {
+                    SetError(Resources.OptionView_Validate_interval);
+                }
+                else
+                {
+                    ClearError();
+                }
+                _options.Interval = result;
+            }
+        }
+
+        private string _ringBuffer;
+
+        public string RingBuffer
+        {
+            get { return _ringBuffer; }
+            set
+            {
+                SetProperty(ref _ringBuffer, value);
+                int result;
+                if (!int.TryParse(value, out result) || result < 0 || result > 100)
+                {
+                    SetError(Resources.OptionView_Validate_ring_buffer);
+                }
+                else if (_options.AnimationGif && result < 2)
+                {
+                    SetError(Resources.OptionView_Validate_ring_buffer_for_animation_GIF);
+                }
+                else
+                {
+                    ClearError();
+                }
+                _options.RingBuffer = result;
+            }
+        }
+
+        private bool _animationGif;
+
+        public bool AnimationGif
+        {
+            get { return _animationGif; }
+            set
+            {
+                SetProperty(ref _animationGif, value);
+                if (value && _options.RingBuffer <= 1)
+                {
+                    // ReSharper disable once ExplicitCallerInfoArgument
+                    SetError(Resources.OptionView_Validate_ring_buffer_for_animation_GIF, nameof(RingBuffer));
+                }
+                else
+                {
+                    // ReSharper disable once ExplicitCallerInfoArgument
+                    ClearError(nameof(RingBuffer));
+                }
+                _options.AnimationGif = value;
+            }
+        }
+
         private string _title;
 
         public string Title
@@ -85,7 +160,9 @@ namespace BurageSnap
 
         public bool IsKeySelected => HotKey != "";
 
-        public ICommand OkCommand { get; private set; }
+        private readonly ErrorsContainer<string> _errors;
+
+        public ICommand OkCommand { get; }
         public ICommand CancelCommand { get; private set; }
         public ICommand SelectedCommand { get; private set; }
         public ICommand AddTitleCommand { get; private set; }
@@ -95,7 +172,9 @@ namespace BurageSnap
 
         public OptionViewModel()
         {
-            OkCommand = new DelegateCommand(OkInteraction);
+            _errors = new ErrorsContainer<string>(OnErrorsChanged);
+
+            OkCommand = new DelegateCommand(OkInteraction, () => !HasErrors);
             CancelCommand = new DelegateCommand(CancelInteraction);
             SelectedCommand = new DelegateCommand<object[]>(Selected);
             AddTitleCommand = new DelegateCommand(AddTitle);
@@ -149,5 +228,30 @@ namespace BurageSnap
         {
             WindowPicker.Stop();
         }
+
+        public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
+
+        public bool HasErrors => _errors.HasErrors;
+
+        public IEnumerable GetErrors(string propertyName)
+        {
+            return _errors.GetErrors(propertyName);
+        }
+
+        private void OnErrorsChanged([CallerMemberName] string propertyName = null)
+        {
+            ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs(propertyName));
+            ((DelegateCommand)OkCommand).RaiseCanExecuteChanged();
+        }
+
+        private void SetError(string message, [CallerMemberName] string propertyName = null)
+        {
+            _errors.SetErrors(propertyName, new [] {message});
+        }
+
+        private void ClearError([CallerMemberName] string propertyName = null)
+        {
+            _errors.ClearErrors(propertyName);
+        }
     }
 }
\ No newline at end of file
index b244ff9..81d515c 100644 (file)
@@ -350,22 +350,22 @@ namespace BurageSnap.Properties {
                 return ResourceManager.GetString("OptionView_Interval", resourceCulture);
             }
         }
-
+        
         /// <summary>
-        ///   Option に類似しているローカライズされた文字列を検索します。
+        ///   Notify に類似しているローカライズされた文字列を検索します。
         /// </summary>
-        public static string OptionView_Option {
+        public static string OptionView_Notify {
             get {
-                return ResourceManager.GetString("OptionView_Option", resourceCulture);
+                return ResourceManager.GetString("OptionView_Notify", resourceCulture);
             }
         }
-
+        
         /// <summary>
-        ///   Notify に類似しているローカライズされた文字列を検索します。
+        ///   Option に類似しているローカライズされた文字列を検索します。
         /// </summary>
-        public static string OptionView_Notify {
+        public static string OptionView_Option {
             get {
-                return ResourceManager.GetString("OptionView_Notify", resourceCulture);
+                return ResourceManager.GetString("OptionView_Option", resourceCulture);
             }
         }
         
@@ -424,6 +424,33 @@ namespace BurageSnap.Properties {
         }
         
         /// <summary>
+        ///   Interval must be in the range of 10 ms to 1000 sec. に類似しているローカライズされた文字列を検索します。
+        /// </summary>
+        public static string OptionView_Validate_interval {
+            get {
+                return ResourceManager.GetString("OptionView_Validate_interval", resourceCulture);
+            }
+        }
+        
+        /// <summary>
+        ///   The size of the ring buffer must be in the range of 0 to 100. に類似しているローカライズされた文字列を検索します。
+        /// </summary>
+        public static string OptionView_Validate_ring_buffer {
+            get {
+                return ResourceManager.GetString("OptionView_Validate_ring_buffer", resourceCulture);
+            }
+        }
+        
+        /// <summary>
+        ///   The size of the ring buffer must be greater than 1 to generate animation GIF. に類似しているローカライズされた文字列を検索します。
+        /// </summary>
+        public static string OptionView_Validate_ring_buffer_for_animation_GIF {
+            get {
+                return ResourceManager.GetString("OptionView_Validate_ring_buffer_for_animation_GIF", resourceCulture);
+            }
+        }
+        
+        /// <summary>
         ///   Window: に類似しているローカライズされた文字列を検索します。
         /// </summary>
         public static string OptionView_Window {
index d08b49f..3541c02 100644 (file)
   <data name="OptionView_Notify" xml:space="preserve">
     <value>通知する</value>
   </data>
+  <data name="OptionView_Validate_interval" xml:space="preserve">
+    <value>間隔は10msから1000秒の間で指定してください。</value>
+  </data>
+  <data name="OptionView_Validate_ring_buffer" xml:space="preserve">
+    <value>リングバッファのサイズは0から100の間で指定してください。</value>
+  </data>
+  <data name="OptionView_Validate_ring_buffer_for_animation_GIF" xml:space="preserve">
+    <value>アニメーションGIFを出力するにはリングバッファのサイズを2以上にしてください。</value>
+  </data>
 </root>
\ No newline at end of file
index 39caa46..18e388e 100644 (file)
   <data name="OptionView_Notify" xml:space="preserve">
     <value>Notify</value>
   </data>
+  <data name="OptionView_Validate_interval" xml:space="preserve">
+    <value>Interval must be in the range of 10 ms to 1000 sec.</value>
+  </data>
+  <data name="OptionView_Validate_ring_buffer" xml:space="preserve">
+    <value>The size of the ring buffer must be in the range of 0 to 100.</value>
+  </data>
+  <data name="OptionView_Validate_ring_buffer_for_animation_GIF" xml:space="preserve">
+    <value>The size of the ring buffer must be greater than 1 to generate animation GIF.</value>
+  </data>
 </root>
\ No newline at end of file