Ignore:
Timestamp:
09/13/18 13:18:45 (11 months ago)
Author:
ddorfmei
Message:

#2931: Merged [16046-16138/trunk] into branch

Location:
branches/2931_OR-Tools_LP_MIP
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/2931_OR-Tools_LP_MIP

  • branches/2931_OR-Tools_LP_MIP/HeuristicLab.Clients.Hive.JobManager

  • branches/2931_OR-Tools_LP_MIP/HeuristicLab.Clients.Hive.JobManager/3.3/Views/HiveResourceSelectorDialog.cs

    r15583 r16139  
    2020#endregion
    2121
     22using System;
    2223using System.Collections.Generic;
    23 using System.Threading.Tasks;
     24using System.ComponentModel;
     25using System.Linq;
    2426using System.Windows.Forms;
    25 using HeuristicLab.PluginInfrastructure;
     27using HeuristicLab.Clients.Hive.Views;
     28using HeuristicLab.MainForm;
     29using HeuristicLab.Core;
    2630
    2731namespace HeuristicLab.Clients.Hive.JobManager.Views {
    2832  public partial class HiveResourceSelectorDialog : Form {
    29     public HiveResourceSelectorDialog() {
     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;
    3077      InitializeComponent();
    3178    }
    3279
    33     public ISet<Resource> GetSelectedResources() { return hiveResourceSelector.SelectedResources; }
    34 
    35     private void HiveResourceSelectorDialog_Load(object sender, System.EventArgs e) {
    36       HiveAdminClient.Instance.Refreshed += new System.EventHandler(Instance_Refreshed);
    37       DownloadResources();
     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);
    3885    }
    3986
    40     private void HiveResourceSelectorDialog_FormClosing(object sender, FormClosingEventArgs e) {
    41       HiveAdminClient.Instance.Refreshed -= new System.EventHandler(Instance_Refreshed);
     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        var mainForm = MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>();
     99        mainForm.AddOperationProgressToView(this, "Refreshing ...");
     100        refreshButton.Enabled = false;
     101      }
    42102    }
    43103
    44     void Instance_Refreshed(object sender, System.EventArgs e) {
    45       hiveResourceSelector.Content = HiveAdminClient.Instance.Resources;
     104    private void HiveClient_Instance_Refreshed(object sender, EventArgs e) {
     105      if (InvokeRequired) Invoke((Action<object, EventArgs>)HiveClient_Instance_Refreshed, sender, e);
     106      else {
     107        var mainForm = MainFormManager.GetMainForm<MainForm.WindowsForms.MainForm>();
     108        mainForm.RemoveOperationProgressFromView(this);
     109        refreshButton.Enabled = true;
     110      }
    46111    }
    47112
    48     private void refreshButton_Click(object sender, System.EventArgs e) {
    49       DownloadResources();
     113    private async void HiveResourceSelectorDialog_Load(object sender, EventArgs e) {
     114      lock (locker) {
     115        if (updatingProjects) return;
     116        updatingProjects = true;
     117      }
     118
     119      await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
     120        action: () => UpdateProjects(),
     121        finallyCallback: () => updatingProjects = false);
    50122    }
    51123
    52     private void DownloadResources() {
    53       var task = System.Threading.Tasks.Task.Factory.StartNew(DownloadResourcesAsync);
    54       task.ContinueWith(t => {
    55         hiveResourceSelector.FinishProgressView();
    56         ErrorHandling.ShowErrorDialog(this, "An error occurred while downloading the tasks.", t.Exception);
    57       }, TaskContinuationOptions.OnlyOnFaulted);
     124    private async void refreshButton_Click(object sender, EventArgs e) {
     125      lock (locker) {
     126        if (updatingProjects) return;
     127        updatingProjects = true;
     128      }
     129
     130      await SecurityExceptionUtil.TryAsyncAndReportSecurityExceptions(
     131        action: () => UpdateProjects(),
     132        finallyCallback: () => updatingProjects = false);
    58133    }
    59134
    60     private void DownloadResourcesAsync() {
    61       hiveResourceSelector.StartProgressView();
    62       HiveAdminClient.Instance.Refresh();
    63       hiveResourceSelector.FinishProgressView();
     135    private void hiveResourceSelector_SelectedProjectChanged(object sender, EventArgs e) {
     136      okButton.Enabled = hiveResourceSelector.SelectedProject != null && hiveResourceSelector.AssignedResources.Any();
    64137    }
     138
     139    private void hiveResourceSelector_SelectedResourcesChanged(object sender, EventArgs e) {
     140      okButton.Enabled = hiveResourceSelector.AssignedResources.Any();
     141
     142      if(!hiveResourceSelector.AssignedResources.Any()) {
     143        errorProvider.SetError(okButton, "Note: currently no resources are assigned");
     144      } else if(hiveResourceSelector.AssignedCores == 0) {
     145        errorProvider.SetError(okButton, "Note: currently no resources with cores are assigned");
     146      } else {
     147        errorProvider.SetError(okButton, string.Empty);
     148      }
     149    }
     150
     151    private void hiveResourceSelector_ProjectsTreeViewDoubleClicked(object sender, EventArgs e) {
     152      if (hiveResourceSelector.SelectedProject == null) return;
     153      if (!hiveResourceSelector.AssignedResources.Any()) return;
     154
     155      DialogResult = DialogResult.OK;
     156      Close();
     157    }
     158    #endregion
     159
     160    #region Helpers
     161    private void UpdateProjects() {
     162      HiveClient.Instance.RefreshProjectsAndResources();
     163      hiveResourceSelector.JobId = jobId;
     164      hiveResourceSelector.ProjectId = projectId;
     165      hiveResourceSelector.SelectedProjectId = selectedProjectId;
     166      hiveResourceSelector.SelectedResourceIds = selectedResourceIds;
     167      hiveResourceSelector.Content = HiveClient.Instance.Projects;
     168    }
     169
     170    private void ShowHiveInformationDialog() {
     171      if (InvokeRequired) Invoke((Action)ShowHiveInformationDialog);
     172      else {
     173        using (HiveInformationDialog dialog = new HiveInformationDialog()) {
     174          dialog.ShowDialog(this);
     175        }
     176      }
     177    }
     178    #endregion
    65179  }
    66180}
Note: See TracChangeset for help on using the changeset viewer.