Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3083 was 3081, checked in by gkronber, 15 years ago

Integrated PluginAdministrator plugin into HL3.3 solution. #918 (Integrate deployment service into trunk and HL3.3 solution file)

File size: 5.8 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 List<IPluginDescription> checkedPlugins = new List<IPluginDescription>();
50    public IEnumerable<IPluginDescription> CheckedPlugins {
51      get {
52        return checkedPlugins;
53      }
54    }
55
56    public PluginListView() {
57      InitializeComponent();
58    }
59
60    public void CheckPlugin(IPluginDescription plugin) {
61      MarkPluginChecked(plugin);
62      listView.CheckItems(new IPluginDescription[] { plugin }.Select(x => FindItemsForPlugin(x).Single()));
63    }
64
65
66    private void UpdateControls() {
67      if (plugins != null) {
68        listView.Items.Clear();
69        listView.SuppressItemCheckedEvents = true;
70        foreach (var plugin in plugins) {
71          listView.Items.Add(CreateListViewItem(plugin));
72        }
73        listView.SuppressItemCheckedEvents = false;
74      }
75    }
76
77    private ListViewItem CreateListViewItem(IPluginDescription plugin) {
78      var item = new ListViewItem(new string[] { plugin.Name, plugin.Version.ToString() });
79      item.Checked = (from p in checkedPlugins where p.Name == plugin.Name where p.Version == plugin.Version select p).Any();
80      item.Tag = plugin;
81      return item;
82    }
83
84    private void listView_ItemChecked(object sender, ItemCheckedEventArgs e) {
85      List<IPluginDescription> modifiedPlugins = new List<IPluginDescription>();
86      if (e.Item.Checked) {
87        foreach (ListViewItem item in listView.SelectedItems) {
88          var plugin = (IPluginDescription)item.Tag;
89          // also check all dependencies
90          MarkPluginChecked(plugin);
91          modifiedPlugins.Add(plugin);
92          foreach (var dep in GetAllDependencies(plugin)) {
93            MarkPluginChecked(dep);
94            modifiedPlugins.Add(dep);
95          }
96        }
97        listView.CheckItems(modifiedPlugins.Select(x => FindItemsForPlugin(x).Single()));
98        OnItemChecked(e);
99      } else {
100        foreach (ListViewItem item in listView.SelectedItems) {
101          var plugin = (IPluginDescription)item.Tag;
102          // also uncheck all dependent plugins
103          MarkPluginUnchecked(plugin);
104          modifiedPlugins.Add(plugin);
105          foreach (var dep in GetAllDependents(plugin)) {
106            MarkPluginUnchecked(dep);
107            modifiedPlugins.Add(dep);
108          }
109
110        }
111        listView.UncheckItems(modifiedPlugins.Select(x => FindItemsForPlugin(x).Single()));
112        OnItemChecked(e);
113      }
114    }
115
116    private void MarkPluginChecked(IPluginDescription plugin) {
117      var matching = from p in checkedPlugins
118                     where p.Name == plugin.Name
119                     where p.Version == plugin.Version
120                     select p;
121      if (!matching.Any()) checkedPlugins.Add(plugin);
122    }
123
124    private void MarkPluginUnchecked(IPluginDescription plugin) {
125      checkedPlugins = (from p in checkedPlugins
126                        where p.Name != plugin.Name ||
127                              p.Version != plugin.Version
128                        select p).ToList();
129    }
130
131    private IEnumerable<ListViewItem> FindItemsForPlugin(IPluginDescription plugin) {
132      return from item in listView.Items.OfType<ListViewItem>()
133             let p = item.Tag as IPluginDescription
134             where p.Name == plugin.Name
135             where p.Version == plugin.Version
136             select item;
137    }
138
139    private IEnumerable<IPluginDescription> GetAllDependents(IPluginDescription plugin) {
140      return from p in plugins
141             let matchingEntries = from dep in GetAllDependencies(p)
142                                   where dep.Name == plugin.Name
143                                   where dep.Version == plugin.Version
144                                   select dep
145             where matchingEntries.Any()
146             select p;
147    }
148
149    private IEnumerable<IPluginDescription> GetAllDependencies(IPluginDescription plugin) {
150      foreach (var dep in plugin.Dependencies) {
151        foreach (var recDep in GetAllDependencies(dep)) {
152          yield return recDep;
153        }
154        yield return dep;
155      }
156    }
157
158    private void OnItemChecked(ItemCheckedEventArgs e) {
159      if (ItemChecked != null) ItemChecked(this, e);
160    }
161  }
162}
Note: See TracBrowser for help on using the repository browser.