Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2965_CancelablePersistence/HeuristicLab.Clients.Hive.JobManager/3.3/Views/HiveResourceSelectorDialog.cs @ 16433

Last change on this file since 16433 was 16433, checked in by pfleck, 5 years ago

#2965 Merged recent trunk changes.
Enabled the prepared hooks that allows to cancel the save file using the recently introduced cancelable progressbars (in FileManager).

File size: 6.2 KB
RevLine 
[7910]1#region License Information
2/* HeuristicLab
[15583]3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[7910]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
[16117]22using System;
[7910]23using System.Collections.Generic;
[16117]24using System.ComponentModel;
25using System.Linq;
[7910]26using System.Windows.Forms;
[16117]27using HeuristicLab.Clients.Hive.Views;
28using HeuristicLab.MainForm;
29using HeuristicLab.Core;
[7910]30
31namespace HeuristicLab.Clients.Hive.JobManager.Views {
32  public partial class HiveResourceSelectorDialog : Form {
[16117]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;
[7910]77      InitializeComponent();
78    }
79
[16117]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    }
[7910]86
[16117]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);
[7910]91    }
[16117]92    #endregion
[7910]93
[16117]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 {
[16433]98        Progress.ShowOnControl(this, "Refreshing", ProgressMode.Indeterminate);
[16117]99        refreshButton.Enabled = false;
100      }
[7910]101    }
102
[16117]103    private void HiveClient_Instance_Refreshed(object sender, EventArgs e) {
104      if (InvokeRequired) Invoke((Action<object, EventArgs>)HiveClient_Instance_Refreshed, sender, e);
105      else {
[16433]106        Progress.HideFromControl(this);
[16117]107        refreshButton.Enabled = true;
108      }
[7910]109    }
110
[16117]111    private async void HiveResourceSelectorDialog_Load(object sender, EventArgs e) {
112      lock (locker) {
113        if (updatingProjects) return;
114        updatingProjects = true;
115      }
116
117      await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
118        action: () => UpdateProjects(),
119        finallyCallback: () => updatingProjects = false);
[7910]120    }
121
[16117]122    private async void refreshButton_Click(object sender, EventArgs e) {
123      lock (locker) {
124        if (updatingProjects) return;
125        updatingProjects = true;
126      }
127
128      await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
129        action: () => UpdateProjects(),
130        finallyCallback: () => updatingProjects = false);
[7910]131    }
132
[16117]133    private void hiveResourceSelector_SelectedProjectChanged(object sender, EventArgs e) {
134      okButton.Enabled = hiveResourceSelector.SelectedProject != null && hiveResourceSelector.AssignedResources.Any();
[7910]135    }
[16117]136
137    private void hiveResourceSelector_SelectedResourcesChanged(object sender, EventArgs e) {
138      okButton.Enabled = hiveResourceSelector.AssignedResources.Any();
139
[16433]140      if (!hiveResourceSelector.AssignedResources.Any()) {
[16117]141        errorProvider.SetError(okButton, "Note: currently no resources are assigned");
[16433]142      } else if (hiveResourceSelector.AssignedCores == 0) {
[16117]143        errorProvider.SetError(okButton, "Note: currently no resources with cores are assigned");
144      } else {
145        errorProvider.SetError(okButton, string.Empty);
146      }
147    }
148
149    private void hiveResourceSelector_ProjectsTreeViewDoubleClicked(object sender, EventArgs e) {
150      if (hiveResourceSelector.SelectedProject == null) return;
151      if (!hiveResourceSelector.AssignedResources.Any()) return;
152
153      DialogResult = DialogResult.OK;
154      Close();
155    }
156    #endregion
157
158    #region Helpers
159    private void UpdateProjects() {
160      HiveClient.Instance.RefreshProjectsAndResources();
161      hiveResourceSelector.JobId = jobId;
162      hiveResourceSelector.ProjectId = projectId;
163      hiveResourceSelector.SelectedProjectId = selectedProjectId;
164      hiveResourceSelector.SelectedResourceIds = selectedResourceIds;
165      hiveResourceSelector.Content = HiveClient.Instance.Projects;
166    }
167
168    private void ShowHiveInformationDialog() {
169      if (InvokeRequired) Invoke((Action)ShowHiveInformationDialog);
170      else {
171        using (HiveInformationDialog dialog = new HiveInformationDialog()) {
172          dialog.ShowDialog(this);
173        }
174      }
175    }
176    #endregion
[7910]177  }
178}
Note: See TracBrowser for help on using the repository browser.