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 | using System;
|
---|
22 | using System.Collections.Generic;
|
---|
23 | using System.ComponentModel;
|
---|
24 | using System.Drawing;
|
---|
25 | using System.Data;
|
---|
26 | using System.Linq;
|
---|
27 | using System.Text;
|
---|
28 | using System.Windows.Forms;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.PluginInfrastructure.Advanced {
|
---|
31 | internal partial class RemotePluginInstaller : UserControl {
|
---|
32 | public event ItemCheckedEventHandler ItemChecked;
|
---|
33 |
|
---|
34 | private ListViewGroup newPluginsGroup;
|
---|
35 | private ListViewGroup productsGroup;
|
---|
36 | private ListViewGroup allPluginsGroup;
|
---|
37 | private bool showAllPlugins;
|
---|
38 |
|
---|
39 | public RemotePluginInstaller() {
|
---|
40 | InitializeComponent();
|
---|
41 |
|
---|
42 | imageListForRemoteItems.Images.Add(HeuristicLab.PluginInfrastructure.Resources.Resources.Assembly);
|
---|
43 | imageListForRemoteItems.Images.Add(HeuristicLab.PluginInfrastructure.Resources.Resources.Install);
|
---|
44 |
|
---|
45 | newPluginsGroup = remotePluginsListView.Groups["newPluginsGroup"];
|
---|
46 | productsGroup = remotePluginsListView.Groups["productsGroup"];
|
---|
47 | allPluginsGroup = remotePluginsListView.Groups["allPluginsGroup"];
|
---|
48 | }
|
---|
49 |
|
---|
50 | private IEnumerable<DeploymentService.ProductDescription> products;
|
---|
51 | public IEnumerable<DeploymentService.ProductDescription> Products {
|
---|
52 | get { return products ?? Enumerable.Empty<DeploymentService.ProductDescription>(); }
|
---|
53 | set {
|
---|
54 | if (value != this.products) {
|
---|
55 | this.products = value;
|
---|
56 | UpdateControl();
|
---|
57 | }
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | private IEnumerable<IPluginDescription> plugins;
|
---|
62 | public IEnumerable<IPluginDescription> AllPlugins {
|
---|
63 | get { return plugins ?? Enumerable.Empty<IPluginDescription>(); }
|
---|
64 | set {
|
---|
65 | if (value != this.plugins) {
|
---|
66 | this.plugins = value;
|
---|
67 | UpdateControl();
|
---|
68 | }
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | private IEnumerable<IPluginDescription> newPlugins;
|
---|
73 | public IEnumerable<IPluginDescription> NewPlugins {
|
---|
74 | get { return newPlugins ?? Enumerable.Empty<IPluginDescription>(); }
|
---|
75 | set {
|
---|
76 | if (value != this.newPlugins) {
|
---|
77 | this.newPlugins = value;
|
---|
78 | UpdateControl();
|
---|
79 | }
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | public IEnumerable<IPluginDescription> CheckedPlugins {
|
---|
84 | get {
|
---|
85 | return (from item in remotePluginsListView.Items.OfType<ListViewItem>()
|
---|
86 | where item.Checked
|
---|
87 | let plugin = item.Tag as IPluginDescription
|
---|
88 | where plugin != null
|
---|
89 | select plugin).ToList();
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | private void UpdateControl() {
|
---|
94 | ClearListView();
|
---|
95 | remotePluginsListView.SuppressItemCheckedEvents = true;
|
---|
96 | foreach (var newPlugin in NewPlugins) {
|
---|
97 | var item = CreateListViewItem(newPlugin);
|
---|
98 | item.Group = newPluginsGroup;
|
---|
99 | remotePluginsListView.Items.Add(item);
|
---|
100 | }
|
---|
101 |
|
---|
102 | foreach (var product in Products) {
|
---|
103 | var item = CreateListViewItem(product);
|
---|
104 | item.Group = productsGroup;
|
---|
105 | remotePluginsListView.Items.Add(item);
|
---|
106 | }
|
---|
107 |
|
---|
108 | if (showAllPlugins) {
|
---|
109 | foreach (var plugin in AllPlugins) {
|
---|
110 | var item = CreateListViewItem(plugin);
|
---|
111 | item.Group = allPluginsGroup;
|
---|
112 | remotePluginsListView.Items.Add(item);
|
---|
113 | }
|
---|
114 | }
|
---|
115 | remotePluginsListView.SuppressItemCheckedEvents = false;
|
---|
116 | }
|
---|
117 |
|
---|
118 | private void ClearListView() {
|
---|
119 | List<ListViewItem> itemsToDelete = new List<ListViewItem>(remotePluginsListView.Items.OfType<ListViewItem>());
|
---|
120 | itemsToDelete.ForEach(item => remotePluginsListView.Items.Remove(item));
|
---|
121 | }
|
---|
122 |
|
---|
123 | private ListViewItem CreateListViewItem(DeploymentService.ProductDescription product) {
|
---|
124 | ListViewItem item = new ListViewItem(new string[] { product.Name, product.Version.ToString(), string.Empty });
|
---|
125 | item.Tag = product;
|
---|
126 | return item;
|
---|
127 | }
|
---|
128 |
|
---|
129 | private ListViewItem CreateListViewItem(IPluginDescription plugin) {
|
---|
130 | ListViewItem item = new ListViewItem(new string[] { plugin.Name, plugin.Version.ToString(), plugin.Description });
|
---|
131 | item.Tag = plugin;
|
---|
132 | return item;
|
---|
133 | }
|
---|
134 |
|
---|
135 | #region button event handlers
|
---|
136 | private void advancedViewButton_CheckedChanged(object sender, EventArgs e) {
|
---|
137 | if (advancedViewButton.Checked) {
|
---|
138 | showAllPlugins = true;
|
---|
139 | } else {
|
---|
140 | showAllPlugins = false;
|
---|
141 | }
|
---|
142 | UpdateControl();
|
---|
143 | }
|
---|
144 |
|
---|
145 | #endregion
|
---|
146 |
|
---|
147 | #region item checked event handler
|
---|
148 | private void remotePluginsListView_ItemChecked(object sender, ItemCheckedEventArgs e) {
|
---|
149 | foreach (ListViewItem item in remotePluginsListView.SelectedItems) {
|
---|
150 | // dispatch by check state and type of item (product/plugin)
|
---|
151 | IPluginDescription plugin = item.Tag as IPluginDescription;
|
---|
152 | if (plugin != null)
|
---|
153 | if (e.Item.Checked)
|
---|
154 | HandlePluginChecked(plugin);
|
---|
155 | else
|
---|
156 | HandlePluginUnchecked(plugin);
|
---|
157 | else {
|
---|
158 | DeploymentService.ProductDescription product = item.Tag as DeploymentService.ProductDescription;
|
---|
159 | if (product != null)
|
---|
160 | if (e.Item.Checked)
|
---|
161 | HandleProductChecked(product);
|
---|
162 | else
|
---|
163 | HandleProductUnchecked(product);
|
---|
164 | }
|
---|
165 | }
|
---|
166 | OnItemChecked(e);
|
---|
167 | }
|
---|
168 |
|
---|
169 | private void HandleProductUnchecked(HeuristicLab.PluginInfrastructure.Advanced.DeploymentService.ProductDescription product) {
|
---|
170 | // also uncheck the plugins of the product
|
---|
171 | List<ListViewItem> modifiedItems = new List<ListViewItem>();
|
---|
172 | modifiedItems.Add(FindItemForProduct(product));
|
---|
173 | foreach (var plugin in product.Plugins) {
|
---|
174 | // there can be multiple entries for a single plugin in different groups
|
---|
175 | foreach (var item in FindItemsForPlugin(plugin)) {
|
---|
176 | if (item != null && item.Checked)
|
---|
177 | modifiedItems.Add(item);
|
---|
178 | }
|
---|
179 | }
|
---|
180 | remotePluginsListView.UncheckItems(modifiedItems);
|
---|
181 | }
|
---|
182 |
|
---|
183 | private void HandleProductChecked(HeuristicLab.PluginInfrastructure.Advanced.DeploymentService.ProductDescription product) {
|
---|
184 | // also check all plugins of the product
|
---|
185 | List<ListViewItem> modifiedItems = new List<ListViewItem>();
|
---|
186 | modifiedItems.Add(FindItemForProduct(product));
|
---|
187 | foreach (var plugin in product.Plugins) {
|
---|
188 | // there can be multiple entries for a single plugin in different groups
|
---|
189 | foreach (var item in FindItemsForPlugin(plugin)) {
|
---|
190 | if (item != null && !item.Checked) {
|
---|
191 | if (!modifiedItems.Contains(item))
|
---|
192 | modifiedItems.Add(item);
|
---|
193 | }
|
---|
194 | }
|
---|
195 | }
|
---|
196 | remotePluginsListView.CheckItems(modifiedItems);
|
---|
197 | }
|
---|
198 |
|
---|
199 | private void HandlePluginUnchecked(IPluginDescription plugin) {
|
---|
200 | // also uncheck all dependent plugins
|
---|
201 | List<ListViewItem> modifiedItems = new List<ListViewItem>();
|
---|
202 | modifiedItems.AddRange(FindItemsForPlugin(plugin));
|
---|
203 | var dependentPlugins = from otherPlugin in plugins
|
---|
204 | where otherPlugin.Dependencies.Any(dep => dep.Name == plugin.Name && dep.Version == plugin.Version)
|
---|
205 | select otherPlugin;
|
---|
206 | foreach (var dependentPlugin in dependentPlugins) {
|
---|
207 | // there can be multiple entries for a single plugin in different groups
|
---|
208 | foreach (var item in FindItemsForPlugin(dependentPlugin)) {
|
---|
209 | if (item != null && item.Checked) {
|
---|
210 | if (!modifiedItems.Contains(item))
|
---|
211 | modifiedItems.Add(item);
|
---|
212 | }
|
---|
213 | }
|
---|
214 | }
|
---|
215 | // also uncheck all products containing this plugin
|
---|
216 | var dependentProducts = from product in products
|
---|
217 | where product.Plugins.Any(p => p.Name == plugin.Name && p.Version == plugin.Version)
|
---|
218 | select product;
|
---|
219 | foreach (var dependentProduct in dependentProducts) {
|
---|
220 | var item = FindItemForProduct(dependentProduct);
|
---|
221 | if (item != null && item.Checked) {
|
---|
222 | if (!modifiedItems.Contains(item))
|
---|
223 | modifiedItems.Add(item);
|
---|
224 | }
|
---|
225 | }
|
---|
226 | remotePluginsListView.UncheckItems(modifiedItems);
|
---|
227 | }
|
---|
228 |
|
---|
229 | private void HandlePluginChecked(IPluginDescription plugin) {
|
---|
230 | // also check all dependencies
|
---|
231 | List<ListViewItem> modifiedItems = new List<ListViewItem>();
|
---|
232 | modifiedItems.AddRange(FindItemsForPlugin(plugin));
|
---|
233 | foreach (var dep in plugin.Dependencies) {
|
---|
234 | // there can be multiple entries for a single plugin in different groups
|
---|
235 | foreach (ListViewItem item in FindItemsForPlugin(dep)) {
|
---|
236 | if (item != null && !item.Checked) {
|
---|
237 | if (!modifiedItems.Contains(item))
|
---|
238 | modifiedItems.Add(item);
|
---|
239 | }
|
---|
240 | }
|
---|
241 | }
|
---|
242 | remotePluginsListView.CheckItems(modifiedItems);
|
---|
243 | }
|
---|
244 |
|
---|
245 | private void OnItemChecked(ItemCheckedEventArgs e) {
|
---|
246 | if (ItemChecked != null) ItemChecked(this, e);
|
---|
247 | }
|
---|
248 | #endregion
|
---|
249 |
|
---|
250 | #region helper methods
|
---|
251 | private IEnumerable<ListViewItem> FindItemsForPlugin(IPluginDescription plugin) {
|
---|
252 | return (from item in remotePluginsListView.Items.OfType<ListViewItem>()
|
---|
253 | let otherPlugin = item.Tag as IPluginDescription
|
---|
254 | where otherPlugin != null && otherPlugin.Name == plugin.Name && otherPlugin.Version == plugin.Version
|
---|
255 | select item);
|
---|
256 | }
|
---|
257 |
|
---|
258 | private ListViewItem FindItemForProduct(DeploymentService.ProductDescription product) {
|
---|
259 | return (from item in remotePluginsListView.Items.OfType<ListViewItem>()
|
---|
260 | let otherProduct = item.Tag as DeploymentService.ProductDescription
|
---|
261 | where otherProduct != null && otherProduct.Name == product.Name && otherProduct.Version == product.Version
|
---|
262 | select item).SingleOrDefault();
|
---|
263 | }
|
---|
264 |
|
---|
265 | #endregion
|
---|
266 |
|
---|
267 | }
|
---|
268 | }
|
---|