comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:3e1a2131f897
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9
10 namespace ServerMonitorApp
11 {
12 public partial class ServerSummaryForm : Form
13 {
14 private readonly Dictionary<Server, ServerForm> serverForms = new Dictionary<Server, ServerForm>();
15 private ServerMonitor monitor;
16
17 public ServerSummaryForm()
18 {
19 InitializeComponent();
20 }
21
22 private void ServerSummaryForm_Load(object sender, EventArgs e)
23 {
24 Helpers.FormatImageButton(NewServerButton);
25 monitor = new ServerMonitor();
26 while (true)
27 {
28 try
29 {
30 monitor.LoadServers();
31 break;
32 }
33 catch (Exception ex)
34 {
35 DialogResult result = MessageBox.Show("Could not load servers. Please fix or delete the file " + monitor.ConfigFile + Environment.NewLine + Environment.NewLine
36 + "Error details:" + Environment.NewLine + ex.GetAllMessages(),
37 "Error loading servers", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
38 if (result == DialogResult.Cancel)
39 {
40 Environment.Exit(1);
41 }
42 }
43 }
44 RefreshDisplay();
45 }
46
47 private void RefreshDisplay()
48 {
49 ServerPanel.Controls.Clear();
50 foreach (Server server in monitor.Servers)
51 {
52 ServerSummaryControl control = new ServerSummaryControl(server);
53 control.Click += ServerSummaryControl_Click;
54 ServerPanel.Controls.Add(control);
55 }
56 }
57
58 private void ShowServerForm(Server server)
59 {
60 bool isNewServer = false;
61 if (server == null)
62 {
63 server = new Server();
64 monitor.AddServer(server);
65 isNewServer = true;
66 }
67 if (serverForms.TryGetValue(server, out ServerForm form))
68 {
69 form.Activate();
70 }
71 else
72 {
73 form = new ServerForm(monitor, server, isNewServer);
74 serverForms[server] = form;
75 form.FormClosing += ServerForm_FormClosing;
76 form.Show();
77 }
78 }
79
80 private void ServerSummaryControl_Click(object sender, EventArgs e)
81 {
82 ShowServerForm(((ServerSummaryControl)sender).Server);
83 }
84
85 private void ServerForm_FormClosing(object sender, FormClosingEventArgs e)
86 {
87 ServerForm form = (ServerForm)sender;
88 form.FormClosing -= ServerForm_FormClosing;
89 serverForms.Remove(form.Server);
90 RefreshDisplay();
91 }
92
93 private void NewServerButton_Click(object sender, EventArgs e)
94 {
95 ShowServerForm(null);
96 }
97 }
98 }