Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2870_AutoDiff-nuget/HeuristicLab.Clients.Access.Views/3.3/UserViews/RefreshableLightweightUserView.cs @ 18078

Last change on this file since 18078 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 4.7 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.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("RefreshableLightweightUser View")]
33  [Content(typeof(AccessClient), false)]
34  public partial class RefreshableLightweightUserView : RefreshableView {
35    public RefreshableLightweightUserView() {
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 RefreshData() {
61      Action completeRefreshAction = new Action(delegate {
62        selectedUsers = FetchSelectedUsers();
63        Content.Refresh();
64      });
65
66      Content.ExecuteActionAsync(completeRefreshAction, new Action<Exception>((Exception ex) => ErrorHandling.ShowErrorDialog(this, "Refresh failed.", ex)));
67    }
68
69    protected override void Content_Refreshing(object sender, EventArgs e) {
70      base.Content_Refreshing(sender, e);
71      lightweightUserView.Content = null;
72    }
73
74    protected override void Content_Refreshed(object sender, EventArgs e) {
75      base.Content_Refreshed(sender, e);
76
77      if (Content.UsersAndGroups != null) {
78        lightweightUserView.Content = new ItemList<UserGroupBase>(Content.UsersAndGroups.Where(x => selectedUsers.Contains(x.Id)));
79        if (lightweightUserView.Content != null) OnStorableStateChanged();
80      }
81    }
82
83    protected override void SetEnabledStateOfControls() {
84      base.SetEnabledStateOfControls();
85      refreshButton.Enabled = FetchSelectedUsers != null && Content != null;
86    }
87
88    public IItemList<UserGroupBase> GetDeletedUsers() {
89      if (lightweightUserView.Content == null) {
90        return null;
91      } else {
92        var deletedIds = selectedUsers.Where(x => lightweightUserView.Content.Count(y => y.Id == x) == 0);
93        List<UserGroupBase> deletedUGs = Content.UsersAndGroups.Where(x => deletedIds.Contains(x.Id)).ToList();
94        return new ItemList<UserGroupBase>(deletedUGs);
95      }
96    }
97
98    public IItemList<UserGroupBase> GetCheckedUsers() {
99      return (lightweightUserView.Content == null) ? null : lightweightUserView.Content;
100    }
101
102    public IItemList<UserGroupBase> GetAddedUsers() {
103      return (lightweightUserView.Content == null) ? null : new ItemList<UserGroupBase>(lightweightUserView.Content.Where(x => !selectedUsers.Contains(x.Id)));
104    }
105
106    public event EventHandler SelectedUsersChanged;
107    protected virtual void OnSelectedUsersChanged() {
108      if (InvokeRequired)
109        Invoke((MethodInvoker)OnSelectedUsersChanged);
110      else {
111        EventHandler handler = SelectedUsersChanged;
112        if (handler != null)
113          handler(this, EventArgs.Empty);
114      }
115    }
116
117    public event EventHandler StorableStateChanged;
118    protected virtual void OnStorableStateChanged() {
119      if (InvokeRequired)
120        Invoke((MethodInvoker)OnStorableStateChanged);
121      else {
122        EventHandler handler = StorableStateChanged;
123        if (handler != null)
124          handler(this, EventArgs.Empty);
125      }
126    }
127
128    private void lightweightUserView_SelectedUsersChanged(object sender, EventArgs e) {
129      OnSelectedUsersChanged();
130    }
131  }
132}
Note: See TracBrowser for help on using the repository browser.