Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Clients.Access.Views/3.3/UserViews/RefreshableLightweightUserInformationView.cs @ 17181

Last change on this file since 17181 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 4.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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.Windows.Forms;
24using HeuristicLab.MainForm;
25using HeuristicLab.MainForm.WindowsForms;
26
27namespace HeuristicLab.Clients.Access.Views {
28  [View("RefreshableLightweightUserInformation View")]
29  [Content(typeof(LightweightUser), false)]
30  public partial class RefreshableLightweightUserInformationView : AsynchronousContentView {
31    public new LightweightUser Content {
32      get { return (LightweightUser)base.Content; }
33      set { base.Content = value; }
34    }
35
36    public RefreshableLightweightUserInformationView() {
37      InitializeComponent();
38    }
39
40    protected override void OnContentChanged() {
41      base.OnContentChanged();
42      lightweightUserInformationView.Content = Content;
43    }
44
45    protected override void SetEnabledStateOfControls() {
46      base.SetEnabledStateOfControls();
47      this.Locked = true;
48      refreshButton.Enabled = Content != null;
49    }
50
51    protected override void DeregisterContentEvents() {
52      Refreshing -= new EventHandler(Content_Refreshing);
53      Refreshed -= new EventHandler(Content_Refreshed);
54      base.DeregisterContentEvents();
55    }
56
57    protected override void RegisterContentEvents() {
58      base.RegisterContentEvents();
59      Refreshing += new EventHandler(Content_Refreshing);
60      Refreshed += new EventHandler(Content_Refreshed);
61    }
62
63    protected void Content_Refreshing(object sender, EventArgs e) {
64      if (InvokeRequired) {
65        Invoke(new EventHandler(Content_Refreshing), sender, e);
66      } else {
67        Cursor = Cursors.AppStarting;
68        refreshButton.Enabled = false;
69
70        lightweightUserInformationView.Enabled = false;
71      }
72    }
73
74    protected void Content_Refreshed(object sender, EventArgs e) {
75      if (InvokeRequired) {
76        Invoke(new EventHandler(Content_Refreshed), sender, e);
77      } else {
78        Cursor = Cursors.Default;
79        refreshButton.Enabled = true;
80
81        lightweightUserInformationView.Enabled = true;
82        if (!UserInformation.Instance.UserExists) {
83          MessageBox.Show("Couldn't fetch user information from the server." + Environment.NewLine +
84            "Please verify that you have an existing user and that your user name and password is correct. ", "HeuristicLab Access Service", MessageBoxButtons.OK, MessageBoxIcon.Error);
85          lightweightUserInformationView.Content = null;
86        } else {
87          lightweightUserInformationView.Content = Content;
88        }
89      }
90    }
91
92    void lightweightUserInformationView_Changed(object sender, EventArgs e) {
93      // nothing to do
94    }
95
96    private void RefreshUserData() {
97      UserInformation.Instance.Refresh();
98      lightweightUserInformationView.UserInformationChanged += new EventHandler(lightweightUserInformationView_Changed);
99    }
100
101    private void RefreshData() {
102      ExecuteActionAsync(RefreshUserData, PluginInfrastructure.ErrorHandling.ShowErrorDialog);
103    }
104
105    public void ManualRefresh() {
106      RefreshData();
107    }
108
109    protected void refreshButton_Click(object sender, System.EventArgs e) {
110      RefreshData();
111    }
112
113    public void ExecuteActionAsync(Action action, Action<Exception> exceptionCallback) {
114      var call = new Func<Exception>(delegate () {
115        try {
116          OnRefreshing();
117          action();
118        } catch (Exception ex) {
119          return ex;
120        } finally {
121          OnRefreshed();
122        }
123        return null;
124      });
125      call.BeginInvoke(delegate (IAsyncResult result) {
126        Exception ex = call.EndInvoke(result);
127        if (ex != null) exceptionCallback(ex);
128      }, null);
129    }
130
131    #region Events
132    public event EventHandler Refreshing;
133    private void OnRefreshing() {
134      EventHandler handler = Refreshing;
135      if (handler != null) handler(this, EventArgs.Empty);
136    }
137    public event EventHandler Refreshed;
138    private void OnRefreshed() {
139      EventHandler handler = Refreshed;
140      if (handler != null) handler(this, EventArgs.Empty);
141    }
142    #endregion
143  }
144}
Note: See TracBrowser for help on using the repository browser.