Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3190 was 3112, checked in by gkronber, 15 years ago
  • Added missing license headers
  • performance improvement in listview_itemchecked handlers
  • added functionality to switch between simple and advanced view in remote plugins view
  • changed url of deployment service in preparation for next release of the test version
  • added default user credentials for the deployment service in preparation for the next release of the test version

#891 (Refactor GUI for plugin management)

File size: 10.3 KB
RevLine 
[3090]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;
[2922]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 {
[3090]31  internal partial class RemotePluginInstaller : UserControl {
[3112]32    public event ItemCheckedEventHandler ItemChecked;
33
[2922]34    private ListViewGroup newPluginsGroup;
35    private ListViewGroup productsGroup;
36    private ListViewGroup allPluginsGroup;
[3112]37    private bool showAllPlugins;
[2922]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();
[3068]95      remotePluginsListView.SuppressItemCheckedEvents = true;
[2922]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
[3112]108      if (showAllPlugins) {
109        foreach (var plugin in AllPlugins) {
110          var item = CreateListViewItem(plugin);
111          item.Group = allPluginsGroup;
112          remotePluginsListView.Items.Add(item);
113        }
[2922]114      }
[3068]115      remotePluginsListView.SuppressItemCheckedEvents = false;
[2922]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
[3112]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
[2922]147    #region item checked event handler
148    private void remotePluginsListView_ItemChecked(object sender, ItemCheckedEventArgs e) {
[3068]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)
[2922]153          if (e.Item.Checked)
[3068]154            HandlePluginChecked(plugin);
[2922]155          else
[3068]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        }
[2922]165      }
166      OnItemChecked(e);
167    }
168
169    private void HandleProductUnchecked(HeuristicLab.PluginInfrastructure.Advanced.DeploymentService.ProductDescription product) {
170      // also uncheck the plugins of the product
[3068]171      List<ListViewItem> modifiedItems = new List<ListViewItem>();
172      modifiedItems.Add(FindItemForProduct(product));
[2922]173      foreach (var plugin in product.Plugins) {
[3112]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        }
[2922]179      }
[3068]180      remotePluginsListView.UncheckItems(modifiedItems);
[2922]181    }
182
183    private void HandleProductChecked(HeuristicLab.PluginInfrastructure.Advanced.DeploymentService.ProductDescription product) {
184      // also check all plugins of the product
[3068]185      List<ListViewItem> modifiedItems = new List<ListViewItem>();
186      modifiedItems.Add(FindItemForProduct(product));
[2922]187      foreach (var plugin in product.Plugins) {
[3112]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          }
[2922]194        }
195      }
[3068]196      remotePluginsListView.CheckItems(modifiedItems);
[2922]197    }
198
199    private void HandlePluginUnchecked(IPluginDescription plugin) {
200      // also uncheck all dependent plugins
[3068]201      List<ListViewItem> modifiedItems = new List<ListViewItem>();
[3112]202      modifiedItems.AddRange(FindItemsForPlugin(plugin));
[2922]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) {
[3112]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          }
[2922]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) {
[3112]222          if (!modifiedItems.Contains(item))
223            modifiedItems.Add(item);
[2922]224        }
225      }
[3068]226      remotePluginsListView.UncheckItems(modifiedItems);
[2922]227    }
228
229    private void HandlePluginChecked(IPluginDescription plugin) {
230      // also check all dependencies
[3068]231      List<ListViewItem> modifiedItems = new List<ListViewItem>();
[3112]232      modifiedItems.AddRange(FindItemsForPlugin(plugin));
[2922]233      foreach (var dep in plugin.Dependencies) {
[3112]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          }
[2922]240        }
241      }
[3068]242      remotePluginsListView.CheckItems(modifiedItems);
[2922]243    }
244
245    private void OnItemChecked(ItemCheckedEventArgs e) {
246      if (ItemChecked != null) ItemChecked(this, e);
247    }
248    #endregion
249
250    #region helper methods
[3112]251    private IEnumerable<ListViewItem> FindItemsForPlugin(IPluginDescription plugin) {
[2922]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
[3112]255              select item);
[2922]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
[3112]266
[2922]267  }
268}
Note: See TracBrowser for help on using the repository browser.