[6976] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[15401] | 3 | * Copyright (C) 2002-2017 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[6976] | 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;
|
---|
[15422] | 23 | using System.Collections.Generic;
|
---|
[15401] | 24 | using System.ComponentModel;
|
---|
[6976] | 25 | using System.Drawing;
|
---|
| 26 | using System.Linq;
|
---|
| 27 | using System.Windows.Forms;
|
---|
[8051] | 28 | using HeuristicLab.Clients.Access;
|
---|
[6976] | 29 | using HeuristicLab.Clients.Hive.Views;
|
---|
[15412] | 30 | using HeuristicLab.Collections;
|
---|
[15401] | 31 | using HeuristicLab.Common.Resources;
|
---|
[6976] | 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")]
|
---|
[7928] | 38 | [Content(typeof(IItemList<Resource>), false)]
|
---|
[6976] | 39 | public partial class ResourcesView : ItemView, IDisposable {
|
---|
[15401] | 40 | private const int slaveImageIndex = 0;
|
---|
| 41 | private const int slaveGroupImageIndex = 1;
|
---|
[15422] | 42 | public const string ungroupedGroupName = "UNGROUPED";
|
---|
| 43 | public const string ungroupedGroupDescription = "Contains slaves that are not assigned to any group.";
|
---|
[15401] | 44 |
|
---|
[15742] | 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 |
|
---|
[15401] | 49 | private readonly object locker = new object();
|
---|
| 50 |
|
---|
[6976] | 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 |
|
---|
[15422] | 59 | treeView.ImageList.Images.Add(VSImageLibrary.MonitorLarge);
|
---|
| 60 | treeView.ImageList.Images.Add(VSImageLibrary.NetworkCenterLarge);
|
---|
[8051] | 61 |
|
---|
[15401] | 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;
|
---|
[6976] | 66 | }
|
---|
| 67 |
|
---|
[15401] | 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);
|
---|
[6976] | 75 | }
|
---|
| 76 |
|
---|
[15401] | 77 | protected override void RegisterContentEvents() {
|
---|
| 78 | base.RegisterContentEvents();
|
---|
| 79 | Content.ItemsAdded += Content_ItemsAdded;
|
---|
| 80 | Content.ItemsRemoved += Content_ItemsRemoved;
|
---|
[6976] | 81 | }
|
---|
| 82 |
|
---|
| 83 | protected override void DeregisterContentEvents() {
|
---|
[15401] | 84 | Content.ItemsRemoved -= Content_ItemsRemoved;
|
---|
| 85 | Content.ItemsAdded -= Content_ItemsAdded;
|
---|
[6976] | 86 | base.DeregisterContentEvents();
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | protected override void OnContentChanged() {
|
---|
| 90 | base.OnContentChanged();
|
---|
| 91 | if (Content == null) {
|
---|
[15422] | 92 | treeView.Nodes.Clear();
|
---|
| 93 | viewHost.Content = null;
|
---|
[8051] | 94 | scheduleView.Content = null;
|
---|
[6976] | 95 | } else {
|
---|
[15422] | 96 | var top = BuildResourceTree(Content);
|
---|
[15760] | 97 | SetEnabledStateOfControlsForSelectedResource();
|
---|
| 98 |
|
---|
[15422] | 99 | viewHost.Content = top;
|
---|
[15760] | 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 | }
|
---|
[6976] | 107 | }
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | protected override void SetEnabledStateOfControls() {
|
---|
| 111 | base.SetEnabledStateOfControls();
|
---|
[15742] | 112 | bool enabled = Content != null && !Locked && IsAdmin();
|
---|
[15401] | 113 | btnAddGroup.Enabled = enabled;
|
---|
| 114 | btnRemoveGroup.Enabled = enabled;
|
---|
| 115 | btnSave.Enabled = enabled;
|
---|
[15760] | 116 | scheduleView.SetEnabledStateOfSchedule(enabled && IsAdmin());
|
---|
[6976] | 117 | }
|
---|
[15401] | 118 | #endregion
|
---|
[6976] | 119 |
|
---|
[15401] | 120 | #region Event Handlers
|
---|
[15412] | 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 | }
|
---|
[8051] | 126 | }
|
---|
[6976] | 127 |
|
---|
[15412] | 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 | }
|
---|
[6976] | 133 | }
|
---|
| 134 |
|
---|
[15401] | 135 | private void SlaveViewContent_PropertyChanged(object sender, PropertyChangedEventArgs e) {
|
---|
[15412] | 136 | if (InvokeRequired) Invoke((Action<object, PropertyChangedEventArgs>)SlaveViewContent_PropertyChanged, sender, e);
|
---|
| 137 | else {
|
---|
| 138 | OnContentChanged();
|
---|
| 139 | if (e.PropertyName == "HbInterval") {
|
---|
[15422] | 140 | UpdateChildHbIntervall((Resource)viewHost.Content);
|
---|
[15412] | 141 | }
|
---|
[6976] | 142 | }
|
---|
| 143 | }
|
---|
| 144 |
|
---|
[15401] | 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();
|
---|
[6976] | 151 | }
|
---|
| 152 | }
|
---|
| 153 |
|
---|
[15401] | 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 | }
|
---|
[6976] | 161 | }
|
---|
| 162 |
|
---|
[15401] | 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 | }
|
---|
[6976] | 170 | }
|
---|
| 171 |
|
---|
[15401] | 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 | }
|
---|
[6976] | 179 | }
|
---|
| 180 |
|
---|
[15401] | 181 | private async void ResourcesView_Load(object sender, EventArgs e) {
|
---|
| 182 | await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
|
---|
[15412] | 183 | action: () => UpdateResources());
|
---|
[15401] | 184 | }
|
---|
| 185 |
|
---|
| 186 | private async void btnRefresh_Click(object sender, EventArgs e) {
|
---|
| 187 | lock (locker) {
|
---|
[15422] | 188 | if (!btnRefresh.Enabled) return;
|
---|
| 189 | btnRefresh.Enabled = false;
|
---|
[6976] | 190 | }
|
---|
[15401] | 191 |
|
---|
| 192 | await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
|
---|
| 193 | action: () => UpdateResources(),
|
---|
[15422] | 194 | finallyCallback: () => btnRefresh.Enabled = true);
|
---|
[6976] | 195 | }
|
---|
| 196 |
|
---|
[15401] | 197 | private void btnAddGroup_Click(object sender, EventArgs e) {
|
---|
[15742] | 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;
|
---|
[15401] | 216 | var group = new SlaveGroup {
|
---|
| 217 | Name = "New Group",
|
---|
[15742] | 218 | OwnerUserId = UserInformation.Instance.User.Id,
|
---|
| 219 | ParentResourceId = parentResourceId
|
---|
[15401] | 220 | };
|
---|
[15742] | 221 |
|
---|
| 222 | selectedResource = group;
|
---|
[15401] | 223 | Content.Add(group);
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | private async void btnRemoveGroup_Click(object sender, EventArgs e) {
|
---|
[15742] | 227 | if (selectedResource == null) return;
|
---|
| 228 |
|
---|
[15401] | 229 | lock (locker) {
|
---|
[15422] | 230 | if (!btnRemoveGroup.Enabled) return;
|
---|
| 231 | btnRemoveGroup.Enabled = false;
|
---|
[6976] | 232 | }
|
---|
[15401] | 233 |
|
---|
[15742] | 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 | }
|
---|
[6976] | 264 | }
|
---|
| 265 |
|
---|
[15401] | 266 | private async void btnSave_Click(object sender, EventArgs e) {
|
---|
| 267 | lock (locker) {
|
---|
[15422] | 268 | if (!btnSave.Enabled) return;
|
---|
| 269 | btnSave.Enabled = false;
|
---|
[15401] | 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();
|
---|
[15742] | 277 | UpdateResources();
|
---|
[15401] | 278 | },
|
---|
[15422] | 279 | finallyCallback: () => btnSave.Enabled = true);
|
---|
[15742] | 280 |
|
---|
| 281 | OnContentChanged();
|
---|
[15401] | 282 | }
|
---|
| 283 |
|
---|
[15742] | 284 | private void treeSlaveGroup_BeforeSelect(object sender, TreeViewCancelEventArgs e) {
|
---|
| 285 | if (((Resource)e.Node.Tag).Name == ungroupedGroupName) e.Cancel = true;
|
---|
| 286 | }
|
---|
| 287 |
|
---|
[15401] | 288 | private async void treeSlaveGroup_AfterSelect(object sender, TreeViewEventArgs e) {
|
---|
[15742] | 289 | ChangeSelectedResourceNode(e.Node);
|
---|
| 290 | SetEnabledStateOfControlsForSelectedResource();
|
---|
[15401] | 291 |
|
---|
[15742] | 292 | //if (viewHost.Content != null && viewHost.Content is SlaveGroup)
|
---|
| 293 | // ((SlaveGroup)viewHost.Content).PropertyChanged -= SlaveViewContent_PropertyChanged;
|
---|
[15401] | 294 |
|
---|
[15422] | 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);
|
---|
[15401] | 300 |
|
---|
[15742] | 301 | //if (selectedResource != null && selectedResource is SlaveGroup)
|
---|
| 302 | // selectedResource.PropertyChanged += SlaveViewContent_PropertyChanged;
|
---|
[15401] | 303 |
|
---|
[15742] | 304 | bool locked = !IsAdmin();
|
---|
| 305 | viewHost.Locked = locked;
|
---|
| 306 | scheduleView.Locked = locked;
|
---|
[15401] | 307 | }
|
---|
| 308 |
|
---|
[6976] | 309 | private void treeSlaveGroup_DragDrop(object sender, DragEventArgs e) {
|
---|
[15422] | 310 | var sourceNode = (TreeNode)e.Data.GetData(typeof(TreeNode));
|
---|
| 311 | if (sourceNode == null) return;
|
---|
[15742] | 312 | var sourceResource = ((Resource)sourceNode.Tag);
|
---|
| 313 | if (sourceResource == null) return;
|
---|
[6976] | 314 |
|
---|
[15422] | 315 | var treeView = (TreeView)sender;
|
---|
| 316 | if (sourceNode.TreeView != treeView) return;
|
---|
[6976] | 317 |
|
---|
[15422] | 318 | var targetPoint = treeView.PointToClient(new Point(e.X, e.Y));
|
---|
| 319 | var targetNode = treeView.GetNodeAt(targetPoint);
|
---|
[6976] | 320 |
|
---|
[15422] | 321 | if (sourceNode == targetNode) return;
|
---|
[15742] | 322 | var targetResource = (targetNode != null) ? (Resource)targetNode.Tag : null;
|
---|
[6976] | 323 |
|
---|
[15742] | 324 | if (!IsAdmin()) {
|
---|
[15422] | 325 | MessageBox.Show(
|
---|
[15742] | 326 | "You cannot drag resources. This is solely granted for HeuristicLab Hive Administrators.",
|
---|
| 327 | "HeuristicLab Hive Administrator",
|
---|
| 328 | MessageBoxButtons.OK,
|
---|
| 329 | MessageBoxIcon.Information);
|
---|
| 330 | return;
|
---|
| 331 | } else if (targetResource != null && targetResource is Slave) {
|
---|
| 332 | MessageBox.Show(
|
---|
| 333 | "You cannot drag resources on to a slave.",
|
---|
| 334 | "HeuristicLab Hive Administrator",
|
---|
| 335 | MessageBoxButtons.OK,
|
---|
| 336 | MessageBoxIcon.Information);
|
---|
| 337 | return;
|
---|
| 338 | } else if(targetResource != null && targetResource.Id == Guid.Empty) {
|
---|
| 339 | MessageBox.Show(
|
---|
| 340 | "You cannot drag resources to a not yet stored resource group.",
|
---|
| 341 | "HeuristicLab Hive Administrator",
|
---|
| 342 | MessageBoxButtons.OK,
|
---|
| 343 | MessageBoxIcon.Information);
|
---|
| 344 | return;
|
---|
| 345 | } else if (targetNode != null && (targetNode.Text == ungroupedGroupName || targetNode.Parent != null && targetNode.Parent.Text == ungroupedGroupName)) {
|
---|
| 346 | MessageBox.Show(
|
---|
[15422] | 347 | 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),
|
---|
| 348 | "HeuristicLab Hive Administrator",
|
---|
| 349 | MessageBoxButtons.OK,
|
---|
| 350 | MessageBoxIcon.Information);
|
---|
| 351 | return;
|
---|
| 352 | }
|
---|
[8051] | 353 |
|
---|
[15422] | 354 | if (sourceNode.Parent == null)
|
---|
| 355 | treeView.Nodes.Remove(sourceNode);
|
---|
| 356 | else {
|
---|
| 357 | sourceNode.Parent.Nodes.Remove(sourceNode);
|
---|
[15742] | 358 | sourceResource.ParentResourceId = null;
|
---|
[15422] | 359 | }
|
---|
[8051] | 360 |
|
---|
[15422] | 361 | if (targetNode == null) {
|
---|
| 362 | treeView.Nodes.Add(sourceNode);
|
---|
[15742] | 363 | } else if(targetResource.Id != Guid.Empty) {
|
---|
[15422] | 364 | targetNode.Nodes.Add(sourceNode);
|
---|
[15742] | 365 | sourceResource.ParentResourceId = targetResource.Id;
|
---|
[6976] | 366 | }
|
---|
[15742] | 367 |
|
---|
| 368 | selectedResource = sourceResource;
|
---|
| 369 | OnContentChanged();
|
---|
[6976] | 370 | }
|
---|
| 371 |
|
---|
| 372 | private void treeSlaveGroup_ItemDrag(object sender, ItemDragEventArgs e) {
|
---|
| 373 | TreeNode sourceNode = (TreeNode)e.Item;
|
---|
[15742] | 374 | if (IsAdmin())
|
---|
[8051] | 375 | DoDragDrop(sourceNode, DragDropEffects.All);
|
---|
[6976] | 376 | }
|
---|
| 377 |
|
---|
| 378 | private void treeSlaveGroup_DragEnter(object sender, DragEventArgs e) {
|
---|
| 379 | e.Effect = DragDropEffects.Move;
|
---|
| 380 | }
|
---|
| 381 |
|
---|
| 382 | private void treeSlaveGroup_DragOver(object sender, DragEventArgs e) {
|
---|
| 383 | e.Effect = DragDropEffects.Move;
|
---|
| 384 | }
|
---|
| 385 |
|
---|
| 386 | private void treeSlaveGroup_QueryContinueDrag(object sender, QueryContinueDragEventArgs e) {
|
---|
| 387 | e.Action = DragAction.Continue;
|
---|
| 388 | }
|
---|
[15401] | 389 | #endregion
|
---|
| 390 |
|
---|
| 391 | #region Helpers
|
---|
[15742] | 392 | private void ResetTreeNodes(TreeNodeCollection nodes, Color c1, Color c2, bool resetText) {
|
---|
| 393 | foreach (TreeNode n in nodes) {
|
---|
| 394 | var res = ((Resource)n.Tag);
|
---|
| 395 | if (n.BackColor.Equals(c1)) {
|
---|
| 396 | n.BackColor = c2;
|
---|
| 397 | }
|
---|
| 398 | if (resetText) {
|
---|
| 399 | n.Text = res.Name;
|
---|
| 400 |
|
---|
| 401 | if (res.Id == Guid.Empty && res.Name != ungroupedGroupName) {
|
---|
| 402 | n.Text += " [not stored]";
|
---|
| 403 | } else if (res.Modified && res.Name != ungroupedGroupName) {
|
---|
| 404 | n.Text += " [changes not stored]";
|
---|
| 405 | }
|
---|
| 406 | }
|
---|
| 407 | if (n.Nodes.Count > 0) {
|
---|
| 408 | ResetTreeNodes(n.Nodes, c1, c2, resetText);
|
---|
| 409 | }
|
---|
| 410 | }
|
---|
| 411 | }
|
---|
| 412 |
|
---|
[15422] | 413 | private Resource BuildResourceTree(IEnumerable<Resource> resources) {
|
---|
| 414 | treeView.Nodes.Clear();
|
---|
| 415 | if (!resources.Any()) return null;
|
---|
| 416 |
|
---|
[15742] | 417 | var mainResources = new HashSet<Resource>(resources.OfType<SlaveGroup>()
|
---|
| 418 | .Where(x => x.ParentResourceId == null));
|
---|
[15658] | 419 | var parentedMainResources = new HashSet<Resource>(resources.OfType<SlaveGroup>()
|
---|
| 420 | .Where(x => x.ParentResourceId.HasValue && !resources.Select(y => y.Id).Contains(x.ParentResourceId.Value)));
|
---|
| 421 | mainResources.UnionWith(parentedMainResources);
|
---|
| 422 | var subResources = new HashSet<Resource>(resources.Except(mainResources).OrderByDescending(x => x.Name));
|
---|
[15422] | 423 |
|
---|
[15557] | 424 | var stack = new Stack<Resource>(mainResources.OrderByDescending(x => x.Name));
|
---|
[15742] | 425 | if (selectedResource != null) selectedResource = resources.Where(x => x.Id == selectedResource.Id).FirstOrDefault();
|
---|
[15760] | 426 | bool nodeSelected = false;
|
---|
[15422] | 427 |
|
---|
| 428 | TreeNode currentNode = null;
|
---|
| 429 | Resource currentResource = null;
|
---|
| 430 |
|
---|
[15557] | 431 | while(stack.Any()) {
|
---|
[15422] | 432 | var newResource = stack.Pop();
|
---|
| 433 | var newNode = new TreeNode(newResource.Name) { Tag = newResource };
|
---|
| 434 |
|
---|
[15742] | 435 | if(newResource.Id == Guid.Empty) {
|
---|
| 436 | newNode.Text += " [not stored]";
|
---|
| 437 | } else if(newResource.Modified) {
|
---|
| 438 | newNode.Text += " [changes not stored]";
|
---|
| 439 | }
|
---|
| 440 | if (selectedResource == null) {
|
---|
| 441 | selectedResource = newResource;
|
---|
| 442 | }
|
---|
[15760] | 443 | if (newResource.Id == selectedResource.Id && !nodeSelected) {
|
---|
[15742] | 444 | newNode.BackColor = selectedColor;
|
---|
| 445 | newNode.Text += " [selected]";
|
---|
[15760] | 446 | nodeSelected = true;
|
---|
[15742] | 447 | }
|
---|
| 448 |
|
---|
[15557] | 449 | // search for parent node of newNode and save in currentNode
|
---|
| 450 | // necessary since newNodes (stack top items) might be siblings
|
---|
| 451 | // or grand..grandparents of previous node (currentNode)
|
---|
[15742] | 452 | while (currentNode != null && newResource.ParentResourceId != currentResource.Id) {
|
---|
[15422] | 453 | currentNode = currentNode.Parent;
|
---|
| 454 | currentResource = currentNode == null ? null : (Resource)currentNode.Tag;
|
---|
[15401] | 455 | }
|
---|
| 456 |
|
---|
[15557] | 457 | if(currentNode == null) {
|
---|
[15422] | 458 | treeView.Nodes.Add(newNode);
|
---|
| 459 | } else {
|
---|
| 460 | currentNode.Nodes.Add(newNode);
|
---|
| 461 | }
|
---|
| 462 |
|
---|
[15557] | 463 | if(newResource is Slave) {
|
---|
[15422] | 464 | newNode.ImageIndex = slaveImageIndex;
|
---|
| 465 | } else {
|
---|
| 466 | newNode.ImageIndex = slaveGroupImageIndex;
|
---|
| 467 |
|
---|
[15557] | 468 | var childResources = subResources.Where(x => x.ParentResourceId == newResource.Id);
|
---|
| 469 | if(childResources.Any()) {
|
---|
| 470 | foreach(var resource in childResources.OrderByDescending(x => x.Name)) {
|
---|
| 471 | subResources.Remove(resource);
|
---|
| 472 | stack.Push(resource);
|
---|
[15422] | 473 | }
|
---|
| 474 | currentNode = newNode;
|
---|
| 475 | currentResource = newResource;
|
---|
| 476 | }
|
---|
| 477 | }
|
---|
| 478 | newNode.SelectedImageIndex = newNode.ImageIndex;
|
---|
[15576] | 479 | //if (newResource.OwnerUserId == UserInformation.Instance.User.Id)
|
---|
| 480 | // newNode.BackColor = ownedResourceColor;
|
---|
[15401] | 481 | }
|
---|
[15422] | 482 |
|
---|
| 483 | var ungroupedNode = new TreeNode(ungroupedGroupName) {
|
---|
| 484 | ForeColor = SystemColors.GrayText,
|
---|
| 485 | Tag = new SlaveGroup() {
|
---|
| 486 | Name = ungroupedGroupName,
|
---|
| 487 | Description = ungroupedGroupDescription
|
---|
| 488 | }
|
---|
| 489 | };
|
---|
| 490 |
|
---|
[15557] | 491 | foreach (var slave in subResources.OfType<Slave>().OrderBy(x => x.Name)) {
|
---|
[15422] | 492 | var slaveNode = new TreeNode(slave.Name) { Tag = slave };
|
---|
| 493 | ungroupedNode.Nodes.Add(slaveNode);
|
---|
[15742] | 494 | if(selectedResource == null) {
|
---|
| 495 | selectedResource = slave;
|
---|
| 496 | }
|
---|
| 497 | if (slave.Id == Guid.Empty) {
|
---|
| 498 | slaveNode.Text += " [not stored]";
|
---|
| 499 | } else if (slave.Modified) {
|
---|
| 500 | slaveNode.Text += " [changes not stored]";
|
---|
| 501 | }
|
---|
[15760] | 502 | if (slave.Id == selectedResource.Id && !nodeSelected) {
|
---|
[15742] | 503 | slaveNode.BackColor = selectedColor;
|
---|
| 504 | slaveNode.Text += " [selected]";
|
---|
[15760] | 505 | nodeSelected = true;
|
---|
[15742] | 506 | }
|
---|
[15422] | 507 | }
|
---|
| 508 |
|
---|
| 509 | treeView.Nodes.Add(ungroupedNode);
|
---|
| 510 | treeView.ExpandAll();
|
---|
| 511 |
|
---|
[15742] | 512 | return selectedResource;
|
---|
[15401] | 513 | }
|
---|
| 514 |
|
---|
| 515 | private void UpdateChildHbIntervall(Resource resource) {
|
---|
| 516 | foreach (Resource r in Content.Where(x => x.ParentResourceId == resource.Id)) {
|
---|
| 517 | r.HbInterval = resource.HbInterval;
|
---|
| 518 | if (r is SlaveGroup) {
|
---|
| 519 | UpdateChildHbIntervall(r);
|
---|
| 520 | }
|
---|
| 521 | }
|
---|
| 522 | }
|
---|
| 523 |
|
---|
| 524 | private bool CheckParentsEqualsMovedNode(TreeNode dest, TreeNode movedNode) {
|
---|
| 525 | TreeNode tmp = dest;
|
---|
| 526 |
|
---|
| 527 | while (tmp != null) {
|
---|
| 528 | if (tmp == movedNode) {
|
---|
| 529 | return true;
|
---|
| 530 | }
|
---|
| 531 | tmp = tmp.Parent;
|
---|
| 532 | }
|
---|
| 533 | return false;
|
---|
| 534 | }
|
---|
| 535 |
|
---|
| 536 | private void ResetView() {
|
---|
| 537 | if (InvokeRequired) Invoke((Action)ResetView);
|
---|
| 538 | else {
|
---|
[15422] | 539 | treeView.Nodes.Clear();
|
---|
[6976] | 540 |
|
---|
[15422] | 541 | if (viewHost.Content != null && viewHost.Content is SlaveGroup) {
|
---|
| 542 | ((SlaveGroup)viewHost.Content).PropertyChanged -= SlaveViewContent_PropertyChanged;
|
---|
[7256] | 543 | }
|
---|
[15401] | 544 |
|
---|
[15422] | 545 | viewHost.Content = null;
|
---|
[7256] | 546 | if (scheduleView.Content != null) {
|
---|
| 547 | scheduleView.Content.Clear();
|
---|
| 548 | }
|
---|
[15401] | 549 |
|
---|
[7256] | 550 | HiveAdminClient.Instance.ResetDowntime();
|
---|
[6976] | 551 | }
|
---|
| 552 | }
|
---|
| 553 |
|
---|
| 554 | private void UpdateResources() {
|
---|
[15742] | 555 | try {
|
---|
| 556 | HiveAdminClient.Instance.Refresh();
|
---|
| 557 | Content = HiveAdminClient.Instance.Resources;
|
---|
| 558 | } catch(AnonymousUserException) {
|
---|
| 559 | ShowHiveInformationDialog();
|
---|
| 560 | }
|
---|
[6976] | 561 | }
|
---|
| 562 |
|
---|
[15742] | 563 | private void RemoveResource(Resource resource) {
|
---|
| 564 | if (resource == null || resource.Id == Guid.Empty) return;
|
---|
[7256] | 565 |
|
---|
[15742] | 566 | try {
|
---|
| 567 | HiveAdminClient.Delete(resource);
|
---|
| 568 | Content.Remove(selectedResource);
|
---|
| 569 | } catch(AnonymousUserException) {
|
---|
| 570 | ShowHiveInformationDialog();
|
---|
[7256] | 571 | }
|
---|
| 572 | }
|
---|
| 573 |
|
---|
[6976] | 574 | private void UpdateSchedule() {
|
---|
[15412] | 575 | try {
|
---|
| 576 | HiveAdminClient.Instance.RefreshCalendar();
|
---|
| 577 | } catch (AnonymousUserException) {
|
---|
| 578 | ShowHiveInformationDialog();
|
---|
[15401] | 579 | }
|
---|
[8051] | 580 | }
|
---|
| 581 |
|
---|
[15401] | 582 | private bool IsAuthorized(Resource resource) {
|
---|
| 583 | return resource != null
|
---|
[15422] | 584 | && resource.Name != ungroupedGroupName
|
---|
[15742] | 585 | //&& resource.Id != Guid.Empty
|
---|
[15401] | 586 | && UserInformation.Instance.UserExists
|
---|
[15742] | 587 | && (HiveRoles.CheckAdminUserPermissions()
|
---|
| 588 | || HiveAdminClient.Instance.CheckOwnershipOfResource(resource, UserInformation.Instance.User.Id));
|
---|
[8051] | 589 | }
|
---|
| 590 |
|
---|
[15742] | 591 | private bool IsAdmin() {
|
---|
| 592 | return HiveRoles.CheckAdminUserPermissions();
|
---|
| 593 | }
|
---|
| 594 |
|
---|
| 595 | private void ChangeSelectedResourceNode(TreeNode resourceNode) {
|
---|
| 596 | selectedResource = (Resource)resourceNode.Tag;
|
---|
| 597 | ResetTreeNodes(treeView.Nodes, selectedColor, Color.Transparent, true);
|
---|
| 598 | resourceNode.BackColor = selectedColor;
|
---|
| 599 | resourceNode.Text += " [selected]";
|
---|
| 600 | }
|
---|
| 601 |
|
---|
| 602 | private void SetEnabledStateOfControlsForSelectedResource() {
|
---|
| 603 | bool enabled = (IsAdmin()) ? true : false;
|
---|
| 604 | btnAddGroup.Enabled = enabled;
|
---|
| 605 | btnRemoveGroup.Enabled = enabled;
|
---|
| 606 | btnSave.Enabled = enabled;
|
---|
| 607 | }
|
---|
| 608 |
|
---|
[15401] | 609 | private void ShowHiveInformationDialog() {
|
---|
| 610 | if (InvokeRequired) Invoke((Action)ShowHiveInformationDialog);
|
---|
| 611 | else {
|
---|
| 612 | using (HiveInformationDialog dialog = new HiveInformationDialog()) {
|
---|
| 613 | dialog.ShowDialog(this);
|
---|
| 614 | }
|
---|
[6976] | 615 | }
|
---|
| 616 | }
|
---|
[15401] | 617 | #endregion
|
---|
[6976] | 618 | }
|
---|
| 619 | }
|
---|