Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Clients.Hive.JobManager/3.3/Views/HiveResourceSelectorDialog.cs @ 18190

Last change on this file since 18190 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 6.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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
22using System;
23using System.Collections.Generic;
24using System.ComponentModel;
25using System.Linq;
26using System.Windows.Forms;
27using HeuristicLab.Clients.Hive.Views;
28using HeuristicLab.MainForm;
29
30namespace HeuristicLab.Clients.Hive.JobManager.Views {
31  public partial class HiveResourceSelectorDialog : Form {
32    private readonly object locker = new object();
33    private bool updatingProjects = false;
34
35    public Project SelectedProject {
36      get { return hiveResourceSelector.SelectedProject; }
37    }
38
39    public IEnumerable<Resource> SelectedResources {
40      get { return hiveResourceSelector.AssignedResources; }
41    }
42
43    private Guid jobId;
44    public Guid JobId {
45      get { return hiveResourceSelector.JobId; }
46      set { jobId = value; }
47    }
48
49    // persisted projectId
50    private Guid? projectId;
51    public Guid? ProjectId {
52      get { return hiveResourceSelector.ProjectId; }
53      set { projectId = value; }
54    }
55
56    // currently selected projectId (initially the persisted projectId)
57    private Guid? selectedProjectId;
58    public Guid? SelectedProjectId {
59      get { return selectedProjectId; }
60      set { selectedProjectId = value; }
61    }
62
63    // currently selected resourceIds (if null, perform lookup in HiveResourceSelector)
64    private IEnumerable<Guid> selectedResourceIds;
65    public IEnumerable<Guid> SelectedResourceIds {
66      get { return selectedResourceIds; }
67      set { selectedResourceIds = value; }
68    }
69
70
71
72    public HiveResourceSelectorDialog(Guid jobId, Guid projectId) {
73      this.jobId = jobId;
74      this.projectId = projectId;
75      this.selectedProjectId = projectId;
76      InitializeComponent();
77    }
78
79    #region Overrides
80    protected override void OnLoad(EventArgs e) {
81      HiveClient.Instance.Refreshing += HiveClient_Instance_Refreshing;
82      HiveClient.Instance.Refreshed += HiveClient_Instance_Refreshed;
83      base.OnLoad(e);
84    }
85
86    protected override void OnClosing(CancelEventArgs e) {
87      HiveClient.Instance.Refreshed -= HiveClient_Instance_Refreshed;
88      HiveClient.Instance.Refreshing -= HiveClient_Instance_Refreshing;
89      base.OnClosing(e);
90    }
91    #endregion
92
93    #region Event Handlers
94    private void HiveClient_Instance_Refreshing(object sender, EventArgs e) {
95      if (InvokeRequired) Invoke((Action<object, EventArgs>)HiveClient_Instance_Refreshing, sender, e);
96      else {
97        refreshButton.Enabled = false;
98        okButton.Enabled = false;
99        cancelButton.Enabled = false;
100        // Progress cannot be shown on dialog (no parent control), thus it is shown on the selector
101        Progress.Show(hiveResourceSelector, "Refreshing", ProgressMode.Indeterminate);
102      }
103    }
104
105    private void HiveClient_Instance_Refreshed(object sender, EventArgs e) {
106      if (InvokeRequired) Invoke((Action<object, EventArgs>)HiveClient_Instance_Refreshed, sender, e);
107      else {
108        Progress.Hide(hiveResourceSelector);
109        okButton.Enabled = true;
110        cancelButton.Enabled = true;
111        refreshButton.Enabled = true;
112      }
113    }
114
115    private async void HiveResourceSelectorDialog_Load(object sender, EventArgs e) {
116      lock (locker) {
117        if (updatingProjects) return;
118        updatingProjects = true;
119      }
120
121      await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
122        action: () => UpdateProjects(),
123        finallyCallback: () => updatingProjects = false);
124    }
125
126    private async void refreshButton_Click(object sender, EventArgs e) {
127      lock (locker) {
128        if (updatingProjects) return;
129        updatingProjects = true;
130      }
131
132      await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
133        action: () => UpdateProjects(),
134        finallyCallback: () => updatingProjects = false);
135    }
136
137    private void hiveResourceSelector_SelectedProjectChanged(object sender, EventArgs e) {
138      okButton.Enabled = hiveResourceSelector.SelectedProject != null && hiveResourceSelector.AssignedResources.Any();
139    }
140
141    private void hiveResourceSelector_SelectedResourcesChanged(object sender, EventArgs e) {
142      okButton.Enabled = hiveResourceSelector.AssignedResources.Any();
143
144      if (!hiveResourceSelector.AssignedResources.Any()) {
145        errorProvider.SetError(okButton, "Note: currently no resources are assigned");
146      } else if (hiveResourceSelector.AssignedCores == 0) {
147        errorProvider.SetError(okButton, "Note: currently no resources with cores are assigned");
148      } else {
149        errorProvider.SetError(okButton, string.Empty);
150      }
151    }
152
153    private void hiveResourceSelector_ProjectsTreeViewDoubleClicked(object sender, EventArgs e) {
154      if (hiveResourceSelector.SelectedProject == null) return;
155      if (!hiveResourceSelector.AssignedResources.Any()) return;
156
157      DialogResult = DialogResult.OK;
158      Close();
159    }
160    #endregion
161
162    #region Helpers
163    private void UpdateProjects() {
164      HiveClient.Instance.RefreshProjectsAndResources();
165      hiveResourceSelector.JobId = jobId;
166      hiveResourceSelector.ProjectId = projectId;
167      hiveResourceSelector.SelectedProjectId = selectedProjectId;
168      hiveResourceSelector.SelectedResourceIds = selectedResourceIds;
169      hiveResourceSelector.Content = HiveClient.Instance.Projects;
170    }
171
172    private void ShowHiveInformationDialog() {
173      if (InvokeRequired) Invoke((Action)ShowHiveInformationDialog);
174      else {
175        using (HiveInformationDialog dialog = new HiveInformationDialog()) {
176          dialog.ShowDialog(this);
177        }
178      }
179    }
180    #endregion
181  }
182}
Note: See TracBrowser for help on using the repository browser.