/// <summary> /// Loads the options from the local file storage. /// </summary> private void ReloadOptions() { if (!File.Exists(_optionsFilePath)) { return; } try { var fileContent = File.ReadAllText(_optionsFilePath); var settings = new JsonSerializerSettings(); var options = JsonConvert.DeserializeObject <OptionsModel>(fileContent, settings); foreach (var job in options.JobDefinitions) { JobDefinitions.Add(job); } Options = options; JobDefinitionsView = CollectionViewSource.GetDefaultView(JobDefinitions) as ListCollectionView; if (JobDefinitionsView == null) { return; } JobDefinitionsView.CurrentChanged += (s, e) => { RaisePropertyChanged(() => CurrentSelectedJobDefinition); RaisePropertyChanged(() => IsJobDefinitionAvailable); CleanJobHistoryCommand.RaiseCanExecuteChanged(); InitJobsView(); }; JobDefinitions.CollectionChanged += (s, e) => { if (e.NewItems != null) { foreach (INotifyPropertyChanged added in e.NewItems) { added.PropertyChanged += JobDefintionsOnPropertyChanged; } } if (e.OldItems != null) { foreach (INotifyPropertyChanged removed in e.OldItems) { removed.PropertyChanged -= JobDefintionsOnPropertyChanged; } } }; } catch (Exception e) { Console.WriteLine(e); throw; } }
/// <inheritdoc /> protected override void OnInternalPropertyChanged(string propertyName) { if (propertyName == nameof(CurrentSelectedJobDefinition)) { Variables.CurrentSelectedJob = CurrentSelectedJobDefinition; RemoveJobCommand.RaiseCanExecuteChanged(); StartStopJobCommand.RaiseCanExecuteChanged(); if (CurrentSelectedJobDefinition == null && JobDefinitions.Any()) { CurrentSelectedJobDefinition = JobDefinitions.First(); } } base.OnInternalPropertyChanged(propertyName); }
/// <inheritdoc /> protected override void InitCommands() { AddJobCommand = new RelayCommand( () => { MessengerInstance.Send(new AddJobWindowOpenMessage()); }); RemoveJobCommand = new RelayCommand <Window>( window => { if (window == null) { window = MessageListener.GetWindow(typeof(MainWindow)); } if (MessageBox.Show(window, "Do you want to delete the job?", "Delete job", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes) { JobDefinitions.Remove(CurrentSelectedJobDefinition); CurrentSelectedJobDefinition = null; if (JobDefinitionsView.Count > 0) { JobDefinitionsView.MoveCurrentToFirst(); } } }, window => CurrentSelectedJobDefinition != null); ClosingCommand = new RelayCommand( () => { while (JobDefinitions.Any(j => j.CurrentJob != null && j.CurrentJob.State == JobStateEnum.Stopping)) { Task.Delay(100).Wait(); } JobDefinitions.Where(j => j.CurrentJob != null && !j.CurrentJob.Finished.HasValue).ToList().ForEach( j => { j.CurrentJob.Stop(); }); SaveCommand.Execute(null); }); LoadedCommand = new RelayCommand(ReloadOptions); SaveCommand = new RelayCommand( () => { Options.JobDefinitions = JobDefinitions.ToList(); var fileContent = JsonConvert.SerializeObject(Options); try { File.WriteAllText(_optionsFilePath, fileContent); } catch (Exception ex) { // TODO Trace.TraceError(ex.Message); } }); StartStopJobCommand = new RelayCommand <JobModel>( jobDef => { if (jobDef == null) { // don't know if this can happen return; } if (jobDef.IsBusy) { jobDef.CurrentJob.Stop(); return; } CurrentSelectedPingJob = jobDef.StartNew(); }, job => CurrentSelectedJobDefinition != null); CleanJobHistoryCommand = new RelayCommand <JobModel>( jobDef => { if (jobDef == null) { // don't know if this can happen return; } jobDef.Jobs.Clear(); JobDefinitionsView.Refresh(); }, job => CurrentSelectedJobDefinition != null && !CurrentSelectedJobDefinition.IsBusy && CurrentSelectedJobDefinition.Jobs.Any()); }
/// <summary> /// Provides possiblity to add jobs to the internal list. /// </summary> /// <param name="newJob">The data for the new job.</param> public void AddJob(JobModel newJob) { JobDefinitions.Add(newJob); CurrentSelectedJobDefinition = newJob; }