Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2989_MovingPeaksBenchmark/HeuristicLab.Clients.Hive.JobManager/3.3/Views/HiveResourceSelectorDialog.cs

Last change on this file was 16512, checked in by pfleck, 6 years ago

#2845

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