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

Last change on this file since 6373 was 6373, checked in by cneumuel, 13 years ago

#1233

  • moved ExperimentManager into separate plugin
  • moved Administration into separate plugin
File size: 10.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.Core;
27using HeuristicLab.Core.Views;
28using HeuristicLab.MainForm;
29using HeuristicLab.Clients.Hive.Views;
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      } else if (e.Node.Tag is SlaveGroup) {
125        slaveView.Content.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(SlaveViewContent_PropertyChanged);
126      }
127    }
128
129    void SlaveViewContent_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) {
130      OnContentChanged();
131    }
132
133    private void btnAddGroup_Click(object sender, EventArgs e) {
134      SlaveGroup newGroup = new SlaveGroup();
135      newGroup.Name = "New Group";
136      Content.Add(newGroup);
137    }
138
139    void Content_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<Collections.IndexedItem<Resource>> e) {
140      OnContentChanged();
141    }
142
143    void Content_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<Collections.IndexedItem<Resource>> e) {
144      OnContentChanged();
145    }
146
147    private void btnRemoveGroup_Click(object sender, EventArgs e) {
148      //TODO: display some warning?
149      if (treeSlaveGroup.SelectedNode != null && treeSlaveGroup.SelectedNode.Tag != null) {
150        Resource res = (Resource)treeSlaveGroup.SelectedNode.Tag;
151        Content.Remove(res);
152
153        if (res is Slave) {
154          ServiceLocator.Instance.CallHiveService(service => service.DeleteSlave(res.Id));
155        } else if (res is SlaveGroup) {
156          //only delete empty groups
157          if (Content.Where(s => s.ParentResourceId == res.Id).Count() < 0) {
158            ServiceLocator.Instance.CallHiveService(service => service.DeleteSlaveGroup(res.Id));
159          } else {
160            MessageBox.Show("Only empty groups can be deleted.");
161          }
162        }
163      }
164    }
165
166    private void btnSave_Click(object sender, EventArgs e) {
167      foreach (Resource res in Content) {
168        if (res is SlaveGroup && res.Id == Guid.Empty) {
169          SlaveGroup slaveGroup = (SlaveGroup)res;
170          ServiceLocator.Instance.CallHiveService(service => service.AddSlaveGroup(slaveGroup));
171        }
172        if (res.Id != Guid.Empty && res.Modified) {
173          if (res is SlaveGroup) {
174            ServiceLocator.Instance.CallHiveService(service => service.UpdateSlaveGroup((SlaveGroup)res));
175          } else if (res is Slave) {
176            ServiceLocator.Instance.CallHiveService(service => service.UpdateSlave((Slave)res));
177          }
178        }
179      }
180    }
181
182    private void treeSlaveGroup_DragDrop(object sender, DragEventArgs e) {
183      if (e.Data.GetDataPresent("System.Windows.Forms.TreeNode", false)) {
184        Point pt = ((TreeView)sender).PointToClient(new Point(e.X, e.Y));
185        TreeNode destNode = ((TreeView)sender).GetNodeAt(pt);
186        TreeNode newNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode");
187
188        if (destNode.TreeView == newNode.TreeView) {
189          SlaveGroup sgrp = null;
190          if (destNode.Tag != null && destNode.Tag is SlaveGroup) {
191            sgrp = (SlaveGroup)destNode.Tag;
192          }
193          if (destNode.Parent != null && destNode.Parent.Tag is SlaveGroup) {
194            sgrp = (SlaveGroup)destNode.Parent.Tag;
195          }
196
197          if (sgrp != null) {
198            if (newNode.Tag != null && newNode.Tag is Slave) {
199              Slave slave = (Slave)newNode.Tag;
200              if (slave.ParentResourceId != sgrp.Id) {
201                slave.ParentResourceId = sgrp.Id;
202                OnContentChanged();
203              }
204            }
205          }
206        }
207      }
208    }
209
210    private void treeSlaveGroup_ItemDrag(object sender, ItemDragEventArgs e) {
211      TreeNode sourceNode = (TreeNode)e.Item;
212      DoDragDrop(sourceNode, DragDropEffects.All);
213    }
214
215    private void treeSlaveGroup_DragEnter(object sender, DragEventArgs e) {
216      e.Effect = DragDropEffects.Move;
217    }
218
219    private void treeSlaveGroup_DragOver(object sender, DragEventArgs e) {
220      e.Effect = DragDropEffects.Move;
221    }
222
223    private void treeSlaveGroup_QueryContinueDrag(object sender, QueryContinueDragEventArgs e) {
224      e.Action = DragAction.Continue;
225    }
226
227    void ResetView() {
228      treeSlaveGroup.Nodes.Clear();
229      if (slaveView.Content != null && slaveView.Content is SlaveGroup) {
230        slaveView.Content.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(SlaveViewContent_PropertyChanged);
231      }
232      slaveView.Content = null;
233      scheduleView.ResourceId = Guid.Empty;
234    }
235
236    private void UpdateSlaveGroups() {
237      this.Invoke(new Action(ResetView));
238      Content.Clear();
239      IItemList<Resource> resources = new ItemList<Resource>();
240
241      ServiceLocator.Instance.CallHiveService(service => {
242        service.GetSlaveGroups().ForEach(g => resources.Add(g));
243        service.GetSlaves().ForEach(s => resources.Add(s));
244      });
245      Content = resources;
246    }
247
248    private void UpdateSchedule() {
249      Guid resourceId = scheduleView.ResourceId;
250      if (resourceId != null) {
251        ServiceLocator.Instance.CallHiveService(service => {
252          var appointments = service.GetScheduleForResource(resourceId);
253          ItemList<Appointment> ias = new ItemList<Appointment>();
254          appointments.ForEach(a => ias.Add(a));
255          scheduleView.Invoke(new Action(() => scheduleView.Content = ias));
256        });
257      }
258    }
259  }
260}
Note: See TracBrowser for help on using the repository browser.