view ServerMonitor/Forms/ServerSummaryForm.cs @ 0:3e1a2131f897

Initial commit. Ping check, scheduling, UI working. SSH check mostly working.
author Brad Greco <brad@bgreco.net>
date Mon, 31 Dec 2018 18:32:14 -0500
parents
children 3142e52cbe69
line wrap: on
line source

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ServerMonitorApp
{
    public partial class ServerSummaryForm : Form
    {
        private readonly Dictionary<Server, ServerForm> serverForms = new Dictionary<Server, ServerForm>();
        private ServerMonitor monitor;

        public ServerSummaryForm()
        {
            InitializeComponent();
        }

        private void ServerSummaryForm_Load(object sender, EventArgs e)
        {
            Helpers.FormatImageButton(NewServerButton);
            monitor = new ServerMonitor();
            while (true)
            {
                try
                {
                    monitor.LoadServers();
                    break;
                }
                catch (Exception ex)
                {
                    DialogResult result = MessageBox.Show("Could not load servers. Please fix or delete the file " + monitor.ConfigFile + Environment.NewLine + Environment.NewLine
                        + "Error details:" + Environment.NewLine + ex.GetAllMessages(),
                        "Error loading servers", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
                    if (result == DialogResult.Cancel)
                    {
                        Environment.Exit(1);
                    }
                }
            }
            RefreshDisplay();
        }

        private void RefreshDisplay()
        {
            ServerPanel.Controls.Clear();
            foreach (Server server in monitor.Servers)
            {
                ServerSummaryControl control = new ServerSummaryControl(server);
                control.Click += ServerSummaryControl_Click;
                ServerPanel.Controls.Add(control);
            }
        }

        private void ShowServerForm(Server server)
        {
            bool isNewServer = false;
            if (server == null)
            {
                server = new Server();
                monitor.AddServer(server);
                isNewServer = true;
            }
            if (serverForms.TryGetValue(server, out ServerForm form))
            {
                form.Activate();
            }
            else
            {
                form = new ServerForm(monitor, server, isNewServer);
                serverForms[server] = form;
                form.FormClosing += ServerForm_FormClosing;
                form.Show();
            }
        }

        private void ServerSummaryControl_Click(object sender, EventArgs e)
        {
            ShowServerForm(((ServerSummaryControl)sender).Server);
        }

        private void ServerForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            ServerForm form = (ServerForm)sender;
            form.FormClosing -= ServerForm_FormClosing;
            serverForms.Remove(form.Server);
            RefreshDisplay();
        }

        private void NewServerButton_Click(object sender, EventArgs e)
        {
            ShowServerForm(null);
        }
    }
}