public DefaultCommandsHandler(
            IDatabaseInteractions databaseInteractions)
        {
            _databaseInteractions = databaseInteractions;

            Add(ApplicationCommands.Open, (sender, args) =>
            {
                _databaseInteractions.OpenDatabase();
            });

            Add(ApplicationCommands.New, (sender, args) =>
            {
                _databaseInteractions.CreateAndOpenDatabase();
            });

            Add(ApplicationCommands.Close, (sender, args) =>
            {
                Store.Current.CloseSelectedDatabase();
            }, (sender, e) =>
            {
                e.CanExecute = Store.Current.HasSelectedDatabase;
            });

            Add(ApplicationCommands.Copy, (sender, args) =>
            {
                _databaseInteractions.CopyDocuments(Store.Current.SelectedDocuments);
            }, (sender, e) =>
            {
                e.CanExecute = Store.Current.SelectedDocumentsCount > 0 && Store.Current.SelectedCollection != null && Store.Current.SelectedCollection.Name != "_files";
            });

            Add(ApplicationCommands.Paste, (sender, args) =>
            {
                try
                {
                    var textData = Clipboard.GetText();
                    _databaseInteractions.ImportDataFromText(Store.Current.SelectedCollection, textData);
                }
                catch (Exception exc)
                {
                    Logger.Warn(exc, "Cannot process clipboard data.");
                    MessageBox.Show("Failed to paste document from clipboard: " + exc.Message, "Import Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }, (sender, e) =>
            {
                e.CanExecute = Store.Current.SelectedCollection != null && Store.Current.SelectedCollection.Name != "_files" && Clipboard.ContainsText();
            });


            Add(Commands.EditDbProperties, (sender, args) =>
            {
                _databaseInteractions.OpenDatabaseProperties(Store.Current.SelectedDatabase.LiteDatabase);
            }, (sender, e) =>
            {
                e.CanExecute = Store.Current.HasSelectedDatabase;
            });


            Add(Commands.Exit, (sender, args) =>
            {
                Store.Current.CloseDatabases();

                if (Application.Current.MainWindow != null)
                {
                    Application.Current.MainWindow.Close();
                }
            });

            Add(Commands.Add, (sender, args) =>
            {
                var result = _databaseInteractions.CreateItem(Store.Current.SelectedCollection);
                if (result.IsSuccess)
                {
                    var reference = result.Value;

                    if (reference.Type == DocumentType.File)
                    {
                        Store.Current.SelectCollection(Store.Current.SelectedCollection.Database.Collections.First(a => a.Name == "_files"));
                        Store.Current.SelectDocument(reference.DocumentReference);
                    }
                    else
                    {
                        Store.Current.SelectDocument(reference.DocumentReference);
                        // UpdateGridColumns(reference.DocumentReference.LiteDocument);
                    }

                    // CollectionListView.ScrollIntoSelectedItem();

                    // UpdateDocumentPreview();
                }
            }, (sender, e) =>
            {
                e.CanExecute = Store.Current.HasSelectedDatabase;
            });

            Add(Commands.AddFile, (sender, args) =>
            {
                var database = Store.Current.SelectedDatabase;
                var result   = _databaseInteractions.AddFileToDatabase(database);
                if (result.IsSuccess)
                {
                    Store.Current.SelectCollection(database.Collections.First(a => a.Name == "_files"));
                    Store.Current.SelectDocument(result.Value.DocumentReference);

                    // CollectionListView.ScrollIntoSelectedItem();
                }
            }, (sender, e) =>
            {
                e.CanExecute = Store.Current.HasSelectedDatabase;
            });

            Add(Commands.Edit, (sender, args) =>
            {
                var item = Store.Current.SelectedDocument;

                var document = _databaseInteractions.OpenEditDocument(item);
                if (document.HasValue)
                {
                    // UpdateGridColumns(document.Value.LiteDocument);
                    // UpdateDocumentPreview();
                }
            }, (sender, e) =>
            {
                e.CanExecute = Store.Current.SelectedDocumentsCount == 1;
            });

            Add(Commands.Remove, (sender, args) =>
            {
                var currentSelectedDocuments = Store.Current.SelectedDocuments.ToList();
                _databaseInteractions.RemoveDocuments(currentSelectedDocuments);
            }, (sender, e) =>
            {
                e.CanExecute = Store.Current.SelectedDocumentsCount > 0;
            });

            Add(Commands.Export, (sender, args) =>
            {
                _databaseInteractions.ExportDocuments(Store.Current.SelectedDocuments);
            }, (sender, e) =>
            {
                e.CanExecute = Store.Current.SelectedDocumentsCount > 0;
            });

            Add(Commands.RefreshCollection, (sender, args) =>
            {
                Store.Current.SelectedCollection?.Refresh();
            }, (sender, e) =>
            {
                e.CanExecute = Store.Current.HasSelectedCollection;
            });

            Add(Commands.AddCollection, (sender, args) =>
            {
                _databaseInteractions.AddCollection(Store.Current.SelectedDatabase);
            }, (sender, e) =>
            {
                e.CanExecute = Store.Current.HasSelectedDatabase;
            });

            Add(Commands.RenameCollection, (sender, args) =>
            {
                _databaseInteractions.RenameCollection(Store.Current.SelectedCollection);
            }, (sender, e) =>
            {
                e.CanExecute = Store.Current.SelectedCollection != null && Store.Current.SelectedCollection.Name != "_files" && Store.Current.SelectedCollection.Name != "_chunks";
            });

            Add(Commands.DropCollection, (sender, args) =>
            {
                var result = _databaseInteractions.DropCollection(Store.Current.SelectedCollection);
                if (result.IsSuccess && result.Value)
                {
                    Store.Current.ResetSelectedCollection();
                }
            }, (sender, e) =>
            {
                e.CanExecute = Store.Current.SelectedCollection != null && Store.Current.SelectedCollection.Name != "_files" && Store.Current.SelectedCollection.Name != "_chunks";
            });


            Add(Commands.ExportCollection, (sender, args) =>
            {
                _databaseInteractions.ExportCollection(Store.Current.SelectedCollection);
            }, (sender, e) =>
            {
                e.CanExecute = Store.Current.SelectedCollection != null && Store.Current.SelectedCollection.Name != "_files" && Store.Current.SelectedCollection.Name != "_chunks";
            });


            Add(Commands.RefreshDatabase, (sender, args) =>
            {
                Store.Current.ResetSelectedCollection();
                Store.Current.SelectedDatabase.Refresh();
            }, (sender, e) =>
            {
                e.CanExecute = Store.Current.HasSelectedDatabase;
            });
        }
Exemple #2
0
        public DefaultCommandsHandler(
            IDatabaseInteractions databaseInteractions,
            IViewInteractionResolver viewInteractionResolver,
            IEventAggregator eventAggregator)
        {
            _databaseInteractions    = databaseInteractions;
            _viewInteractionResolver = viewInteractionResolver;
            _eventAggregator         = eventAggregator;

            Add(Commands.Exit, (sender, args) =>
            {
                Store.Current.CloseDatabases();

                if (Application.Current.MainWindow != null)
                {
                    Application.Current.MainWindow.Close();
                }
            });

            Add(ApplicationCommands.Open, (sender, args) =>
            {
                _databaseInteractions.OpenDatabase();
            });

            Add(ApplicationCommands.New, (sender, args) =>
            {
                _databaseInteractions.CreateAndOpenDatabase();
            });

            Add(ApplicationCommands.Close, (sender, args) =>
            {
                GetDatabaseReference(sender, args)
                .OnSuccess(reference =>
                {
                    _databaseInteractions.CloseDatabase(reference);
                    _eventAggregator.PublishOnUIThread(new InteractionEvents.DatabaseClosed(reference));
                });
            }, (sender, args) =>
            {
                args.CanExecute = HasDatabaseReference(sender, args);
            });

            Add(ApplicationCommands.Copy, (sender, args) =>
            {
                GetManyDocumentsReference(sender, args)
                .OnSuccess(references => _databaseInteractions.CopyDocuments(references));
            }, (sender, e) =>
            {
                e.CanExecute = HasAnyDocumentsReference(sender, e, DocumentTypeFilter.BsonDocument);
            });

            Add(ApplicationCommands.Paste, (sender, args) =>
            {
                try
                {
                    GetCollectionReference(sender, args)
                    .OnSuccess(reference =>
                    {
                        var textData = Clipboard.GetText();
                        return(_databaseInteractions.ImportDataFromText(reference, textData));
                    })
                    .OnSuccess(update => _eventAggregator.PublishOnUIThread(update));
                }
                catch (Exception exc)
                {
                    Logger.Warn(exc, "Cannot process clipboard data.");
                    MessageBox.Show("Failed to paste document from clipboard: " + exc.Message, "Import Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }, (sender, e) =>
            {
                e.CanExecute = NotIsFilesCollection(sender, e) && Clipboard.ContainsText();
            });


            Add(Commands.EditDbProperties, (sender, args) =>
            {
                GetDatabaseReference(sender, args)
                .OnSuccess(reference => _viewInteractionResolver.OpenDatabaseProperties(reference.LiteDatabase));
            }, (sender, args) =>
            {
                args.CanExecute = HasDatabaseReference(sender, args);
            });

            Add(Commands.Add, (sender, args) =>
            {
                GetCollectionReference(sender, args)
                .OnSuccess(reference => _databaseInteractions.CreateItem(reference))
                .OnSuccess(reference =>
                {
                    _viewInteractionResolver.ActivateCollection(reference.CollectionReference, reference.NewDocuments);
                    _eventAggregator.PublishOnUIThread(reference);
                });
            }, (sender, args) =>
            {
                args.CanExecute = HasCollectionReference(sender, args);
            });

            Add(Commands.AddFile, (sender, args) =>
            {
                GetDatabaseReference(sender, args)
                .OnSuccess(reference => _databaseInteractions.AddFileToDatabase(reference))
                .OnSuccess(reference =>
                {
                    _viewInteractionResolver.ActivateCollection(reference.CollectionReference, reference.NewDocuments);
                    _eventAggregator.PublishOnUIThread(reference);
                });
            }, (sender, args) =>
            {
                args.CanExecute = HasDatabaseReference(sender, args);
            });

            Add(Commands.Edit, (sender, args) =>
            {
                GetDocumentReference(sender, args)
                .OnSuccess(reference => _databaseInteractions.OpenEditDocument(reference))
                .OnSuccess(maybe =>
                {
                    maybe.Execute(value => _eventAggregator.PublishOnUIThread(new InteractionEvents.DocumentUpdated(value)));
                });
            }, (sender, args) =>
            {
                args.CanExecute = HasDocumentReference(sender, args);
            });

            Add(Commands.AddCollection, (sender, args) =>
            {
                GetDatabaseReference(sender, args)
                .OnSuccess(reference =>
                {
                    var addCollection = _databaseInteractions.AddCollection(reference);
                    if (addCollection.IsSuccess)
                    {
                        _viewInteractionResolver.ActivateCollection(addCollection.Value);
                    }
                });
            }, (sender, args) =>
            {
                args.CanExecute = HasDatabaseReference(sender, args);
            });

            Add(Commands.Remove, (sender, args) =>
            {
                GetManyDocumentsReference(sender, args)
                .OnSuccess(references => _databaseInteractions.RemoveDocuments(references));
            }, (sender, args) =>
            {
                args.CanExecute = HasAnyDocumentsReference(sender, args);
            });

            Add(Commands.Export, (sender, args) =>
            {
                GetManyDocumentsReference(sender, args)
                .OnSuccess(references => _databaseInteractions.ExportDocuments(references.ToList()));
            }, (sender, args) =>
            {
                args.CanExecute = HasAnyDocumentsReference(sender, args);
            });

            Add(Commands.RefreshCollection, (sender, args) =>
            {
                GetCollectionReference(sender, args)
                .OnSuccess(reference => reference.Refresh());
            }, (sender, e) =>
            {
                e.CanExecute = HasCollectionReference(sender, e);
            });

            Add(Commands.RenameCollection, (sender, args) =>
            {
                GetCollectionReference(sender, args)
                .OnSuccess(reference => _databaseInteractions.RenameCollection(reference));
            }, (sender, args) =>
            {
                args.CanExecute = NotIsFilesCollection(sender, args);
            });

            Add(Commands.DropCollection, (sender, args) =>
            {
                GetCollectionReference(sender, args)
                .OnSuccess(reference =>
                {
                    var dropCollection = _databaseInteractions.DropCollection(reference);
                    if (dropCollection.IsSuccess)
                    {
                        _eventAggregator.PublishOnUIThread(new InteractionEvents.CollectionRemoved(reference));
                    }
                });
            }, (sender, args) =>
            {
                args.CanExecute = HasCollectionReference(sender, args);
            });


            Add(Commands.ExportCollection, (sender, args) =>
            {
                GetCollectionReference(sender, args)
                .OnSuccess(reference => _databaseInteractions.ExportCollection(reference));
            }, (sender, args) =>
            {
                args.CanExecute = HasCollectionReference(sender, args);
            });


            Add(Commands.RefreshDatabase, (sender, args) =>
            {
                GetDatabaseReference(sender, args)
                .OnSuccess(reference => reference.Refresh());
            }, (sender, args) =>
            {
                args.CanExecute = HasDatabaseReference(sender, args);
            });

            Add(Commands.RevealInExplorer, (sender, args) =>
            {
                GetDatabaseReference(sender, args)
                .OnSuccess(reference => _databaseInteractions.RevealInExplorer(reference));
            }, (sender, args) =>
            {
                args.CanExecute = HasDatabaseReference(sender, args);
            });
        }