Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2994-AutoDiffForIntervals/HeuristicLab.Clients.Access.Views/3.3/UserViews/RefreshableLightweightAccessClientView.cs @ 16671

Last change on this file since 16671 was 16565, checked in by gkronber, 6 years ago

#2520: merged changes from PersistenceOverhaul branch (r16451:16564) into trunk

File size: 4.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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.Collections.Generic;
24using System.Linq;
25using System.Windows.Forms;
26using HeuristicLab.Core;
27using HeuristicLab.MainForm;
28using HeuristicLab.MainForm.WindowsForms;
29using HeuristicLab.PluginInfrastructure;
30
31namespace HeuristicLab.Clients.Access.Views {
32  [View("RefreshableLightweightAccessClient View")]
33  [Content(typeof(AccessClient), false)]
34  public partial class RefreshableLightweightAccessClientView : RefreshableView {
35    public RefreshableLightweightAccessClientView() {
36      InitializeComponent();
37    }
38
39    //set an action like a webservice call to retrieve the users which should be marked as checked           
40    private Func<List<Guid>> fetchSelectedUsers;
41    public Func<List<Guid>> FetchSelectedUsers {
42      get { return fetchSelectedUsers; }
43      set {
44        fetchSelectedUsers = value;
45        selectedUsers = null;
46        lightweightUserView.Content = null;
47        SetEnabledStateOfControls();
48      }
49    }
50
51    private List<Guid> selectedUsers;
52
53    protected override void OnContentChanged() {
54      base.OnContentChanged();
55
56      selectedUsers = null;
57      lightweightUserView.Content = null;
58    }
59
60    protected override void SetEnabledStateOfControls() {
61      base.SetEnabledStateOfControls();
62      refreshButton.Enabled = FetchSelectedUsers != null && Content != null;
63      lightweightUserView.Locked = Locked;
64    }
65
66    protected override void RefreshData() {
67      Action completeRefreshAction = new Action(delegate {
68        selectedUsers = FetchSelectedUsers();
69        Content.Refresh();
70      });
71
72      Content.ExecuteActionAsync(completeRefreshAction, new Action<Exception>((Exception ex) => ErrorHandling.ShowErrorDialog(this, "Refresh failed.", ex)));
73    }
74
75    protected override void Content_Refreshing(object sender, EventArgs e) {
76      base.Content_Refreshing(sender, e);
77      lightweightUserView.Content = null;
78    }
79
80    protected override void Content_Refreshed(object sender, EventArgs e) {
81      base.Content_Refreshed(sender, e);
82
83      if (Content.UsersAndGroups != null) {
84        lightweightUserView.Content = new ItemList<UserGroupBase>(Content.UsersAndGroups.Where(x => selectedUsers.Contains(x.Id)));
85        if (lightweightUserView.Content != null) OnStorableStateChanged();
86      }
87    }
88
89    public IItemList<UserGroupBase> GetDeletedUsers() {
90      if (lightweightUserView.Content == null) {
91        return null;
92      } else {
93        var deletedIds = selectedUsers.Where(x => lightweightUserView.Content.Count(y => y.Id == x) == 0);
94        List<UserGroupBase> deletedUGs = Content.UsersAndGroups.Where(x => deletedIds.Contains(x.Id)).ToList();
95        return new ItemList<UserGroupBase>(deletedUGs);
96      }
97    }
98
99    public IItemList<UserGroupBase> GetCheckedUsers() {
100      return (lightweightUserView.Content == null) ? null : lightweightUserView.Content;
101    }
102
103    public IItemList<UserGroupBase> GetAddedUsers() {
104      return (lightweightUserView.Content == null) ? null : new ItemList<UserGroupBase>(lightweightUserView.Content.Where(x => !selectedUsers.Contains(x.Id)));
105    }
106
107    public event EventHandler SelectedUsersChanged;
108    protected virtual void OnSelectedUsersChanged() {
109      if (InvokeRequired)
110        Invoke((MethodInvoker)OnSelectedUsersChanged);
111      else {
112        EventHandler handler = SelectedUsersChanged;
113        if (handler != null)
114          handler(this, EventArgs.Empty);
115      }
116    }
117
118    public event EventHandler StorableStateChanged;
119    protected virtual void OnStorableStateChanged() {
120      if (InvokeRequired)
121        Invoke((MethodInvoker)OnStorableStateChanged);
122      else {
123        EventHandler handler = StorableStateChanged;
124        if (handler != null)
125          handler(this, EventArgs.Empty);
126      }
127    }
128
129    private void lightweightUserView_SelectedUsersChanged(object sender, EventArgs e) {
130      OnSelectedUsersChanged();
131    }
132  }
133}
Note: See TracBrowser for help on using the repository browser.