Files
PatientMgr-CSharp/SaPatMgrWinForms/Win/Forms/Panels/SpmPanel_Operations.cs
T

605 lines
20 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sarand.Core.Bus;
using Sarand.PatientMgr.Core.Bus;
using Sarand.Win.Forms.Utils;
using Sarand.Core.Base;
using Sarand.PatientMgr.Core.App;
using Sarand.PatientMgr.Win.Forms.Maint;
using Sarand.PatientMgr.Win.Forms.Controls;
using Sarand.PatientMgr.Win.Forms.TabPages;
using Sarand.PatientMgr.Win.Forms.Filters;
using Sarand.PatientMgr.Win.Forms.Base;
namespace Sarand.PatientMgr.Win.Forms.Panels
{
//=======================================================================
public partial class SpmPanel_Operations : SpmPanel_Items
{
//-------------------------------------------------------------------
// Configuration.
public enum PanelMode
{
Search,
Maintain,
}
public new class Config : SpmPanel_Items.Config
{
public Config (PanelMode mode)
{
Mode = mode;
// Defaults based on mode.
DisplayFilter = false; // Mode == PanelMode.Maintain || Mode == PanelMode.Search;
DisplayTabs = Mode == PanelMode.Maintain;
DisplayActionButtons = Mode == PanelMode.Maintain;
DisplayNavigationButtons = Mode == PanelMode.Search;
}
public PanelMode Mode { get; } = PanelMode.Maintain;
public SpmForm SearchForm { get; set; }
// Display options.
public bool DisplayFilter { get; set; } = false;
public bool DisplayContextMenu { get; set; } = true;
public bool DisplayActionButtons { get; set; } = false;
public bool DisplayNavigationButtons { get; set; } = false;
public bool DisplayTabs { get; set; } = false;
}
//-------------------------------------------------------------------
// Data members.
private Config config = new Config(PanelMode.Search);
private readonly List<SpmTabPage_Operation> tabPages = new List<SpmTabPage_Operation>();
//-------------------------------------------------------------------
// Construction.
private SpmPanel_Operations () : this(new Config(PanelMode.Search)) { }
public SpmPanel_Operations (Config config) : base(config)
{
this.config = config;
InitializeComponent();
}
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (components != null) components.Dispose();
}
base.Dispose(disposing);
}
//-------------------------------------------------------------------
// Properties.
protected override String DefaultMessageTitle { get { return "Operation"; } }
public override int ItemTotal { get { return listViewOperation.ItemTotal; } }
public override int ItemCount { get { return listViewOperation.ItemCount; } }
public override int SelectedItemCount { get { return listViewOperation.SelectedItemCount; } }
public SysId SelectedItemId { get { return listViewOperation.SelectedItemId; } }
public List<SysId> SelectedItemIds { get { return listViewOperation.SelectedItemIds; } }
public SpmOperation SelectedOperation { get { return listViewOperation.SelectedOperation; } }
//-------------------------------------------------------------------
// Configure.
public void SetConfig (Config config)
{
base.SetConfig(config);
this.config = config;
ConfigurePanel();
}
//-------------------------------------------------------------------
// I n i t i a l i s a t i o n
//-------------------------------------------------------------------
protected override void OnCreateControl ()
{
base.OnCreateControl();
if (this.IsDesignerHosted()) return;
ConfigurePanel();
}
private void ConfigurePanel ()
{
if (!Created || this.IsDesignerHosted()) return;
// Configure filter.
if (config.DisplayFilter)
{
SpmItemListFilter_Operation.Config configFilter = new SpmItemListFilter_Operation.Config();
configFilter.Internals = Internals;
listFilterOperation.SetConfig(configFilter);
}
listFilterOperation.Visible = config.DisplayFilter;
// Configure list.
SpmItemListView_Operation.Config configList = new SpmItemListView_Operation.Config();
configList.Internals = Internals;
configList.Mode = SpmItemListView_Operation.ListMode.General;
listViewOperation.SetConfig(configList);
// Tabs.
tabControl.TabPages.Clear();
tabControl.Visible = config.DisplayTabs;
RefreshTabs();
// Configure buttons.
buttonCreate.Visible = config.DisplayActionButtons && SecurityProfile.Admin.CanManageConfig();
buttonUpdate.Visible = config.DisplayActionButtons;
buttonRemove.Visible = config.DisplayActionButtons && buttonCreate.Visible;
buttonOk.Visible = config.DisplayNavigationButtons && config.Mode == PanelMode.Search;
buttonCancel.Visible = config.DisplayNavigationButtons;
// Complete form initialisation.
RefreshDataItems();
SetControlActivity();
PositionControls();
}
//-------------------------------------------------------------------
// Initialise tabs.
private void InitTabs ()
{
String fn = GetType().Name + ".InitTabs";
tabControl.TabPages.Clear();
tabPages.Clear();
if (!config.DisplayTabs)
{
tabPages.Clear();
tabControl.TabPages.Clear();
tabControl.Visible = false;
return;
}
// Selected Operation.
SpmOperation operation = SelectedOperation;
// Updates.
List<SpmTabPage_Operation.PanelType> panelTypes_Updates = new List<SpmTabPage_Operation.PanelType>();
panelTypes_Updates.Add(SpmTabPage_Operation.PanelType.Updates);
if (panelTypes_Updates.Count > 0)
{
SpmTabPage_Operation.Config configTabPage = new SpmTabPage_Operation.Config();
configTabPage.Internals = Internals;
configTabPage.ReadOnly = ReadOnly;
configTabPage.PanelTypes.AddRange(panelTypes_Updates);
SpmTabPage_Operation tabPage = new SpmTabPage_Operation(configTabPage) { Text = "Updates" };
tabPages.Add(tabPage);
}
// Populate tab pages.
tabControl.Visible = tabPages.Count > 0;
foreach (var tabPage in tabPages)
tabControl.TabPages.Add(tabPage);
}
//-------------------------------------------------------------------
// P a n e l I n t e r f a c e
//-------------------------------------------------------------------
private String previouslySelectedTabPageText = "";
private void RefreshTabs ()
{
if (tabControl.SelectedTab != null)
previouslySelectedTabPageText = tabControl.SelectedTab.Text;
// Reinitialise tabs whenever the selection changes in case the set of tab pages displayed needs to be changed.
InitTabs();
// Select previous tab page if still present.
if (!StringUtil.IsEmpty(previouslySelectedTabPageText))
{
foreach (TabPage tabPage in tabControl.TabPages)
{
if (tabPage.Text.Equals(previouslySelectedTabPageText))
{
tabControl.SelectTab(tabPage);
break;
}
}
}
RefreshTabPages();
SetControlActivity();
PositionControls();
}
private void RefreshTabPages ()
{
// Selected Operation - populated when needed.
SpmOperation operation = null;
// Refresh tabs.
if (tabControl.Visible)
{
if (operation == null) operation = SelectedOperation;
SpmOperation.Data operationData = operation != null ? operation.GetData() : new SpmOperation.Data();
tabPages.PopulateToControls(operationData);
}
}
//-------------------------------------------------------------------
// F i l t e r
//-------------------------------------------------------------------
public bool ParamsAreValid (SpmOperation.Params prmOperation)
{
return true;
}
public SpmOperation.Params GetParams ()
{
SpmOperation.Params prmOperation = new SpmOperation.Params();
// Sort columns.
prmOperation.SortColumns.AddRange(listViewOperation.GetSortColumns());
return prmOperation;
}
//-------------------------------------------------------------------
// A c t i o n s
//-------------------------------------------------------------------
// Refresh.
public override void RefreshDataItems ()
{
SpmOperation.Params prmOperation = GetParams();
if (!ParamsAreValid(prmOperation)) prmOperation = null;
listViewOperation.RefreshOperations(prmOperation);
}
public override void RefreshDataItem (SysId id)
{
listViewOperation.RefreshOperation(id);
}
public override void RemoveDataItem (SysId id)
{
listViewOperation.RemoveItemById(id);
}
//-------------------------------------------------------------------
// Create item.
public override bool CanCreateItem ()
{
if (ReadOnly) return false;
if (!SecurityProfile.Admin.CanManageConfig()) return false;
return true;
}
public override void CreateItem ()
{
if (!CanCreateItem()) return;
if (config.Mode == PanelMode.Search) return;
// Create new Operation.
SpmForm_Operation.Config configOperation = new SpmForm_Operation.Config();
using (var form = new SpmForm_Operation(configOperation))
{
if (form.ShowDialog() == DialogResult.OK)
{
RefreshDataItem(form.NewItemId);
InvokeItemCreatedEvent(Spm.DataItemType.Operation, form.NewItemId);
}
}
}
//-------------------------------------------------------------------
// View/Update item.
public override bool CanViewItem ()
{
return true;
}
public override bool CanUpdateItem ()
{
if (ReadOnly) return false;
if (!SecurityProfile.Admin.CanManageConfig()) return false;
return true;
}
public override void UpdateItem (bool updateItem = true)
{
if (config.Mode == PanelMode.Search) return;
SpmOperation operation = SelectedOperation;
if (SelectedItemCount != 1) return;
if (!CanUpdateItem() && !CanViewItem()) return;
if (!CanUpdateItem()) updateItem = false;
if (operation == null)
{
MessageConsole.ShowMessageWarning("Failed to retrieve selected item");
return;
}
SpmForm_Operation.Config configOperation = new SpmForm_Operation.Config();
configOperation.OperationToUpdate = operation;
configOperation.ReadOnly = (config.ReadOnly || !updateItem);
using (SpmForm_Operation formOperation = new SpmForm_Operation(configOperation))
{
// Perform the update.
if (formOperation.ShowDialog() == DialogResult.OK)
{
// Operation has been updated.
RefreshDataItem(operation.Id);
listViewOperation.RefreshOperation(operation.Id);
InvokeItemUpdatedEvent(Spm.DataItemType.Operation, operation.Id);
}
}
}
//-------------------------------------------------------------------
// Delete item.
public override bool CanDeleteItem ()
{
if (ReadOnly) return false;
if (!SecurityProfile.Admin.CanManageConfig()) return false;
return true;
}
public override void DeleteItem ()
{
if (!CanDeleteItem()) return;
if (config.Mode == PanelMode.Search) return;
if (SelectedItemCount != 1) return;
SpmOperation operation = SelectedOperation;
if (operation == null)
{
MessageConsole.ShowMessageWarning("Failed to retrieve item for deletion.");
return;
}
if (!InstitutionMgr.OperationCanBeDeleted(operation))
{
MessageConsole.ShowMessageError("Unable to remove operation - " + InstitutionMgr.ErrorStateMessage + ".");
return;
}
if (MessageConsole.ShowMessageConfirm("Are you sure that this operation should be removed from the system?"))
{
if (InstitutionMgr.DeleteOperation(operation, DbUpdateInfo))
{
int selectIdx = listViewOperation.SelectedIndex;
listViewOperation.RemoveItemById(operation.Id.ToString());
if (selectIdx >= listViewOperation.ItemCount - 1) selectIdx--;
listViewOperation.SelectedIndices.Clear();
listViewOperation.SelectedIndices.Add(selectIdx);
}
else MessageConsole.ShowMessageError("Failed to remove operation - " + InstitutionMgr.ErrorStateMessage + ".");
}
}
//-------------------------------------------------------------------
// Import data.
public override bool CanImportData ()
{
return false;
}
public override void ImportData ()
{
if (!CanImportData()) return;
}
//-------------------------------------------------------------------
// Export data.
public override bool CanExportData ()
{
return false;
}
public override void ExportData ()
{
}
//-------------------------------------------------------------------
// C o n t e x t M e n u
//-------------------------------------------------------------------
private SpmContextMenuStrip contextMenuStrip = new SpmContextMenuStrip();
private void RefreshContextMenu ()
{
contextMenuStrip.Name = "Operation";
contextMenuStrip.Items.Clear();
if (CanCreateItem())
contextMenuStrip.Items.Add("Create", null, CreateItem);
if (SysId.IsValid(listViewOperation.SelectedItemId))
{
SpmOperation operation = SelectedOperation;
if (operation != null)
{
if (CanUpdateItem() || CanDeleteItem())
{
if (contextMenuStrip.Items.Count > 0)
contextMenuStrip.Items.Add(new ToolStripSeparator());
contextMenuStrip.Items.Add(CanUpdateItem() ? "Update" : "View", null, UpdateItem);
if (CanDeleteItem())
contextMenuStrip.Items.Add("Remove", null, DeleteItem);
}
}
}
if (CanExportData() || CanImportData())
{
if (contextMenuStrip.Items.Count > 0)
contextMenuStrip.Items.Add(new ToolStripSeparator());
if (CanImportData())
contextMenuStrip.Items.Add("Import", null, ImportData);
if (CanExportData())
contextMenuStrip.Items.Add("Export", null, ExportData);
}
}
//-------------------------------------------------------------------
// C o n t r o l A c t i v i t y
//-------------------------------------------------------------------
protected override void SetControlActivity ()
{
SetControlsReadOnly();
bool itemSelected = SysId.IsValid(SelectedItemId);
listViewOperation.Enabled = Enabled;
buttonCreate.Enabled = Enabled;
buttonUpdate.Enabled = Enabled && itemSelected;
buttonRemove.Enabled = Enabled && itemSelected;
buttonOk.Enabled = Enabled;
buttonCancel.Enabled = Enabled;
}
protected override void SetControlsReadOnly ()
{
}
//-------------------------------------------------------------------
// P o s i t i o n C o n t r o l s
//-------------------------------------------------------------------
protected override void PositionControls ()
{
if (this.IsDesignerHosted() || !Created) return;
Rectangle clientRect = ClientRectangle;
//---------------------------------------------------------------
// Filter panel.
if (listFilterOperation.Visible)
{
listFilterOperation.PositionAtTopLeftOf(this);
listFilterOperation.SetFullWidthOf(this);
listFilterOperation.ResizeToBottomControl();
listViewOperation.PositionBelow(listFilterOperation);
}
else
{
listViewOperation.PositionAtTopLeftOf(this);
}
//---------------------------------------------------------------
// List view.
int buttonLeft = clientRect.Width - buttonCreate.Width - buttonCreate.Margin.Right - Padding.Right;
listViewOperation.Left = listViewOperation.Margin.Left;
listViewOperation.Width = buttonLeft - buttonCreate.Margin.Left - listViewOperation.Left - listViewOperation.Margin.Right;
if (!(buttonCreate.Visible || buttonUpdate.Visible || buttonRemove.Visible || buttonOk.Visible || buttonCancel.Visible))
listViewOperation.Width = clientRect.Width - Padding.Right - listViewOperation.Left - listViewOperation.Margin.Right;
//---------------------------------------------------------------
// Tab control.
if (tabControl.Visible)
{
tabControl.Left = listViewOperation.Left;
tabControl.Width = clientRect.Width - (tabControl.Left * 2);
listViewOperation.Height = (int)((clientRect.Height - listViewOperation.Top) * 0.40);
tabControl.Top = listViewOperation.Bottom + listViewOperation.Margin.Bottom + tabControl.Margin.Top;
tabControl.Height = clientRect.Height - tabControl.Top - tabControl.Margin.Bottom - Padding.Bottom;
}
else
{
listViewOperation.Height = clientRect.Height - listViewOperation.Top - listViewOperation.Margin.Bottom - Padding.Bottom;
}
//---------------------------------------------------------------
// Create button.
int buttonTop = listViewOperation.Top + listViewOperation.HeaderHeight;
buttonCreate.Top = buttonTop;
buttonCreate.Left = buttonLeft;
if (buttonCreate.Visible)
buttonTop += buttonCreate.Height + buttonCreate.Margin.Bottom;
// Update button.
buttonUpdate.Top = buttonTop;
buttonUpdate.Left = buttonLeft;
if (buttonUpdate.Visible)
buttonTop += buttonUpdate.Height + buttonUpdate.Margin.Bottom;
// Remove button.
buttonRemove.Top = buttonTop;
buttonRemove.Left = buttonLeft;
if (buttonRemove.Visible)
buttonTop += buttonRemove.Height + buttonRemove.Margin.Bottom;
//---------------------------------------------------------------
// Navigation buttons.
// Cancel button.
buttonCancel.Top = clientRect.Bottom - Padding.Bottom - buttonCancel.Margin.Bottom - buttonCancel.Height;
buttonCancel.Left = buttonLeft;
// OK button.
buttonOk.Top = buttonCancel.Top - buttonOk.Height - buttonCancel.Padding.Top - buttonCancel.Margin.Bottom;
buttonOk.Left = buttonLeft;
if (buttonOk.Visible)
buttonTop += buttonOk.Height + buttonOk.Margin.Bottom;
//---------------------------------------------------------------
}
//-------------------------------------------------------------------
// P a n e l E v e n t s
//-------------------------------------------------------------------
// Filter.
private void listFilterOperation_FilterChanged (object sender, EventArgs e)
{
RefreshDataItems();
SetControlActivity();
}
//-------------------------------------------------------------------
// Item list.
private void listViewOperation_ColumnClick (object sender, ColumnClickEventArgs e)
{
RefreshDataItems();
}
private void listViewOperation_ItemCountChanged (object sender, Sarand.Win.Forms.Controls.SacsListView.ItemCountChangedEventArgs e)
{
InvokeItemCountChangedEvent(e);
}
private void listViewOperation_SelectedIndexChanged (object sender, EventArgs e)
{
RefreshTabs();
InvokeItemSelectionChangedEvent(e);
SetControlActivity();
}
private void listViewOperation_DoubleClick (object sender, EventArgs e)
{
if (config.Mode == PanelMode.Search)
{
//if (config.SearchForm != null) config.SearchForm.OnOK();
}
else
UpdateItem();
SetControlActivity();
}
private void listViewOperation_MouseUp (object sender, MouseEventArgs e)
{
switch (e.Button)
{
case MouseButtons.Right:
// Refresh the context menu and display at pointer position.
RefreshContextMenu();
contextMenuStrip.Show(listViewOperation, new Point(e.X, e.Y), ToolStripDropDownDirection.Default);
break;
}
SetControlActivity();
}
//-------------------------------------------------------------------
// Actions.
private void buttonCreate_Click (object sender, EventArgs e)
{
CreateItem();
SetControlActivity();
}
private void buttonUpdate_Click (object sender, EventArgs e)
{
UpdateItem();
SetControlActivity();
}
private void buttonRemove_Click (object sender, EventArgs e)
{
DeleteItem();
SetControlActivity();
}
private void buttonOk_Click (object sender, EventArgs e)
{
if (config.SearchForm != null)
{
//config.SearchForm.OnOK();
return;
}
SetControlActivity();
}
private void buttonCancel_Click (object sender, EventArgs e)
{
if (config.SearchForm != null)
{
//config.SearchForm.OnCancel();
return;
}
SetControlActivity();
}
//-------------------------------------------------------------------
}
//=======================================================================
}