[7910] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 3 | * Copyright (C) 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] | 22 | using System;
|
---|
[7910] | 23 | using System.Collections.Generic;
|
---|
[16117] | 24 | using System.ComponentModel;
|
---|
| 25 | using System.Linq;
|
---|
[7910] | 26 | using System.Windows.Forms;
|
---|
[16117] | 27 | using HeuristicLab.Clients.Hive.Views;
|
---|
| 28 | using HeuristicLab.MainForm;
|
---|
[7910] | 29 |
|
---|
| 30 | namespace HeuristicLab.Clients.Hive.JobManager.Views {
|
---|
| 31 | public partial class HiveResourceSelectorDialog : Form {
|
---|
[16117] | 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;
|
---|
[7910] | 76 | InitializeComponent();
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[16117] | 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 | }
|
---|
[7910] | 85 |
|
---|
[16117] | 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);
|
---|
[7910] | 90 | }
|
---|
[16117] | 91 | #endregion
|
---|
[7910] | 92 |
|
---|
[16117] | 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;
|
---|
[16512] | 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);
|
---|
[16117] | 102 | }
|
---|
[7910] | 103 | }
|
---|
| 104 |
|
---|
[16117] | 105 | private void HiveClient_Instance_Refreshed(object sender, EventArgs e) {
|
---|
| 106 | if (InvokeRequired) Invoke((Action<object, EventArgs>)HiveClient_Instance_Refreshed, sender, e);
|
---|
| 107 | else {
|
---|
[16512] | 108 | Progress.Hide(hiveResourceSelector);
|
---|
| 109 | okButton.Enabled = true;
|
---|
| 110 | cancelButton.Enabled = true;
|
---|
[16117] | 111 | refreshButton.Enabled = true;
|
---|
| 112 | }
|
---|
[7910] | 113 | }
|
---|
| 114 |
|
---|
[16117] | 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);
|
---|
[7910] | 124 | }
|
---|
| 125 |
|
---|
[16117] | 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);
|
---|
[7910] | 135 | }
|
---|
| 136 |
|
---|
[16117] | 137 | private void hiveResourceSelector_SelectedProjectChanged(object sender, EventArgs e) {
|
---|
| 138 | okButton.Enabled = hiveResourceSelector.SelectedProject != null && hiveResourceSelector.AssignedResources.Any();
|
---|
[7910] | 139 | }
|
---|
[16117] | 140 |
|
---|
| 141 | private void hiveResourceSelector_SelectedResourcesChanged(object sender, EventArgs e) {
|
---|
| 142 | okButton.Enabled = hiveResourceSelector.AssignedResources.Any();
|
---|
| 143 |
|
---|
[16430] | 144 | if (!hiveResourceSelector.AssignedResources.Any()) {
|
---|
[16117] | 145 | errorProvider.SetError(okButton, "Note: currently no resources are assigned");
|
---|
[16430] | 146 | } else if (hiveResourceSelector.AssignedCores == 0) {
|
---|
[16117] | 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
|
---|
[7910] | 181 | }
|
---|
| 182 | }
|
---|