Source

main / Source / Excel / Excel.AddIn / Excel.AddIn / ViewModels / ErrorPaneViewModel.cs

Full commit
Naethra D ba1cb57 




























































































































































































































































































using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows.Input;
using System.Windows.Threading;

namespace DataUp.Excel.Addin
{
    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class ErrorPaneViewModel : ViewModelBase
    {
        private ErrorDetailViewModel selectedError;
        private ICommand deleteEntityCommand;
        private ICommand fixAllCommand;
        private ICommand fixSelectedCommand;
        private ICommand selectSheetCommand;
        private bool disableRemoveAll;

        private ErrorDetailsViewModelBase selectedSheet;

        public ObservableCollection<ErrorDetailsViewModelBase> SheetDetails { get; set; }

        public ICommand DeleteEntityCommand
        {
            get
            {
                return deleteEntityCommand;
            }
            set
            {
                if (deleteEntityCommand != value)
                {
                    deleteEntityCommand = value;
                    OnNotifyPropertyChanged("DeleteEntityCommand");
                }
            }
        }

        public bool DisableRemoveAll
        {
            get
            {
                return disableRemoveAll;
            }
            set
            {
                disableRemoveAll = value;
                OnNotifyPropertyChanged("DisableRemoveAll");
            }
        }

        public ICommand FixAllCommand
        {
            get
            {
                return fixAllCommand;
            }
            set
            {
                if (fixAllCommand != value)
                {
                    fixAllCommand = value;
                    OnNotifyPropertyChanged("FixAllCommand");
                }
            }
        }

        public ICommand FixSelectedCommand
        {
            get
            {
                return fixSelectedCommand;
            }
            set
            {
                if (fixSelectedCommand != value)
                {
                    fixSelectedCommand = value;
                    OnNotifyPropertyChanged("FixSelectedCommand");
                }
            }
        }

        public ICommand SelectSheetCommand
        {
            get
            {
                return selectSheetCommand;
            }
            set
            {
                if (selectSheetCommand != value)
                {
                    selectSheetCommand = value;
                    OnNotifyPropertyChanged("SelectSheetCommand");
                }
            }
        }

        public ErrorDetailViewModel SelectedError
        {
            get
            {
                return selectedError;
            }
            set
            {
                selectedError = value;
                OnNotifyPropertyChanged("SelectedError");
                SelectError(this.SelectedError);
            }
        }

        public ErrorDetailsViewModelBase SelectedSheet
        {
            get
            {
                return selectedSheet;
            }
            set
            {
                selectedSheet = value;
                OnNotifyPropertyChanged("SelectedSheet");

                if (value != null)
                {
                    this.SelectedSheet.OnRefresh += new EventHandler(OnRefresh);
                }
            }
        }

        public bool IsLoaded { get; set; }

        public ErrorPaneViewModel()
        {
            this.SheetDetails = new ObservableCollection<ErrorDetailsViewModelBase>();

            InitCommands();
        }

        private void OnRefresh(object sender, EventArgs e)
        {
            RefreshErrors();
        }

        public void Validate(bool selectSummary = true)
        {
            using (BackgroundWorker worker = new BackgroundWorker())
            {
                Dispatcher UiDispatcher = Dispatcher.CurrentDispatcher;
                worker.DoWork += (o, ea) =>
                {
                    var errors = Controller.Instance.GetErrors().ToList();

                    UiDispatcher.Invoke((Action)(() =>
                    {
                        this.Update(errors, Controller.Instance.GetSheetNames());

                        if (selectSummary)
                        {
                            this.SelectSummary();
                        }
                    }));
                };

                worker.RunWorkerCompleted += (o, ea) =>
                {
                    this.RaiseRunWorkerCompleted(o, ea);
                    IsBusy = false;
                };

                this.IsBusy = true;
                this.RaiseRunWorkerStarted(this);
                worker.RunWorkerAsync();
            }
        }

        public void SelectSheet(string sheetName)
        {
            var sheetDetail = this.SheetDetails.Where(pane => (string.Compare(pane.SheetName, sheetName, true) == 0 && pane is SheetViewModel)).FirstOrDefault();
            if (sheetDetail != null)
            {
                this.SelectedSheet = sheetDetail;
            }
            else
            {
                this.SelectSummary();
            }
        }

        public void SelectSummary()
        {
            // var sheetDetail = this.SheetDetails.Where(pane => pane is SummaryViewModel).FirstOrDefault();
            var sheetDetail = this.SheetDetails.FirstOrDefault();
            if (sheetDetail != null)
            {
                this.SelectedSheet = sheetDetail;
            }
        }

        private void InitCommands()
        {
            this.DeleteEntityCommand = new DelegateCommand<ErrorDetailViewModel>(error =>
            {
                if (error != null)
                {
                    // Controller.Instance.FixError(error);
                    Controller.Instance.ResolveAllErrors(error.Error, error.SheetName);
                    try
                    {
                        foreach (var item in SheetDetails)
                        {
                            if (string.Compare(item.SheetName, error.SheetName, true) == 0)
                            {
                                var sheetViewModel = (item as SheetViewModel);
                                if (sheetViewModel != null)
                                {
                                    sheetViewModel.Errors.Remove(error);
                                    sheetViewModel.TotalErrorCount = sheetViewModel.Errors.Count;
                                }
                                break;
                            }
                        }
                    }
                    catch { }
                }
            });

            this.FixAllCommand = new DelegateCommand<object>(error =>
            {
                Controller.Instance.ResolveAllErrors();
            });

            this.SelectSheetCommand = new DelegateCommand<SummaryDetailViewModel>(sheet =>
            {
                if (sheet != null)
                {
                    this.SelectSheet(sheet.SheetName);
                }
            });

            this.FixSelectedCommand = new DelegateCommand(this.RemoveSelectedItemsExecute, this.RemoveSelectedCanExecute);
        }

        private void SelectError(ErrorDetailViewModel error)
        {
            if (error != null)
            {
                Controller.Instance.SelectError(error);
            }
        }

        private void RemoveSelectedItemsExecute()
        {
            if (this.SelectedSheet is SheetViewModel)
            {
                (this.SelectedSheet as SheetViewModel).RemoveSelected();
            }
        }

        private bool RemoveSelectedCanExecute()
        {
            bool canExecute = false;
            if (this.SelectedSheet is SheetViewModel)
            {
                var sheetViewModel = this.SelectedSheet as SheetViewModel;

                canExecute = sheetViewModel.Errors.ToList().FindAll(s => s.IsRemovable).Count > 0;
            }

            return canExecute;
        }

        private void RefreshErrors()
        {
            var currentSheetName = this.SelectedSheet.SheetName;

            this.Validate(false);

            this.SelectSheet(currentSheetName);
        }
    }
}