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

Last change on this file since 6437 was 6437, checked in by ascheibe, 12 years ago

#1233
Admin UI:

  • some bugfixes
  • removed dummy stuff
File size: 10.8 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            TreeNode tn = new TreeNode();
82            tn.ImageIndex = treeSlaveGroup.ImageList.Images.Count - 1;
83            tn.SelectedImageIndex = tn.ImageIndex;
84
85            tn.Tag = g;
86            tn.Text = g.Name;
87            foreach (Resource r in Content.Where(s => s.ParentResourceId != null && s.ParentResourceId == g.Id)) {
88              var stn = new TreeNode(r.Name);
89              stn.ImageIndex = 0;
90              stn.SelectedImageIndex = stn.ImageIndex;
91              stn.Tag = r;
92              tn.Nodes.Add(stn);
93            }
94            treeSlaveGroup.Nodes.Add(tn);
95
96          } else if (g.GetType() == typeof(Slave)) {
97            if (g.ParentResourceId == null) {
98              var stn = new TreeNode(g.Name);
99              stn.ImageIndex = 0;
100              stn.SelectedImageIndex = stn.ImageIndex;
101              stn.Tag = g;
102              ungrp.Nodes.Add(stn);
103            }
104          }
105        }
106        treeSlaveGroup.Nodes.Add(ungrp);
107      }
108    }
109
110    protected override void SetEnabledStateOfControls() {
111      base.SetEnabledStateOfControls();
112      // TODO: Put code here to enable or disable controls based on whether the Content is/not null or the view is ReadOnly
113    }
114
115    private void treeSlaveGroup_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) {
116      if (slaveView.Content != null && slaveView.Content is SlaveGroup) {
117        slaveView.Content.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(SlaveViewContent_PropertyChanged);
118      }
119
120      slaveView.Content = (Resource)e.Node.Tag;
121
122      if (e.Node.Tag.GetType() == typeof(Slave)) {
123        scheduleView.ResourceId = ((Slave)e.Node.Tag).Id;
124        if (tabSlaveGroup.SelectedIndex == 1) {
125          UpdateSchedule();
126        }
127      } else if (e.Node.Tag is SlaveGroup) {
128        slaveView.Content.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(SlaveViewContent_PropertyChanged);
129      }
130    }
131
132    void SlaveViewContent_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) {
133      OnContentChanged();
134    }
135
136    private void btnAddGroup_Click(object sender, EventArgs e) {
137      SlaveGroup newGroup = new SlaveGroup();
138      newGroup.Name = "New Group";
139      Content.Add(newGroup);
140    }
141
142    void Content_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<Collections.IndexedItem<Resource>> e) {
143      OnContentChanged();
144    }
145
146    void Content_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<Collections.IndexedItem<Resource>> e) {
147      OnContentChanged();
148    }
149
150    private void btnRemoveGroup_Click(object sender, EventArgs e) {
151      //TODO: display some warning?
152      if (treeSlaveGroup.SelectedNode != null && treeSlaveGroup.SelectedNode.Tag != null) {
153        Resource res = (Resource)treeSlaveGroup.SelectedNode.Tag;
154
155        if (res is Slave) {
156          Content.Remove(res);
157          ServiceLocator.Instance.CallHiveService(service => service.DeleteSlave(res.Id));
158        } else if (res is SlaveGroup) {
159          //only delete empty groups
160          if (Content.Where(s => s.ParentResourceId == res.Id).Count() < 1) {
161            Content.Remove(res);
162            ServiceLocator.Instance.CallHiveService(service => service.DeleteSlaveGroup(res.Id));
163          } else {
164            MessageBox.Show("Only empty groups can be deleted.", "HeuristicLab Hive Administration", MessageBoxButtons.OK, MessageBoxIcon.Error);
165          }
166        }
167      }
168    }
169
170    private void btnSave_Click(object sender, EventArgs e) {
171      foreach (Resource res in Content) {
172        if (res is SlaveGroup && res.Id == Guid.Empty) {
173          SlaveGroup slaveGroup = (SlaveGroup)res;
174          ServiceLocator.Instance.CallHiveService(service => slaveGroup.Id = service.AddSlaveGroup(slaveGroup));
175        }
176        if (res.Id != Guid.Empty && res.Modified) {
177          if (res is SlaveGroup) {
178            ServiceLocator.Instance.CallHiveService(service => service.UpdateSlaveGroup((SlaveGroup)res));
179          } else if (res is Slave) {
180            ServiceLocator.Instance.CallHiveService(service => service.UpdateSlave((Slave)res));
181          }
182        }
183      }
184    }
185
186    private void treeSlaveGroup_DragDrop(object sender, DragEventArgs e) {
187      if (e.Data.GetDataPresent("System.Windows.Forms.TreeNode", false)) {
188        Point pt = ((TreeView)sender).PointToClient(new Point(e.X, e.Y));
189        TreeNode destNode = ((TreeView)sender).GetNodeAt(pt);
190        TreeNode newNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode");
191
192        if (destNode.TreeView == newNode.TreeView) {
193          SlaveGroup sgrp = null;
194          if (destNode.Tag != null && destNode.Tag is SlaveGroup) {
195            sgrp = (SlaveGroup)destNode.Tag;
196          }
197          if (destNode.Parent != null && destNode.Parent.Tag is SlaveGroup) {
198            sgrp = (SlaveGroup)destNode.Parent.Tag;
199          }
200
201          if (sgrp != null) {
202            if (newNode.Tag != null && newNode.Tag is Slave) {
203              Slave slave = (Slave)newNode.Tag;
204
205              if (sgrp.Id == Guid.Empty) {
206                ServiceLocator.Instance.CallHiveService(service => sgrp.Id = service.AddSlaveGroup(sgrp));
207              }
208
209              if (slave.ParentResourceId == null || (slave.ParentResourceId != null && slave.ParentResourceId != sgrp.Id)) {
210                slave.ParentResourceId = sgrp.Id;
211                OnContentChanged();
212              }
213            }
214          }
215        }
216      }
217    }
218
219    private void treeSlaveGroup_ItemDrag(object sender, ItemDragEventArgs e) {
220      TreeNode sourceNode = (TreeNode)e.Item;
221      DoDragDrop(sourceNode, DragDropEffects.All);
222    }
223
224    private void treeSlaveGroup_DragEnter(object sender, DragEventArgs e) {
225      e.Effect = DragDropEffects.Move;
226    }
227
228    private void treeSlaveGroup_DragOver(object sender, DragEventArgs e) {
229      e.Effect = DragDropEffects.Move;
230    }
231
232    private void treeSlaveGroup_QueryContinueDrag(object sender, QueryContinueDragEventArgs e) {
233      e.Action = DragAction.Continue;
234    }
235
236    void ResetView() {
237      treeSlaveGroup.Nodes.Clear();
238      if (slaveView.Content != null && slaveView.Content is SlaveGroup) {
239        slaveView.Content.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(SlaveViewContent_PropertyChanged);
240      }
241      slaveView.Content = null;
242      scheduleView.ResourceId = Guid.Empty;
243    }
244
245    private void UpdateSlaveGroups() {
246      this.Invoke(new Action(ResetView));
247      Content.Clear();
248      IItemList<Resource> resources = new ItemList<Resource>();
249
250      ServiceLocator.Instance.CallHiveService(service => {
251        service.GetSlaveGroups().ForEach(g => resources.Add(g));
252        service.GetSlaves().ForEach(s => resources.Add(s));
253      });
254      Content = resources;
255    }
256
257    private void UpdateSchedule() {
258      Guid resourceId = scheduleView.ResourceId;
259      if (resourceId != null) {
260        ServiceLocator.Instance.CallHiveService(service => {
261          var appointments = service.GetScheduleForResource(resourceId);
262          ItemList<Appointment> ias = new ItemList<Appointment>();
263          appointments.ForEach(a => ias.Add(a));
264          scheduleView.Invoke(new Action(() => scheduleView.Content = ias));
265        });
266      }
267    }
268
269    private void tabSlaveGroup_SelectedIndexChanged(object sender, EventArgs e) {
270      if (tabSlaveGroup.SelectedIndex == 1) {
271        UpdateSchedule();
272      }
273    }
274  }
275}
Note: See TracBrowser for help on using the repository browser.