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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.ComponentModel;
|
---|
25 | using System.Drawing;
|
---|
26 | using System.Data;
|
---|
27 | using System.Linq;
|
---|
28 | using System.Text;
|
---|
29 | using System.Windows.Forms;
|
---|
30 | using HeuristicLab.PluginInfrastructure;
|
---|
31 |
|
---|
32 | namespace 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 | listView.SuppressItemCheckedEvents = false;
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | private ListViewItem CreateListViewItem(IPluginDescription plugin) {
|
---|
80 | var item = new ListViewItem(new string[] { plugin.Name, plugin.Version.ToString() });
|
---|
81 | item.Checked = checkedPlugins.ContainsKey(plugin) ? checkedPlugins[plugin] : false;
|
---|
82 | item.Tag = plugin;
|
---|
83 | return item;
|
---|
84 | }
|
---|
85 |
|
---|
86 | private void listView_ItemChecked(object sender, ItemCheckedEventArgs e) {
|
---|
87 | List<IPluginDescription> modifiedPlugins = new List<IPluginDescription>();
|
---|
88 | if (e.Item.Checked) {
|
---|
89 | foreach (ListViewItem item in listView.SelectedItems) {
|
---|
90 | var plugin = (IPluginDescription)item.Tag;
|
---|
91 | // also check all dependencies
|
---|
92 | MarkPluginChecked(plugin);
|
---|
93 | if (!modifiedPlugins.Contains(plugin))
|
---|
94 | modifiedPlugins.Add(plugin);
|
---|
95 | foreach (var dep in GetAllDependencies(plugin)) {
|
---|
96 | MarkPluginChecked(dep);
|
---|
97 | if (!modifiedPlugins.Contains(dep))
|
---|
98 | modifiedPlugins.Add(dep);
|
---|
99 | }
|
---|
100 | }
|
---|
101 | listView.CheckItems(modifiedPlugins.Select(x => FindItemsForPlugin(x).Single()));
|
---|
102 | OnItemChecked(e);
|
---|
103 | } else {
|
---|
104 | foreach (ListViewItem item in listView.SelectedItems) {
|
---|
105 | var plugin = (IPluginDescription)item.Tag;
|
---|
106 | // also uncheck all dependent plugins
|
---|
107 | MarkPluginUnchecked(plugin);
|
---|
108 | if (!modifiedPlugins.Contains(plugin))
|
---|
109 | modifiedPlugins.Add(plugin);
|
---|
110 | foreach (var dep in GetAllDependents(plugin)) {
|
---|
111 | MarkPluginUnchecked(dep);
|
---|
112 | if (!modifiedPlugins.Contains(dep))
|
---|
113 | modifiedPlugins.Add(dep);
|
---|
114 | }
|
---|
115 |
|
---|
116 | }
|
---|
117 | listView.UncheckItems(modifiedPlugins.Select(x => FindItemsForPlugin(x).Single()));
|
---|
118 | OnItemChecked(e);
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | private void MarkPluginChecked(IPluginDescription plugin) {
|
---|
123 | checkedPlugins[plugin] = true;
|
---|
124 | }
|
---|
125 |
|
---|
126 | private void MarkPluginUnchecked(IPluginDescription plugin) {
|
---|
127 | checkedPlugins[plugin] = false;
|
---|
128 | }
|
---|
129 |
|
---|
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;
|
---|
136 | }
|
---|
137 |
|
---|
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;
|
---|
146 | }
|
---|
147 |
|
---|
148 | private IEnumerable<IPluginDescription> GetAllDependencies(IPluginDescription plugin) {
|
---|
149 | HashSet<IPluginDescription> yieldedPlugins = new HashSet<IPluginDescription>();
|
---|
150 | foreach (var dep in plugin.Dependencies) {
|
---|
151 | foreach (var recDep in GetAllDependencies(dep)) {
|
---|
152 | if (!yieldedPlugins.Contains(recDep)) {
|
---|
153 | yieldedPlugins.Add(recDep);
|
---|
154 | yield return recDep;
|
---|
155 | }
|
---|
156 | }
|
---|
157 | if (!yieldedPlugins.Contains(dep)) {
|
---|
158 | yieldedPlugins.Add(dep);
|
---|
159 | yield return dep;
|
---|
160 | }
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | private void OnItemChecked(ItemCheckedEventArgs e) {
|
---|
165 | if (ItemChecked != null) ItemChecked(this, e);
|
---|
166 | }
|
---|
167 | }
|
---|
168 | }
|
---|