Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Clients.Access.Views/3.3/UserViews/RefreshableLightweightUserInformationView.cs @ 16186

Last change on this file since 16186 was 16186, checked in by jzenisek, 4 years ago

#2839 fixed lightweightUserInformationView

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