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 | UpdateResourcesAsync();
|
---|
60 | }
|
---|
61 |
|
---|
62 | public new void Dispose() {
|
---|
63 | HiveAdminClient.Instance.Refreshing -= new EventHandler(Instance_Refreshing);
|
---|
64 | HiveAdminClient.Instance.Refreshed -= new EventHandler(Instance_Refreshed);
|
---|
65 | }
|
---|
66 |
|
---|
67 | private void UpdateProgress() {
|
---|
68 | while (!stopProgressTask) {
|
---|
69 | int diff = (progressBar.Maximum - progressBar.Minimum) / 10;
|
---|
70 |
|
---|
71 | if (progressBar.InvokeRequired) {
|
---|
72 | progressBar.Invoke(new Action(delegate() { progressBar.Value = (progressBar.Value + diff) % progressBar.Maximum; }));
|
---|
73 | } else {
|
---|
74 | progressBar.Value = (progressBar.Value + diff) % progressBar.Maximum;
|
---|
75 | }
|
---|
76 |
|
---|
77 | //ok, this is not very clever...
|
---|
78 | Thread.Sleep(500);
|
---|
79 | }
|
---|
80 | if (progressBar.InvokeRequired) {
|
---|
81 | progressBar.Invoke(new Action(delegate() { progressBar.Value = progressBar.Minimum; }));
|
---|
82 | } else {
|
---|
83 | progressBar.Value = progressBar.Minimum;
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | void Instance_Refreshing(object sender, EventArgs e) {
|
---|
88 | stopProgressTask = false;
|
---|
89 | progressTask = new TS.Task(UpdateProgress);
|
---|
90 | progressTask.Start();
|
---|
91 | }
|
---|
92 |
|
---|
93 | void Instance_Refreshed(object sender, EventArgs e) {
|
---|
94 | stopProgressTask = true;
|
---|
95 | }
|
---|
96 |
|
---|
97 | #region Register Content Events
|
---|
98 | protected override void DeregisterContentEvents() {
|
---|
99 | Content.ItemsAdded -= new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<Resource>>(Content_ItemsAdded);
|
---|
100 | Content.ItemsRemoved -= new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<Resource>>(Content_ItemsRemoved);
|
---|
101 | base.DeregisterContentEvents();
|
---|
102 | }
|
---|
103 | protected override void RegisterContentEvents() {
|
---|
104 | base.RegisterContentEvents();
|
---|
105 | Content.ItemsAdded += new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<Resource>>(Content_ItemsAdded);
|
---|
106 | Content.ItemsRemoved += new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<Resource>>(Content_ItemsRemoved);
|
---|
107 | }
|
---|
108 | #endregion
|
---|
109 |
|
---|
110 | protected override void OnContentChanged() {
|
---|
111 | base.OnContentChanged();
|
---|
112 | if (Content == null) {
|
---|
113 | slaveView.Content = null;
|
---|
114 | treeSlaveGroup.Nodes.Clear();
|
---|
115 | } else {
|
---|
116 | treeSlaveGroup.Nodes.Clear();
|
---|
117 |
|
---|
118 | //rebuild
|
---|
119 | TreeNode ungrp = new TreeNode(UngroupedGroupName);
|
---|
120 | ungrp.ImageIndex = slaveGroupImageIndex;
|
---|
121 | ungrp.SelectedImageIndex = ungrp.ImageIndex;
|
---|
122 | var newGroup = new SlaveGroup();
|
---|
123 | newGroup.Name = UngroupedGroupName;
|
---|
124 | newGroup.Id = Guid.NewGuid();
|
---|
125 | newGroup.Description = "Contains slaves which are in no group";
|
---|
126 | ungrp.Tag = newGroup;
|
---|
127 |
|
---|
128 | foreach (Resource g in Content) {
|
---|
129 | if (g.GetType() == typeof(SlaveGroup)) {
|
---|
130 | //root node
|
---|
131 | if (g.ParentResourceId == null) {
|
---|
132 | TreeNode tn = new TreeNode();
|
---|
133 | tn.ImageIndex = slaveGroupImageIndex;
|
---|
134 | tn.SelectedImageIndex = tn.ImageIndex;
|
---|
135 |
|
---|
136 | tn.Tag = g;
|
---|
137 | tn.Text = g.Name;
|
---|
138 |
|
---|
139 | BuildSlaveGroupTree(g, tn);
|
---|
140 | tn.ExpandAll();
|
---|
141 | treeSlaveGroup.Nodes.Add(tn);
|
---|
142 | }
|
---|
143 | } else if (g.GetType() == typeof(Slave)) {
|
---|
144 | if (g.ParentResourceId == null) {
|
---|
145 | var stn = new TreeNode(g.Name);
|
---|
146 | stn.ImageIndex = slaveImageIndex;
|
---|
147 | stn.SelectedImageIndex = stn.ImageIndex;
|
---|
148 | stn.Tag = g;
|
---|
149 | ungrp.Nodes.Add(stn);
|
---|
150 | }
|
---|
151 | }
|
---|
152 | }
|
---|
153 | ungrp.ExpandAll();
|
---|
154 | treeSlaveGroup.Nodes.Add(ungrp);
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | private void BuildSlaveGroupTree(Resource g, TreeNode tn) {
|
---|
159 | foreach (Resource r in Content.Where(s => s.ParentResourceId != null && s.ParentResourceId == g.Id)) {
|
---|
160 | TreeNode stn = new TreeNode(r.Name);
|
---|
161 | if (r is Slave) {
|
---|
162 | stn.ImageIndex = slaveImageIndex;
|
---|
163 | } else if (r is SlaveGroup) {
|
---|
164 | stn.ImageIndex = slaveGroupImageIndex;
|
---|
165 | }
|
---|
166 | stn.SelectedImageIndex = stn.ImageIndex;
|
---|
167 | stn.Tag = r;
|
---|
168 | tn.Nodes.Add(stn);
|
---|
169 |
|
---|
170 | BuildSlaveGroupTree(r, stn);
|
---|
171 | tn.ExpandAll();
|
---|
172 | }
|
---|
173 | }
|
---|
174 |
|
---|
175 | protected override void SetEnabledStateOfControls() {
|
---|
176 | base.SetEnabledStateOfControls();
|
---|
177 | }
|
---|
178 |
|
---|
179 | private void treeSlaveGroup_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) {
|
---|
180 | if (slaveView.Content != null && slaveView.Content is SlaveGroup) {
|
---|
181 | slaveView.Content.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(SlaveViewContent_PropertyChanged);
|
---|
182 | }
|
---|
183 |
|
---|
184 | slaveView.Content = (Resource)e.Node.Tag;
|
---|
185 | HiveAdminClient.Instance.DowntimeForResourceId = ((Resource)e.Node.Tag).Id;
|
---|
186 |
|
---|
187 | if (e.Node.Tag is SlaveGroup) {
|
---|
188 | slaveView.Content.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(SlaveViewContent_PropertyChanged);
|
---|
189 | }
|
---|
190 |
|
---|
191 | if (tabSlaveGroup.SelectedIndex == 1) {
|
---|
192 | UpdateScheduleAsync();
|
---|
193 | }
|
---|
194 | }
|
---|
195 |
|
---|
196 | void SlaveViewContent_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) {
|
---|
197 | OnContentChanged();
|
---|
198 | if (e.PropertyName == "HbInterval") {
|
---|
199 | UpdateChildHbIntervall(slaveView.Content);
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|
203 | private void UpdateChildHbIntervall(Resource resource) {
|
---|
204 | foreach (Resource r in Content.Where(x => x.ParentResourceId == resource.Id)) {
|
---|
205 | r.HbInterval = resource.HbInterval;
|
---|
206 | if (r is SlaveGroup) {
|
---|
207 | UpdateChildHbIntervall(r);
|
---|
208 | }
|
---|
209 | }
|
---|
210 | }
|
---|
211 |
|
---|
212 | private void btnAddGroup_Click(object sender, EventArgs e) {
|
---|
213 | SlaveGroup newGroup = new SlaveGroup();
|
---|
214 | newGroup.Name = "New Group";
|
---|
215 | Content.Add(newGroup);
|
---|
216 | }
|
---|
217 |
|
---|
218 | void Content_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<Collections.IndexedItem<Resource>> e) {
|
---|
219 | OnContentChanged();
|
---|
220 | }
|
---|
221 |
|
---|
222 | void Content_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<Collections.IndexedItem<Resource>> e) {
|
---|
223 | OnContentChanged();
|
---|
224 | }
|
---|
225 |
|
---|
226 | private void btnRemoveGroup_Click(object sender, EventArgs e) {
|
---|
227 | if (treeSlaveGroup.SelectedNode != null && treeSlaveGroup.SelectedNode.Tag != null) {
|
---|
228 | Resource res = (Resource)treeSlaveGroup.SelectedNode.Tag;
|
---|
229 |
|
---|
230 | DialogResult diagRes = MessageBox.Show("Do you really want to delete " + res.Name + "?", "HeuristicLab Hive Administrator", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
|
---|
231 | if (diagRes == DialogResult.Yes) {
|
---|
232 | if (res is Slave) {
|
---|
233 | Content.Remove(res);
|
---|
234 | HiveAdminClient.Delete(res);
|
---|
235 | } else if (res is SlaveGroup) {
|
---|
236 | //only delete empty groups
|
---|
237 | if (Content.Where(s => s.ParentResourceId == res.Id).Count() < 1) {
|
---|
238 | Content.Remove(res);
|
---|
239 | HiveAdminClient.Delete(res);
|
---|
240 | } else {
|
---|
241 | MessageBox.Show("Only empty groups can be deleted.", "HeuristicLab Hive Administrator", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
242 | }
|
---|
243 | }
|
---|
244 | }
|
---|
245 | }
|
---|
246 | }
|
---|
247 |
|
---|
248 | private void btnSave_Click(object sender, EventArgs e) {
|
---|
249 | foreach (Resource res in Content) {
|
---|
250 | if (res is SlaveGroup && res.Id == Guid.Empty) {
|
---|
251 | SlaveGroup slaveGroup = (SlaveGroup)res;
|
---|
252 | slaveGroup.Store();
|
---|
253 | } else if (res.Id != Guid.Empty && res.Modified) {
|
---|
254 | res.Store();
|
---|
255 | }
|
---|
256 | }
|
---|
257 | }
|
---|
258 |
|
---|
259 | private void treeSlaveGroup_DragDrop(object sender, DragEventArgs e) {
|
---|
260 | if (e.Data.GetDataPresent("System.Windows.Forms.TreeNode", false)) {
|
---|
261 | Point pt = ((TreeView)sender).PointToClient(new Point(e.X, e.Y));
|
---|
262 | TreeNode destNode = ((TreeView)sender).GetNodeAt(pt);
|
---|
263 | TreeNode newNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode");
|
---|
264 |
|
---|
265 | if (destNode.TreeView == newNode.TreeView) {
|
---|
266 | if (destNode.Text == UngroupedGroupName || (destNode.Parent != null && destNode.Parent.Text == UngroupedGroupName)) {
|
---|
267 | 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.",
|
---|
268 | UngroupedGroupName, Environment.NewLine), "HeuristicLab Hive Administrator", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
---|
269 | return;
|
---|
270 | }
|
---|
271 |
|
---|
272 | SlaveGroup sgrp = null;
|
---|
273 | TreeNode parentNode = null;
|
---|
274 | if (destNode.Tag != null && destNode.Tag is SlaveGroup) {
|
---|
275 | sgrp = (SlaveGroup)destNode.Tag;
|
---|
276 | parentNode = destNode;
|
---|
277 | } else if (destNode.Parent != null && destNode.Parent.Tag is SlaveGroup) {
|
---|
278 | sgrp = (SlaveGroup)destNode.Parent.Tag;
|
---|
279 | parentNode = destNode.Parent;
|
---|
280 | }
|
---|
281 |
|
---|
282 | if (newNode.Tag is SlaveGroup && CheckParentsEqualsMovedNode(parentNode, newNode)) {
|
---|
283 | return;
|
---|
284 | }
|
---|
285 |
|
---|
286 | if (sgrp != null && newNode.Tag != null) {
|
---|
287 | //save parent group to get an id
|
---|
288 | if (sgrp.Id == Guid.Empty) {
|
---|
289 | sgrp.Store();
|
---|
290 | }
|
---|
291 |
|
---|
292 | if (newNode.Tag is Slave) {
|
---|
293 | Slave slave = (Slave)newNode.Tag;
|
---|
294 | if (slave.ParentResourceId == null || (slave.ParentResourceId != null && slave.ParentResourceId != sgrp.Id)) {
|
---|
295 | slave.ParentResourceId = sgrp.Id;
|
---|
296 | newNode.Remove();
|
---|
297 | parentNode.Nodes.Clear();
|
---|
298 | BuildSlaveGroupTree(sgrp, parentNode);
|
---|
299 | }
|
---|
300 | } else if (newNode.Tag is SlaveGroup) {
|
---|
301 | SlaveGroup slaveGroup = (SlaveGroup)newNode.Tag;
|
---|
302 | if (slaveGroup.ParentResourceId == null || (slaveGroup.ParentResourceId != null && slaveGroup.ParentResourceId != sgrp.Id)) {
|
---|
303 | slaveGroup.ParentResourceId = sgrp.Id;
|
---|
304 | newNode.Remove();
|
---|
305 | parentNode.Nodes.Clear();
|
---|
306 | BuildSlaveGroupTree(sgrp, parentNode);
|
---|
307 | }
|
---|
308 | }
|
---|
309 | }
|
---|
310 | }
|
---|
311 | }
|
---|
312 | }
|
---|
313 |
|
---|
314 | private bool CheckParentsEqualsMovedNode(TreeNode dest, TreeNode movedNode) {
|
---|
315 | TreeNode tmp = dest;
|
---|
316 |
|
---|
317 | while (tmp != null) {
|
---|
318 | if (tmp == movedNode) {
|
---|
319 | return true;
|
---|
320 | }
|
---|
321 | tmp = tmp.Parent;
|
---|
322 | }
|
---|
323 | return false;
|
---|
324 | }
|
---|
325 |
|
---|
326 | private void treeSlaveGroup_ItemDrag(object sender, ItemDragEventArgs e) {
|
---|
327 | TreeNode sourceNode = (TreeNode)e.Item;
|
---|
328 | DoDragDrop(sourceNode, DragDropEffects.All);
|
---|
329 | }
|
---|
330 |
|
---|
331 | private void treeSlaveGroup_DragEnter(object sender, DragEventArgs e) {
|
---|
332 | e.Effect = DragDropEffects.Move;
|
---|
333 | }
|
---|
334 |
|
---|
335 | private void treeSlaveGroup_DragOver(object sender, DragEventArgs e) {
|
---|
336 | e.Effect = DragDropEffects.Move;
|
---|
337 | }
|
---|
338 |
|
---|
339 | private void treeSlaveGroup_QueryContinueDrag(object sender, QueryContinueDragEventArgs e) {
|
---|
340 | e.Action = DragAction.Continue;
|
---|
341 | }
|
---|
342 |
|
---|
343 | void ResetView() {
|
---|
344 | treeSlaveGroup.Nodes.Clear();
|
---|
345 |
|
---|
346 | if (slaveView.Content != null && slaveView.Content is SlaveGroup) {
|
---|
347 | slaveView.Content.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(SlaveViewContent_PropertyChanged);
|
---|
348 | }
|
---|
349 | slaveView.Content = null;
|
---|
350 | if (scheduleView.Content != null) {
|
---|
351 | scheduleView.Content.Clear();
|
---|
352 | }
|
---|
353 | HiveAdminClient.Instance.ResetDowntime();
|
---|
354 | }
|
---|
355 |
|
---|
356 | private void UpdateResources() {
|
---|
357 | if (this.InvokeRequired) {
|
---|
358 | this.Invoke(new Action(ResetView));
|
---|
359 | } else {
|
---|
360 | ResetView();
|
---|
361 | }
|
---|
362 |
|
---|
363 | try {
|
---|
364 | HiveAdminClient.Instance.Refresh();
|
---|
365 | Content = HiveAdminClient.Instance.Resources;
|
---|
366 | }
|
---|
367 | catch (MessageSecurityException) {
|
---|
368 | 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);
|
---|
369 | }
|
---|
370 | }
|
---|
371 |
|
---|
372 | private void UpdateResourcesAsync() {
|
---|
373 | TS.Task.Factory.StartNew(UpdateResources).ContinueWith((t) => {
|
---|
374 | DisplayError(t.Exception);
|
---|
375 | }, TaskContinuationOptions.OnlyOnFaulted);
|
---|
376 | }
|
---|
377 |
|
---|
378 | private void UpdateSchedule() {
|
---|
379 | HiveAdminClient.Instance.RefreshCalendar();
|
---|
380 | scheduleView.Invoke(new Action(() => scheduleView.Content = HiveAdminClient.Instance.Downtimes));
|
---|
381 | }
|
---|
382 |
|
---|
383 | private void UpdateScheduleAsync() {
|
---|
384 | TS.Task.Factory.StartNew(UpdateSchedule).ContinueWith((t) => {
|
---|
385 | DisplayError(t.Exception);
|
---|
386 | }, TaskContinuationOptions.OnlyOnFaulted);
|
---|
387 | }
|
---|
388 |
|
---|
389 | private void DisplayError(Exception ex) {
|
---|
390 | MessageBox.Show(string.Format("An error occured while updating: {0} {1}", Environment.NewLine, ex.Message), "HeuristicLab Hive Administrator", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
---|
391 | }
|
---|
392 |
|
---|
393 | private void tabSlaveGroup_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
394 | if (tabSlaveGroup.SelectedIndex == 1) {
|
---|
395 | UpdateScheduleAsync();
|
---|
396 | }
|
---|
397 | }
|
---|
398 |
|
---|
399 | private void btnRefresh_Click(object sender, EventArgs e) {
|
---|
400 | UpdateResourcesAsync();
|
---|
401 | }
|
---|
402 | }
|
---|
403 | }
|
---|