1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using HeuristicLab.Clients.Hive;
|
---|
5 | using HeuristicLab.Core.Views;
|
---|
6 | using HeuristicLab.MainForm;
|
---|
7 |
|
---|
8 | namespace 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 | }
|
---|