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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.ComponentModel;
|
---|
25 | using System.Drawing;
|
---|
26 | using System.Linq;
|
---|
27 | using System.Windows.Forms;
|
---|
28 | using HeuristicLab.Clients.Access;
|
---|
29 | using HeuristicLab.Clients.Hive.Views;
|
---|
30 | using HeuristicLab.Collections;
|
---|
31 | using HeuristicLab.Common.Resources;
|
---|
32 | using HeuristicLab.Core;
|
---|
33 | using HeuristicLab.Core.Views;
|
---|
34 | using HeuristicLab.MainForm;
|
---|
35 |
|
---|
36 | namespace HeuristicLab.Clients.Hive.Administrator.Views {
|
---|
37 | [View("Resources View")]
|
---|
38 | [Content(typeof(IItemList<Resource>), false)]
|
---|
39 | public partial class ResourcesView : ItemView, IDisposable {
|
---|
40 | private const int slaveImageIndex = 0;
|
---|
41 | private const int slaveGroupImageIndex = 1;
|
---|
42 | public const string ungroupedGroupName = "UNGROUPED";
|
---|
43 | public const string ungroupedGroupDescription = "Contains slaves that are not assigned to any group.";
|
---|
44 |
|
---|
45 | private readonly Color changedColor = Color.FromArgb(255, 87, 191, 193); // #57bfc1
|
---|
46 | private readonly Color selectedColor = Color.FromArgb(255, 240, 194, 59); // #f0c23b
|
---|
47 | private Resource selectedResource = null;
|
---|
48 |
|
---|
49 | private readonly object locker = new object();
|
---|
50 |
|
---|
51 | public new IItemList<Resource> Content {
|
---|
52 | get { return (IItemList<Resource>)base.Content; }
|
---|
53 | set { base.Content = value; }
|
---|
54 | }
|
---|
55 |
|
---|
56 | public ResourcesView() {
|
---|
57 | InitializeComponent();
|
---|
58 |
|
---|
59 | treeView.ImageList.Images.Add(VSImageLibrary.MonitorLarge);
|
---|
60 | treeView.ImageList.Images.Add(VSImageLibrary.NetworkCenterLarge);
|
---|
61 |
|
---|
62 | HiveAdminClient.Instance.Refreshing += HiveAdminClient_Instance_Refreshing;
|
---|
63 | HiveAdminClient.Instance.Refreshed += HiveAdminClient_Instance_Refreshed;
|
---|
64 | AccessClient.Instance.Refreshing += AccessClient_Instance_Refreshing;
|
---|
65 | AccessClient.Instance.Refreshed += AccessClient_Instance_Refreshed;
|
---|
66 | }
|
---|
67 |
|
---|
68 | #region Overrides
|
---|
69 | protected override void OnClosing(FormClosingEventArgs e) {
|
---|
70 | AccessClient.Instance.Refreshed -= AccessClient_Instance_Refreshed;
|
---|
71 | AccessClient.Instance.Refreshing -= AccessClient_Instance_Refreshing;
|
---|
72 | HiveAdminClient.Instance.Refreshed -= HiveAdminClient_Instance_Refreshed;
|
---|
73 | HiveAdminClient.Instance.Refreshing -= HiveAdminClient_Instance_Refreshing;
|
---|
74 | base.OnClosing(e);
|
---|
75 | }
|
---|
76 |
|
---|
77 | protected override void RegisterContentEvents() {
|
---|
78 | base.RegisterContentEvents();
|
---|
79 | Content.ItemsAdded += Content_ItemsAdded;
|
---|
80 | Content.ItemsRemoved += Content_ItemsRemoved;
|
---|
81 | }
|
---|
82 |
|
---|
83 | protected override void DeregisterContentEvents() {
|
---|
84 | Content.ItemsRemoved -= Content_ItemsRemoved;
|
---|
85 | Content.ItemsAdded -= Content_ItemsAdded;
|
---|
86 | base.DeregisterContentEvents();
|
---|
87 | }
|
---|
88 |
|
---|
89 | protected override void OnContentChanged() {
|
---|
90 | base.OnContentChanged();
|
---|
91 | if (Content == null) {
|
---|
92 | treeView.Nodes.Clear();
|
---|
93 | viewHost.Content = null;
|
---|
94 | scheduleView.Content = null;
|
---|
95 | } else {
|
---|
96 | var top = BuildResourceTree(Content);
|
---|
97 | SetEnabledStateOfControlsForSelectedResource();
|
---|
98 |
|
---|
99 | viewHost.Content = top;
|
---|
100 | bool locked = !IsAdmin();
|
---|
101 | viewHost.Locked = locked;
|
---|
102 | scheduleView.Locked = locked;
|
---|
103 |
|
---|
104 | if (top != null && top.Id == Guid.Empty) {
|
---|
105 | scheduleView.SetEnabledStateOfSchedule(false);
|
---|
106 | }
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | protected override void SetEnabledStateOfControls() {
|
---|
111 | base.SetEnabledStateOfControls();
|
---|
112 | bool enabled = Content != null && !Locked && IsAdmin();
|
---|
113 | btnAddGroup.Enabled = enabled;
|
---|
114 | btnRemoveGroup.Enabled = enabled;
|
---|
115 | btnSave.Enabled = enabled;
|
---|
116 | scheduleView.SetEnabledStateOfSchedule(enabled && IsAdmin());
|
---|
117 | }
|
---|
118 | #endregion
|
---|
119 |
|
---|
120 | #region Event Handlers
|
---|
121 | private void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<Resource>> e) {
|
---|
122 | if (InvokeRequired) Invoke((Action<object, CollectionItemsChangedEventArgs<IndexedItem<Resource>>>)Content_ItemsAdded, sender, e);
|
---|
123 | else {
|
---|
124 | OnContentChanged();
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | private void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<Resource>> e) {
|
---|
129 | if (InvokeRequired) Invoke((Action<object, CollectionItemsChangedEventArgs<IndexedItem<Resource>>>)Content_ItemsRemoved, sender, e);
|
---|
130 | else {
|
---|
131 | OnContentChanged();
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | private void SlaveViewContent_PropertyChanged(object sender, PropertyChangedEventArgs e) {
|
---|
136 | if (InvokeRequired) Invoke((Action<object, PropertyChangedEventArgs>)SlaveViewContent_PropertyChanged, sender, e);
|
---|
137 | else {
|
---|
138 | OnContentChanged();
|
---|
139 | if (e.PropertyName == "HbInterval") {
|
---|
140 | UpdateChildHbIntervall((Resource)viewHost.Content);
|
---|
141 | }
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | private void HiveAdminClient_Instance_Refreshing(object sender, EventArgs e) {
|
---|
146 | if (InvokeRequired) Invoke((Action<object, EventArgs>)HiveAdminClient_Instance_Refreshing, sender, e);
|
---|
147 | else {
|
---|
148 | var mainForm = MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>();
|
---|
149 | mainForm.AddOperationProgressToView(this, "Refreshing ...");
|
---|
150 | SetEnabledStateOfControls();
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | private void HiveAdminClient_Instance_Refreshed(object sender, EventArgs e) {
|
---|
155 | if (InvokeRequired) Invoke((Action<object, EventArgs>)HiveAdminClient_Instance_Refreshed, sender, e);
|
---|
156 | else {
|
---|
157 | var mainForm = MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>();
|
---|
158 | mainForm.RemoveOperationProgressFromView(this);
|
---|
159 | SetEnabledStateOfControls();
|
---|
160 | }
|
---|
161 | }
|
---|
162 |
|
---|
163 | private void AccessClient_Instance_Refreshing(object sender, EventArgs e) {
|
---|
164 | if (InvokeRequired) Invoke((Action<object, EventArgs>)AccessClient_Instance_Refreshing, sender, e);
|
---|
165 | else {
|
---|
166 | var mainForm = MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>();
|
---|
167 | mainForm.AddOperationProgressToView(this, "Refreshing ...");
|
---|
168 | SetEnabledStateOfControls();
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 | private void AccessClient_Instance_Refreshed(object sender, EventArgs e) {
|
---|
173 | if (InvokeRequired) Invoke((Action<object, EventArgs>)AccessClient_Instance_Refreshed, sender, e);
|
---|
174 | else {
|
---|
175 | var mainForm = MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>();
|
---|
176 | mainForm.RemoveOperationProgressFromView(this);
|
---|
177 | SetEnabledStateOfControls();
|
---|
178 | }
|
---|
179 | }
|
---|
180 |
|
---|
181 | private async void ResourcesView_Load(object sender, EventArgs e) {
|
---|
182 | await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
|
---|
183 | action: () => UpdateResources());
|
---|
184 | }
|
---|
185 |
|
---|
186 | private async void btnRefresh_Click(object sender, EventArgs e) {
|
---|
187 | lock (locker) {
|
---|
188 | if (!btnRefresh.Enabled) return;
|
---|
189 | btnRefresh.Enabled = false;
|
---|
190 | }
|
---|
191 |
|
---|
192 | await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
|
---|
193 | action: () => UpdateResources(),
|
---|
194 | finallyCallback: () => btnRefresh.Enabled = true);
|
---|
195 | }
|
---|
196 |
|
---|
197 | private void btnAddGroup_Click(object sender, EventArgs e) {
|
---|
198 | Guid? parentResourceId = null;
|
---|
199 | if(!IsAdmin()) {
|
---|
200 | MessageBox.Show(
|
---|
201 | "You have no permission to add a resource group.",
|
---|
202 | "HeuristicLab Hive Administrator",
|
---|
203 | MessageBoxButtons.OK,
|
---|
204 | MessageBoxIcon.Information);
|
---|
205 | return;
|
---|
206 | } else if(selectedResource != null && selectedResource.Id == Guid.Empty) {
|
---|
207 | MessageBox.Show(
|
---|
208 | "You cannot add a resource group to a not yet stored group.",
|
---|
209 | "HeuristicLab Hive Administrator",
|
---|
210 | MessageBoxButtons.OK,
|
---|
211 | MessageBoxIcon.Information);
|
---|
212 | return;
|
---|
213 | }
|
---|
214 |
|
---|
215 | if (selectedResource != null && selectedResource is SlaveGroup) parentResourceId = selectedResource.Id;
|
---|
216 | var group = new SlaveGroup {
|
---|
217 | Name = "New Group",
|
---|
218 | OwnerUserId = UserInformation.Instance.User.Id,
|
---|
219 | ParentResourceId = parentResourceId
|
---|
220 | };
|
---|
221 |
|
---|
222 | selectedResource = group;
|
---|
223 | Content.Add(group);
|
---|
224 | }
|
---|
225 |
|
---|
226 | private async void btnRemoveGroup_Click(object sender, EventArgs e) {
|
---|
227 | if (selectedResource == null) return;
|
---|
228 |
|
---|
229 | lock (locker) {
|
---|
230 | if (!btnRemoveGroup.Enabled) return;
|
---|
231 | btnRemoveGroup.Enabled = false;
|
---|
232 | }
|
---|
233 |
|
---|
234 | if (!IsAdmin()) {
|
---|
235 | MessageBox.Show(
|
---|
236 | "You have no permission to delete resources.",
|
---|
237 | "HeuristicLab Hive Administrator",
|
---|
238 | MessageBoxButtons.OK,
|
---|
239 | MessageBoxIcon.Information);
|
---|
240 | return;
|
---|
241 | }
|
---|
242 |
|
---|
243 | if (Content.Any(x => x.ParentResourceId == selectedResource.Id)) {
|
---|
244 | MessageBox.Show(
|
---|
245 | "Only empty resources can be deleted.",
|
---|
246 | "HeuristicLab Hive Administrator",
|
---|
247 | MessageBoxButtons.OK,
|
---|
248 | MessageBoxIcon.Error);
|
---|
249 | return;
|
---|
250 | }
|
---|
251 |
|
---|
252 | var result = MessageBox.Show(
|
---|
253 | "Do you really want to delete " + selectedResource.Name + "?",
|
---|
254 | "HeuristicLab Hive Administrator",
|
---|
255 | MessageBoxButtons.YesNo,
|
---|
256 | MessageBoxIcon.Question);
|
---|
257 | if (result == DialogResult.Yes) {
|
---|
258 | await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
|
---|
259 | action: () => RemoveResource(selectedResource),
|
---|
260 | finallyCallback: () => {
|
---|
261 | btnRemoveGroup.Enabled = true;
|
---|
262 | });
|
---|
263 | }
|
---|
264 | }
|
---|
265 |
|
---|
266 | private async void btnSave_Click(object sender, EventArgs e) {
|
---|
267 | lock (locker) {
|
---|
268 | if (!btnSave.Enabled) return;
|
---|
269 | btnSave.Enabled = false;
|
---|
270 | }
|
---|
271 |
|
---|
272 | await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
|
---|
273 | action: () => {
|
---|
274 | var resourcesToSave = Content.Where(x => x.Id == Guid.Empty || x.Modified);
|
---|
275 | foreach (var resource in resourcesToSave)
|
---|
276 | resource.Store();
|
---|
277 | UpdateResources();
|
---|
278 | },
|
---|
279 | finallyCallback: () => btnSave.Enabled = true);
|
---|
280 |
|
---|
281 | OnContentChanged();
|
---|
282 | }
|
---|
283 |
|
---|
284 | private void treeSlaveGroup_BeforeSelect(object sender, TreeViewCancelEventArgs e) {
|
---|
285 | if (((Resource)e.Node.Tag).Name == ungroupedGroupName) e.Cancel = true;
|
---|
286 | }
|
---|
287 |
|
---|
288 | private async void treeSlaveGroup_AfterSelect(object sender, TreeViewEventArgs e) {
|
---|
289 | ChangeSelectedResourceNode(e.Node);
|
---|
290 | SetEnabledStateOfControlsForSelectedResource();
|
---|
291 |
|
---|
292 | //if (viewHost.Content != null && viewHost.Content is SlaveGroup)
|
---|
293 | // ((SlaveGroup)viewHost.Content).PropertyChanged -= SlaveViewContent_PropertyChanged;
|
---|
294 |
|
---|
295 | viewHost.Content = selectedResource;
|
---|
296 | HiveAdminClient.Instance.DowntimeForResourceId = selectedResource != null ? selectedResource.Id : Guid.Empty;
|
---|
297 | await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
|
---|
298 | action: () => UpdateSchedule(),
|
---|
299 | finallyCallback: () => scheduleView.Content = HiveAdminClient.Instance.Downtimes);
|
---|
300 |
|
---|
301 | //if (selectedResource != null && selectedResource is SlaveGroup)
|
---|
302 | // selectedResource.PropertyChanged += SlaveViewContent_PropertyChanged;
|
---|
303 |
|
---|
304 | bool locked = !IsAdmin();
|
---|
305 | viewHost.Locked = locked;
|
---|
306 | scheduleView.Locked = locked;
|
---|
307 | }
|
---|
308 |
|
---|
309 | private void treeSlaveGroup_DragDrop(object sender, DragEventArgs e) {
|
---|
310 | if (e.Effect == DragDropEffects.None) return;
|
---|
311 |
|
---|
312 | var sourceNode = (TreeNode)e.Data.GetData(typeof(TreeNode));
|
---|
313 | if (sourceNode == null) return;
|
---|
314 | var sourceResource = ((Resource)sourceNode.Tag);
|
---|
315 | if (sourceResource == null) return;
|
---|
316 |
|
---|
317 | var treeView = (TreeView)sender;
|
---|
318 | if (sourceNode.TreeView != treeView) return;
|
---|
319 |
|
---|
320 | var targetPoint = treeView.PointToClient(new Point(e.X, e.Y));
|
---|
321 | var targetNode = treeView.GetNodeAt(targetPoint);
|
---|
322 |
|
---|
323 | if (sourceNode == targetNode) return;
|
---|
324 | var targetResource = (targetNode != null) ? (Resource)targetNode.Tag : null;
|
---|
325 |
|
---|
326 | if (!IsAdmin()) {
|
---|
327 | MessageBox.Show(
|
---|
328 | "You cannot drag resources. This is solely granted for HeuristicLab Hive Administrators.",
|
---|
329 | "HeuristicLab Hive Administrator",
|
---|
330 | MessageBoxButtons.OK,
|
---|
331 | MessageBoxIcon.Information);
|
---|
332 | return;
|
---|
333 | } else if (targetResource != null && targetResource is Slave) {
|
---|
334 | MessageBox.Show(
|
---|
335 | "You cannot drag resources on to a slave.",
|
---|
336 | "HeuristicLab Hive Administrator",
|
---|
337 | MessageBoxButtons.OK,
|
---|
338 | MessageBoxIcon.Information);
|
---|
339 | return;
|
---|
340 | } else if (targetResource != null && targetResource.Id == Guid.Empty) {
|
---|
341 | MessageBox.Show(
|
---|
342 | "You cannot drag resources to a not yet stored resource group.",
|
---|
343 | "HeuristicLab Hive Administrator",
|
---|
344 | MessageBoxButtons.OK,
|
---|
345 | MessageBoxIcon.Information);
|
---|
346 | return;
|
---|
347 | } else if(!HiveAdminClient.Instance.CheckParentChange(sourceResource, targetResource)) {
|
---|
348 | MessageBox.Show(
|
---|
349 | "You cannot drag resources to this group.",
|
---|
350 | "HeuristicLab Hive Administrator",
|
---|
351 | MessageBoxButtons.OK,
|
---|
352 | MessageBoxIcon.Information);
|
---|
353 | return;
|
---|
354 | } else if (targetNode != null && (targetNode.Text == ungroupedGroupName || targetNode.Parent != null && targetNode.Parent.Text == ungroupedGroupName)) {
|
---|
355 | MessageBox.Show(
|
---|
356 | string.Format(@"You cannot drag resources to group ""{0}"". This group only contains slaves which have not been assigned to a real group yet.", ungroupedGroupName),
|
---|
357 | "HeuristicLab Hive Administrator",
|
---|
358 | MessageBoxButtons.OK,
|
---|
359 | MessageBoxIcon.Information);
|
---|
360 | return;
|
---|
361 | }
|
---|
362 |
|
---|
363 | if (sourceNode.Parent == null)
|
---|
364 | treeView.Nodes.Remove(sourceNode);
|
---|
365 | else {
|
---|
366 | sourceNode.Parent.Nodes.Remove(sourceNode);
|
---|
367 | sourceResource.ParentResourceId = null;
|
---|
368 | }
|
---|
369 |
|
---|
370 | if (targetNode == null) {
|
---|
371 | treeView.Nodes.Add(sourceNode);
|
---|
372 | } else if(targetResource.Id != Guid.Empty) {
|
---|
373 | targetNode.Nodes.Add(sourceNode);
|
---|
374 | sourceResource.ParentResourceId = targetResource.Id;
|
---|
375 | }
|
---|
376 |
|
---|
377 | selectedResource = sourceResource;
|
---|
378 | OnContentChanged();
|
---|
379 | }
|
---|
380 |
|
---|
381 | private void treeSlaveGroup_ItemDrag(object sender, ItemDragEventArgs e) {
|
---|
382 | TreeNode sourceNode = (TreeNode)e.Item;
|
---|
383 | if (IsAdmin())
|
---|
384 | DoDragDrop(sourceNode, DragDropEffects.All);
|
---|
385 | }
|
---|
386 |
|
---|
387 | private void treeSlaveGroup_DragEnterOver(object sender, DragEventArgs e) {
|
---|
388 | e.Effect = DragDropEffects.Move;
|
---|
389 |
|
---|
390 | var sourceNode = (TreeNode)e.Data.GetData(typeof(TreeNode));
|
---|
391 | var sourceResource = ((Resource)sourceNode.Tag);
|
---|
392 |
|
---|
393 | var targetPoint = treeView.PointToClient(new Point(e.X, e.Y));
|
---|
394 | var targetNode = treeView.GetNodeAt(targetPoint);
|
---|
395 | var targetResource = (targetNode != null) ? (Resource)targetNode.Tag : null;
|
---|
396 |
|
---|
397 | if(!IsAdmin()
|
---|
398 | || sourceNode == null
|
---|
399 | || sourceResource == null
|
---|
400 | || sourceNode == targetNode
|
---|
401 | || (targetResource != null && targetResource is Slave)
|
---|
402 | || (targetResource != null && targetResource.Id == Guid.Empty)
|
---|
403 | || (targetResource != null && targetResource.Id == sourceResource.ParentResourceId)
|
---|
404 | || !HiveAdminClient.Instance.CheckParentChange(sourceResource, targetResource)
|
---|
405 | || (targetNode != null && (targetNode.Text == ungroupedGroupName || targetNode.Parent != null && targetNode.Parent.Text == ungroupedGroupName))) {
|
---|
406 | e.Effect = DragDropEffects.None;
|
---|
407 | }
|
---|
408 | }
|
---|
409 |
|
---|
410 | private void treeSlaveGroup_QueryContinueDrag(object sender, QueryContinueDragEventArgs e) {
|
---|
411 | e.Action = DragAction.Continue;
|
---|
412 | }
|
---|
413 | #endregion
|
---|
414 |
|
---|
415 | #region Helpers
|
---|
416 | private void ResetTreeNodes(TreeNodeCollection nodes, Color c1, Color c2, bool resetText) {
|
---|
417 | foreach (TreeNode n in nodes) {
|
---|
418 | var res = ((Resource)n.Tag);
|
---|
419 | if (n.BackColor.Equals(c1)) {
|
---|
420 | n.BackColor = c2;
|
---|
421 | }
|
---|
422 | if (resetText) {
|
---|
423 | n.Text = res.Name;
|
---|
424 |
|
---|
425 | if (res.Id == Guid.Empty && res.Name != ungroupedGroupName) {
|
---|
426 | n.Text += " [not stored]";
|
---|
427 | } else if (res.Modified && res.Name != ungroupedGroupName) {
|
---|
428 | n.Text += " [changes not stored]";
|
---|
429 | }
|
---|
430 | }
|
---|
431 | if (n.Nodes.Count > 0) {
|
---|
432 | ResetTreeNodes(n.Nodes, c1, c2, resetText);
|
---|
433 | }
|
---|
434 | }
|
---|
435 | }
|
---|
436 |
|
---|
437 | private Resource BuildResourceTree(IEnumerable<Resource> resources) {
|
---|
438 | treeView.Nodes.Clear();
|
---|
439 | if (!resources.Any()) return null;
|
---|
440 |
|
---|
441 | var mainResources = new HashSet<Resource>(resources.OfType<SlaveGroup>()
|
---|
442 | .Where(x => x.ParentResourceId == null));
|
---|
443 | var parentedMainResources = new HashSet<Resource>(resources.OfType<SlaveGroup>()
|
---|
444 | .Where(x => x.ParentResourceId.HasValue && !resources.Select(y => y.Id).Contains(x.ParentResourceId.Value)));
|
---|
445 | mainResources.UnionWith(parentedMainResources);
|
---|
446 | var subResources = new HashSet<Resource>(resources.Except(mainResources).OrderByDescending(x => x.Name));
|
---|
447 |
|
---|
448 | var stack = new Stack<Resource>(mainResources.OrderByDescending(x => x.Name));
|
---|
449 | if (selectedResource != null) selectedResource = resources.Where(x => x.Id == selectedResource.Id).FirstOrDefault();
|
---|
450 | bool nodeSelected = false;
|
---|
451 |
|
---|
452 | TreeNode currentNode = null;
|
---|
453 | Resource currentResource = null;
|
---|
454 |
|
---|
455 | while(stack.Any()) {
|
---|
456 | var newResource = stack.Pop();
|
---|
457 | var newNode = new TreeNode(newResource.Name) { Tag = newResource };
|
---|
458 |
|
---|
459 | if(newResource.Id == Guid.Empty) {
|
---|
460 | newNode.Text += " [not stored]";
|
---|
461 | } else if(newResource.Modified) {
|
---|
462 | newNode.Text += " [changes not stored]";
|
---|
463 | }
|
---|
464 | if (selectedResource == null) {
|
---|
465 | selectedResource = newResource;
|
---|
466 | }
|
---|
467 | if (newResource.Id == selectedResource.Id && !nodeSelected) {
|
---|
468 | newNode.BackColor = selectedColor;
|
---|
469 | newNode.Text += " [selected]";
|
---|
470 | nodeSelected = true;
|
---|
471 | }
|
---|
472 |
|
---|
473 | // search for parent node of newNode and save in currentNode
|
---|
474 | // necessary since newNodes (stack top items) might be siblings
|
---|
475 | // or grand..grandparents of previous node (currentNode)
|
---|
476 | while (currentNode != null && newResource.ParentResourceId != currentResource.Id) {
|
---|
477 | currentNode = currentNode.Parent;
|
---|
478 | currentResource = currentNode == null ? null : (Resource)currentNode.Tag;
|
---|
479 | }
|
---|
480 |
|
---|
481 | if(currentNode == null) {
|
---|
482 | treeView.Nodes.Add(newNode);
|
---|
483 | } else {
|
---|
484 | currentNode.Nodes.Add(newNode);
|
---|
485 | }
|
---|
486 |
|
---|
487 | if(newResource is Slave) {
|
---|
488 | newNode.ImageIndex = slaveImageIndex;
|
---|
489 | } else {
|
---|
490 | newNode.ImageIndex = slaveGroupImageIndex;
|
---|
491 |
|
---|
492 | var childResources = subResources.Where(x => x.ParentResourceId == newResource.Id);
|
---|
493 | if(childResources.Any()) {
|
---|
494 | foreach(var resource in childResources.OrderByDescending(x => x.Name)) {
|
---|
495 | subResources.Remove(resource);
|
---|
496 | stack.Push(resource);
|
---|
497 | }
|
---|
498 | currentNode = newNode;
|
---|
499 | currentResource = newResource;
|
---|
500 | }
|
---|
501 | }
|
---|
502 | newNode.SelectedImageIndex = newNode.ImageIndex;
|
---|
503 | //if (newResource.OwnerUserId == UserInformation.Instance.User.Id)
|
---|
504 | // newNode.BackColor = ownedResourceColor;
|
---|
505 | }
|
---|
506 |
|
---|
507 | var ungroupedNode = new TreeNode(ungroupedGroupName) {
|
---|
508 | ForeColor = SystemColors.GrayText,
|
---|
509 | Tag = new SlaveGroup() {
|
---|
510 | Name = ungroupedGroupName,
|
---|
511 | Description = ungroupedGroupDescription
|
---|
512 | }
|
---|
513 | };
|
---|
514 |
|
---|
515 | foreach (var slave in subResources.OfType<Slave>().OrderBy(x => x.Name)) {
|
---|
516 | var slaveNode = new TreeNode(slave.Name) { Tag = slave };
|
---|
517 | ungroupedNode.Nodes.Add(slaveNode);
|
---|
518 | if(selectedResource == null) {
|
---|
519 | selectedResource = slave;
|
---|
520 | }
|
---|
521 | if (slave.Id == Guid.Empty) {
|
---|
522 | slaveNode.Text += " [not stored]";
|
---|
523 | } else if (slave.Modified) {
|
---|
524 | slaveNode.Text += " [changes not stored]";
|
---|
525 | }
|
---|
526 | if (slave.Id == selectedResource.Id && !nodeSelected) {
|
---|
527 | slaveNode.BackColor = selectedColor;
|
---|
528 | slaveNode.Text += " [selected]";
|
---|
529 | nodeSelected = true;
|
---|
530 | }
|
---|
531 | }
|
---|
532 |
|
---|
533 | treeView.Nodes.Add(ungroupedNode);
|
---|
534 | treeView.ExpandAll();
|
---|
535 |
|
---|
536 | return selectedResource;
|
---|
537 | }
|
---|
538 |
|
---|
539 | private void UpdateChildHbIntervall(Resource resource) {
|
---|
540 | foreach (Resource r in Content.Where(x => x.ParentResourceId == resource.Id)) {
|
---|
541 | r.HbInterval = resource.HbInterval;
|
---|
542 | if (r is SlaveGroup) {
|
---|
543 | UpdateChildHbIntervall(r);
|
---|
544 | }
|
---|
545 | }
|
---|
546 | }
|
---|
547 |
|
---|
548 | private bool CheckParentsEqualsMovedNode(TreeNode dest, TreeNode movedNode) {
|
---|
549 | TreeNode tmp = dest;
|
---|
550 |
|
---|
551 | while (tmp != null) {
|
---|
552 | if (tmp == movedNode) {
|
---|
553 | return true;
|
---|
554 | }
|
---|
555 | tmp = tmp.Parent;
|
---|
556 | }
|
---|
557 | return false;
|
---|
558 | }
|
---|
559 |
|
---|
560 | private void ResetView() {
|
---|
561 | if (InvokeRequired) Invoke((Action)ResetView);
|
---|
562 | else {
|
---|
563 | treeView.Nodes.Clear();
|
---|
564 |
|
---|
565 | if (viewHost.Content != null && viewHost.Content is SlaveGroup) {
|
---|
566 | ((SlaveGroup)viewHost.Content).PropertyChanged -= SlaveViewContent_PropertyChanged;
|
---|
567 | }
|
---|
568 |
|
---|
569 | viewHost.Content = null;
|
---|
570 | if (scheduleView.Content != null) {
|
---|
571 | scheduleView.Content.Clear();
|
---|
572 | }
|
---|
573 |
|
---|
574 | HiveAdminClient.Instance.ResetDowntime();
|
---|
575 | }
|
---|
576 | }
|
---|
577 |
|
---|
578 | private void UpdateResources() {
|
---|
579 | try {
|
---|
580 | HiveAdminClient.Instance.Refresh();
|
---|
581 | Content = HiveAdminClient.Instance.Resources;
|
---|
582 | } catch(AnonymousUserException) {
|
---|
583 | ShowHiveInformationDialog();
|
---|
584 | }
|
---|
585 | }
|
---|
586 |
|
---|
587 | private void RemoveResource(Resource resource) {
|
---|
588 | if (resource == null || resource.Id == Guid.Empty) return;
|
---|
589 |
|
---|
590 | try {
|
---|
591 | HiveAdminClient.Delete(resource);
|
---|
592 | Content.Remove(selectedResource);
|
---|
593 | } catch(AnonymousUserException) {
|
---|
594 | ShowHiveInformationDialog();
|
---|
595 | }
|
---|
596 | }
|
---|
597 |
|
---|
598 | private void UpdateSchedule() {
|
---|
599 | try {
|
---|
600 | HiveAdminClient.Instance.RefreshCalendar();
|
---|
601 | } catch (AnonymousUserException) {
|
---|
602 | ShowHiveInformationDialog();
|
---|
603 | }
|
---|
604 | }
|
---|
605 |
|
---|
606 | private bool IsAuthorized(Resource resource) {
|
---|
607 | return resource != null
|
---|
608 | && resource.Name != ungroupedGroupName
|
---|
609 | //&& resource.Id != Guid.Empty
|
---|
610 | && UserInformation.Instance.UserExists
|
---|
611 | && (HiveRoles.CheckAdminUserPermissions()
|
---|
612 | || HiveAdminClient.Instance.CheckOwnershipOfResource(resource, UserInformation.Instance.User.Id));
|
---|
613 | }
|
---|
614 |
|
---|
615 | private bool IsAdmin() {
|
---|
616 | return HiveRoles.CheckAdminUserPermissions();
|
---|
617 | }
|
---|
618 |
|
---|
619 | private void ChangeSelectedResourceNode(TreeNode resourceNode) {
|
---|
620 | selectedResource = (Resource)resourceNode.Tag;
|
---|
621 | ResetTreeNodes(treeView.Nodes, selectedColor, Color.Transparent, true);
|
---|
622 | resourceNode.BackColor = selectedColor;
|
---|
623 | resourceNode.Text += " [selected]";
|
---|
624 | }
|
---|
625 |
|
---|
626 | private void SetEnabledStateOfControlsForSelectedResource() {
|
---|
627 | bool enabled = (IsAdmin()) ? true : false;
|
---|
628 | btnAddGroup.Enabled = enabled;
|
---|
629 | btnRemoveGroup.Enabled = enabled;
|
---|
630 | btnSave.Enabled = enabled;
|
---|
631 | }
|
---|
632 |
|
---|
633 | private void ShowHiveInformationDialog() {
|
---|
634 | if (InvokeRequired) Invoke((Action)ShowHiveInformationDialog);
|
---|
635 | else {
|
---|
636 | using (HiveInformationDialog dialog = new HiveInformationDialog()) {
|
---|
637 | dialog.ShowDialog(this);
|
---|
638 | }
|
---|
639 | }
|
---|
640 | }
|
---|
641 | #endregion
|
---|
642 | }
|
---|
643 | }
|
---|