source: branches/UnloadJobs/HeuristicLab.Clients.Hive.JobManager/3.3/Views/HiveJobManagerView.cs @ 9169

Last change on this file since 9169 was 9169, checked in by ascheibe, 9 years ago

#2005

  • renamed some events and methods
  • fixed some memory leaks
File size: 5.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Linq;
24using System.ServiceModel.Security;
25using System.Windows.Forms;
26using HeuristicLab.Clients.Hive.Views;
27using HeuristicLab.Collections;
28using HeuristicLab.MainForm;
29using HeuristicLab.MainForm.WindowsForms;
30using HeuristicLab.PluginInfrastructure;
31
32namespace HeuristicLab.Clients.Hive.JobManager.Views {
33  /// <summary>
34  /// The base class for visual representations of items.
35  /// </summary>
36  [View("Hive Job Manager")]
37  [Content(typeof(HiveClient), true)]
38  public partial class HiveJobManagerView : AsynchronousContentView {
39
40    public new HiveClient Content {
41      get { return (HiveClient)base.Content; }
42      set { base.Content = value; }
43    }
44
45    /// <summary>
46    /// Initializes a new instance of <see cref="ItemBaseView"/>.
47    /// </summary>
48    public HiveJobManagerView() {
49      InitializeComponent();
50    }
51
52    protected override void RegisterContentEvents() {
53      base.RegisterContentEvents();
54      Content.Refreshing += new EventHandler(Content_Refreshing);
55      Content.Refreshed += new EventHandler(Content_Refreshed);
56      Content.HiveJobsChanged += new EventHandler(Content_HiveJobsChanged);
57
58    }
59
60    protected override void DeregisterContentEvents() {
61      Content.Refreshing -= new EventHandler(Content_Refreshing);
62      Content.Refreshed -= new EventHandler(Content_Refreshed);
63      Content.HiveJobsChanged -= new EventHandler(Content_HiveJobsChanged);
64      base.DeregisterContentEvents();
65    }
66
67    protected override void OnContentChanged() {
68      base.OnContentChanged();
69      if (Content == null) {
70        hiveExperimentListView.Content = null;
71      } else {
72        hiveExperimentListView.Content = Content.Jobs;
73        if (Content != null)
74          Content.RefreshAsync(new Action<Exception>((Exception ex) => HandleServiceException(ex)));
75      }
76    }
77
78    protected override void SetEnabledStateOfControls() {
79      base.SetEnabledStateOfControls();
80      refreshButton.Enabled = Content != null;
81      hiveExperimentListView.Enabled = Content != null;
82    }
83
84    private void Content_Refreshing(object sender, EventArgs e) {
85      if (InvokeRequired) {
86        Invoke(new EventHandler(Content_Refreshing), sender, e);
87      } else {
88        Cursor = Cursors.AppStarting;
89        refreshButton.Enabled = false;
90        hiveExperimentListView.Enabled = false;
91      }
92    }
93    private void Content_Refreshed(object sender, EventArgs e) {
94      if (InvokeRequired) {
95        Invoke(new EventHandler(Content_Refreshed), sender, e);
96      } else {
97        hiveExperimentListView.Content = Content.Jobs;
98        refreshButton.Enabled = true;
99        hiveExperimentListView.Enabled = true;
100        Cursor = Cursors.Default;
101      }
102    }
103
104    private void refreshButton_Click(object sender, EventArgs e) {
105      Content.RefreshAsync(new Action<Exception>((Exception ex) => HandleServiceException(ex)));
106    }
107
108    private void HandleServiceException(Exception ex) {
109      if (this.InvokeRequired) {
110        Invoke(new Action<Exception>(HandleServiceException), ex);
111      } else {
112        if (ex is MessageSecurityException) {
113          MessageBox.Show("A Message Security error has occured. This normally means that your user name or password is wrong.", "HeuristicLab Hive Job Manager", MessageBoxButtons.OK, MessageBoxIcon.Error);
114        } else if (ex is AnonymousUserException) {
115          using (HiveInformationDialog dialog = new HiveInformationDialog()) {
116            dialog.ShowDialog(this);
117          }
118        } else {
119          ErrorHandling.ShowErrorDialog(this, "Refresh failed.", ex);
120        }
121      }
122    }
123
124    protected override void OnClosing(FormClosingEventArgs e) {
125      if (Content.Jobs.Any(x => x.IsProgressing)) {
126        MessageBox.Show("The Hive Job Manager can only be closed after all down/uploads are finished. ", "HeuristicLab Hive Job Manager", MessageBoxButtons.OK, MessageBoxIcon.Information);
127        e.Cancel = true;
128      } else {
129        base.OnClosing(e);
130        if (Content != null && Content.Jobs != null) {
131          Content.Jobs.ItemsRemoved -= new CollectionItemsChangedEventHandler<RefreshableJob>(HiveExperiments_ItemsRemoved);
132          Content.ClearHiveClient();
133        }
134      }
135    }
136
137    private void HiveExperiments_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<RefreshableJob> e) {
138      foreach (var item in e.Items) {
139        HiveClient.Delete(item);
140      }
141    }
142
143    private void Content_HiveJobsChanged(object sender, EventArgs e) {
144      if (Content.Jobs != null) {
145        Content.Jobs.ItemsRemoved += new CollectionItemsChangedEventHandler<RefreshableJob>(HiveExperiments_ItemsRemoved);
146      }
147    }
148  }
149}
Note: See TracBrowser for help on using the repository browser.