using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Tango.Core.Commands; using Tango.DAL.Observables; using Tango.MachineStudio.Common.Notifications; using Tango.MachineStudio.DB.Managers; using Tango.SharedUI; using Tango.MachineStudio.DB.ExtensionMethods; namespace Tango.MachineStudio.DB.ViewModels { public abstract class DbTableViewModel : ViewModel where T : class, IObservableEntity { private INotificationProvider _notification; /// /// Initializes a new instance of the class. /// public DbTableViewModel(INotificationProvider notification) : base() { _notification = notification; Adapter = ObservablesEntitiesAdapter.Instance; AddCommand = new RelayCommand(OnAdd); EditCommand = new RelayCommand(OnEdit,(x) => SelectedEntity != null); DeleteCommand = new RelayCommand(OnDelete, (x) => SelectedEntity != null); DialogOKCommand = new RelayCommand(() => OnDialogOKPressed(DialogOpenMode, EditEntity)); DialogCancelCommand = new RelayCommand(() => OnDialogCancelPressed(DialogOpenMode, EditEntity)); IsDialogOpen = false; } private T _editEntity; /// /// Gets or sets the edit entity. /// public T EditEntity { get { return _editEntity; } set { _editEntity = value; RaisePropertyChangedAuto(); } } private DialogOpenMode _dialogOpenMode; /// /// Gets or sets the dialog open mode. /// public DialogOpenMode DialogOpenMode { get { return _dialogOpenMode; } set { _dialogOpenMode = value; RaisePropertyChangedAuto(); } } private bool _isDialogOpen; /// /// Gets or sets a value indicating whether this instance is dialog open. /// public bool IsDialogOpen { get { return _isDialogOpen; } set { _isDialogOpen = value; RaisePropertyChangedAuto(); } } private ObservablesEntitiesAdapter _adapter; /// /// Gets or sets the DB adapter. /// public ObservablesEntitiesAdapter Adapter { get { return _adapter; } set { _adapter = value; RaisePropertyChangedAuto(); } } private T _selectedEntity; /// /// Gets or sets the selected entity. /// public T SelectedEntity { get { return _selectedEntity; } set { _selectedEntity = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); } } private String _filter; /// /// Gets or sets the search filter. /// public String Filter { get { return _filter; } set { _filter = value; RaisePropertyChangedAuto(); OnFilterChanged(value); } } /// /// Gets or sets the dialog OK command. /// public RelayCommand DialogOKCommand { get; set; } /// /// Gets or sets the dialog cancel command. /// public RelayCommand DialogCancelCommand { get; set; } /// /// Gets or sets the add command. /// public RelayCommand AddCommand { get; set; } /// /// Gets or sets the edit command. /// public RelayCommand EditCommand { get; set; } /// /// Gets or sets the delete command. /// public RelayCommand DeleteCommand { get; set; } /// /// Called when delete command invoked. /// protected virtual void OnDelete() { SelectedEntity.Deleted = true; SelectedEntity.Save(); } /// /// Called when edit command invoked. /// protected virtual void OnEdit() { DialogOpenMode = DialogOpenMode.Editing; EditEntity = GetEditableEntity(DialogOpenMode); _notification.ShowDialog(DialogOpenMode, this); IsDialogOpen = true; } /// /// Called when add command invoked. /// protected virtual void OnAdd() { DialogOpenMode = DialogOpenMode.Adding; EditEntity = GetEditableEntity(DialogOpenMode); _notification.ShowDialog(DialogOpenMode, this); IsDialogOpen = true; } /// /// Called when dialog closes with OK button. /// /// The mode. protected virtual void OnDialogOKPressed(DialogOpenMode mode, T entity) { if (mode == DialogOpenMode.Editing) { entity.ShallowCopyTo(SelectedEntity); entity = SelectedEntity; } OnBeforeEntitySave(mode, entity); entity.Save(); IsDialogOpen = false; SelectedEntity = EditEntity; SelectedEntity = null; } /// /// Called when [before entity save]. /// /// The mode. /// The entity. protected virtual void OnBeforeEntitySave(DialogOpenMode mode,T entity) { } /// /// Called when dialog closes with cancel button. /// /// The mode. protected virtual void OnDialogCancelPressed(DialogOpenMode mode, T entity) { IsDialogOpen = false; } /// /// Gets the editable entity. /// /// The mode. /// private T GetEditableEntity(DialogOpenMode mode) { if (mode == DialogOpenMode.Adding) { var newEntity = Activator.CreateInstance(); InitializeEntity(newEntity); return newEntity; } else { return SelectedEntity.ShallowClone(); } } protected virtual void OnFilterChanged(String filter) { } protected virtual void InitializeEntity(T entity) { } } }