Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1233

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