[15401] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16117] | 3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[15401] | 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("Projects View")]
|
---|
| 38 | [Content(typeof(IItemList<Project>), false)]
|
---|
| 39 | public partial class ProjectsView : ItemView, IDisposable {
|
---|
| 40 | private const int greenFlagImageIndex = 0;
|
---|
| 41 | private const int redFlagImageIndex = 1;
|
---|
[16117] | 42 | private const string SELECTED_TAG = ""; // " [selected]";
|
---|
| 43 | private const string NOT_STORED_TAG = "*"; // " [not stored]";
|
---|
| 44 | private const string CHANGES_NOT_STORED_TAG = "*"; // " [changes not stored]";
|
---|
[16202] | 45 | private const string INACTIVE_TAG = " [inactive]";
|
---|
[15742] | 46 |
|
---|
[16117] | 47 | private readonly Color selectedBackColor = Color.DodgerBlue;
|
---|
| 48 | private readonly Color selectedForeColor = Color.White;
|
---|
| 49 | private readonly Color grayTextColor = SystemColors.GrayText;
|
---|
| 50 |
|
---|
[15742] | 51 | private Project selectedProject = null;
|
---|
[16117] | 52 | public Project SelectedProject {
|
---|
| 53 | get { return selectedProject; }
|
---|
| 54 | set { if (selectedProject != value) ChangeSelectedProject(value); }
|
---|
| 55 | }
|
---|
[15401] | 56 |
|
---|
| 57 | private readonly object locker = new object();
|
---|
| 58 |
|
---|
| 59 | public new IItemList<Project> Content {
|
---|
| 60 | get { return (IItemList<Project>)base.Content; }
|
---|
| 61 | set { base.Content = value; }
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | public ProjectsView() {
|
---|
| 65 | InitializeComponent();
|
---|
| 66 |
|
---|
| 67 | projectsTreeView.ImageList.Images.Add(VSImageLibrary.FlagGreen);
|
---|
| 68 | projectsTreeView.ImageList.Images.Add(VSImageLibrary.FlagRed);
|
---|
| 69 |
|
---|
| 70 | HiveAdminClient.Instance.Refreshing += HiveAdminClient_Instance_Refreshing;
|
---|
| 71 | HiveAdminClient.Instance.Refreshed += HiveAdminClient_Instance_Refreshed;
|
---|
| 72 | AccessClient.Instance.Refreshing += AccessClient_Instance_Refreshing;
|
---|
| 73 | AccessClient.Instance.Refreshed += AccessClient_Instance_Refreshed;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | #region Overrides
|
---|
| 77 | protected override void OnClosing(FormClosingEventArgs e) {
|
---|
| 78 | AccessClient.Instance.Refreshed -= AccessClient_Instance_Refreshed;
|
---|
| 79 | AccessClient.Instance.Refreshing -= AccessClient_Instance_Refreshing;
|
---|
| 80 | HiveAdminClient.Instance.Refreshed -= HiveAdminClient_Instance_Refreshed;
|
---|
| 81 | HiveAdminClient.Instance.Refreshing -= HiveAdminClient_Instance_Refreshing;
|
---|
| 82 | base.OnClosing(e);
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | protected override void RegisterContentEvents() {
|
---|
| 86 | base.RegisterContentEvents();
|
---|
| 87 | Content.ItemsAdded += Content_ItemsAdded;
|
---|
| 88 | Content.ItemsRemoved += Content_ItemsRemoved;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | protected override void DeregisterContentEvents() {
|
---|
| 92 | Content.ItemsRemoved -= Content_ItemsRemoved;
|
---|
| 93 | Content.ItemsAdded -= Content_ItemsAdded;
|
---|
| 94 | base.DeregisterContentEvents();
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | protected override void OnContentChanged() {
|
---|
| 98 | base.OnContentChanged();
|
---|
| 99 | if (Content == null) {
|
---|
| 100 | projectsTreeView.Nodes.Clear();
|
---|
| 101 | projectView.Content = null;
|
---|
[15422] | 102 | projectPermissionsView.Content = null;
|
---|
| 103 | projectResourcesView.Content = null;
|
---|
[16117] | 104 | projectJobsView.Content = null;
|
---|
| 105 | selectedProject = null;
|
---|
[15401] | 106 | } else {
|
---|
[16117] | 107 | BuildProjectTree(Content);
|
---|
[15401] | 108 | }
|
---|
[16185] | 109 | SetEnabledStateOfControls();
|
---|
[15401] | 110 | }
|
---|
| 111 |
|
---|
| 112 | protected override void SetEnabledStateOfControls() {
|
---|
[16185] | 113 | base.SetEnabledStateOfControls();
|
---|
[16117] | 114 |
|
---|
[16185] | 115 | bool locked = Content == null || Locked || ReadOnly;
|
---|
| 116 | bool parentOwner = selectedProject != null && HiveAdminClient.Instance.CheckOwnershipOfParentProject(selectedProject, UserInformation.Instance.User.Id);
|
---|
| 117 | bool selectedProjectDisabled = selectedProject == null
|
---|
| 118 | || selectedProject != null && selectedProject.Id == Guid.Empty;
|
---|
[16117] | 119 |
|
---|
[16185] | 120 | bool selectedProjectHasJobs =
|
---|
| 121 | !selectedProjectDisabled && HiveAdminClient.Instance.Jobs.ContainsKey(selectedProject.Id)
|
---|
| 122 | && HiveAdminClient.Instance.Jobs[selectedProject.Id] != null
|
---|
| 123 | && HiveAdminClient.Instance.Jobs[selectedProject.Id].Any();
|
---|
| 124 |
|
---|
| 125 | bool addLocked = locked
|
---|
| 126 | || (selectedProject == null && !IsAdmin())
|
---|
| 127 | || (selectedProject != null && selectedProject.Id == Guid.Empty)
|
---|
| 128 | || (selectedProject != null && !IsAdmin() && !parentOwner && selectedProject.OwnerUserId != UserInformation.Instance.User.Id)
|
---|
| 129 | || (selectedProject != null && (DateTime.Now < selectedProject.StartDate || DateTime.Now > selectedProject.EndDate));
|
---|
| 130 |
|
---|
| 131 | bool deleteLocked = locked
|
---|
| 132 | || !Content.Any()
|
---|
| 133 | || selectedProject == null
|
---|
| 134 | || Content.Any(x => x.ParentProjectId == selectedProject.Id)
|
---|
| 135 | || selectedProjectHasJobs
|
---|
| 136 | || (!IsAdmin() && !parentOwner);
|
---|
| 137 |
|
---|
| 138 | bool saveLocked = locked
|
---|
| 139 | || !Content.Any()
|
---|
| 140 | || selectedProject == null
|
---|
| 141 | || (!IsAdmin() && !parentOwner && selectedProject.OwnerUserId != UserInformation.Instance.User.Id);
|
---|
| 142 |
|
---|
| 143 |
|
---|
[16117] | 144 | addButton.Enabled = !addLocked;
|
---|
| 145 | removeButton.Enabled = !deleteLocked;
|
---|
| 146 | saveProjectButton.Enabled = !saveLocked;
|
---|
[16185] | 147 |
|
---|
[16117] | 148 | projectView.Enabled = !locked;
|
---|
[16185] | 149 | projectPermissionsView.Enabled = !locked && !selectedProjectDisabled;
|
---|
| 150 | projectResourcesView.Enabled = !locked && !selectedProjectDisabled;
|
---|
| 151 | projectJobsView.Enabled = !locked && !selectedProjectDisabled;
|
---|
[16117] | 152 |
|
---|
| 153 | projectView.Locked = locked;
|
---|
[16185] | 154 | projectPermissionsView.Locked = locked || selectedProjectDisabled;
|
---|
| 155 | projectResourcesView.Locked = locked || selectedProjectDisabled;
|
---|
| 156 | projectJobsView.Locked = locked || selectedProjectDisabled;
|
---|
[15401] | 157 | }
|
---|
| 158 | #endregion
|
---|
| 159 |
|
---|
| 160 | #region Event Handlers
|
---|
| 161 | private void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<Project>> e) {
|
---|
[15412] | 162 | if (InvokeRequired) Invoke((Action<object, CollectionItemsChangedEventArgs<IndexedItem<Project>>>)Content_ItemsAdded, sender, e);
|
---|
| 163 | else {
|
---|
| 164 | OnContentChanged();
|
---|
| 165 | }
|
---|
[15401] | 166 | }
|
---|
| 167 |
|
---|
| 168 | private void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<Project>> e) {
|
---|
[15412] | 169 | if (InvokeRequired) Invoke((Action<object, CollectionItemsChangedEventArgs<IndexedItem<Project>>>)Content_ItemsRemoved, sender, e);
|
---|
| 170 | else {
|
---|
| 171 | OnContentChanged();
|
---|
| 172 | }
|
---|
[15401] | 173 | }
|
---|
| 174 |
|
---|
| 175 | private void ProjectViewContent_PropertyChanged(object sender, PropertyChangedEventArgs e) {
|
---|
[15412] | 176 | if (InvokeRequired) Invoke((Action<object, PropertyChangedEventArgs>)ProjectViewContent_PropertyChanged, sender, e);
|
---|
| 177 | else {
|
---|
| 178 | OnContentChanged();
|
---|
| 179 | }
|
---|
[15401] | 180 | }
|
---|
| 181 |
|
---|
| 182 | private void HiveAdminClient_Instance_Refreshing(object sender, EventArgs e) {
|
---|
| 183 | if (InvokeRequired) Invoke((Action<object, EventArgs>)HiveAdminClient_Instance_Refreshing, sender, e);
|
---|
| 184 | else {
|
---|
| 185 | var mainForm = MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>();
|
---|
| 186 | mainForm.AddOperationProgressToView(this, "Refreshing ...");
|
---|
| 187 | SetEnabledStateOfControls();
|
---|
| 188 | }
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | private void HiveAdminClient_Instance_Refreshed(object sender, EventArgs e) {
|
---|
| 192 | if (InvokeRequired) Invoke((Action<object, EventArgs>)HiveAdminClient_Instance_Refreshed, sender, e);
|
---|
| 193 | else {
|
---|
| 194 | var mainForm = MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>();
|
---|
| 195 | mainForm.RemoveOperationProgressFromView(this);
|
---|
| 196 | SetEnabledStateOfControls();
|
---|
| 197 | }
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | private void AccessClient_Instance_Refreshing(object sender, EventArgs e) {
|
---|
| 201 | if (InvokeRequired) Invoke((Action<object, EventArgs>)AccessClient_Instance_Refreshing, sender, e);
|
---|
| 202 | else {
|
---|
| 203 | var mainForm = MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>();
|
---|
| 204 | mainForm.AddOperationProgressToView(this, "Refreshing ...");
|
---|
| 205 | SetEnabledStateOfControls();
|
---|
| 206 | }
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 | private void AccessClient_Instance_Refreshed(object sender, EventArgs e) {
|
---|
| 210 | if (InvokeRequired) Invoke((Action<object, EventArgs>)AccessClient_Instance_Refreshed, sender, e);
|
---|
| 211 | else {
|
---|
| 212 | var mainForm = MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>();
|
---|
| 213 | mainForm.RemoveOperationProgressFromView(this);
|
---|
| 214 | SetEnabledStateOfControls();
|
---|
| 215 | }
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 | private async void ProjectsView_Load(object sender, EventArgs e) {
|
---|
| 219 | await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
|
---|
[15742] | 220 | action: () => UpdateProjects());
|
---|
[15401] | 221 | }
|
---|
| 222 |
|
---|
| 223 | private async void refreshButton_Click(object sender, EventArgs e) {
|
---|
| 224 | lock (locker) {
|
---|
[15422] | 225 | if (!refreshButton.Enabled) return;
|
---|
| 226 | refreshButton.Enabled = false;
|
---|
[15401] | 227 | }
|
---|
| 228 |
|
---|
| 229 | await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
|
---|
| 230 | action: () => UpdateProjects(),
|
---|
[15412] | 231 | finallyCallback: () => {
|
---|
[15422] | 232 | refreshButton.Enabled = true;
|
---|
[15412] | 233 | });
|
---|
[15401] | 234 | }
|
---|
| 235 |
|
---|
[16117] | 236 | private void addButton_Click(object sender, EventArgs e) {
|
---|
[15742] | 237 |
|
---|
[16117] | 238 | if (selectedProject == null && !IsAdmin()) {
|
---|
| 239 | MessageBox.Show(
|
---|
| 240 | "You are not allowed to add a root project - please select a parent project.",
|
---|
| 241 | "HeuristicLab Hive Administrator",
|
---|
| 242 | MessageBoxButtons.OK,
|
---|
| 243 | MessageBoxIcon.Information);
|
---|
| 244 | return;
|
---|
| 245 | }
|
---|
| 246 |
|
---|
[15742] | 247 | if (selectedProject != null && selectedProject.Id == Guid.Empty) {
|
---|
| 248 | MessageBox.Show(
|
---|
| 249 | "You cannot add a project to a not yet stored project.",
|
---|
| 250 | "HeuristicLab Hive Administrator",
|
---|
| 251 | MessageBoxButtons.OK,
|
---|
| 252 | MessageBoxIcon.Information);
|
---|
| 253 | return;
|
---|
| 254 | }
|
---|
| 255 |
|
---|
[15401] | 256 | var project = new Project {
|
---|
| 257 | Name = "New Project",
|
---|
[16117] | 258 | OwnerUserId = UserInformation.Instance.User.Id,
|
---|
[15401] | 259 | };
|
---|
[16117] | 260 | if(selectedProject != null) {
|
---|
| 261 | project.ParentProjectId = selectedProject.Id;
|
---|
| 262 | project.EndDate = selectedProject.EndDate;
|
---|
| 263 | }
|
---|
[15742] | 264 |
|
---|
[16117] | 265 | SelectedProject = project;
|
---|
[15401] | 266 | Content.Add(project);
|
---|
| 267 | }
|
---|
| 268 |
|
---|
| 269 | private async void removeButton_Click(object sender, EventArgs e) {
|
---|
[15742] | 270 | if (selectedProject == null) return;
|
---|
| 271 |
|
---|
[15401] | 272 | lock (locker) {
|
---|
[16117] | 273 | // for details go to ChangeSelectedProject(..)
|
---|
[15422] | 274 | if (!removeButton.Enabled) return;
|
---|
| 275 | removeButton.Enabled = false;
|
---|
[15401] | 276 | }
|
---|
| 277 |
|
---|
[15412] | 278 | var result = MessageBox.Show(
|
---|
[15742] | 279 | "Do you really want to delete " + selectedProject.Name + "?",
|
---|
[15412] | 280 | "HeuristicLab Hive Administrator",
|
---|
| 281 | MessageBoxButtons.YesNo,
|
---|
| 282 | MessageBoxIcon.Question);
|
---|
| 283 |
|
---|
| 284 | if (result == DialogResult.Yes) {
|
---|
[15742] | 285 | await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
|
---|
[16117] | 286 | action: () => {
|
---|
| 287 | RemoveProject(selectedProject);
|
---|
[15742] | 288 | });
|
---|
[15412] | 289 | }
|
---|
[16185] | 290 | SetEnabledStateOfControls();
|
---|
[15401] | 291 | }
|
---|
| 292 |
|
---|
| 293 | private async void saveProjectButton_Click(object sender, EventArgs e) {
|
---|
| 294 | lock (locker) {
|
---|
[15422] | 295 | if (!saveProjectButton.Enabled) return;
|
---|
| 296 | saveProjectButton.Enabled = false;
|
---|
[15401] | 297 | }
|
---|
| 298 |
|
---|
| 299 | await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
|
---|
| 300 | action: () => {
|
---|
[16117] | 301 | //if (selectedProject != null && selectedProject.Id == Guid.Empty)
|
---|
| 302 | // SelectedProject = HiveAdminClient.Instance.GetAvailableProjectAncestors(selectedProject.Id).LastOrDefault();
|
---|
[15401] | 303 | var projectsToSave = Content.Where(x => x.Id == Guid.Empty || x.Modified);
|
---|
| 304 | foreach (var project in projectsToSave)
|
---|
| 305 | project.Store();
|
---|
[16117] | 306 |
|
---|
[15742] | 307 | UpdateProjects();
|
---|
[15401] | 308 | },
|
---|
[15422] | 309 | finallyCallback: () => saveProjectButton.Enabled = true);
|
---|
[15627] | 310 |
|
---|
| 311 | OnContentChanged();
|
---|
[15401] | 312 | }
|
---|
| 313 |
|
---|
[16117] | 314 | private void projectsTreeView_MouseDown(object sender, MouseEventArgs e) {
|
---|
| 315 | var node = projectsTreeView.GetNodeAt(e.Location);
|
---|
| 316 | if (node == null) return;
|
---|
| 317 | var p = (Project)node.Tag;
|
---|
| 318 | if(!HiveAdminClient.Instance.DisabledParentProjects.Contains(p)) ChangeSelectedProjectNode(node);
|
---|
| 319 | }
|
---|
[15401] | 320 |
|
---|
[16117] | 321 | private void projectsTreeView_BeforeSelect(object sender, TreeViewCancelEventArgs e) {
|
---|
| 322 | e.Cancel = true;
|
---|
[15401] | 323 | }
|
---|
| 324 |
|
---|
| 325 | private void projectsTreeView_DragDrop(object sender, DragEventArgs e) {
|
---|
[16117] | 326 | if (e.Effect == DragDropEffects.None) return;
|
---|
| 327 |
|
---|
[15401] | 328 | var sourceNode = (TreeNode)e.Data.GetData(typeof(TreeNode));
|
---|
| 329 | if (sourceNode == null) return;
|
---|
[16117] | 330 | var sourceProject = ((Project)sourceNode.Tag);
|
---|
| 331 | if (sourceProject == null) return;
|
---|
[15401] | 332 |
|
---|
| 333 | var treeView = (TreeView)sender;
|
---|
| 334 | if (sourceNode.TreeView != treeView) return;
|
---|
| 335 |
|
---|
| 336 | var targetPoint = treeView.PointToClient(new Point(e.X, e.Y));
|
---|
| 337 | var targetNode = treeView.GetNodeAt(targetPoint);
|
---|
| 338 |
|
---|
[15742] | 339 | var targetProject = (targetNode != null) ? (Project)targetNode.Tag : null;
|
---|
[15401] | 340 |
|
---|
[16117] | 341 | if (!HiveAdminClient.Instance.CheckParentChange(sourceProject, targetProject)) {
|
---|
[15742] | 342 | MessageBox.Show(
|
---|
[16117] | 343 | "You cannot drag projects to this project.",
|
---|
[15742] | 344 | "HeuristicLab Hive Administrator",
|
---|
| 345 | MessageBoxButtons.OK,
|
---|
| 346 | MessageBoxIcon.Information);
|
---|
[16117] | 347 | return;
|
---|
[15742] | 348 | }
|
---|
| 349 |
|
---|
| 350 | if (sourceNode.Parent == null) {
|
---|
[15401] | 351 | treeView.Nodes.Remove(sourceNode);
|
---|
[15742] | 352 | } else {
|
---|
[15401] | 353 | sourceNode.Parent.Nodes.Remove(sourceNode);
|
---|
[16117] | 354 | sourceProject.ParentProjectId = null;
|
---|
[15401] | 355 | }
|
---|
| 356 |
|
---|
| 357 | if (targetNode == null) {
|
---|
| 358 | treeView.Nodes.Add(sourceNode);
|
---|
[15742] | 359 | } else if(targetProject.Id != Guid.Empty) {
|
---|
[15401] | 360 | targetNode.Nodes.Add(sourceNode);
|
---|
[16117] | 361 | sourceProject.ParentProjectId = targetProject.Id;
|
---|
[15401] | 362 | }
|
---|
[15742] | 363 |
|
---|
[16117] | 364 | SelectedProject = sourceProject;
|
---|
[15742] | 365 | OnContentChanged();
|
---|
[15401] | 366 | }
|
---|
| 367 |
|
---|
| 368 | private void projectsTreeView_ItemDrag(object sender, ItemDragEventArgs e) {
|
---|
| 369 | var sourceNode = (TreeNode)e.Item;
|
---|
| 370 | if (IsAuthorized((Project)sourceNode.Tag))
|
---|
| 371 | DoDragDrop(sourceNode, DragDropEffects.All);
|
---|
| 372 | }
|
---|
| 373 |
|
---|
[16117] | 374 | private void projectsTreeView_DragEnterOver(object sender, DragEventArgs e) {
|
---|
[15401] | 375 | e.Effect = DragDropEffects.Move;
|
---|
| 376 |
|
---|
[16117] | 377 | var sourceNode = (TreeNode)e.Data.GetData(typeof(TreeNode));
|
---|
| 378 | var sourceProject = ((Project)sourceNode.Tag);
|
---|
| 379 |
|
---|
| 380 | var targetPoint = projectsTreeView.PointToClient(new Point(e.X, e.Y));
|
---|
| 381 | var targetNode = projectsTreeView.GetNodeAt(targetPoint);
|
---|
| 382 | var targetProject = (targetNode != null) ? (Project)targetNode.Tag : null;
|
---|
| 383 |
|
---|
| 384 | if ((!IsAdmin() && (targetNode == null || targetProject == null))
|
---|
| 385 | || sourceNode == null
|
---|
| 386 | || sourceProject == null
|
---|
| 387 | || sourceNode == targetNode
|
---|
| 388 | || !HiveAdminClient.Instance.CheckParentChange(sourceProject, targetProject)) {
|
---|
| 389 | e.Effect = DragDropEffects.None;
|
---|
| 390 | }
|
---|
[15401] | 391 | }
|
---|
| 392 |
|
---|
| 393 | private void projectsTreeView_QueryContinueDrag(object sender, QueryContinueDragEventArgs e) {
|
---|
| 394 | e.Action = DragAction.Continue;
|
---|
| 395 | }
|
---|
| 396 | #endregion
|
---|
| 397 |
|
---|
| 398 | #region Helpers
|
---|
[16117] | 399 | private void BuildProjectTree(IEnumerable<Project> projects) {
|
---|
[15401] | 400 | projectsTreeView.Nodes.Clear();
|
---|
[16117] | 401 | if (!projects.Any()) return;
|
---|
[15401] | 402 |
|
---|
[16117] | 403 | var disabledParentProjects = HiveAdminClient.Instance.DisabledParentProjects;
|
---|
[15401] | 404 | var mainProjects = new HashSet<Project>(projects.Where(x => x.ParentProjectId == null));
|
---|
[16117] | 405 | //var parentedMainProjects = new HashSet<Project>(projects
|
---|
| 406 | // .Where(x => x.ParentProjectId.HasValue
|
---|
| 407 | // && !projects.Select(y => y.Id).Contains(x.ParentProjectId.Value)));
|
---|
| 408 | //mainProjects.UnionWith(parentedMainProjects);
|
---|
| 409 | var mainDisabledParentProjects = new HashSet<Project>(disabledParentProjects.Where(x => x.ParentProjectId == null || x.ParentProjectId == Guid.Empty));
|
---|
| 410 | mainProjects.UnionWith(mainDisabledParentProjects);
|
---|
| 411 | var subProbjects = new HashSet<Project>(projects.Union(disabledParentProjects).Except(mainProjects));
|
---|
[15401] | 412 |
|
---|
| 413 | var stack = new Stack<Project>(mainProjects.OrderByDescending(x => x.Name));
|
---|
[16117] | 414 | if (selectedProject != null) SelectedProject = projects.Where(x => x.Id == selectedProject.Id).FirstOrDefault();
|
---|
[15760] | 415 | bool nodeSelected = false;
|
---|
[15401] | 416 |
|
---|
| 417 | TreeNode currentNode = null;
|
---|
| 418 | Project currentProject = null;
|
---|
[16117] | 419 |
|
---|
[15401] | 420 | while (stack.Any()) {
|
---|
| 421 | var newProject = stack.Pop();
|
---|
| 422 | var newNode = new TreeNode(newProject.Name) { Tag = newProject };
|
---|
[16117] | 423 | StyleTreeNode(newNode, newProject);
|
---|
| 424 |
|
---|
| 425 | if (selectedProject == null && !disabledParentProjects.Contains(newProject)) {
|
---|
| 426 | SelectedProject = newProject;
|
---|
[15742] | 427 | }
|
---|
[16117] | 428 | if (!nodeSelected && selectedProject != null && selectedProject.Id == newProject.Id) {
|
---|
| 429 | newNode.BackColor = selectedBackColor;
|
---|
| 430 | newNode.ForeColor = selectedForeColor;
|
---|
| 431 | newNode.Text += SELECTED_TAG;
|
---|
[15760] | 432 | nodeSelected = true;
|
---|
[15559] | 433 | }
|
---|
[15401] | 434 |
|
---|
[15742] | 435 | // search for parent node of newNode and save in currentNode
|
---|
| 436 | // necessary since newNodes (stack top items) might be siblings
|
---|
| 437 | // or grand..grandparents of previous node (currentNode)
|
---|
[15401] | 438 | while (currentNode != null && newProject.ParentProjectId != currentProject.Id) {
|
---|
| 439 | currentNode = currentNode.Parent;
|
---|
| 440 | currentProject = currentNode == null ? null : (Project)currentNode.Tag;
|
---|
| 441 | }
|
---|
| 442 |
|
---|
| 443 | if (currentNode == null) {
|
---|
| 444 | projectsTreeView.Nodes.Add(newNode);
|
---|
| 445 | newNode.ImageIndex = greenFlagImageIndex;
|
---|
| 446 | } else {
|
---|
| 447 | currentNode.Nodes.Add(newNode);
|
---|
| 448 | newNode.ImageIndex = redFlagImageIndex;
|
---|
| 449 | }
|
---|
| 450 |
|
---|
| 451 | newNode.SelectedImageIndex = newNode.ImageIndex;
|
---|
| 452 |
|
---|
[16117] | 453 | if (disabledParentProjects.Contains(newProject)) {
|
---|
| 454 | newNode.Checked = false;
|
---|
| 455 | newNode.ForeColor = grayTextColor;
|
---|
| 456 | }
|
---|
| 457 |
|
---|
[15401] | 458 | var childProjects = subProbjects.Where(x => x.ParentProjectId == newProject.Id);
|
---|
| 459 | if (childProjects.Any()) {
|
---|
| 460 | foreach (var project in childProjects.OrderByDescending(x => x.Name)) {
|
---|
| 461 | subProbjects.Remove(project);
|
---|
| 462 | stack.Push(project);
|
---|
| 463 | }
|
---|
| 464 | currentNode = newNode;
|
---|
| 465 | currentProject = newProject;
|
---|
| 466 | }
|
---|
| 467 | }
|
---|
| 468 |
|
---|
| 469 | projectsTreeView.ExpandAll();
|
---|
[16117] | 470 | }
|
---|
[15401] | 471 |
|
---|
[16117] | 472 | private void StyleTreeNode(TreeNode n, Project p) {
|
---|
| 473 | n.Text = p.Name;
|
---|
| 474 | n.BackColor = Color.Transparent;
|
---|
| 475 | n.ForeColor = Color.Black;
|
---|
| 476 |
|
---|
| 477 | if (HiveAdminClient.Instance.DisabledParentProjects.Select(x => x.Id).Contains(p.Id)) {
|
---|
| 478 | n.ForeColor = grayTextColor;
|
---|
[16202] | 479 | } else {
|
---|
| 480 | if (p.Id == Guid.Empty) {
|
---|
| 481 | n.Text += NOT_STORED_TAG;
|
---|
| 482 | } else if (p.Modified) {
|
---|
| 483 | n.Text += CHANGES_NOT_STORED_TAG;
|
---|
| 484 | }
|
---|
| 485 |
|
---|
| 486 | if (!IsActive(p)) {
|
---|
| 487 | n.ForeColor = grayTextColor;
|
---|
| 488 | n.Text += INACTIVE_TAG;
|
---|
| 489 | }
|
---|
[16117] | 490 | }
|
---|
[15401] | 491 | }
|
---|
| 492 |
|
---|
[16117] | 493 | private void ResetTreeNodes(TreeNodeCollection nodes) {
|
---|
| 494 | foreach (TreeNode n in nodes) {
|
---|
| 495 | StyleTreeNode(n, (Project)n.Tag);
|
---|
| 496 | if (n.Nodes.Count > 0) {
|
---|
| 497 | ResetTreeNodes(n.Nodes);
|
---|
| 498 | }
|
---|
| 499 | }
|
---|
| 500 | }
|
---|
| 501 |
|
---|
| 502 | private void ChangeSelectedProject(Project project) {
|
---|
| 503 | projectView.Content = project;
|
---|
| 504 | projectPermissionsView.Content = project;
|
---|
| 505 | projectResourcesView.Content = project;
|
---|
| 506 | projectJobsView.Content = project;
|
---|
| 507 | selectedProject = project;
|
---|
| 508 | SetEnabledStateOfControls();
|
---|
| 509 | }
|
---|
| 510 |
|
---|
| 511 | private void ChangeSelectedProjectNode(TreeNode projectNode) {
|
---|
| 512 | if (projectNode == null) return;
|
---|
| 513 | SelectedProject = (Project)projectNode.Tag;
|
---|
| 514 | ResetTreeNodes(projectsTreeView.Nodes);
|
---|
| 515 | projectNode.BackColor = selectedBackColor;
|
---|
| 516 | projectNode.ForeColor = selectedForeColor;
|
---|
| 517 | projectNode.Text += SELECTED_TAG;
|
---|
| 518 | }
|
---|
| 519 |
|
---|
[15401] | 520 | private void UpdateProjects() {
|
---|
| 521 | try {
|
---|
[15412] | 522 | HiveAdminClient.Instance.Refresh();
|
---|
[15742] | 523 | Content = HiveAdminClient.Instance.Projects;
|
---|
[15401] | 524 | } catch (AnonymousUserException) {
|
---|
| 525 | ShowHiveInformationDialog();
|
---|
| 526 | }
|
---|
| 527 | }
|
---|
| 528 |
|
---|
[15412] | 529 | private void RemoveProject(Project project) {
|
---|
[16117] | 530 | if (project == null) return;
|
---|
[15742] | 531 |
|
---|
[15412] | 532 | try {
|
---|
[16117] | 533 | if (project.Id != Guid.Empty) {
|
---|
| 534 | SelectedProject = HiveAdminClient.Instance.GetAvailableProjectAncestors(project.Id).LastOrDefault();
|
---|
| 535 | HiveAdminClient.Delete(project);
|
---|
| 536 | UpdateProjects();
|
---|
[16185] | 537 | } else {
|
---|
| 538 | SelectedProject = Content.FirstOrDefault(x => x.Id == project.ParentProjectId);
|
---|
[16117] | 539 | Content.Remove(project);
|
---|
| 540 | }
|
---|
[15412] | 541 | } catch (AnonymousUserException) {
|
---|
| 542 | ShowHiveInformationDialog();
|
---|
[15401] | 543 | }
|
---|
| 544 | }
|
---|
| 545 |
|
---|
[16202] | 546 | private bool IsActive(Project project) {
|
---|
| 547 | return DateTime.Now >= project.StartDate
|
---|
| 548 | && (project.EndDate == null
|
---|
| 549 | || DateTime.Now < project.EndDate.Value);
|
---|
| 550 | }
|
---|
| 551 |
|
---|
[15401] | 552 | private bool IsAuthorized(Project project) {
|
---|
[15658] | 553 | return project != null && UserInformation.Instance.UserExists;
|
---|
[15401] | 554 | }
|
---|
| 555 |
|
---|
[15742] | 556 | private bool IsAdmin() {
|
---|
| 557 | return HiveRoles.CheckAdminUserPermissions();
|
---|
| 558 | }
|
---|
| 559 |
|
---|
[15401] | 560 | private void ShowHiveInformationDialog() {
|
---|
| 561 | if (InvokeRequired) Invoke((Action)ShowHiveInformationDialog);
|
---|
| 562 | else {
|
---|
| 563 | using (HiveInformationDialog dialog = new HiveInformationDialog()) {
|
---|
| 564 | dialog.ShowDialog(this);
|
---|
| 565 | }
|
---|
| 566 | }
|
---|
| 567 | }
|
---|
| 568 | #endregion
|
---|
| 569 | }
|
---|
| 570 | }
|
---|