Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3262 was 3208, checked in by gkronber, 15 years ago

Implemented changes in plugin administrator UI as requested by swagner. #949 (Overhaul look and feel of plugin administrator)

File size: 6.0 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.PluginAdministrator {
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.Width = -1;
77        listView.SuppressItemCheckedEvents = false;
78      }
79    }
80
81    private ListViewItem CreateListViewItem(IPluginDescription plugin) {
82      var item = new ListViewItem(new string[] { plugin.Name, plugin.Version.ToString() });
83      item.Checked = checkedPlugins.ContainsKey(plugin) ? checkedPlugins[plugin] : false;
84      item.Tag = plugin;
85      return item;
86    }
87
88    private void listView_ItemChecked(object sender, ItemCheckedEventArgs e) {
89      List<IPluginDescription> modifiedPlugins = new List<IPluginDescription>();
90      if (e.Item.Checked) {
91        foreach (ListViewItem item in listView.SelectedItems) {
92          var plugin = (IPluginDescription)item.Tag;
93          // also check all dependencies
94          MarkPluginChecked(plugin);
95          if (!modifiedPlugins.Contains(plugin))
96            modifiedPlugins.Add(plugin);
97          foreach (var dep in GetAllDependencies(plugin)) {
98            MarkPluginChecked(dep);
99            if (!modifiedPlugins.Contains(dep))
100              modifiedPlugins.Add(dep);
101          }
102        }
103        listView.CheckItems(modifiedPlugins.Select(x => FindItemsForPlugin(x).Single()));
104        OnItemChecked(e);
105      } else {
106        foreach (ListViewItem item in listView.SelectedItems) {
107          var plugin = (IPluginDescription)item.Tag;
108          // also uncheck all dependent plugins
109          MarkPluginUnchecked(plugin);
110          if (!modifiedPlugins.Contains(plugin))
111            modifiedPlugins.Add(plugin);
112          foreach (var dep in GetAllDependents(plugin)) {
113            MarkPluginUnchecked(dep);
114            if (!modifiedPlugins.Contains(dep))
115              modifiedPlugins.Add(dep);
116          }
117
118        }
119        listView.UncheckItems(modifiedPlugins.Select(x => FindItemsForPlugin(x).Single()));
120        OnItemChecked(e);
121      }
122    }
123
124    private void MarkPluginChecked(IPluginDescription plugin) {
125      checkedPlugins[plugin] = true;
126    }
127
128    private void MarkPluginUnchecked(IPluginDescription plugin) {
129      checkedPlugins[plugin] = false;
130    }
131
132    private IEnumerable<ListViewItem> FindItemsForPlugin(IPluginDescription plugin) {
133      return from item in listView.Items.OfType<ListViewItem>()
134             let p = item.Tag as IPluginDescription
135             where p.Name == plugin.Name
136             where p.Version == plugin.Version
137             select item;
138    }
139
140    private IEnumerable<IPluginDescription> GetAllDependents(IPluginDescription plugin) {
141      return from p in plugins
142             let matchingEntries = from dep in GetAllDependencies(p)
143                                   where dep.Name == plugin.Name
144                                   where dep.Version == plugin.Version
145                                   select dep
146             where matchingEntries.Any()
147             select p;
148    }
149
150    private IEnumerable<IPluginDescription> GetAllDependencies(IPluginDescription plugin) {
151      HashSet<IPluginDescription> yieldedPlugins = new HashSet<IPluginDescription>();
152      foreach (var dep in plugin.Dependencies) {
153        foreach (var recDep in GetAllDependencies(dep)) {
154          if (!yieldedPlugins.Contains(recDep)) {
155            yieldedPlugins.Add(recDep);
156            yield return recDep;
157          }
158        }
159        if (!yieldedPlugins.Contains(dep)) {
160          yieldedPlugins.Add(dep);
161          yield return dep;
162        }
163      }
164    }
165
166    private void OnItemChecked(ItemCheckedEventArgs e) {
167      if (ItemChecked != null) ItemChecked(this, e);
168    }
169  }
170}
Note: See TracBrowser for help on using the repository browser.