Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.Administration/3.4/Views/ResourcesView.cs @ 6454

Last change on this file since 6454 was 6454, checked in by cneumuel, 12 years ago

#1233

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