#region License Information
/* HeuristicLab
* Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using HeuristicLab.Core;
using HeuristicLab.MainForm;
using HeuristicLab.MainForm.WindowsForms;
using HeuristicLab.PluginInfrastructure;
namespace HeuristicLab.Clients.Access.Views {
[View("RefreshableLightweightUser View")]
[Content(typeof(AccessClient), false)]
public partial class RefreshableLightweightUserView : RefreshableView {
public RefreshableLightweightUserView() {
InitializeComponent();
}
//set an action like a webservice call to retrieve the users which should be marked as checked
private Func> fetchSelectedUsers;
public Func> FetchSelectedUsers {
get { return fetchSelectedUsers; }
set {
fetchSelectedUsers = value;
selectedUsers = null;
lightweightUserView.Content = null;
SetEnabledStateOfControls();
}
}
private List selectedUsers;
protected override void OnContentChanged() {
base.OnContentChanged();
selectedUsers = null;
lightweightUserView.Content = null;
}
protected override void RefreshData() {
Action completeRefreshAction = new Action(delegate {
selectedUsers = FetchSelectedUsers();
Content.Refresh();
});
Content.ExecuteActionAsync(completeRefreshAction, new Action((Exception ex) => ErrorHandling.ShowErrorDialog(this, "Refresh failed.", ex)));
}
protected override void Content_Refreshing(object sender, EventArgs e) {
base.Content_Refreshing(sender, e);
lightweightUserView.Content = null;
}
protected override void Content_Refreshed(object sender, EventArgs e) {
base.Content_Refreshed(sender, e);
if (Content.UsersAndGroups != null) {
lightweightUserView.Content = new ItemList(Content.UsersAndGroups.Where(x => selectedUsers.Contains(x.Id)));
if (lightweightUserView.Content != null) OnStorableStateChanged();
}
}
protected override void SetEnabledStateOfControls() {
base.SetEnabledStateOfControls();
refreshButton.Enabled = FetchSelectedUsers != null && Content != null;
}
public IItemList GetDeletedUsers() {
if (lightweightUserView.Content == null) {
return null;
} else {
var deletedIds = selectedUsers.Where(x => lightweightUserView.Content.Count(y => y.Id == x) == 0);
List deletedUGs = Content.UsersAndGroups.Where(x => deletedIds.Contains(x.Id)).ToList();
return new ItemList(deletedUGs);
}
}
public IItemList GetCheckedUsers() {
return (lightweightUserView.Content == null) ? null : lightweightUserView.Content;
}
public IItemList GetAddedUsers() {
return (lightweightUserView.Content == null) ? null : new ItemList(lightweightUserView.Content.Where(x => !selectedUsers.Contains(x.Id)));
}
public event EventHandler SelectedUsersChanged;
protected virtual void OnSelectedUsersChanged() {
if (InvokeRequired)
Invoke((MethodInvoker)OnSelectedUsersChanged);
else {
EventHandler handler = SelectedUsersChanged;
if (handler != null)
handler(this, EventArgs.Empty);
}
}
public event EventHandler StorableStateChanged;
protected virtual void OnStorableStateChanged() {
if (InvokeRequired)
Invoke((MethodInvoker)OnStorableStateChanged);
else {
EventHandler handler = StorableStateChanged;
if (handler != null)
handler(this, EventArgs.Empty);
}
}
private void lightweightUserView_SelectedUsersChanged(object sender, EventArgs e) {
OnSelectedUsersChanged();
}
}
}