Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.PluginAdministrator/3.3/PluginListView.cs @ 3188

Last change on this file since 3188 was 3179, checked in by gkronber, 15 years ago

Improved controls for deployment service interaction.
Increased max values for message sizes and related limits in the deployment service configuration.
Recreated proxy classes for the deployment service.

#891 (Refactor GUI for plugin management)

File size: 5.9 KB
RevLine 
[3081]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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;
[2802]23using System.Collections.Generic;
24using System.ComponentModel;
25using System.Drawing;
26using System.Data;
27using System.Linq;
28using System.Text;
29using System.Windows.Forms;
30using HeuristicLab.PluginInfrastructure;
31
[3081]32namespace HeuristicLab.PluginAdministrator {
[3068]33  /// <summary>
34  /// Wraps a ListView and adds functionality to automatically check and uncheck dependencies of plugins.
35  /// </summary>
[3081]36  internal partial class PluginListView : UserControl {
[3045]37    public event ItemCheckedEventHandler ItemChecked;
[2802]38
[3045]39    private IEnumerable<IPluginDescription> plugins;
40    public IEnumerable<IPluginDescription> Plugins {
41      get { return plugins; }
42      set {
43        plugins = value;
[3068]44        checkedPlugins.Clear();
[3045]45        UpdateControls();
46      }
[2802]47    }
48
[3179]49    private Dictionary<IPluginDescription, bool> checkedPlugins = new Dictionary<IPluginDescription, bool>();
[3045]50    public IEnumerable<IPluginDescription> CheckedPlugins {
51      get {
[3179]52        return from pair in checkedPlugins
53               where pair.Value
54               select pair.Key;
[2816]55      }
56    }
57
[3045]58    public PluginListView() {
59      InitializeComponent();
[2816]60    }
61
[3045]62    public void CheckPlugin(IPluginDescription plugin) {
[3068]63      MarkPluginChecked(plugin);
64      listView.CheckItems(new IPluginDescription[] { plugin }.Select(x => FindItemsForPlugin(x).Single()));
[2802]65    }
66
[3068]67
[3045]68    private void UpdateControls() {
69      if (plugins != null) {
70        listView.Items.Clear();
[3068]71        listView.SuppressItemCheckedEvents = true;
[3045]72        foreach (var plugin in plugins) {
73          listView.Items.Add(CreateListViewItem(plugin));
[2816]74        }
[3068]75        listView.SuppressItemCheckedEvents = false;
[2802]76      }
77    }
78
[3045]79    private ListViewItem CreateListViewItem(IPluginDescription plugin) {
80      var item = new ListViewItem(new string[] { plugin.Name, plugin.Version.ToString() });
[3179]81      item.Checked = checkedPlugins.ContainsKey(plugin) ? checkedPlugins[plugin] : false;
[3045]82      item.Tag = plugin;
83      return item;
[2860]84    }
85
86    private void listView_ItemChecked(object sender, ItemCheckedEventArgs e) {
[3068]87      List<IPluginDescription> modifiedPlugins = new List<IPluginDescription>();
[2860]88      if (e.Item.Checked) {
[3068]89        foreach (ListViewItem item in listView.SelectedItems) {
90          var plugin = (IPluginDescription)item.Tag;
91          // also check all dependencies
92          MarkPluginChecked(plugin);
[3179]93          if (!modifiedPlugins.Contains(plugin))
94            modifiedPlugins.Add(plugin);
[3068]95          foreach (var dep in GetAllDependencies(plugin)) {
96            MarkPluginChecked(dep);
[3179]97            if (!modifiedPlugins.Contains(dep))
98              modifiedPlugins.Add(dep);
[3068]99          }
[2860]100        }
[3068]101        listView.CheckItems(modifiedPlugins.Select(x => FindItemsForPlugin(x).Single()));
102        OnItemChecked(e);
[2860]103      } else {
[3068]104        foreach (ListViewItem item in listView.SelectedItems) {
105          var plugin = (IPluginDescription)item.Tag;
106          // also uncheck all dependent plugins
107          MarkPluginUnchecked(plugin);
[3179]108          if (!modifiedPlugins.Contains(plugin))
109            modifiedPlugins.Add(plugin);
[3068]110          foreach (var dep in GetAllDependents(plugin)) {
111            MarkPluginUnchecked(dep);
[3179]112            if (!modifiedPlugins.Contains(dep))
113              modifiedPlugins.Add(dep);
[3068]114          }
115
[2860]116        }
[3068]117        listView.UncheckItems(modifiedPlugins.Select(x => FindItemsForPlugin(x).Single()));
118        OnItemChecked(e);
[2860]119      }
120    }
121
[3068]122    private void MarkPluginChecked(IPluginDescription plugin) {
[3179]123      checkedPlugins[plugin] = true;
[3068]124    }
125
126    private void MarkPluginUnchecked(IPluginDescription plugin) {
[3179]127      checkedPlugins[plugin] = false;
[3068]128    }
129
[3045]130    private IEnumerable<ListViewItem> FindItemsForPlugin(IPluginDescription plugin) {
131      return from item in listView.Items.OfType<ListViewItem>()
132             let p = item.Tag as IPluginDescription
133             where p.Name == plugin.Name
134             where p.Version == plugin.Version
135             select item;
[2816]136    }
137
[3045]138    private IEnumerable<IPluginDescription> GetAllDependents(IPluginDescription plugin) {
139      return from p in plugins
140             let matchingEntries = from dep in GetAllDependencies(p)
141                                   where dep.Name == plugin.Name
142                                   where dep.Version == plugin.Version
143                                   select dep
144             where matchingEntries.Any()
145             select p;
[2802]146    }
147
[3045]148    private IEnumerable<IPluginDescription> GetAllDependencies(IPluginDescription plugin) {
[3179]149      HashSet<IPluginDescription> yieldedPlugins = new HashSet<IPluginDescription>();
[3045]150      foreach (var dep in plugin.Dependencies) {
151        foreach (var recDep in GetAllDependencies(dep)) {
[3179]152          if (!yieldedPlugins.Contains(recDep)) {
153            yieldedPlugins.Add(recDep);
154            yield return recDep;
155          }
[2860]156        }
[3179]157        if (!yieldedPlugins.Contains(dep)) {
158          yieldedPlugins.Add(dep);
159          yield return dep;
160        }
[2802]161      }
162    }
[2816]163
[3045]164    private void OnItemChecked(ItemCheckedEventArgs e) {
[3068]165      if (ItemChecked != null) ItemChecked(this, e);
[2816]166    }
[2802]167  }
168}
Note: See TracBrowser for help on using the repository browser.