Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Clients.Hive.JobManager/3.3/Views/HiveJobManagerView.cs @ 17317

Last change on this file since 17317 was 17267, checked in by mkommend, 5 years ago

#3018: Refactored condition for clean up of hive client.

File size: 5.6 KB
RevLine 
[6976]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[6976]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.Linq;
24using System.ServiceModel.Security;
25using System.Windows.Forms;
[7249]26using HeuristicLab.Clients.Hive.Views;
[6976]27using HeuristicLab.MainForm;
28using HeuristicLab.MainForm.WindowsForms;
29using HeuristicLab.PluginInfrastructure;
30
31namespace HeuristicLab.Clients.Hive.JobManager.Views {
32  /// <summary>
33  /// The base class for visual representations of items.
34  /// </summary>
35  [View("Hive Job Manager")]
36  [Content(typeof(HiveClient), true)]
37  public partial class HiveJobManagerView : AsynchronousContentView {
38
39    public new HiveClient Content {
40      get { return (HiveClient)base.Content; }
41      set { base.Content = value; }
42    }
43
44    /// <summary>
45    /// Initializes a new instance of <see cref="ItemBaseView"/>.
46    /// </summary>
47    public HiveJobManagerView() {
48      InitializeComponent();
49    }
50
51    protected override void RegisterContentEvents() {
52      base.RegisterContentEvents();
53      Content.Refreshing += new EventHandler(Content_Refreshing);
54      Content.Refreshed += new EventHandler(Content_Refreshed);
55    }
56
57    protected override void DeregisterContentEvents() {
58      Content.Refreshing -= new EventHandler(Content_Refreshing);
59      Content.Refreshed -= new EventHandler(Content_Refreshed);
60      base.DeregisterContentEvents();
61    }
62
[15419]63    protected async override void OnContentChanged() {
[6976]64      base.OnContentChanged();
65      if (Content == null) {
66        hiveExperimentListView.Content = null;
67      } else {
68        hiveExperimentListView.Content = Content.Jobs;
[15478]69        try {
70          await System.Threading.Tasks.Task.Run(() => Content.Refresh());
71        } catch (Exception ex) {
72          HandleServiceException(ex);
[15419]73        }
[6976]74      }
75    }
76
77    protected override void SetEnabledStateOfControls() {
78      base.SetEnabledStateOfControls();
79      refreshButton.Enabled = Content != null;
80      hiveExperimentListView.Enabled = Content != null;
81    }
82
83    private void Content_Refreshing(object sender, EventArgs e) {
84      if (InvokeRequired) {
85        Invoke(new EventHandler(Content_Refreshing), sender, e);
86      } else {
87        Cursor = Cursors.AppStarting;
88        refreshButton.Enabled = false;
89        hiveExperimentListView.Enabled = false;
90      }
91    }
92    private void Content_Refreshed(object sender, EventArgs e) {
93      if (InvokeRequired) {
94        Invoke(new EventHandler(Content_Refreshed), sender, e);
95      } else {
96        hiveExperimentListView.Content = Content.Jobs;
97        refreshButton.Enabled = true;
98        hiveExperimentListView.Enabled = true;
99        Cursor = Cursors.Default;
100      }
101    }
102
[15419]103    private async void refreshButton_Click(object sender, EventArgs e) {
104      try {
105        await System.Threading.Tasks.Task.Run(() => Content.Refresh());
106      } catch (Exception ex) {
107        HandleServiceException(ex);
108      }
[6976]109    }
110
111    private void HandleServiceException(Exception ex) {
[7256]112      if (this.InvokeRequired) {
113        Invoke(new Action<Exception>(HandleServiceException), ex);
[6976]114      } else {
[7256]115        if (ex is MessageSecurityException) {
[13060]116          string message =
117            "A Message Security error has occured. This normally means that your user name or password is wrong. Detailed information: " + Environment.NewLine;
118          message += HiveServiceLocator.Instance.GetEndpointInformation();
119          ErrorHandling.ShowErrorDialog(this, message, ex);
[7256]120        } else if (ex is AnonymousUserException) {
121          using (HiveInformationDialog dialog = new HiveInformationDialog()) {
122            dialog.ShowDialog(this);
123          }
124        } else {
[13060]125          ErrorHandling.ShowErrorDialog(this,
126            "Refresh failed. Detailed information: " + Environment.NewLine + HiveServiceLocator.Instance.GetEndpointInformation(), ex);
[7256]127        }
[6976]128      }
129    }
130
131    protected override void OnClosing(FormClosingEventArgs e) {
[9442]132      if (Content != null && Content.Jobs != null && Content.Jobs.Any(x => x.IsProgressing)) {
[9222]133        DialogResult result = MessageBox.Show("There are still unfinished down/uploads. Are you sure you want to close the window?", "HeuristicLab Hive Job Manager", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
134
135        if (result == DialogResult.No) {
136          e.Cancel = true;
137        }
[9219]138      } else {
139        base.OnClosing(e);
[6976]140      }
141    }
[9230]142
143    protected override void OnClosed(FormClosedEventArgs e) {
[9442]144      if (Content != null && Content.Jobs != null) {
[17221]145        // clear hive client only if it is not displayed by any other content view (i.e. job manager)
[17267]146        var contentViews = MainFormManager.MainForm.Views.OfType<IContentView>()
147          .Where(v => v.Content == Content && v != this);
148        if (!contentViews.Any())
[17221]149          Content.ClearHiveClient();
150
[9230]151        Content = null;
152      }
153      base.OnClosed(e);
154    }
[6976]155  }
156}
Note: See TracBrowser for help on using the repository browser.