1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Drawing;
|
---|
24 | using System.Linq;
|
---|
25 | using System.ServiceModel.Security;
|
---|
26 | using System.Threading;
|
---|
27 | using System.Threading.Tasks;
|
---|
28 | using System.Windows.Forms;
|
---|
29 | using HeuristicLab.Clients.Hive.Views;
|
---|
30 | using HeuristicLab.Core;
|
---|
31 | using HeuristicLab.Core.Views;
|
---|
32 | using HeuristicLab.MainForm;
|
---|
33 | using TS = System.Threading.Tasks;
|
---|
34 |
|
---|
35 | namespace HeuristicLab.Clients.Hive.Administrator.Views {
|
---|
36 | [View("Resources View")]
|
---|
37 | [Content(typeof(IItemList<Resource>), IsDefaultView = true)]
|
---|
38 | public partial class ResourcesView : ItemView, IDisposable {
|
---|
39 | public new IItemList<Resource> Content {
|
---|
40 | get { return (IItemList<Resource>)base.Content; }
|
---|
41 | set { base.Content = value; }
|
---|
42 | }
|
---|
43 |
|
---|
44 | public const string UngroupedGroupName = "UNGROUPED";
|
---|
45 | private const int slaveImageIndex = 0;
|
---|
46 | private const int slaveGroupImageIndex = 1;
|
---|
47 | private TS.Task progressTask;
|
---|
48 | private bool stopProgressTask;
|
---|
49 |
|
---|
50 |
|
---|
51 | public ResourcesView() {
|
---|
52 | InitializeComponent();
|
---|
53 | treeSlaveGroup.ImageList.Images.Add(HiveImageLibrary.Slave);
|
---|
54 | treeSlaveGroup.ImageList.Images.Add(HiveImageLibrary.SlaveGroup);
|
---|
55 |
|
---|
56 | HiveAdminClient.Instance.Refreshing += new EventHandler(Instance_Refreshing);
|
---|
57 | HiveAdminClient.Instance.Refreshed += new EventHandler(Instance_Refreshed);
|
---|
58 | }
|
---|
59 |
|
---|
60 | public new void Dispose() {
|
---|
61 | HiveAdminClient.Instance.Refreshing -= new EventHandler(Instance_Refreshing);
|
---|
62 | HiveAdminClient.Instance.Refreshed -= new EventHandler(Instance_Refreshed);
|
---|
63 | }
|
---|
64 |
|
---|
65 | private void UpdateProgress() {
|
---|
66 | while (!stopProgressTask) {
|
---|
67 | int diff = (progressBar.Maximum - progressBar.Minimum) / 10;
|
---|
68 |
|
---|
69 | if (progressBar.InvokeRequired) {
|
---|
70 | progressBar.Invoke(new Action(delegate() { progressBar.Value = (progressBar.Value + diff) % progressBar.Maximum; }));
|
---|
71 | } else {
|
---|
72 | progressBar.Value = (progressBar.Value + diff) % progressBar.Maximum;
|
---|
73 | }
|
---|
74 |
|
---|
75 | //ok, this is not very clever...
|
---|
76 | Thread.Sleep(500);
|
---|
77 | }
|
---|
78 | if (progressBar.InvokeRequired) {
|
---|
79 | progressBar.Invoke(new Action(delegate() { progressBar.Value = progressBar.Minimum; }));
|
---|
80 | } else {
|
---|
81 | progressBar.Value = progressBar.Minimum;
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | void Instance_Refreshing(object sender, EventArgs e) {
|
---|
86 | stopProgressTask = false;
|
---|
87 | progressTask = new TS.Task(UpdateProgress);
|
---|
88 | progressTask.Start();
|
---|
89 | }
|
---|
90 |
|
---|
91 | void Instance_Refreshed(object sender, EventArgs e) {
|
---|
92 | stopProgressTask = true;
|
---|
93 | }
|
---|
94 |
|
---|
95 | #region Register Content Events
|
---|
96 | protected override void DeregisterContentEvents() {
|
---|
97 | Content.ItemsAdded -= new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<Resource>>(Content_ItemsAdded);
|
---|
98 | Content.ItemsRemoved -= new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<Resource>>(Content_ItemsRemoved);
|
---|
99 | base.DeregisterContentEvents();
|
---|
100 | }
|
---|
101 | protected override void RegisterContentEvents() {
|
---|
102 | base.RegisterContentEvents();
|
---|
103 | Content.ItemsAdded += new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<Resource>>(Content_ItemsAdded);
|
---|
104 | Content.ItemsRemoved += new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<Resource>>(Content_ItemsRemoved);
|
---|
105 | }
|
---|
106 | #endregion
|
---|
107 |
|
---|
108 | protected override void OnContentChanged() {
|
---|
109 | base.OnContentChanged();
|
---|
110 | if (Content == null) {
|
---|
111 | slaveView.Content = null;
|
---|
112 | treeSlaveGroup.Nodes.Clear();
|
---|
113 | } else {
|
---|
114 | treeSlaveGroup.Nodes.Clear();
|
---|
115 |
|
---|
116 | //rebuild
|
---|
117 | TreeNode ungrp = new TreeNode(UngroupedGroupName);
|
---|
118 | ungrp.ImageIndex = slaveGroupImageIndex;
|
---|
119 | ungrp.SelectedImageIndex = ungrp.ImageIndex;
|
---|
120 | var newGroup = new SlaveGroup();
|
---|
121 | newGroup.Name = UngroupedGroupName;
|
---|
122 | newGroup.Id = Guid.NewGuid();
|
---|
123 | newGroup.Description = "Contains slaves which are in no group";
|
---|
124 | ungrp.Tag = newGroup;
|
---|
125 |
|
---|
126 | foreach (Resource g in Content) {
|
---|
127 | if (g.GetType() == typeof(SlaveGroup)) {
|
---|
128 | //root node
|
---|
129 | if (g.ParentResourceId == null) {
|
---|
130 | TreeNode tn = new TreeNode();
|
---|
131 | tn.ImageIndex = slaveGroupImageIndex;
|
---|
132 | tn.SelectedImageIndex = tn.ImageIndex;
|
---|
133 |
|
---|
134 | tn.Tag = g;
|
---|
135 | tn.Text = g.Name;
|
---|
136 |
|
---|
137 | BuildSlaveGroupTree(g, tn);
|
---|
138 | tn.ExpandAll();
|
---|
139 | treeSlaveGroup.Nodes.Add(tn);
|
---|
140 | }
|
---|
141 | } else if (g.GetType() == typeof(Slave)) {
|
---|
142 | if (g.ParentResourceId == null) {
|
---|
143 | var stn = new TreeNode(g.Name);
|
---|
144 | stn.ImageIndex = slaveImageIndex;
|
---|
145 | stn.SelectedImageIndex = stn.ImageIndex;
|
---|
146 | stn.Tag = g;
|
---|
147 | ungrp.Nodes.Add(stn);
|
---|
148 | }
|
---|
149 | }
|
---|
150 | }
|
---|
151 | ungrp.ExpandAll();
|
---|
152 | treeSlaveGroup.Nodes.Add(ungrp);
|
---|
153 | }
|
---|
154 | }
|
---|
155 |
|
---|
156 | private void BuildSlaveGroupTree(Resource g, TreeNode tn) {
|
---|
157 | foreach (Resource r in Content.Where(s => s.ParentResourceId != null && s.ParentResourceId == g.Id)) {
|
---|
158 | TreeNode stn = new TreeNode(r.Name);
|
---|
159 | if (r is Slave) {
|
---|
160 | stn.ImageIndex = slaveImageIndex;
|
---|
161 | } else if (r is SlaveGroup) {
|
---|
162 | stn.ImageIndex = slaveGroupImageIndex;
|
---|
163 | }
|
---|
164 | stn.SelectedImageIndex = stn.ImageIndex;
|
---|
165 | stn.Tag = r;
|
---|
166 | tn.Nodes.Add(stn);
|
---|
167 |
|
---|
168 | BuildSlaveGroupTree(r, stn);
|
---|
169 | tn.ExpandAll();
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | protected override void SetEnabledStateOfControls() {
|
---|
174 | base.SetEnabledStateOfControls();
|
---|
175 | }
|
---|
176 |
|
---|
177 | private void treeSlaveGroup_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) {
|
---|
178 | if (slaveView.Content != null && slaveView.Content is SlaveGroup) {
|
---|
179 | slaveView.Content.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(SlaveViewContent_PropertyChanged);
|
---|
180 | }
|
---|
181 |
|
---|
182 | slaveView.Content = (Resource)e.Node.Tag;
|
---|
183 | HiveAdminClient.Instance.DowntimeForResourceId = ((Resource)e.Node.Tag).Id;
|
---|
184 |
|
---|
185 | if (e.Node.Tag is SlaveGroup) {
|
---|
186 | slaveView.Content.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(SlaveViewContent_PropertyChanged);
|
---|
187 | }
|
---|
188 |
|
---|
189 | if (tabSlaveGroup.SelectedIndex == 1) {
|
---|
190 | UpdateScheduleAsync();
|
---|
191 | }
|
---|
192 | }
|
---|
193 |
|
---|
194 | void SlaveViewContent_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) {
|
---|
195 | OnContentChanged();
|
---|
196 | if (e.PropertyName == "HbInterval") {
|
---|
197 | UpdateChildHbIntervall(slaveView.Content);
|
---|
198 | }
|
---|
199 | }
|
---|
200 |
|
---|
201 | private void UpdateChildHbIntervall(Resource resource) {
|
---|
202 | foreach (Resource r in Content.Where(x => x.ParentResourceId == resource.Id)) {
|
---|
203 | r.HbInterval = resource.HbInterval;
|
---|
204 | if (r is SlaveGroup) {
|
---|
205 | UpdateChildHbIntervall(r);
|
---|
206 | }
|
---|
207 | }
|
---|
208 | }
|
---|
209 |
|
---|
210 | private void btnAddGroup_Click(object sender, EventArgs e) {
|
---|
211 | SlaveGroup newGroup = new SlaveGroup();
|
---|
212 | newGroup.Name = "New Group";
|
---|
213 | Content.Add(newGroup);
|
---|
214 | }
|
---|
215 |
|
---|
216 | void Content_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<Collections.IndexedItem<Resource>> e) {
|
---|
217 | OnContentChanged();
|
---|
218 | }
|
---|
219 |
|
---|
220 | void Content_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<Collections.IndexedItem<Resource>> e) {
|
---|
221 | OnContentChanged();
|
---|
222 | }
|
---|
223 |
|
---|
224 | private void btnRemoveGroup_Click(object sender, EventArgs e) {
|
---|
225 | if (treeSlaveGroup.SelectedNode != null && treeSlaveGroup.SelectedNode.Tag != null) {
|
---|
226 | Resource res = (Resource)treeSlaveGroup.SelectedNode.Tag;
|
---|
227 |
|
---|
228 | DialogResult diagRes = MessageBox.Show("Do you really want to delete " + res.Name + "?", "HeuristicLab Hive Administrator", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
|
---|
229 | if (diagRes == DialogResult.Yes) {
|
---|
230 | if (res is Slave) {
|
---|
231 | Content.Remove(res);
|
---|
232 | HiveAdminClient.Delete(res);
|
---|
233 | } else if (res is SlaveGroup) {
|
---|
234 | //only delete empty groups
|
---|
235 | if (Content.Where(s => s.ParentResourceId == res.Id).Count() < 1) {
|
---|
236 | Content.Remove(res);
|
---|
237 | HiveAdminClient.Delete(res);
|
---|
238 | } else {
|
---|
239 | MessageBox.Show("Only empty groups can be deleted.", "HeuristicLab Hive Administrator", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
240 | }
|
---|
241 | }
|
---|
242 | }
|
---|
243 | }
|
---|
244 | }
|
---|
245 |
|
---|
246 | private void btnSave_Click(object sender, EventArgs e) {
|
---|
247 | foreach (Resource res in Content) {
|
---|
248 | if (res is SlaveGroup && res.Id == Guid.Empty) {
|
---|
249 | SlaveGroup slaveGroup = (SlaveGroup)res;
|
---|
250 | slaveGroup.Store();
|
---|
251 | } else if (res.Id != Guid.Empty && res.Modified) {
|
---|
252 | res.Store();
|
---|
253 | }
|
---|
254 | }
|
---|
255 | }
|
---|
256 |
|
---|
257 | private void treeSlaveGroup_DragDrop(object sender, DragEventArgs e) {
|
---|
258 | if (e.Data.GetDataPresent("System.Windows.Forms.TreeNode", false)) {
|
---|
259 | Point pt = ((TreeView)sender).PointToClient(new Point(e.X, e.Y));
|
---|
260 | TreeNode destNode = ((TreeView)sender).GetNodeAt(pt);
|
---|
261 | TreeNode newNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode");
|
---|
262 |
|
---|
263 | if (destNode.TreeView == newNode.TreeView) {
|
---|
264 | if (destNode.Text == UngroupedGroupName || (destNode.Parent != null && destNode.Parent.Text == UngroupedGroupName)) {
|
---|
265 | MessageBox.Show(String.Format("You can't drag items to the {0} group.{1}This group only contains slaves which haven't yet been assigned to a real group.",
|
---|
266 | UngroupedGroupName, Environment.NewLine), "HeuristicLab Hive Administrator", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
---|
267 | return;
|
---|
268 | }
|
---|
269 |
|
---|
270 | SlaveGroup sgrp = null;
|
---|
271 | TreeNode parentNode = null;
|
---|
272 | if (destNode.Tag != null && destNode.Tag is SlaveGroup) {
|
---|
273 | sgrp = (SlaveGroup)destNode.Tag;
|
---|
274 | parentNode = destNode;
|
---|
275 | } else if (destNode.Parent != null && destNode.Parent.Tag is SlaveGroup) {
|
---|
276 | sgrp = (SlaveGroup)destNode.Parent.Tag;
|
---|
277 | parentNode = destNode.Parent;
|
---|
278 | }
|
---|
279 |
|
---|
280 | if (newNode.Tag is SlaveGroup && CheckParentsEqualsMovedNode(parentNode, newNode)) {
|
---|
281 | return;
|
---|
282 | }
|
---|
283 |
|
---|
284 | if (sgrp != null && newNode.Tag != null) {
|
---|
285 | //save parent group to get an id
|
---|
286 | if (sgrp.Id == Guid.Empty) {
|
---|
287 | sgrp.Store();
|
---|
288 | }
|
---|
289 |
|
---|
290 | if (newNode.Tag is Slave) {
|
---|
291 | Slave slave = (Slave)newNode.Tag;
|
---|
292 | if (slave.ParentResourceId == null || (slave.ParentResourceId != null && slave.ParentResourceId != sgrp.Id)) {
|
---|
293 | slave.ParentResourceId = sgrp.Id;
|
---|
294 | newNode.Remove();
|
---|
295 | parentNode.Nodes.Clear();
|
---|
296 | BuildSlaveGroupTree(sgrp, parentNode);
|
---|
297 | }
|
---|
298 | } else if (newNode.Tag is SlaveGroup) {
|
---|
299 | SlaveGroup slaveGroup = (SlaveGroup)newNode.Tag;
|
---|
300 | if (slaveGroup.ParentResourceId == null || (slaveGroup.ParentResourceId != null && slaveGroup.ParentResourceId != sgrp.Id)) {
|
---|
301 | slaveGroup.ParentResourceId = sgrp.Id;
|
---|
302 | newNode.Remove();
|
---|
303 | parentNode.Nodes.Clear();
|
---|
304 | BuildSlaveGroupTree(sgrp, parentNode);
|
---|
305 | }
|
---|
306 | }
|
---|
307 | }
|
---|
308 | }
|
---|
309 | }
|
---|
310 | }
|
---|
311 |
|
---|
312 | private bool CheckParentsEqualsMovedNode(TreeNode dest, TreeNode movedNode) {
|
---|
313 | TreeNode tmp = dest;
|
---|
314 |
|
---|
315 | while (tmp != null) {
|
---|
316 | if (tmp == movedNode) {
|
---|
317 | return true;
|
---|
318 | }
|
---|
319 | tmp = tmp.Parent;
|
---|
320 | }
|
---|
321 | return false;
|
---|
322 | }
|
---|
323 |
|
---|
324 | private void treeSlaveGroup_ItemDrag(object sender, ItemDragEventArgs e) {
|
---|
325 | TreeNode sourceNode = (TreeNode)e.Item;
|
---|
326 | DoDragDrop(sourceNode, DragDropEffects.All);
|
---|
327 | }
|
---|
328 |
|
---|
329 | private void treeSlaveGroup_DragEnter(object sender, DragEventArgs e) {
|
---|
330 | e.Effect = DragDropEffects.Move;
|
---|
331 | }
|
---|
332 |
|
---|
333 | private void treeSlaveGroup_DragOver(object sender, DragEventArgs e) {
|
---|
334 | e.Effect = DragDropEffects.Move;
|
---|
335 | }
|
---|
336 |
|
---|
337 | private void treeSlaveGroup_QueryContinueDrag(object sender, QueryContinueDragEventArgs e) {
|
---|
338 | e.Action = DragAction.Continue;
|
---|
339 | }
|
---|
340 |
|
---|
341 | void ResetView() {
|
---|
342 | treeSlaveGroup.Nodes.Clear();
|
---|
343 |
|
---|
344 | if (slaveView.Content != null && slaveView.Content is SlaveGroup) {
|
---|
345 | slaveView.Content.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(SlaveViewContent_PropertyChanged);
|
---|
346 | }
|
---|
347 | slaveView.Content = null;
|
---|
348 | if (scheduleView.Content != null) {
|
---|
349 | scheduleView.Content.Clear();
|
---|
350 | }
|
---|
351 | HiveAdminClient.Instance.ResetDowntime();
|
---|
352 | }
|
---|
353 |
|
---|
354 | private void UpdateResources() {
|
---|
355 | if (this.InvokeRequired) {
|
---|
356 | this.Invoke(new Action(ResetView));
|
---|
357 | } else {
|
---|
358 | ResetView();
|
---|
359 | }
|
---|
360 |
|
---|
361 | try {
|
---|
362 | HiveAdminClient.Instance.Refresh();
|
---|
363 | Content = HiveAdminClient.Instance.Resources;
|
---|
364 | }
|
---|
365 | catch (MessageSecurityException) {
|
---|
366 | MessageBox.Show("A Message Security error has occured. This normally means that your user name or password is wrong.", "HeuristicLab Hive Administrator", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
367 | }
|
---|
368 | }
|
---|
369 |
|
---|
370 | private void UpdateResourcesAsync() {
|
---|
371 | TS.Task.Factory.StartNew(UpdateResources).ContinueWith((t) => {
|
---|
372 | DisplayError(t.Exception);
|
---|
373 | }, TaskContinuationOptions.OnlyOnFaulted);
|
---|
374 | }
|
---|
375 |
|
---|
376 | private void UpdateSchedule() {
|
---|
377 | HiveAdminClient.Instance.RefreshCalendar();
|
---|
378 | scheduleView.Invoke(new Action(() => scheduleView.Content = HiveAdminClient.Instance.Downtimes));
|
---|
379 | }
|
---|
380 |
|
---|
381 | private void UpdateScheduleAsync() {
|
---|
382 | TS.Task.Factory.StartNew(UpdateSchedule).ContinueWith((t) => {
|
---|
383 | DisplayError(t.Exception);
|
---|
384 | }, TaskContinuationOptions.OnlyOnFaulted);
|
---|
385 | }
|
---|
386 |
|
---|
387 | private void DisplayError(Exception ex) {
|
---|
388 | MessageBox.Show(string.Format("An error occured while updating: {0} {1}", Environment.NewLine, ex.Message), "HeuristicLab Hive Administrator", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
389 | }
|
---|
390 |
|
---|
391 | private void tabSlaveGroup_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
392 | if (tabSlaveGroup.SelectedIndex == 1) {
|
---|
393 | UpdateScheduleAsync();
|
---|
394 | }
|
---|
395 | }
|
---|
396 |
|
---|
397 | private void btnRefresh_Click(object sender, EventArgs e) {
|
---|
398 | UpdateResourcesAsync();
|
---|
399 | }
|
---|
400 |
|
---|
401 | private void ResourcesView_Load(object sender, EventArgs e) {
|
---|
402 | UpdateResourcesAsync();
|
---|
403 | }
|
---|
404 | }
|
---|
405 | }
|
---|