source: trunk/sources/HeuristicLab.Clients.Hive.Administrator/3.3/Views/ResourcesView.cs @ 7249

Last change on this file since 7249 was 7249, checked in by ascheibe, 10 years ago

#1725

  • added a dialog which displays information for anonymous hive users
  • HiveServiceLocator now checks if the username is anonymous and throws an exception if that's the case
File size: 15.1 KB
Line 
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
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(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              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    }
173
174    private void treeSlaveGroup_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) {
175      if (slaveView.Content != null && slaveView.Content is SlaveGroup) {
176        slaveView.Content.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(SlaveViewContent_PropertyChanged);
177      }
178
179      slaveView.Content = (Resource)e.Node.Tag;
180      HiveAdminClient.Instance.DowntimeForResourceId = ((Resource)e.Node.Tag).Id;
181
182      if (e.Node.Tag is SlaveGroup) {
183        slaveView.Content.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(SlaveViewContent_PropertyChanged);
184      }
185
186      if (tabSlaveGroup.SelectedIndex == 1) {
187        UpdateScheduleAsync();
188      }
189    }
190
191    void SlaveViewContent_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) {
192      OnContentChanged();
193      if (e.PropertyName == "HbInterval") {
194        UpdateChildHbIntervall(slaveView.Content);
195      }
196    }
197
198    private void UpdateChildHbIntervall(Resource resource) {
199      foreach (Resource r in Content.Where(x => x.ParentResourceId == resource.Id)) {
200        r.HbInterval = resource.HbInterval;
201        if (r is SlaveGroup) {
202          UpdateChildHbIntervall(r);
203        }
204      }
205    }
206
207    private void btnAddGroup_Click(object sender, EventArgs e) {
208      SlaveGroup newGroup = new SlaveGroup();
209      newGroup.Name = "New Group";
210      Content.Add(newGroup);
211    }
212
213    void Content_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<Collections.IndexedItem<Resource>> e) {
214      OnContentChanged();
215    }
216
217    void Content_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<Collections.IndexedItem<Resource>> e) {
218      OnContentChanged();
219    }
220
221    private void btnRemoveGroup_Click(object sender, EventArgs e) {
222      if (treeSlaveGroup.SelectedNode != null && treeSlaveGroup.SelectedNode.Tag != null) {
223        Resource res = (Resource)treeSlaveGroup.SelectedNode.Tag;
224
225        DialogResult diagRes = MessageBox.Show("Do you really want to delete " + res.Name + "?", "HeuristicLab Hive Administrator", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
226        if (diagRes == DialogResult.Yes) {
227          if (res is Slave) {
228            Content.Remove(res);
229            HiveAdminClient.Delete(res);
230          } else if (res is SlaveGroup) {
231            //only delete empty groups
232            if (Content.Where(s => s.ParentResourceId == res.Id).Count() < 1) {
233              Content.Remove(res);
234              HiveAdminClient.Delete(res);
235            } else {
236              MessageBox.Show("Only empty groups can be deleted.", "HeuristicLab Hive Administrator", MessageBoxButtons.OK, MessageBoxIcon.Error);
237            }
238          }
239        }
240      }
241    }
242
243    private void btnSave_Click(object sender, EventArgs e) {
244      foreach (Resource res in Content) {
245        if (res is SlaveGroup && res.Id == Guid.Empty) {
246          SlaveGroup slaveGroup = (SlaveGroup)res;
247          slaveGroup.Store();
248        } else if (res.Id != Guid.Empty && res.Modified) {
249          res.Store();
250        }
251      }
252    }
253
254    private void treeSlaveGroup_DragDrop(object sender, DragEventArgs e) {
255      if (e.Data.GetDataPresent("System.Windows.Forms.TreeNode", false)) {
256        Point pt = ((TreeView)sender).PointToClient(new Point(e.X, e.Y));
257        TreeNode destNode = ((TreeView)sender).GetNodeAt(pt);
258        TreeNode newNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode");
259
260        if (destNode.TreeView == newNode.TreeView) {
261          if (destNode.Text == UngroupedGroupName || (destNode.Parent != null && destNode.Parent.Text == UngroupedGroupName)) {
262            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.",
263              UngroupedGroupName, Environment.NewLine), "HeuristicLab Hive Administrator", MessageBoxButtons.OK, MessageBoxIcon.Information);
264            return;
265          }
266
267          SlaveGroup sgrp = null;
268          TreeNode parentNode = null;
269          if (destNode.Tag != null && destNode.Tag is SlaveGroup) {
270            sgrp = (SlaveGroup)destNode.Tag;
271            parentNode = destNode;
272          } else if (destNode.Parent != null && destNode.Parent.Tag is SlaveGroup) {
273            sgrp = (SlaveGroup)destNode.Parent.Tag;
274            parentNode = destNode.Parent;
275          }
276
277          if (newNode.Tag is SlaveGroup && CheckParentsEqualsMovedNode(parentNode, newNode)) {
278            return;
279          }
280
281          if (sgrp != null && newNode.Tag != null) {
282            //save parent group to get an id
283            if (sgrp.Id == Guid.Empty) {
284              sgrp.Store();
285            }
286
287            if (newNode.Tag is Slave) {
288              Slave slave = (Slave)newNode.Tag;
289              if (slave.ParentResourceId == null || (slave.ParentResourceId != null && slave.ParentResourceId != sgrp.Id)) {
290                slave.ParentResourceId = sgrp.Id;
291                newNode.Remove();
292                parentNode.Nodes.Clear();
293                BuildSlaveGroupTree(sgrp, parentNode);
294              }
295            } else if (newNode.Tag is SlaveGroup) {
296              SlaveGroup slaveGroup = (SlaveGroup)newNode.Tag;
297              if (slaveGroup.ParentResourceId == null || (slaveGroup.ParentResourceId != null && slaveGroup.ParentResourceId != sgrp.Id)) {
298                slaveGroup.ParentResourceId = sgrp.Id;
299                newNode.Remove();
300                parentNode.Nodes.Clear();
301                BuildSlaveGroupTree(sgrp, parentNode);
302              }
303            }
304          }
305        }
306      }
307    }
308
309    private bool CheckParentsEqualsMovedNode(TreeNode dest, TreeNode movedNode) {
310      TreeNode tmp = dest;
311
312      while (tmp != null) {
313        if (tmp == movedNode) {
314          return true;
315        }
316        tmp = tmp.Parent;
317      }
318      return false;
319    }
320
321    private void treeSlaveGroup_ItemDrag(object sender, ItemDragEventArgs e) {
322      TreeNode sourceNode = (TreeNode)e.Item;
323      DoDragDrop(sourceNode, DragDropEffects.All);
324    }
325
326    private void treeSlaveGroup_DragEnter(object sender, DragEventArgs e) {
327      e.Effect = DragDropEffects.Move;
328    }
329
330    private void treeSlaveGroup_DragOver(object sender, DragEventArgs e) {
331      e.Effect = DragDropEffects.Move;
332    }
333
334    private void treeSlaveGroup_QueryContinueDrag(object sender, QueryContinueDragEventArgs e) {
335      e.Action = DragAction.Continue;
336    }
337
338    void ResetView() {
339      treeSlaveGroup.Nodes.Clear();
340
341      if (slaveView.Content != null && slaveView.Content is SlaveGroup) {
342        slaveView.Content.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(SlaveViewContent_PropertyChanged);
343      }
344      slaveView.Content = null;
345      if (scheduleView.Content != null) {
346        scheduleView.Content.Clear();
347      }
348      HiveAdminClient.Instance.ResetDowntime();
349    }
350
351    private void UpdateResources() {
352      if (this.InvokeRequired) {
353        this.Invoke(new Action(ResetView));
354      } else {
355        ResetView();
356      }
357
358      try {
359        HiveAdminClient.Instance.Refresh();
360        Content = HiveAdminClient.Instance.Resources;
361      }
362      catch (MessageSecurityException) {
363        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);
364      }
365      catch (AnonymousUserException) {
366        HiveInformationDialog dialog = new HiveInformationDialog();
367        dialog.ShowDialog(this);
368      }
369    }
370
371    private void UpdateResourcesAsync() {
372      TS.Task.Factory.StartNew(UpdateResources).ContinueWith((t) => {
373        DisplayError(t.Exception);
374      }, TaskContinuationOptions.OnlyOnFaulted);
375    }
376
377    private void UpdateSchedule() {
378      HiveAdminClient.Instance.RefreshCalendar();
379      scheduleView.Invoke(new Action(() => scheduleView.Content = HiveAdminClient.Instance.Downtimes));
380    }
381
382    private void UpdateScheduleAsync() {
383      TS.Task.Factory.StartNew(UpdateSchedule).ContinueWith((t) => {
384        DisplayError(t.Exception);
385      }, TaskContinuationOptions.OnlyOnFaulted);
386    }
387
388    private void DisplayError(Exception ex) {
389      MessageBox.Show(string.Format("An error occured while updating: {0} {1}", Environment.NewLine, ex.Message), "HeuristicLab Hive Administrator", MessageBoxButtons.OK, MessageBoxIcon.Error);
390    }
391
392    private void tabSlaveGroup_SelectedIndexChanged(object sender, EventArgs e) {
393      if (tabSlaveGroup.SelectedIndex == 1) {
394        UpdateScheduleAsync();
395      }
396    }
397
398    private void btnRefresh_Click(object sender, EventArgs e) {
399      UpdateResourcesAsync();
400    }
401
402    private void ResourcesView_Load(object sender, EventArgs e) {
403      UpdateResourcesAsync();
404    }
405  }
406}
Note: See TracBrowser for help on using the repository browser.