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 @ 6703

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

#1233 some renaming to be more consistent with OKB

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