Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.Administrator/3.3/Views/ResourcesView.cs @ 6734

Last change on this file since 6734 was 6734, checked in by ascheibe, 13 years ago

#1233 some minor improvements in the Slave UI and Administrator UI

File size: 13.0 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.Windows.Forms;
26using HeuristicLab.Clients.Hive.Views;
27using HeuristicLab.Core;
28using HeuristicLab.Core.Views;
29using HeuristicLab.MainForm;
30
31namespace HeuristicLab.Clients.Hive.Administrator.Views {
32  [View("ResourcesView")]
33  [Content(typeof(IItemList<Resource>), IsDefaultView = true)]
34  public partial class ResourcesView : ItemView {
35    public new IItemList<Resource> Content {
36      get { return (IItemList<Resource>)base.Content; }
37      set { base.Content = value; }
38    }
39
40    private const string ungroupedGroupName = "UNGROUPED";
41    private const int slaveImageIndex = 0;
42    private const int slaveGroupImageIndex = 1;
43
44
45    public ResourcesView() {
46      InitializeComponent();
47      treeSlaveGroup.ImageList.Images.Add(HiveImageLibrary.Slave);
48      treeSlaveGroup.ImageList.Images.Add(HiveImageLibrary.SlaveGroup);
49      updateScheduleControl.UpdateAction = new Action(UpdateSchedule);
50      updateSlaveGroup.UpdateAction = new Action(UpdateSlaveGroups);
51
52      updateSlaveGroup.Update();
53    }
54
55
56    #region Register Content Events
57    protected override void DeregisterContentEvents() {
58      Content.ItemsAdded -= new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<Resource>>(Content_ItemsAdded);
59      Content.ItemsRemoved -= new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<Resource>>(Content_ItemsRemoved);
60      base.DeregisterContentEvents();
61    }
62    protected override void RegisterContentEvents() {
63      base.RegisterContentEvents();
64      Content.ItemsAdded += new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<Resource>>(Content_ItemsAdded);
65      Content.ItemsRemoved += new Collections.CollectionItemsChangedEventHandler<Collections.IndexedItem<Resource>>(Content_ItemsRemoved);
66    }
67    #endregion
68
69    protected override void OnContentChanged() {
70      base.OnContentChanged();
71      if (Content == null) {
72        slaveView.Content = null;
73        treeSlaveGroup.Nodes.Clear();
74      } else {
75        treeSlaveGroup.Nodes.Clear();
76
77        //rebuild
78        TreeNode ungrp = new TreeNode(ungroupedGroupName);
79        ungrp.ImageIndex = slaveGroupImageIndex;
80        ungrp.SelectedImageIndex = ungrp.ImageIndex;
81        var newGroup = new SlaveGroup();
82        newGroup.Name = ungroupedGroupName;
83        newGroup.Id = Guid.NewGuid();
84        newGroup.Description = "Contains slaves which are in no group";
85        ungrp.Tag = newGroup;
86
87        foreach (Resource g in Content) {
88          if (g.GetType() == typeof(SlaveGroup)) {
89            //root node
90            if (g.ParentResourceId == null) {
91              TreeNode tn = new TreeNode();
92              tn.ImageIndex = slaveGroupImageIndex;
93              tn.SelectedImageIndex = tn.ImageIndex;
94
95              tn.Tag = g;
96              tn.Text = g.Name;
97
98              BuildSlaveGroupTree(g, tn);
99              tn.ExpandAll();
100              treeSlaveGroup.Nodes.Add(tn);
101            }
102          } else if (g.GetType() == typeof(Slave)) {
103            if (g.ParentResourceId == null) {
104              var stn = new TreeNode(g.Name);
105              stn.ImageIndex = slaveImageIndex;
106              stn.SelectedImageIndex = stn.ImageIndex;
107              stn.Tag = g;
108              ungrp.Nodes.Add(stn);
109            }
110          }
111        }
112        ungrp.ExpandAll();
113        treeSlaveGroup.Nodes.Add(ungrp);
114      }
115    }
116
117    private void BuildSlaveGroupTree(Resource g, TreeNode tn) {
118      foreach (Resource r in Content.Where(s => s.ParentResourceId != null && s.ParentResourceId == g.Id)) {
119        TreeNode stn = new TreeNode(r.Name);
120        if (r is Slave) {
121          stn.ImageIndex = slaveImageIndex;
122        } else if (r is SlaveGroup) {
123          stn.ImageIndex = slaveGroupImageIndex;
124        }
125        stn.SelectedImageIndex = stn.ImageIndex;
126        stn.Tag = r;
127        tn.Nodes.Add(stn);
128
129        BuildSlaveGroupTree(r, stn);
130        tn.ExpandAll();
131      }
132    }
133
134    protected override void SetEnabledStateOfControls() {
135      base.SetEnabledStateOfControls();
136    }
137
138    private void treeSlaveGroup_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) {
139      if (slaveView.Content != null && slaveView.Content is SlaveGroup) {
140        slaveView.Content.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(SlaveViewContent_PropertyChanged);
141      }
142
143      slaveView.Content = (Resource)e.Node.Tag;
144      scheduleView.ResourceId = ((Resource)e.Node.Tag).Id;
145
146      if (e.Node.Tag is SlaveGroup) {
147        slaveView.Content.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(SlaveViewContent_PropertyChanged);
148      }
149
150      if (tabSlaveGroup.SelectedIndex == 1) {
151        UpdateSchedule();
152      }
153    }
154
155    void SlaveViewContent_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) {
156      OnContentChanged();
157    }
158
159    private void btnAddGroup_Click(object sender, EventArgs e) {
160      SlaveGroup newGroup = new SlaveGroup();
161      newGroup.Name = "New Group";
162      Content.Add(newGroup);
163    }
164
165    void Content_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<Collections.IndexedItem<Resource>> e) {
166      OnContentChanged();
167    }
168
169    void Content_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<Collections.IndexedItem<Resource>> e) {
170      OnContentChanged();
171    }
172
173    private void btnRemoveGroup_Click(object sender, EventArgs e) {
174      if (treeSlaveGroup.SelectedNode != null && treeSlaveGroup.SelectedNode.Tag != null) {
175        Resource res = (Resource)treeSlaveGroup.SelectedNode.Tag;
176
177        DialogResult diagRes = MessageBox.Show("Do you really want to delete " + res.Name + "?", "HeuristicLab Hive Administration", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
178        if (diagRes == DialogResult.Yes) {
179          if (res is Slave) {
180            Content.Remove(res);
181            ServiceLocator.Instance.CallHiveService(service => service.DeleteSlave(res.Id));
182          } else if (res is SlaveGroup) {
183            //only delete empty groups
184            if (Content.Where(s => s.ParentResourceId == res.Id).Count() < 1) {
185              Content.Remove(res);
186              ServiceLocator.Instance.CallHiveService(service => service.DeleteSlaveGroup(res.Id));
187            } else {
188              MessageBox.Show("Only empty groups can be deleted.", "HeuristicLab Hive Administration", MessageBoxButtons.OK, MessageBoxIcon.Error);
189            }
190          }
191        }
192      }
193    }
194
195    private void btnSave_Click(object sender, EventArgs e) {
196      foreach (Resource res in Content) {
197        if (res is SlaveGroup && res.Id == Guid.Empty) {
198          SlaveGroup slaveGroup = (SlaveGroup)res;
199          ServiceLocator.Instance.CallHiveService(service => slaveGroup.Id = service.AddSlaveGroup(slaveGroup));
200        }
201        if (res.Id != Guid.Empty && res.Modified) {
202          if (res is SlaveGroup) {
203            ServiceLocator.Instance.CallHiveService(service => service.UpdateSlaveGroup((SlaveGroup)res));
204          } else if (res is Slave) {
205            ServiceLocator.Instance.CallHiveService(service => service.UpdateSlave((Slave)res));
206          }
207        }
208      }
209    }
210
211    private void treeSlaveGroup_DragDrop(object sender, DragEventArgs e) {
212      if (e.Data.GetDataPresent("System.Windows.Forms.TreeNode", false)) {
213        Point pt = ((TreeView)sender).PointToClient(new Point(e.X, e.Y));
214        TreeNode destNode = ((TreeView)sender).GetNodeAt(pt);
215        TreeNode newNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode");
216
217        if (destNode.TreeView == newNode.TreeView) {
218          if (destNode.Text == ungroupedGroupName || (destNode.Parent != null && destNode.Parent.Text == ungroupedGroupName)) {
219            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.",
220              ungroupedGroupName, Environment.NewLine), "HeuristicLab Hive Administration", MessageBoxButtons.OK, MessageBoxIcon.Information);
221            return;
222          }
223
224          SlaveGroup sgrp = null;
225          TreeNode parentNode = null;
226          if (destNode.Tag != null && destNode.Tag is SlaveGroup) {
227            sgrp = (SlaveGroup)destNode.Tag;
228            parentNode = destNode;
229          } else if (destNode.Parent != null && destNode.Parent.Tag is SlaveGroup) {
230            sgrp = (SlaveGroup)destNode.Parent.Tag;
231            parentNode = destNode.Parent;
232          }
233
234          if (newNode.Tag is SlaveGroup && CheckParentsEqualsMovedNode(parentNode, newNode)) {
235            return;
236          }
237
238          if (sgrp != null && newNode.Tag != null) {
239            //save parent group to get an id
240            if (sgrp.Id == Guid.Empty) {
241              ServiceLocator.Instance.CallHiveService(service => sgrp.Id = service.AddSlaveGroup(sgrp));
242            }
243
244            if (newNode.Tag is Slave) {
245              Slave slave = (Slave)newNode.Tag;
246              if (slave.ParentResourceId == null || (slave.ParentResourceId != null && slave.ParentResourceId != sgrp.Id)) {
247                slave.ParentResourceId = sgrp.Id;
248                newNode.Remove();
249                parentNode.Nodes.Clear();
250                BuildSlaveGroupTree(sgrp, parentNode);
251              }
252            } else if (newNode.Tag is SlaveGroup) {
253              SlaveGroup slaveGroup = (SlaveGroup)newNode.Tag;
254              if (slaveGroup.ParentResourceId == null || (slaveGroup.ParentResourceId != null && slaveGroup.ParentResourceId != sgrp.Id)) {
255                slaveGroup.ParentResourceId = sgrp.Id;
256                newNode.Remove();
257                parentNode.Nodes.Clear();
258                BuildSlaveGroupTree(sgrp, parentNode);
259              }
260            }
261          }
262        }
263      }
264    }
265
266    private bool CheckParentsEqualsMovedNode(TreeNode dest, TreeNode movedNode) {
267      TreeNode tmp = dest;
268
269      while (tmp != null) {
270        if (tmp == movedNode) {
271          return true;
272        }
273        tmp = tmp.Parent;
274      }
275      return false;
276    }
277
278    private void treeSlaveGroup_ItemDrag(object sender, ItemDragEventArgs e) {
279      TreeNode sourceNode = (TreeNode)e.Item;
280      DoDragDrop(sourceNode, DragDropEffects.All);
281    }
282
283    private void treeSlaveGroup_DragEnter(object sender, DragEventArgs e) {
284      e.Effect = DragDropEffects.Move;
285    }
286
287    private void treeSlaveGroup_DragOver(object sender, DragEventArgs e) {
288      e.Effect = DragDropEffects.Move;
289    }
290
291    private void treeSlaveGroup_QueryContinueDrag(object sender, QueryContinueDragEventArgs e) {
292      e.Action = DragAction.Continue;
293    }
294
295    void ResetView() {
296      treeSlaveGroup.Nodes.Clear();
297
298      if (slaveView.Content != null && slaveView.Content is SlaveGroup) {
299        slaveView.Content.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(SlaveViewContent_PropertyChanged);
300      }
301      slaveView.Content = null;
302      scheduleView.ResourceId = Guid.Empty;
303    }
304
305    private void UpdateSlaveGroups() {
306      if (this.InvokeRequired) {
307        this.Invoke(new Action(ResetView));
308      } else {
309        ResetView();
310      }
311
312      IItemList<Resource> resources = new ItemList<Resource>();
313
314      ServiceLocator.Instance.CallHiveService(service => {
315        service.GetSlaveGroups().ForEach(g => resources.Add(g));
316        service.GetSlaves().ForEach(s => resources.Add(s));
317      });
318      Content = resources;
319    }
320
321    private void UpdateSchedule() {
322      Guid resourceId = scheduleView.ResourceId;
323      if (resourceId != null) {
324        ServiceLocator.Instance.CallHiveService(service => {
325          var appointments = service.GetDowntimesForResource(resourceId);
326          ItemList<Downtime> ias = new ItemList<Downtime>();
327          appointments.ForEach(a => ias.Add(a));
328          scheduleView.Invoke(new Action(() => scheduleView.Content = ias));
329        });
330      }
331    }
332
333    private void tabSlaveGroup_SelectedIndexChanged(object sender, EventArgs e) {
334      if (tabSlaveGroup.SelectedIndex == 1) {
335        UpdateSchedule();
336      }
337    }
338  }
339}
Note: See TracBrowser for help on using the repository browser.