Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3133_ProblemModifiers/HeuristicLab.Problems.Modifiers.Views/ProjectResourcesValueView.cs @ 18132

Last change on this file since 18132 was 18029, checked in by bwerth, 3 years ago

#3133 added implementation of problem modifiers

File size: 3.8 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using HeuristicLab.Clients.Hive;
5using HeuristicLab.Core.Views;
6using HeuristicLab.MainForm;
7
8namespace HeuristicLab.Problems.Modifiers.Views {
9  [View("ProjectResourcesValueView")]
10  [Content(typeof(ProjectResourcesValue), true)]
11  public partial class ProjectResourcesValueView : ItemView {
12    private readonly object locker = new object();
13    private bool updatingProjects;
14    private bool progressRegistered;
15
16    public new ProjectResourcesValue Content {
17      get { return (ProjectResourcesValue)base.Content; }
18      set { base.Content = value; }
19    }
20
21    public ProjectResourcesValueView() {
22      InitializeComponent();
23    }
24
25    protected override void SetEnabledStateOfControls() {
26      base.SetEnabledStateOfControls();
27
28      hiveProjectSelector.Enabled = Content != null && !ReadOnly && !Locked;
29    }
30
31    protected override void RegisterContentEvents() {
32      base.RegisterContentEvents();
33
34      HiveClient.Instance.Refreshing -= HiveClient_Instance_Refreshing;
35      HiveClient.Instance.Refreshed -= HiveClient_Instance_Refreshed;
36      HiveClient.Instance.Refreshing += HiveClient_Instance_Refreshing;
37      HiveClient.Instance.Refreshed += HiveClient_Instance_Refreshed;
38    }
39
40    protected override void DeregisterContentEvents() {
41      base.DeregisterContentEvents();
42      HiveClient.Instance.Refreshed -= HiveClient_Instance_Refreshed;
43      HiveClient.Instance.Refreshing -= HiveClient_Instance_Refreshing;
44    }
45
46    protected override void OnContentChanged() {
47      base.OnContentChanged();
48
49      if (Content == null) {
50        hiveProjectSelector.SelectedProjectId = Guid.Empty;
51        hiveProjectSelector.SelectedResourceIds = new List<Guid>();
52      } else {
53        hiveProjectSelector.SelectedProjectId = Content.ProjectId;
54        hiveProjectSelector.SelectedResourceIds = Content.ResourceIds.ToList();
55      }
56    }
57
58    private void HiveClient_Instance_Refreshing(object sender, EventArgs e) {
59      if (InvokeRequired) Invoke((Action<object, EventArgs>)HiveClient_Instance_Refreshing, sender, e);
60      else {
61        if (!progressRegistered) {
62          Progress.Show(this, "Refreshing ...");
63          progressRegistered = true;
64        }
65        refreshButton.Enabled = false;
66      }
67    }
68
69    private void HiveClient_Instance_Refreshed(object sender, EventArgs e) {
70      if (InvokeRequired) Invoke((Action<object, EventArgs>)HiveClient_Instance_Refreshed, sender, e);
71      else {
72        if (progressRegistered) {
73          Progress.Hide(this);
74          progressRegistered = false;
75        }
76        refreshButton.Enabled = true;
77      }
78    }
79
80    private void hiveProjectSelector_SelectedProjectChanged(object sender, EventArgs e) {
81      Content.ProjectId = hiveProjectSelector.SelectedProject.Id;
82    }
83
84    private void hiveProjectSelector_AssignedResourcesChanged(object sender, EventArgs e) {
85      Content.ResourceIds = hiveProjectSelector.AssignedResources.Select(x => x.Id).ToList();
86    }
87
88    private async void refreshButton_Click(object sender, EventArgs e) {
89      lock (locker) {
90        if (updatingProjects) return;
91        updatingProjects = true;
92      }
93
94      await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
95        action: UpdateProjects,
96        finallyCallback: () => updatingProjects = false);
97    }
98
99    private void UpdateProjects() {
100      HiveClient.Instance.RefreshProjectsAndResources();
101      hiveProjectSelector.ProjectId = Content?.ProjectId ?? Guid.Empty;
102      hiveProjectSelector.SelectedProjectId = Content?.ProjectId ?? Guid.Empty;
103      hiveProjectSelector.SelectedResourceIds = Content != null ? Content.ResourceIds.ToList() : new List<Guid>();
104      hiveProjectSelector.Content = HiveClient.Instance.Projects;
105    }
106  }
107}
Note: See TracBrowser for help on using the repository browser.