-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMenuCreator.cs
151 lines (139 loc) · 4.93 KB
/
MenuCreator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System;
using System.Drawing;
using System.IO;
using System.ServiceProcess;
using System.Windows.Forms;
namespace EsriApplicationChooser
{
internal class MenuCreator
{
private ContextMenuStrip _menu;
internal void AddMenuItems(ContextMenuStrip contextMenuStrip1)
{
_menu = contextMenuStrip1;
AddApplications();
AddRule();
AddUrls();
AddRule();
AddServices();
}
private void AddRule()
{
ToolStripSeparator separator = new ToolStripSeparator();
_menu.Items.Add(separator);
}
private void AddServices()
{
Configuration config = Configuration.GetConfig();
ToolStripLabel label = new ToolStripLabel();
label.Text = "Services";
Font f = new Font("tahoma", 8, FontStyle.Bold);
label.Font = f;
_menu.Items.Add(label);
foreach (ServiceElement service in config.Services)
{
ToolStripMenuItem menuItem = new ToolStripMenuItem();
menuItem.Text = service.Title;
menuItem.Tag = service.Key;
menuItem.Enabled = CheckEnabled(service);
menuItem.Checked = CheckStatus(service);
menuItem.Click += new EventHandler(MenuItemClick);
_menu.Items.Add(menuItem);
}
}
private bool CheckEnabled(ServiceElement service)
{
ServiceController myService = new ServiceController();
myService.ServiceName = service.Name;
try
{
var status = myService.Status;
}
catch (InvalidOperationException e)
{
return false;
}
return true;
}
private bool CheckStatus(ServiceElement service)
{
ServiceController myService = new ServiceController();
myService.ServiceName = service.Name;
try
{
if (myService.Status == ServiceControllerStatus.Stopped) { return false; }
if (myService.Status == ServiceControllerStatus.Running) { return true; }
}
catch (InvalidOperationException e)
{
return false;
}
return false;
}
private void AddUrls()
{
Configuration config = Configuration.GetConfig();
ToolStripLabel label = new ToolStripLabel();
label.Text = "Websites";
Font f = new Font("tahoma", 8, FontStyle.Bold);
label.Font = f;
_menu.Items.Add(label);
foreach (UrlElement service in config.Urls)
{
ToolStripMenuItem menuItem = new ToolStripMenuItem();
menuItem.Text = service.Title;
menuItem.Tag = service.Key;
menuItem.Click += new EventHandler(MenuItemClick);
_menu.Items.Add(menuItem);
}
}
private void AddApplications()
{
Configuration config = Configuration.GetConfig();
ToolStripLabel label = new ToolStripLabel();
label.Text = "Applicaties";
Font f = new Font("tahoma", 8, FontStyle.Bold);
label.Font = f;
_menu.Items.Add(label);
foreach (ApplicationElement service in config.Applications)
{
ToolStripMenuItem menuItem = new ToolStripMenuItem();
menuItem.Text = service.Title;
menuItem.Tag = service.Key;
menuItem.Enabled = CheckEnabled(service);
menuItem.Click += new EventHandler(MenuItemClick);
_menu.Items.Add(menuItem);
}
}
private bool CheckEnabled(ApplicationElement service)
{
return File.Exists(service.Location);
}
private void MenuItemClick(object sender, EventArgs e)
{
string tag = (sender as ToolStripMenuItem).Tag.ToString();
ClickHandler handler = new ClickHandler(tag);
handler.HandleClick();
SetStatusServices(tag);
}
private void SetStatusServices(string tag)
{
Configuration config = Configuration.GetConfig();
for (int i = 0; i < _menu.Items.Count; i++)
{
object otag = _menu.Items[i].Tag;
if (otag == null) { continue; }
ServiceElement service = config.Services[tag.ToString()];
if (service != null)
{
if (otag.Equals(tag))
{
ToolStripMenuItem item = _menu.Items[i] as ToolStripMenuItem;
item.Checked = !item.Checked;
}
}
if (otag.Equals(tag)) { return; }
}
}
}
}