Free cookie consent management tool by TermsFeed Policy Generator

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

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

Added license headers and set visibility to internal for the classes of the plugin manager. #891 (Refactor GUI for plugin management)

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