Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveProjectManagement/HeuristicLab.Clients.Hive.JobManager/3.3/Views/HiveResourceSelector.cs @ 15401

Last change on this file since 15401 was 15401, checked in by jkarder, 7 years ago

#2839:

  • worked on hive administrator view
  • updated service clients
File size: 11.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2017 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.Collections.Generic;
24using System.Drawing;
25using System.Linq;
26using System.Windows.Forms;
27using HeuristicLab.Core;
28using HeuristicLab.Core.Views;
29using HeuristicLab.MainForm;
30using HeuristicLab.MainForm.WindowsForms;
31
32namespace HeuristicLab.Clients.Hive.JobManager.Views {
33  [View("Hive Project Selector View")]
34  [Content(typeof(IItemList<Project>), true)]
35  public partial class HiveProjectSelector : ItemView, IDisposable {
36    private const int greenFlagImageIndex = 0;
37    private const int redFlagImageIndex = 1;
38    private string currentSearchString;
39    private ISet<TreeNode> mainTreeNodes;
40    private ISet<TreeNode> filteredTreeNodes;
41    private ISet<TreeNode> nodeStore;
42
43    private ISet<Project> selectedResources;
44    public ISet<Project> SelectedResources {
45      get { return selectedResources; }
46      set { selectedResources = value; }
47    }
48
49    public Project SelectedProject {
50      get { return (Project)resourcesTreeView.SelectedNode.Tag; }
51    }
52
53    public new IItemList<Project> Content {
54      get { return (IItemList<Project>)base.Content; }
55      set { base.Content = value; }
56    }
57
58    public HiveProjectSelector() {
59      InitializeComponent();
60      mainTreeNodes = new HashSet<TreeNode>();
61      filteredTreeNodes = new HashSet<TreeNode>();
62      nodeStore = new HashSet<TreeNode>();
63      selectedResources = new HashSet<Project>();
64      imageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.FlagGreen);
65      imageList.Images.Add(HeuristicLab.Common.Resources.VSImageLibrary.FlagRed);
66    }
67
68    public void StartProgressView() {
69      if (InvokeRequired) {
70        Invoke(new Action(StartProgressView));
71      } else {
72        var message = "Downloading projects. Please be patient.";
73        MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().AddOperationProgressToView(this, message);
74      }
75    }
76
77    public void FinishProgressView() {
78      if (InvokeRequired) {
79        Invoke(new Action(FinishProgressView));
80      } else {
81        MainFormManager.GetMainForm<HeuristicLab.MainForm.WindowsForms.MainForm>().RemoveOperationProgressFromView(this);
82      }
83    }
84
85    protected override void OnContentChanged() {
86      base.OnContentChanged();
87
88      if (Content != null) {
89        selectedResources = new HashSet<Project>(Content.Where(x => selectedResources.Any(y => x.Id == y.Id)));
90        UpdateMainTree();
91        ExtractStatistics();
92      } else {
93        mainTreeNodes.Clear();
94        UpdateFilteredTree();
95      }
96    }
97
98    #region MainTree Methods
99    private void UpdateMainTree() {
100      mainTreeNodes.Clear();
101
102      foreach (Project g in Content.OrderBy(x => x.Name)) {
103        //root node
104        if (g.ParentProjectId == null) {
105          TreeNode tn = new TreeNode();
106          tn.ImageIndex = greenFlagImageIndex;
107          tn.SelectedImageIndex = tn.ImageIndex;
108
109          tn.Tag = g;
110          tn.Text = g.Name;
111          tn.Checked = selectedResources.Any(x => x.Id == g.Id);
112
113          BuildMainTree(tn);
114          mainTreeNodes.Add(tn);
115        }
116      }
117      UpdateFilteredTree();
118    }
119
120    private void BuildMainTree(TreeNode tn) {
121      foreach (Project r in Content.Where(s => s.ParentProjectId != null && s.ParentProjectId == ((Project)tn.Tag).Id).OrderBy(x => x.Name)) {
122        TreeNode stn = new TreeNode(r.Name);
123        stn.ImageIndex = redFlagImageIndex;
124        stn.SelectedImageIndex = stn.ImageIndex;
125        stn.Tag = r;
126        stn.Checked = selectedResources.Any(x => x.Id == r.Id);
127        tn.Nodes.Add(stn);
128        mainTreeNodes.Add(stn);
129
130        BuildMainTree(stn);
131      }
132    }
133    #endregion
134
135    #region FilteredTree Methods
136    private void UpdateFilteredTree() {
137      filteredTreeNodes.Clear();
138      foreach (TreeNode n in mainTreeNodes) {
139        n.BackColor = SystemColors.Window;
140        if (currentSearchString == null || ((Project)n.Tag).Name.ToLower().Contains(currentSearchString)) {
141          n.BackColor = string.IsNullOrEmpty(currentSearchString) ? SystemColors.Window : Color.LightBlue;
142          filteredTreeNodes.Add(n);
143          TraverseParentNodes(n);
144        }
145      }
146      UpdateResourceTree();
147    }
148
149    private void TraverseParentNodes(TreeNode node) {
150      if (node != null) {
151        for (TreeNode parent = node.Parent; parent != null; parent = parent.Parent)
152          filteredTreeNodes.Add(parent);
153      }
154    }
155    #endregion
156
157    #region ResourceTree Methods
158    private void UpdateResourceTree() {
159      resourcesTreeView.Nodes.Clear();
160      nodeStore.Clear();
161
162      foreach (TreeNode node in filteredTreeNodes) {
163        var clone = nodeStore.SingleOrDefault(x => ((Project)x.Tag).Id == ((Project)node.Tag).Id);
164        if (clone == null) {
165          clone = (TreeNode)node.Clone();
166          nodeStore.Add(clone);
167          clone.Nodes.Clear();
168        }
169        foreach (TreeNode child in node.Nodes)
170          if (filteredTreeNodes.Any(x => ((Project)x.Tag).Id == ((Project)child.Tag).Id)) {
171            var childClone = nodeStore.SingleOrDefault(x => ((Project)x.Tag).Id == ((Project)child.Tag).Id);
172            if (childClone == null) {
173              childClone = (TreeNode)child.Clone();
174              nodeStore.Add(childClone);
175              childClone.Nodes.Clear();
176            }
177            clone.Nodes.Add(childClone);
178          }
179      }
180      resourcesTreeView.Nodes.AddRange(nodeStore.Where(x => ((Project)x.Tag).ParentProjectId == null).ToArray());
181      if (string.IsNullOrEmpty(currentSearchString)) ExpandSlaveGroupNodes();
182      else resourcesTreeView.ExpandAll();
183    }
184    #endregion
185
186    #region Events
187    private void resourcesTreeView_AfterCheck(object sender, TreeViewEventArgs e) {
188      //if (e.Action != TreeViewAction.Unknown) {
189      //  if (e.Node.Checked) {
190      //    IncludeChildNodes(mainTreeNodes.SingleOrDefault(x => ((Project)x.Tag).Id == ((Project)e.Node.Tag).Id));
191      //    IncludeParentNodes(mainTreeNodes.SingleOrDefault(x => ((Project)x.Tag).Id == ((Project)e.Node.Tag).Id));
192      //  } else {
193      //    ExcludeChildNodes(mainTreeNodes.SingleOrDefault(x => ((Project)x.Tag).Id == ((Project)e.Node.Tag).Id));
194      //    ExcludeParentNodes(mainTreeNodes.SingleOrDefault(x => ((Project)x.Tag).Id == ((Project)e.Node.Tag).Id));
195      //  }
196      //  ExtractStatistics();
197      //}
198    }
199
200    private void resourcesTreeView_AfterSelect(object sender, TreeViewEventArgs e) {
201      if (e.Action != TreeViewAction.Unknown) {
202        ExtractStatistics(e.Node);
203      }
204    }
205
206    private void searchTextBox_TextChanged(object sender, System.EventArgs e) {
207      currentSearchString = searchTextBox.Text.ToLower();
208      UpdateFilteredTree();
209    }
210    #endregion
211
212    #region Helpers
213    private void ExpandSlaveGroupNodes() {
214      foreach (TreeNode n in nodeStore.Where(x => x.Tag is SlaveGroup)) {
215        TreeNode[] children = new TreeNode[n.Nodes.Count];
216        n.Nodes.CopyTo(children, 0);
217        if (children.Any(x => x.Tag is SlaveGroup)) n.Expand();
218      }
219    }
220
221    private void ExtractStatistics(TreeNode treeNode = null) {
222      //StringBuilder sb = new StringBuilder();
223      //Resource resource = treeNode == null ? null : treeNode.Tag as Resource;
224      //ISet<Resource> resources = treeNode == null ? selectedResources : new HashSet<Resource>(treeNode.DescendantNodes().Select(x => x.Tag as Resource)); ;
225      //IEnumerable<SlaveGroup> slaveGroups = resources.OfType<SlaveGroup>();
226      //IEnumerable<Slave> slaves = resources.OfType<Slave>();
227      //int cpuSpeed = 0, cores = 0, freeCores = 0, memory = 0, freeMemory = 0;
228      //string contextString = treeNode == null ? "Selected" : "Included";
229
230      //if (resources.Any() || resource != null) {
231      //  foreach (Slave s in slaves) {
232      //    cpuSpeed += s.CpuSpeed.GetValueOrDefault();
233      //    cores += s.Cores.GetValueOrDefault();
234      //    freeCores += s.FreeCores.GetValueOrDefault();
235      //    memory += s.Memory.GetValueOrDefault();
236      //    freeMemory += s.FreeMemory.GetValueOrDefault();
237      //  }
238      //  if (resource != null) {
239      //    if (resource is SlaveGroup) sb.Append("Slave group: ");
240      //    else if (resource is Slave) {
241      //      sb.Append("Slave: ");
242      //      if (!resources.Any()) {
243      //        Slave s = resource as Slave;
244      //        cpuSpeed = s.CpuSpeed.GetValueOrDefault();
245      //        cores = s.Cores.GetValueOrDefault();
246      //        freeCores = s.FreeCores.GetValueOrDefault();
247      //        memory = s.Memory.GetValueOrDefault();
248      //        freeMemory = s.FreeMemory.GetValueOrDefault();
249      //      }
250      //    }
251      //    sb.AppendLine(string.Format("{0}", resource.Name));
252      //  }
253      //  if (resource == null || resource is SlaveGroup) {
254      //    if (resources.Any()) {
255      //      sb.AppendFormat("{0} slave groups ({1}): ", contextString, slaveGroups.Count());
256      //      foreach (SlaveGroup sg in slaveGroups) sb.AppendFormat("{0}; ", sg.Name);
257      //      sb.AppendLine();
258      //      sb.AppendFormat("{0} slaves ({1}): ", contextString, slaves.Count());
259      //      foreach (Slave s in slaves) sb.AppendFormat("{0}; ", s.Name);
260      //      sb.AppendLine();
261      //    } else {
262      //      sb.Append("The selection does not inlcude any further resources.");
263      //    }
264      //  }
265      //  sb.AppendLine();
266      //  sb.AppendLine(string.Format("CPU speed: {0} MHz", cpuSpeed));
267      //  if (resources.Any()) sb.AppendLine(string.Format("Avg. CPU speed: {0:0.00} MHz", (double)cpuSpeed / resources.Count()));
268      //  sb.AppendLine(string.Format("Cores: {0}", cores));
269      //  sb.AppendLine(string.Format("Free cores: {0}", freeCores));
270      //  if (resources.Any()) sb.AppendLine(string.Format("Avg. free cores: {0:0.00}", (double)freeCores / resources.Count()));
271      //  sb.AppendLine(string.Format("Memory: {0} MB", memory));
272      //  sb.AppendFormat("Free memory: {0} MB", freeMemory);
273      //  if (resources.Any()) sb.Append(string.Format("{0}Avg. free memory: {1:0.00} MB", Environment.NewLine, (double)freeMemory / resources.Count()));
274      //} else {
275      //  sb.Append("No resources selected.");
276      //}
277
278      //descriptionTextBox.Text = sb.ToString();
279    }
280    #endregion
281  }
282}
Note: See TracBrowser for help on using the repository browser.