Free cookie consent management tool by TermsFeed Policy Generator

source: tags/3.3.7/HeuristicLab.Clients.Access.Views/3.3/UserViews/RefreshableLightweightUserView.cs @ 8418

Last change on this file since 8418 was 8141, checked in by jkarder, 12 years ago

#1648: fixed Control.Enabled state handling

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