Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveHiveEngine/HeuristicLab.Clients.Hive.Administrator/3.3/Views/ResourcesView.cs @ 10479

Last change on this file since 10479 was 7259, checked in by swagner, 13 years ago

Updated year of copyrights to 2012 (#1716)

File size: 15.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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
22using System;
23using System.Drawing;
24using System.Linq;
25using System.ServiceModel.Security;
26using System.Threading;
27using System.Threading.Tasks;
28using System.Windows.Forms;
29using HeuristicLab.Clients.Hive.Views;
30using HeuristicLab.Core;
31using HeuristicLab.Core.Views;
32using HeuristicLab.MainForm;
33using TS = System.Threading.Tasks;
34
35namespace 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(HeuristicLab.Common.Resources.VSImageLibrary.MonitorLarge);
54      treeSlaveGroup.ImageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.NetworkCenterLarge);
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              treeSlaveGroup.Nodes.Add(tn);
139            }
140          } else if (g.GetType() == typeof(Slave)) {
141            if (g.ParentResourceId == null) {
142              var stn = new TreeNode(g.Name);
143              stn.ImageIndex = slaveImageIndex;
144              stn.SelectedImageIndex = stn.ImageIndex;
145              stn.Tag = g;
146              ungrp.Nodes.Add(stn);
147            }
148          }
149        }
150        treeSlaveGroup.Nodes.Add(ungrp);
151      }
152    }
153
154    private void BuildSlaveGroupTree(Resource g, TreeNode tn) {
155      foreach (Resource r in Content.Where(s => s.ParentResourceId != null && s.ParentResourceId == g.Id)) {
156        TreeNode stn = new TreeNode(r.Name);
157        if (r is Slave) {
158          stn.ImageIndex = slaveImageIndex;
159        } else if (r is SlaveGroup) {
160          stn.ImageIndex = slaveGroupImageIndex;
161        }
162        stn.SelectedImageIndex = stn.ImageIndex;
163        stn.Tag = r;
164        tn.Nodes.Add(stn);
165
166        BuildSlaveGroupTree(r, stn);
167      }
168    }
169
170    protected override void SetEnabledStateOfControls() {
171      base.SetEnabledStateOfControls();
172      if (Content == null) {
173        btnAddGroup.Enabled = false;
174        btnRemoveGroup.Enabled = false;
175        btnSave.Enabled = false;
176      } else {
177        btnAddGroup.Enabled = true;
178        btnRemoveGroup.Enabled = true;
179        btnSave.Enabled = true;
180      }
181    }
182
183    private void treeSlaveGroup_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) {
184      if (slaveView.Content != null && slaveView.Content is SlaveGroup) {
185        slaveView.Content.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(SlaveViewContent_PropertyChanged);
186      }
187
188      slaveView.Content = (Resource)e.Node.Tag;
189      HiveAdminClient.Instance.DowntimeForResourceId = ((Resource)e.Node.Tag).Id;
190
191      if (e.Node.Tag is SlaveGroup) {
192        slaveView.Content.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(SlaveViewContent_PropertyChanged);
193      }
194
195      if (tabSlaveGroup.SelectedIndex == 1) {
196        UpdateScheduleAsync();
197      }
198    }
199
200    void SlaveViewContent_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) {
201      OnContentChanged();
202      if (e.PropertyName == "HbInterval") {
203        UpdateChildHbIntervall(slaveView.Content);
204      }
205    }
206
207    private void UpdateChildHbIntervall(Resource resource) {
208      foreach (Resource r in Content.Where(x => x.ParentResourceId == resource.Id)) {
209        r.HbInterval = resource.HbInterval;
210        if (r is SlaveGroup) {
211          UpdateChildHbIntervall(r);
212        }
213      }
214    }
215
216    private void btnAddGroup_Click(object sender, EventArgs e) {
217      SlaveGroup newGroup = new SlaveGroup();
218      newGroup.Name = "New Group";
219      Content.Add(newGroup);
220    }
221
222    void Content_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<Collections.IndexedItem<Resource>> e) {
223      OnContentChanged();
224    }
225
226    void Content_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<Collections.IndexedItem<Resource>> e) {
227      OnContentChanged();
228    }
229
230    private void btnRemoveGroup_Click(object sender, EventArgs e) {
231      if (treeSlaveGroup.SelectedNode != null && treeSlaveGroup.SelectedNode.Tag != null) {
232        Resource res = (Resource)treeSlaveGroup.SelectedNode.Tag;
233
234        DialogResult diagRes = MessageBox.Show("Do you really want to delete " + res.Name + "?", "HeuristicLab Hive Administrator", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
235        if (diagRes == DialogResult.Yes) {
236          if (res is Slave) {
237            Content.Remove(res);
238            HiveAdminClient.Delete(res);
239          } else if (res is SlaveGroup) {
240            //only delete empty groups
241            if (Content.Where(s => s.ParentResourceId == res.Id).Count() < 1) {
242              Content.Remove(res);
243              HiveAdminClient.Delete(res);
244            } else {
245              MessageBox.Show("Only empty groups can be deleted.", "HeuristicLab Hive Administrator", MessageBoxButtons.OK, MessageBoxIcon.Error);
246            }
247          }
248        }
249      }
250    }
251
252    private void btnSave_Click(object sender, EventArgs e) {
253      foreach (Resource res in Content) {
254        if (res is SlaveGroup && res.Id == Guid.Empty) {
255          SlaveGroup slaveGroup = (SlaveGroup)res;
256          slaveGroup.Store();
257        } else if (res.Id != Guid.Empty && res.Modified) {
258          res.Store();
259        }
260      }
261    }
262
263    private void treeSlaveGroup_DragDrop(object sender, DragEventArgs e) {
264      if (e.Data.GetDataPresent("System.Windows.Forms.TreeNode", false)) {
265        Point pt = ((TreeView)sender).PointToClient(new Point(e.X, e.Y));
266        TreeNode destNode = ((TreeView)sender).GetNodeAt(pt);
267        TreeNode newNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode");
268
269        if (destNode.TreeView == newNode.TreeView) {
270          if (destNode.Text == UngroupedGroupName || (destNode.Parent != null && destNode.Parent.Text == UngroupedGroupName)) {
271            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.",
272              UngroupedGroupName, Environment.NewLine), "HeuristicLab Hive Administrator", MessageBoxButtons.OK, MessageBoxIcon.Information);
273            return;
274          }
275
276          SlaveGroup sgrp = null;
277          TreeNode parentNode = null;
278          if (destNode.Tag != null && destNode.Tag is SlaveGroup) {
279            sgrp = (SlaveGroup)destNode.Tag;
280            parentNode = destNode;
281          } else if (destNode.Parent != null && destNode.Parent.Tag is SlaveGroup) {
282            sgrp = (SlaveGroup)destNode.Parent.Tag;
283            parentNode = destNode.Parent;
284          }
285
286          if (newNode.Tag is SlaveGroup && CheckParentsEqualsMovedNode(parentNode, newNode)) {
287            return;
288          }
289
290          if (sgrp != null && newNode.Tag != null) {
291            //save parent group to get an id
292            if (sgrp.Id == Guid.Empty) {
293              sgrp.Store();
294            }
295
296            if (newNode.Tag is Slave) {
297              Slave slave = (Slave)newNode.Tag;
298              if (slave.ParentResourceId == null || (slave.ParentResourceId != null && slave.ParentResourceId != sgrp.Id)) {
299                slave.ParentResourceId = sgrp.Id;
300                newNode.Remove();
301                parentNode.Nodes.Clear();
302                BuildSlaveGroupTree(sgrp, parentNode);
303              }
304            } else if (newNode.Tag is SlaveGroup) {
305              SlaveGroup slaveGroup = (SlaveGroup)newNode.Tag;
306              if (slaveGroup.ParentResourceId == null || (slaveGroup.ParentResourceId != null && slaveGroup.ParentResourceId != sgrp.Id)) {
307                slaveGroup.ParentResourceId = sgrp.Id;
308                newNode.Remove();
309                parentNode.Nodes.Clear();
310                BuildSlaveGroupTree(sgrp, parentNode);
311              }
312            }
313          }
314        }
315      }
316    }
317
318    private bool CheckParentsEqualsMovedNode(TreeNode dest, TreeNode movedNode) {
319      TreeNode tmp = dest;
320
321      while (tmp != null) {
322        if (tmp == movedNode) {
323          return true;
324        }
325        tmp = tmp.Parent;
326      }
327      return false;
328    }
329
330    private void treeSlaveGroup_ItemDrag(object sender, ItemDragEventArgs e) {
331      TreeNode sourceNode = (TreeNode)e.Item;
332      DoDragDrop(sourceNode, DragDropEffects.All);
333    }
334
335    private void treeSlaveGroup_DragEnter(object sender, DragEventArgs e) {
336      e.Effect = DragDropEffects.Move;
337    }
338
339    private void treeSlaveGroup_DragOver(object sender, DragEventArgs e) {
340      e.Effect = DragDropEffects.Move;
341    }
342
343    private void treeSlaveGroup_QueryContinueDrag(object sender, QueryContinueDragEventArgs e) {
344      e.Action = DragAction.Continue;
345    }
346
347    void ResetView() {
348      if (this.InvokeRequired) {
349        Invoke(new Action(ResetView));
350      } else {
351        treeSlaveGroup.Nodes.Clear();
352
353        if (slaveView.Content != null && slaveView.Content is SlaveGroup) {
354          slaveView.Content.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(SlaveViewContent_PropertyChanged);
355        }
356        slaveView.Content = null;
357        if (scheduleView.Content != null) {
358          scheduleView.Content.Clear();
359        }
360        HiveAdminClient.Instance.ResetDowntime();
361      }
362    }
363
364    private void UpdateResources() {
365      ResetView();
366
367      try {
368        HiveAdminClient.Instance.Refresh();
369        Content = HiveAdminClient.Instance.Resources;
370      }
371      catch (MessageSecurityException) {
372        ShowMessageSecurityException();
373      }
374      catch (AnonymousUserException) {
375        ShowHiveInformationDialog();
376      }
377    }
378
379    private void ShowMessageSecurityException() {
380      if (this.InvokeRequired) {
381        Invoke(new Action(ShowMessageSecurityException));
382      } else {
383        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);
384      }
385    }
386
387    private void ShowHiveInformationDialog() {
388      if (this.InvokeRequired) {
389        Invoke(new Action(ShowHiveInformationDialog));
390      } else {
391        using (HiveInformationDialog dialog = new HiveInformationDialog()) {
392          dialog.ShowDialog(this);
393        }
394      }
395    }
396
397    private void UpdateResourcesAsync() {
398      TS.Task.Factory.StartNew(UpdateResources).ContinueWith((t) => {
399        DisplayError(t.Exception);
400      }, TaskContinuationOptions.OnlyOnFaulted);
401    }
402
403    private void UpdateSchedule() {
404      HiveAdminClient.Instance.RefreshCalendar();
405      scheduleView.Invoke(new Action(() => scheduleView.Content = HiveAdminClient.Instance.Downtimes));
406    }
407
408    private void UpdateScheduleAsync() {
409      TS.Task.Factory.StartNew(UpdateSchedule).ContinueWith((t) => {
410        DisplayError(t.Exception);
411      }, TaskContinuationOptions.OnlyOnFaulted);
412    }
413
414    private void DisplayError(Exception ex) {
415      MessageBox.Show(string.Format("An error occured while updating: {0} {1}", Environment.NewLine, ex.Message), "HeuristicLab Hive Administrator", MessageBoxButtons.OK, MessageBoxIcon.Error);
416    }
417
418    private void tabSlaveGroup_SelectedIndexChanged(object sender, EventArgs e) {
419      if (tabSlaveGroup.SelectedIndex == 1) {
420        UpdateScheduleAsync();
421      }
422    }
423
424    private void btnRefresh_Click(object sender, EventArgs e) {
425      UpdateResourcesAsync();
426    }
427
428    private void ResourcesView_Load(object sender, EventArgs e) {
429      UpdateResourcesAsync();
430    }
431  }
432}
Note: See TracBrowser for help on using the repository browser.