diff ServerMonitor/Forms/ServerForm.cs @ 8:052aa62cb42a

Single instance. Add autorun option. Add icons. General enhancements.
author Brad Greco <brad@bgreco.net>
date Sat, 09 Mar 2019 20:14:03 -0500
parents b6fe203af9d5
children 2db36ab759de
line wrap: on
line diff
--- a/ServerMonitor/Forms/ServerForm.cs	Fri Mar 01 21:39:22 2019 -0500
+++ b/ServerMonitor/Forms/ServerForm.cs	Sat Mar 09 20:14:03 2019 -0500
@@ -54,12 +54,15 @@
             if (isNewServer)
             {
                 LoginComboBox.SelectedIndex = 0;
+                Icon = CheckStatus.Success.GetIcon();
             }
             else
             {
                 SetTitle();
+                SetIcon();
                 NameTextBox.Text = Server.Name;
                 HostTextBox.Text = Server.Host;
+                EnabledCheckBox.Checked = Server.Enabled;
                 PortTextBox.Text = Server.Port.ToString();
                 UsernameTextBox.Text = Server.Username;
                 PasswordTextBox.Text = "********************";
@@ -71,6 +74,9 @@
             BindChangeListeners();
             FormatImageButtons();
             UpdateCheckButtons();
+
+            if (NameTextBox.Text == string.Empty)
+                ActiveControl = NameTextBox;
         }
 
         public void Show(bool activate)
@@ -85,6 +91,7 @@
             if (e.Check.Server != Server)
                 return;
             CheckGrid.Refresh();
+            SetIcon();
             if (e.CheckResult != null && logResults != null)
             {
                 logResults.Insert(0, e.CheckResult);
@@ -102,6 +109,7 @@
         {
             Server.Name = NameTextBox.Text;
             Server.Host = HostTextBox.Text.Trim();
+            Server.Enabled = EnabledCheckBox.Checked;
             Server.Port = int.TryParse(PortTextBox.Text, out int port) ? port : 0;
             Server.Username = UsernameTextBox.Text.Trim();
             Server.LoginType = (LoginType)LoginComboBox.SelectedIndex;
@@ -123,6 +131,12 @@
             TitleLabel.Text = title;
         }
 
+        private void SetIcon()
+        {
+            if (Server != null)
+                Icon = Server.Status.GetIcon();
+        }
+
         private void NameTextBox_TextChanged(object sender, EventArgs e)
         {
             SetTitle(NameTextBox.Text);
@@ -296,7 +310,7 @@
         {
             if (e.ColumnIndex == StatusColumn.Index)
             {
-                e.Value = ((CheckStatus)e.Value).GetIcon();
+                e.Value = ((CheckStatus)e.Value).GetImage();
             }
         }
 
@@ -304,7 +318,7 @@
         {
             if (e.ColumnIndex == LogStatusColumn.Index)
             {
-                e.Value = ((CheckStatus)e.Value).GetIcon();
+                e.Value = ((CheckStatus)e.Value).GetImage();
             }
         }
 
@@ -312,7 +326,6 @@
         {
             if (Server?.Checks != null)
             {
-                //TODO this might result in the selection being reset to all (if a new check is added, for example.) Restore selected index.
                 LogCheckComboBox.Items.Clear();
                 LogCheckComboBox.Items.Add("(All)");
                 LogCheckComboBox.Items.AddRange(Server.Checks.ToArray());
@@ -400,7 +413,7 @@
 
         public static void OpenPrivateKey(ServerMonitor monitor, Server server, IWin32Window owner)
         {
-            if (server.LoginType != LoginType.PrivateKey)
+            if (server.LoginType != LoginType.PrivateKey || server.KeyFile.IsNullOrEmpty())
                 return;
 
             KeyStatus keyStatus = monitor.OpenPrivateKey(server.KeyFile);
@@ -428,9 +441,27 @@
             }
         }
 
+        private void EnabledCheckBox_Click(object sender, EventArgs e)
+        {
+            bool enabled = EnabledCheckBox.Checked;
+            if (enabled)
+                OpenPrivateKey(monitor, Server, this);
+            Server.Enabled = enabled;
+            EnabledCheckBox.Checked = Server.Enabled;
+        }
+
+        //private void EnabledCheckBox_CheckedChanged(object sender, EventArgs e)
+        //{
+        //    bool enabled = EnabledCheckBox.Checked;
+        //    if (enabled)
+        //        OpenPrivateKey(monitor, Server, this);
+        //    EnabledCheckBox.Checked = Server.Enabled;
+        //}
+
         private void Server_EnabledChanged(object sender, EventArgs e)
         {
             SetTitle();
+            EnabledCheckBox.Checked = Server.Enabled;
         }
     }
 }