source: branches/ClientUserManagement/HeuristicLab.Clients.Access.Views/3.3/UserViews/RefreshableLightweightUserView.cs @ 7931

Last change on this file since 7931 was 7931, checked in by jkarder, 9 years ago

#1648:

  • fixed refreshButton.Enabled state check
  • set accessor of FetchSelectedUsers property now calls SetEnabledStateOfControls
File size: 4.2 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.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      lightweightUserView.Content = new ItemList<UserGroupBase>(Content.UsersAndGroups.Where(x => selectedUsers.Contains(x.Id)));
78    }
79
80    protected override void SetEnabledStateOfControls() {
81      base.SetEnabledStateOfControls();
82      refreshButton.Enabled = FetchSelectedUsers != null && Content != null;
83    }
84
85    public IItemList<UserGroupBase> GetDeletedUsers() {
86      if (lightweightUserView.Content == null) {
87        return null;
88      } else {
89        var deletedIds = selectedUsers.Where(x => lightweightUserView.Content.Count(y => y.Id == x) == 0);
90        List<UserGroupBase> deletedUGs = Content.UsersAndGroups.Where(x => deletedIds.Contains(x.Id)).ToList();
91        return new ItemList<UserGroupBase>(deletedUGs);
92      }
93    }
94
95    public IItemList<UserGroupBase> GetCheckedUsers() {
96      return (lightweightUserView.Content == null) ? null : lightweightUserView.Content;
97    }
98
99    public IItemList<UserGroupBase> GetAddedUsers() {
100      return (lightweightUserView.Content == null) ? null : new ItemList<UserGroupBase>(lightweightUserView.Content.Where(x => !selectedUsers.Contains(x.Id)));
101    }
102
103    public event EventHandler SelectedUsersChanged;
104    protected virtual void OnSelectedUsersChanged() {
105      if (InvokeRequired)
106        Invoke((MethodInvoker)OnSelectedUsersChanged);
107      else {
108        EventHandler handler = SelectedUsersChanged;
109        if (handler != null)
110          handler(this, EventArgs.Empty);
111      }
112    }
113
114    private void lightweightUserView_SelectedUsersChanged(object sender, EventArgs e) {
115      OnSelectedUsersChanged();
116    }
117  }
118}
Note: See TracBrowser for help on using the repository browser.