Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.PluginInfrastructure/Advanced/PluginListView.cs @ 3600

Last change on this file since 3600 was 3600, checked in by gkronber, 14 years ago

Implemented reviewer comments in plugin infrastructure. #989 (Implement review comments in plugin infrastructure)

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