tbf/WorkflowConfigurator/MainWnd.cs
2022-01-07 12:23:23 +01:00

141 lines
5.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using log4net;
using NHibernate;
using Common;
using SharedDatabase;
using SharedDatabase.Entities;
using WorkflowConfigurator.Resources;
namespace WorkflowConfigurator
{
public partial class MainWnd : Form
{
static readonly ILog log = LogManager.GetLogger(typeof(MainWnd));
public MainWnd()
{
InitializeComponent();
///
/// Loads processes from the database.
/// In case of problems (missing database) makes it possible to create a new one.
///
DialogResult settingsDR = DialogResult.OK;
do
{
if (settingsDR == DialogResult.OK)
{
try
{
TracingDB.Session = TracingDB.CreateSession(Program.LocalSettings.ConnectionString);
IList<WorkplaceRegistration> wPlaceRegistations = TracingDB.Session.QueryOver<WorkplaceRegistration>().List(); /// Try DB connection
}
catch (Exception exc)
{
log.FatalFormat("{0}:{1}{2}", Strings.Opening_database_failed, Environment.NewLine, exc.Message);
DialogResult rslt = MessageBox.Show(string.Format("{0}:{1}{2}{3}{1}{4}",
Strings.Opening_database_failed,
Environment.NewLine,
exc.Message,
exc.InnerException != null ? Environment.NewLine + exc.InnerException.Message : string.Empty,
Strings.Do_you_want_to_change_settingsQM),
Strings.Error,
MessageBoxButtons.YesNo,
MessageBoxIcon.Exclamation);
if (rslt != DialogResult.Yes)
{
/// Settings ware not changed -> exit
throw new QuitAppException(Strings.Opening_database_failed);
}
settingsDR = new Forms.SettingsDlg().ShowDialog();
}
}
}
while (settingsDR != DialogResult.Cancel && (TracingDB.Session == null));
if (settingsDR == DialogResult.Cancel)
{
/// Settings were not changed -> exit
throw new QuitAppException(Strings.Opening_database_failed);
}
if (!string.IsNullOrEmpty(Program.LocalSettings.UsersDBConnString))
{
GlobalData.LocalUsersDB = new DBSettings(Common.DBType.MySql, Program.LocalSettings.UsersDBConnString);
}
}
private void MainWnd_Load(object sender, EventArgs e)
{
PrintTitle(false);
Controls.Add(new UserControls.OverviewOfProcessesCtrl(this));
Settings2UI();
}
private void MainWnd_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = false;
UI2Settings();
Program.LocalSettings.Save();
}
#region Helper functions
public static void PrintTitle(bool busy)
{
string user = GlobalData.GetCurrentUserName();
if (!string.IsNullOrEmpty(user)) user = string.Format(" ({0})", user);
Program.MainWnd.Text = string.Format(busy ? "{0} v.{1}{2} ... reading database" : "{0} v.{1}{2}",
Strings.Workflow_configurator, Program.Version, user);
}
/// <summary>
/// Set UI properties (determining the window position, etc) to values obtained from local settings
/// </summary>
void Settings2UI()
{
LocalSettings ls = Program.LocalSettings;
///
WindowState = (ls.MWndMaximized ? FormWindowState.Maximized : FormWindowState.Normal);
Width = ls.MWndWidth;
Height = ls.MWndHeight;
Left = ls.MWndLeft;
Top = ls.MWndTop;
}
/// <summary>
/// Retrieve UI properties and save them to local settings
/// </summary>
void UI2Settings()
{
LocalSettings ls = Program.LocalSettings;
///
ls.MWndMaximized = (WindowState == FormWindowState.Maximized);
if (WindowState == FormWindowState.Normal)
{
ls.MWndLeft = Location.X;
ls.MWndTop = Location.Y;
ls.MWndWidth = Size.Width;
ls.MWndHeight = Size.Height; }
else
{
/// Maximized or minimized (when restarted, minimized window is restored as normal)
ls.MWndLeft = RestoreBounds.Left;
ls.MWndTop = RestoreBounds.Top;
ls.MWndWidth = RestoreBounds.Width;
ls.MWndHeight = RestoreBounds.Height;
}
}
#endregion
}
}