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

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

#1233

  • some Admin UI bugfixes

Slave:

  • fixed bug when Pause is called immediately after Calculate
  • send exceptions when something goes wrong in Pause or Stop
File size: 12.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.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    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 = 0;
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      }
128    }
129
130    protected override void SetEnabledStateOfControls() {
131      base.SetEnabledStateOfControls();
132      // TODO: Put code here to enable or disable controls based on whether the Content is/not null or the view is ReadOnly
133    }
134
135    private void treeSlaveGroup_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) {
136      if (slaveView.Content != null && slaveView.Content is SlaveGroup) {
137        slaveView.Content.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(SlaveViewContent_PropertyChanged);
138      }
139
140      slaveView.Content = (Resource)e.Node.Tag;
141      scheduleView.ResourceId = ((Resource)e.Node.Tag).Id;
142
143      if (e.Node.Tag is SlaveGroup) {
144        slaveView.Content.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(SlaveViewContent_PropertyChanged);
145      }
146
147      if (tabSlaveGroup.SelectedIndex == 1) {
148        UpdateSchedule();
149      }
150    }
151
152    void SlaveViewContent_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) {
153      OnContentChanged();
154    }
155
156    private void btnAddGroup_Click(object sender, EventArgs e) {
157      SlaveGroup newGroup = new SlaveGroup();
158      newGroup.Name = "New Group";
159      Content.Add(newGroup);
160    }
161
162    void Content_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<Collections.IndexedItem<Resource>> e) {
163      OnContentChanged();
164    }
165
166    void Content_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<Collections.IndexedItem<Resource>> e) {
167      OnContentChanged();
168    }
169
170    private void btnRemoveGroup_Click(object sender, EventArgs e) {
171      //TODO: display some warning?
172      if (treeSlaveGroup.SelectedNode != null && treeSlaveGroup.SelectedNode.Tag != null) {
173        Resource res = (Resource)treeSlaveGroup.SelectedNode.Tag;
174
175        if (res is Slave) {
176          Content.Remove(res);
177          ServiceLocator.Instance.CallHiveService(service => service.DeleteSlave(res.Id));
178        } else if (res is SlaveGroup) {
179          //only delete empty groups
180          if (Content.Where(s => s.ParentResourceId == res.Id).Count() < 1) {
181            Content.Remove(res);
182            ServiceLocator.Instance.CallHiveService(service => service.DeleteSlaveGroup(res.Id));
183          } else {
184            MessageBox.Show("Only empty groups can be deleted.", "HeuristicLab Hive Administration", MessageBoxButtons.OK, MessageBoxIcon.Error);
185          }
186        }
187      }
188    }
189
190    private void btnSave_Click(object sender, EventArgs e) {
191      foreach (Resource res in Content) {
192        if (res is SlaveGroup && res.Id == Guid.Empty) {
193          SlaveGroup slaveGroup = (SlaveGroup)res;
194          ServiceLocator.Instance.CallHiveService(service => slaveGroup.Id = service.AddSlaveGroup(slaveGroup));
195        }
196        if (res.Id != Guid.Empty && res.Modified) {
197          if (res is SlaveGroup) {
198            ServiceLocator.Instance.CallHiveService(service => service.UpdateSlaveGroup((SlaveGroup)res));
199          } else if (res is Slave) {
200            ServiceLocator.Instance.CallHiveService(service => service.UpdateSlave((Slave)res));
201          }
202        }
203      }
204    }
205
206    private void treeSlaveGroup_DragDrop(object sender, DragEventArgs e) {
207      if (e.Data.GetDataPresent("System.Windows.Forms.TreeNode", false)) {
208        Point pt = ((TreeView)sender).PointToClient(new Point(e.X, e.Y));
209        TreeNode destNode = ((TreeView)sender).GetNodeAt(pt);
210        TreeNode newNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode");
211
212        if (destNode.TreeView == newNode.TreeView) {
213          if (destNode.Text == ungroupedGroupName || (destNode.Parent != null && destNode.Parent.Text == ungroupedGroupName)) {
214            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.",
215              ungroupedGroupName, Environment.NewLine), "HeuristicLab Hive Administration", MessageBoxButtons.OK, MessageBoxIcon.Information);
216            return;
217          }
218
219          SlaveGroup sgrp = null;
220          if (destNode.Tag != null && destNode.Tag is SlaveGroup) {
221            sgrp = (SlaveGroup)destNode.Tag;
222          } else if (destNode.Parent != null && destNode.Parent.Tag is SlaveGroup) {
223            sgrp = (SlaveGroup)destNode.Parent.Tag;
224          }
225
226          if (sgrp != null && newNode.Tag != null) {
227            //save parent group to get an id
228            if (sgrp.Id == Guid.Empty) {
229              ServiceLocator.Instance.CallHiveService(service => sgrp.Id = service.AddSlaveGroup(sgrp));
230            }
231
232            if (newNode.Tag is Slave) {
233              Slave slave = (Slave)newNode.Tag;
234              if (slave.ParentResourceId == null || (slave.ParentResourceId != null && slave.ParentResourceId != sgrp.Id)) {
235                slave.ParentResourceId = sgrp.Id;
236              }
237            } else if (newNode.Tag is SlaveGroup) {
238              SlaveGroup slaveGroup = (SlaveGroup)newNode.Tag;
239              if (slaveGroup.ParentResourceId == null || (slaveGroup.ParentResourceId != null && slaveGroup.ParentResourceId != sgrp.Id)) {
240                slaveGroup.ParentResourceId = sgrp.Id;
241              }
242            }
243            OnContentChanged();
244          }
245        }
246      }
247    }
248
249    private void treeSlaveGroup_ItemDrag(object sender, ItemDragEventArgs e) {
250      TreeNode sourceNode = (TreeNode)e.Item;
251      DoDragDrop(sourceNode, DragDropEffects.All);
252    }
253
254    private void treeSlaveGroup_DragEnter(object sender, DragEventArgs e) {
255      e.Effect = DragDropEffects.Move;
256    }
257
258    private void treeSlaveGroup_DragOver(object sender, DragEventArgs e) {
259      e.Effect = DragDropEffects.Move;
260    }
261
262    private void treeSlaveGroup_QueryContinueDrag(object sender, QueryContinueDragEventArgs e) {
263      e.Action = DragAction.Continue;
264    }
265
266    void ResetView() {
267      treeSlaveGroup.Nodes.Clear();
268      if (slaveView.Content != null && slaveView.Content is SlaveGroup) {
269        slaveView.Content.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(SlaveViewContent_PropertyChanged);
270      }
271      slaveView.Content = null;
272      scheduleView.ResourceId = Guid.Empty;
273    }
274
275    private void UpdateSlaveGroups() {
276      this.Invoke(new Action(ResetView));
277      Content.Clear();
278      IItemList<Resource> resources = new ItemList<Resource>();
279
280      ServiceLocator.Instance.CallHiveService(service => {
281        service.GetSlaveGroups().ForEach(g => resources.Add(g));
282        service.GetSlaves().ForEach(s => resources.Add(s));
283      });
284      Content = resources;
285    }
286
287    private void UpdateSchedule() {
288      Guid resourceId = scheduleView.ResourceId;
289      if (resourceId != null) {
290        ServiceLocator.Instance.CallHiveService(service => {
291          var appointments = service.GetDowntimesForResource(resourceId);
292          ItemList<Downtime> ias = new ItemList<Downtime>();
293          appointments.ForEach(a => ias.Add(a));
294          scheduleView.Invoke(new Action(() => scheduleView.Content = ias));
295        });
296      }
297    }
298
299    private void tabSlaveGroup_SelectedIndexChanged(object sender, EventArgs e) {
300      if (tabSlaveGroup.SelectedIndex == 1) {
301        UpdateSchedule();
302      }
303    }
304  }
305}
Note: See TracBrowser for help on using the repository browser.