Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.PluginInfrastructure/Advanced/RemotePluginInstaller.cs @ 3509

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

Incorporated review comments by swagner into plugin infrastructure. #989 (Implement review comments in plugin infrastructure)

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