source: branches/HiveProjectManagement/HeuristicLab.Clients.Hive.Administrator/3.3/Views/ResourcesView.cs @ 15557

Last change on this file since 15557 was 15557, checked in by jzenisek, 3 years ago

#2839 fixed tree building routine in ResourcesView

File size: 17.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2017 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.Collections.Generic;
24using System.ComponentModel;
25using System.Drawing;
26using System.Linq;
27using System.Windows.Forms;
28using HeuristicLab.Clients.Access;
29using HeuristicLab.Clients.Hive.Views;
30using HeuristicLab.Collections;
31using HeuristicLab.Common.Resources;
32using HeuristicLab.Core;
33using HeuristicLab.Core.Views;
34using HeuristicLab.MainForm;
35
36namespace HeuristicLab.Clients.Hive.Administrator.Views {
37  [View("Resources View")]
38  [Content(typeof(IItemList<Resource>), false)]
39  public partial class ResourcesView : ItemView, IDisposable {
40    private const int slaveImageIndex = 0;
41    private const int slaveGroupImageIndex = 1;
42    public const string ungroupedGroupName = "UNGROUPED";
43    public const string ungroupedGroupDescription = "Contains slaves that are not assigned to any group.";
44
45    private readonly Color ownedResourceColor = Color.LightGreen;
46    private readonly object locker = new object();
47
48    public new IItemList<Resource> Content {
49      get { return (IItemList<Resource>)base.Content; }
50      set { base.Content = value; }
51    }
52
53    public ResourcesView() {
54      InitializeComponent();
55
56      treeView.ImageList.Images.Add(VSImageLibrary.MonitorLarge);
57      treeView.ImageList.Images.Add(VSImageLibrary.NetworkCenterLarge);
58
59      HiveAdminClient.Instance.Refreshing += HiveAdminClient_Instance_Refreshing;
60      HiveAdminClient.Instance.Refreshed += HiveAdminClient_Instance_Refreshed;
61      AccessClient.Instance.Refreshing += AccessClient_Instance_Refreshing;
62      AccessClient.Instance.Refreshed += AccessClient_Instance_Refreshed;
63    }
64
65    #region Overrides
66    protected override void OnClosing(FormClosingEventArgs e) {
67      AccessClient.Instance.Refreshed -= AccessClient_Instance_Refreshed;
68      AccessClient.Instance.Refreshing -= AccessClient_Instance_Refreshing;
69      HiveAdminClient.Instance.Refreshed -= HiveAdminClient_Instance_Refreshed;
70      HiveAdminClient.Instance.Refreshing -= HiveAdminClient_Instance_Refreshing;
71      base.OnClosing(e);
72    }
73
74    protected override void RegisterContentEvents() {
75      base.RegisterContentEvents();
76      Content.ItemsAdded += Content_ItemsAdded;
77      Content.ItemsRemoved += Content_ItemsRemoved;
78    }
79
80    protected override void DeregisterContentEvents() {
81      Content.ItemsRemoved -= Content_ItemsRemoved;
82      Content.ItemsAdded -= Content_ItemsAdded;
83      base.DeregisterContentEvents();
84    }
85
86    protected override void OnContentChanged() {
87      base.OnContentChanged();
88      if (Content == null) {
89        treeView.Nodes.Clear();
90        viewHost.Content = null;
91        scheduleView.Content = null;
92      } else {
93        var top = BuildResourceTree(Content);
94        viewHost.Content = top;
95      }
96    }
97
98    protected override void SetEnabledStateOfControls() {
99      base.SetEnabledStateOfControls();
100      bool enabled = Content != null;
101      btnAddGroup.Enabled = enabled;
102      btnRemoveGroup.Enabled = enabled;
103      btnSave.Enabled = enabled;
104      scheduleView.SetEnabledStateOfSchedule(enabled && IsAuthorized((Resource)viewHost.Content));
105    }
106    #endregion
107
108    #region Event Handlers
109    private void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<Resource>> e) {
110      if (InvokeRequired) Invoke((Action<object, CollectionItemsChangedEventArgs<IndexedItem<Resource>>>)Content_ItemsAdded, sender, e);
111      else {
112        OnContentChanged();
113      }
114    }
115
116    private void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<Resource>> e) {
117      if (InvokeRequired) Invoke((Action<object, CollectionItemsChangedEventArgs<IndexedItem<Resource>>>)Content_ItemsRemoved, sender, e);
118      else {
119        OnContentChanged();
120      }
121    }
122
123    private void SlaveViewContent_PropertyChanged(object sender, PropertyChangedEventArgs e) {
124      if (InvokeRequired) Invoke((Action<object, PropertyChangedEventArgs>)SlaveViewContent_PropertyChanged, sender, e);
125      else {
126        OnContentChanged();
127        if (e.PropertyName == "HbInterval") {
128          UpdateChildHbIntervall((Resource)viewHost.Content);
129        }
130      }
131    }
132
133    private void HiveAdminClient_Instance_Refreshing(object sender, EventArgs e) {
134      if (InvokeRequired) Invoke((Action<object, EventArgs>)HiveAdminClient_Instance_Refreshing, sender, e);
135      else {
136        var mainForm = MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>();
137        mainForm.AddOperationProgressToView(this, "Refreshing ...");
138        SetEnabledStateOfControls();
139      }
140    }
141
142    private void HiveAdminClient_Instance_Refreshed(object sender, EventArgs e) {
143      if (InvokeRequired) Invoke((Action<object, EventArgs>)HiveAdminClient_Instance_Refreshed, sender, e);
144      else {
145        var mainForm = MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>();
146        mainForm.RemoveOperationProgressFromView(this);
147        SetEnabledStateOfControls();
148      }
149    }
150
151    private void AccessClient_Instance_Refreshing(object sender, EventArgs e) {
152      if (InvokeRequired) Invoke((Action<object, EventArgs>)AccessClient_Instance_Refreshing, sender, e);
153      else {
154        var mainForm = MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>();
155        mainForm.AddOperationProgressToView(this, "Refreshing ...");
156        SetEnabledStateOfControls();
157      }
158    }
159
160    private void AccessClient_Instance_Refreshed(object sender, EventArgs e) {
161      if (InvokeRequired) Invoke((Action<object, EventArgs>)AccessClient_Instance_Refreshed, sender, e);
162      else {
163        var mainForm = MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>();
164        mainForm.RemoveOperationProgressFromView(this);
165        SetEnabledStateOfControls();
166      }
167    }
168
169    private async void ResourcesView_Load(object sender, EventArgs e) {
170      await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
171        action: () => UpdateResources());
172    }
173
174    private async void btnRefresh_Click(object sender, EventArgs e) {
175      lock (locker) {
176        if (!btnRefresh.Enabled) return;
177        btnRefresh.Enabled = false;
178      }
179
180      await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
181        action: () => UpdateResources(),
182        finallyCallback: () => btnRefresh.Enabled = true);
183    }
184
185    private void btnAddGroup_Click(object sender, EventArgs e) {
186      var group = new SlaveGroup {
187        Name = "New Group",
188        OwnerUserId = UserInformation.Instance.User.Id
189      };
190      Content.Add(group);
191    }
192
193    private async void btnRemoveGroup_Click(object sender, EventArgs e) {
194      lock (locker) {
195        if (!btnRemoveGroup.Enabled) return;
196        btnRemoveGroup.Enabled = false;
197      }
198
199      await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
200        action: () => RemoveResource(),
201        finallyCallback: () => btnRemoveGroup.Enabled = true);
202    }
203
204    private async void btnSave_Click(object sender, EventArgs e) {
205      lock (locker) {
206        if (!btnSave.Enabled) return;
207        btnSave.Enabled = false;
208      }
209
210      await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
211        action: () => {
212          var resourcesToSave = Content.Where(x => x.Id == Guid.Empty || x.Modified);
213          foreach (var resource in resourcesToSave)
214            resource.Store();
215        },
216        finallyCallback: () => btnSave.Enabled = true);
217    }
218
219    private async void treeSlaveGroup_AfterSelect(object sender, TreeViewEventArgs e) {
220      var selectedResource = (Resource)e.Node.Tag;
221
222      if (viewHost.Content != null && viewHost.Content is SlaveGroup)
223        ((SlaveGroup)viewHost.Content).PropertyChanged -= SlaveViewContent_PropertyChanged;
224
225      viewHost.Content = selectedResource;
226      HiveAdminClient.Instance.DowntimeForResourceId = selectedResource != null ? selectedResource.Id : Guid.Empty;
227      await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
228        action: () => UpdateSchedule(),
229        finallyCallback: () => scheduleView.Content = HiveAdminClient.Instance.Downtimes);
230
231      if (selectedResource != null && selectedResource is SlaveGroup)
232        selectedResource.PropertyChanged += SlaveViewContent_PropertyChanged;
233
234      if (IsAuthorized(selectedResource)) {
235        if (!tabSlaveGroup.TabPages.Contains(tabSchedule))
236          tabSlaveGroup.TabPages.Add(tabSchedule);
237      } else {
238        if (tabSlaveGroup.TabPages.Contains(tabSchedule))
239          tabSlaveGroup.TabPages.Remove(tabSchedule);
240      }
241    }
242
243    private void treeSlaveGroup_DragDrop(object sender, DragEventArgs e) {
244      var sourceNode = (TreeNode)e.Data.GetData(typeof(TreeNode));
245      if (sourceNode == null) return;
246
247      var treeView = (TreeView)sender;
248      if (sourceNode.TreeView != treeView) return;
249
250      var targetPoint = treeView.PointToClient(new Point(e.X, e.Y));
251      var targetNode = treeView.GetNodeAt(targetPoint);
252
253      if (sourceNode == targetNode) return;
254
255      if (targetNode.Text == ungroupedGroupName || targetNode.Parent != null && targetNode.Parent.Text == ungroupedGroupName) {
256        MessageBox.Show(
257          string.Format(@"You cannot drag resources to group ""{0}"". This group only contains slaves which have not been assigned to a real group yet.", ungroupedGroupName),
258          "HeuristicLab Hive Administrator",
259          MessageBoxButtons.OK,
260          MessageBoxIcon.Information);
261        return;
262      }
263
264      if (sourceNode.Parent == null)
265        treeView.Nodes.Remove(sourceNode);
266      else {
267        sourceNode.Parent.Nodes.Remove(sourceNode);
268        ((Resource)sourceNode.Tag).ParentResourceId = null;
269      }
270
271      if (targetNode == null) {
272        treeView.Nodes.Add(sourceNode);
273      } else {
274        targetNode.Nodes.Add(sourceNode);
275        ((Resource)sourceNode.Tag).ParentResourceId = ((Project)targetNode.Tag).Id;
276      }
277    }
278
279    private void treeSlaveGroup_ItemDrag(object sender, ItemDragEventArgs e) {
280      TreeNode sourceNode = (TreeNode)e.Item;
281      if (IsAuthorized((Resource)sourceNode.Tag))
282        DoDragDrop(sourceNode, DragDropEffects.All);
283    }
284
285    private void treeSlaveGroup_DragEnter(object sender, DragEventArgs e) {
286      e.Effect = DragDropEffects.Move;
287    }
288
289    private void treeSlaveGroup_DragOver(object sender, DragEventArgs e) {
290      e.Effect = DragDropEffects.Move;
291    }
292
293    private void treeSlaveGroup_QueryContinueDrag(object sender, QueryContinueDragEventArgs e) {
294      e.Action = DragAction.Continue;
295    }
296    #endregion
297
298    #region Helpers
299    private Resource BuildResourceTree(IEnumerable<Resource> resources) {
300      treeView.Nodes.Clear();
301      if (!resources.Any()) return null;
302
303      var mainResources = new HashSet<Resource>(resources.OfType<SlaveGroup>().Where(x => x.ParentResourceId == null));
304      var subResources = new HashSet<Resource>(resources.Except(mainResources));
305
306      var stack = new Stack<Resource>(mainResources.OrderByDescending(x => x.Name));
307      var top = stack.Peek();
308
309      TreeNode currentNode = null;
310      Resource currentResource = null;
311
312      while(stack.Any()) {
313        var newResource = stack.Pop();
314        var newNode = new TreeNode(newResource.Name) { Tag = newResource };
315
316        // search for parent node of newNode and save in currentNode
317        // necessary since newNodes (stack top items) might be siblings
318        // or grand..grandparents of previous node (currentNode)
319        while(currentNode != null && newResource.ParentResourceId != currentResource.Id) {
320          currentNode = currentNode.Parent;
321          currentResource = currentNode == null ? null : (Resource)currentNode.Tag;
322        }
323
324        if(currentNode == null) {
325          treeView.Nodes.Add(newNode);
326        } else {
327          currentNode.Nodes.Add(newNode);
328        }
329
330        if(newResource is Slave) {
331          newNode.ImageIndex = slaveImageIndex;
332        } else {
333          newNode.ImageIndex = slaveGroupImageIndex;
334
335          var childResources = subResources.Where(x => x.ParentResourceId == newResource.Id);
336          if(childResources.Any()) {
337            foreach(var resource in childResources.OrderByDescending(x => x.Name)) {
338              subResources.Remove(resource);
339              stack.Push(resource);
340            }
341            currentNode = newNode;
342            currentResource = newResource;
343          }
344        }
345        newNode.SelectedImageIndex = newNode.ImageIndex;
346        if (newResource.OwnerUserId == UserInformation.Instance.User.Id)
347          newNode.BackColor = ownedResourceColor;
348      }
349
350      var ungroupedNode = new TreeNode(ungroupedGroupName) {
351        ForeColor = SystemColors.GrayText,
352        Tag = new SlaveGroup() {
353          Name = ungroupedGroupName,
354          Description = ungroupedGroupDescription
355        }
356      };
357
358      foreach (var slave in subResources.OfType<Slave>().OrderBy(x => x.Name)) {
359        var slaveNode = new TreeNode(slave.Name) { Tag = slave };
360        ungroupedNode.Nodes.Add(slaveNode);
361      }
362
363      treeView.Nodes.Add(ungroupedNode);
364      treeView.ExpandAll();
365
366      return top;
367    }
368
369    private void UpdateChildHbIntervall(Resource resource) {
370      foreach (Resource r in Content.Where(x => x.ParentResourceId == resource.Id)) {
371        r.HbInterval = resource.HbInterval;
372        if (r is SlaveGroup) {
373          UpdateChildHbIntervall(r);
374        }
375      }
376    }
377
378    private bool CheckParentsEqualsMovedNode(TreeNode dest, TreeNode movedNode) {
379      TreeNode tmp = dest;
380
381      while (tmp != null) {
382        if (tmp == movedNode) {
383          return true;
384        }
385        tmp = tmp.Parent;
386      }
387      return false;
388    }
389
390    private void ResetView() {
391      if (InvokeRequired) Invoke((Action)ResetView);
392      else {
393        treeView.Nodes.Clear();
394
395        if (viewHost.Content != null && viewHost.Content is SlaveGroup) {
396          ((SlaveGroup)viewHost.Content).PropertyChanged -= SlaveViewContent_PropertyChanged;
397        }
398
399        viewHost.Content = null;
400        if (scheduleView.Content != null) {
401          scheduleView.Content.Clear();
402        }
403
404        HiveAdminClient.Instance.ResetDowntime();
405      }
406    }
407
408    private void UpdateResources() {
409      HiveAdminClient.Instance.Refresh();
410      Content = HiveAdminClient.Instance.Resources;
411    }
412
413    private void RemoveResource() {
414      var selectedNode = treeView.SelectedNode;
415      if (selectedNode == null || selectedNode.Tag == null) return;
416
417      var resource = (Resource)selectedNode.Tag;
418      var result = MessageBox.Show(
419        "Do you really want to delete " + resource.Name + "?",
420        "HeuristicLab Hive Administrator",
421        MessageBoxButtons.YesNo,
422        MessageBoxIcon.Question);
423
424      if (result == DialogResult.Yes) {
425        if (resource is Slave) {
426          Content.Remove(resource);
427          HiveAdminClient.Delete(resource);
428        } else if (resource is SlaveGroup) {
429          if (Content.Any(x => x.ParentResourceId == resource.Id)) {
430            MessageBox.Show(
431              "Only empty resources can be deleted.",
432              "HeuristicLab Hive Administrator",
433              MessageBoxButtons.OK,
434              MessageBoxIcon.Error);
435          } else {
436            Content.Remove(resource);
437            HiveAdminClient.Delete(resource);
438          }
439        }
440      }
441    }
442
443    private void UpdateSchedule() {
444      try {
445        HiveAdminClient.Instance.RefreshCalendar();
446      } catch (AnonymousUserException) {
447        ShowHiveInformationDialog();
448      }
449    }
450
451    private bool IsAuthorized(Resource resource) {
452      return resource != null
453          && resource.Name != ungroupedGroupName
454          && resource.Id != Guid.Empty
455          && UserInformation.Instance.UserExists
456          && (resource.OwnerUserId == UserInformation.Instance.User.Id || HiveRoles.CheckAdminUserPermissions());
457    }
458
459    private void ShowHiveInformationDialog() {
460      if (InvokeRequired) Invoke((Action)ShowHiveInformationDialog);
461      else {
462        using (HiveInformationDialog dialog = new HiveInformationDialog()) {
463          dialog.ShowDialog(this);
464        }
465      }
466    }
467    #endregion
468  }
469}
Note: See TracBrowser for help on using the repository browser.